Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Remove duplicate rows in multiple join query

Remove duplicate rows in multiple join query

Scheduled Pinned Locked Moved Database
databasehelp
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Eng Hasan Abbas
    wrote on last edited by
    #1

    hey , i have a query that collects data from 3 tables using one inner join and left join but the problem that there are many duplicate rows <

    SELECT tbl_salles_bill.sb_id, ( tbl_salles_bill.sb_price_after_alll_dicount - tbl_salles_bill.sb_paid_money) as Dept,
    tbl_customer.cust_fName + ' ' + tbl_customer.cust_mName + ' ' + tbl_customer.cust_lName AS CustomerName, tbl_customer.cust_city, tbl_receiving_money_receipt.rmr_id,
    tbl_receiving_money_receipt.rmr_value AS Creditor
    FROM tbl_salles_bill INNER JOIN
    tbl_customer ON tbl_salles_bill.cust_id = tbl_customer.cust_id and tbl_salles_bill.sb_status='Paid'
    left JOIN
    tbl_receiving_money_receipt ON tbl_customer.cust_id = tbl_receiving_money_receipt.cust_id and tbl_receiving_money_receipt.rmr_IsPaidForWhat = 'Paid without recieving goods'
    WHERE (tbl_customer.cust_id = 6)

    thanks for your time. >

    M C M 3 Replies Last reply
    0
    • E Eng Hasan Abbas

      hey , i have a query that collects data from 3 tables using one inner join and left join but the problem that there are many duplicate rows <

      SELECT tbl_salles_bill.sb_id, ( tbl_salles_bill.sb_price_after_alll_dicount - tbl_salles_bill.sb_paid_money) as Dept,
      tbl_customer.cust_fName + ' ' + tbl_customer.cust_mName + ' ' + tbl_customer.cust_lName AS CustomerName, tbl_customer.cust_city, tbl_receiving_money_receipt.rmr_id,
      tbl_receiving_money_receipt.rmr_value AS Creditor
      FROM tbl_salles_bill INNER JOIN
      tbl_customer ON tbl_salles_bill.cust_id = tbl_customer.cust_id and tbl_salles_bill.sb_status='Paid'
      left JOIN
      tbl_receiving_money_receipt ON tbl_customer.cust_id = tbl_receiving_money_receipt.cust_id and tbl_receiving_money_receipt.rmr_IsPaidForWhat = 'Paid without recieving goods'
      WHERE (tbl_customer.cust_id = 6)

      thanks for your time. >

      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      Underscore make that unreadable bleh... What I do is a 2 query pass, the inside query uses ROW_NUMBER() and PARTITION OVER the key fields (those that make up the unique record) I then delete records with a row number > 1 Here is a snippet I keep around for deduping

      DECLARE @Tbl TABLE (IDField INT, RowNo INT)

      --insert the primary key (identity field)
      INSERT @Tbl
      SELECT lnkStrategyNodeID IDfield,

      --partition over the unique constraints - order by is required and logically you should use the lowest ID field
      ROW_NUMBER() OVER(PARTITION BY StrategyID,NodeID ORDER BY lnkStrategyNodeID) Rw
      FROM lnkStrategyNode

      --delete anything that is > 1
      --SELECT *
      DELETE
      FROM lnkStrategyNode
      WHERE lnkStrategyNodeID IN (SELECT IDField FROM @Tbl WHERE RowNo > 1)

      Never underestimate the power of human stupidity RAH

      1 Reply Last reply
      0
      • E Eng Hasan Abbas

        hey , i have a query that collects data from 3 tables using one inner join and left join but the problem that there are many duplicate rows <

        SELECT tbl_salles_bill.sb_id, ( tbl_salles_bill.sb_price_after_alll_dicount - tbl_salles_bill.sb_paid_money) as Dept,
        tbl_customer.cust_fName + ' ' + tbl_customer.cust_mName + ' ' + tbl_customer.cust_lName AS CustomerName, tbl_customer.cust_city, tbl_receiving_money_receipt.rmr_id,
        tbl_receiving_money_receipt.rmr_value AS Creditor
        FROM tbl_salles_bill INNER JOIN
        tbl_customer ON tbl_salles_bill.cust_id = tbl_customer.cust_id and tbl_salles_bill.sb_status='Paid'
        left JOIN
        tbl_receiving_money_receipt ON tbl_customer.cust_id = tbl_receiving_money_receipt.cust_id and tbl_receiving_money_receipt.rmr_IsPaidForWhat = 'Paid without recieving goods'
        WHERE (tbl_customer.cust_id = 6)

        thanks for your time. >

        C Offline
        C Offline
        Chris Quinn
        wrote on last edited by
        #3

        Use the DISTINCT keyword

        SELECT DISTINCT tbl_salles_bill.sb_id, ( tbl_salles_bill.sb_price_after_alll_dicount - tbl_salles_bill.sb_paid_money) as Dept,
        tbl_customer.cust_fName + ' ' + tbl_customer.cust_mName + ' ' + tbl_customer.cust_lName AS CustomerName, tbl_customer.cust_city, tbl_receiving_money_receipt.rmr_id,
        tbl_receiving_money_receipt.rmr_value AS Creditor
        FROM tbl_salles_bill INNER JOIN
        tbl_customer ON tbl_salles_bill.cust_id = tbl_customer.cust_id and tbl_salles_bill.sb_status='Paid'
        left JOIN
        tbl_receiving_money_receipt ON tbl_customer.cust_id = tbl_receiving_money_receipt.cust_id and tbl_receiving_money_receipt.rmr_IsPaidForWhat = 'Paid without recieving goods'
        WHERE (tbl_customer.cust_id = 6)

        ========================================================= I'm an optoholic - my glass is always half full of vodka. =========================================================

        1 Reply Last reply
        0
        • E Eng Hasan Abbas

          hey , i have a query that collects data from 3 tables using one inner join and left join but the problem that there are many duplicate rows <

          SELECT tbl_salles_bill.sb_id, ( tbl_salles_bill.sb_price_after_alll_dicount - tbl_salles_bill.sb_paid_money) as Dept,
          tbl_customer.cust_fName + ' ' + tbl_customer.cust_mName + ' ' + tbl_customer.cust_lName AS CustomerName, tbl_customer.cust_city, tbl_receiving_money_receipt.rmr_id,
          tbl_receiving_money_receipt.rmr_value AS Creditor
          FROM tbl_salles_bill INNER JOIN
          tbl_customer ON tbl_salles_bill.cust_id = tbl_customer.cust_id and tbl_salles_bill.sb_status='Paid'
          left JOIN
          tbl_receiving_money_receipt ON tbl_customer.cust_id = tbl_receiving_money_receipt.cust_id and tbl_receiving_money_receipt.rmr_IsPaidForWhat = 'Paid without recieving goods'
          WHERE (tbl_customer.cust_id = 6)

          thanks for your time. >

          M Offline
          M Offline
          Member 10714909
          wrote on last edited by
          #4

          plz visit here this tutorial may be helpful to you. http://www.javatpoint.com/sql-delete-duplicate-rows[^]

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups