views:

59

answers:

2

I'm in need of some help with getting my duplicates from showing more than once in my output.

SELECT 
  accountNumber AS 'Member Number',  
  OD.orderdetails AS 'iNum', 
  FirstName AS 'First Name', 
  LastName AS 'Last Name', 
  HGP.email AS 'Email', 
  points AS 'Points -->', 
  '$' + CONVERT(varchar(50),(CONVERT(int,Points) * .1)) AS '<-- Amount', 
  CountryCode AS 'Country', 
  CONVERT(VARCHAR(10), issued, 101) AS 'Order Date', 
  CONVERT(VARCHAR(10), cs.RedeemedDate, 101) AS 'R Date' 
FROM tblHGP HGP, OrderDetails OD, tblInvoices i
JOIN tblCS cs ON i.InvoiceNumber = cs.InvoiceNumber
JOIN tblECI ac ON i.InvoiceNumber = ac.InvoiceNumber 
  AND cs.Sold = ac.ECIID
WHERE 
  i.InvoiceNumber = HGP.invoiceNumber 
  AND HGP.issued BETWEEN '2010-01-01' AND '2010-09-01' 
  AND HGP.invoiceNumber = OD.orderdetails
ORDER BY 
  HGP.issued

Like i said above, i am getting duplicates on some of the users. I've tried the distinct but doesn't seem to work.

Any help would be great! :o)

David

+2  A: 

More details. From the query it looks like you would get a double whenever an invoice has more than one order detail (more than one item ordered). Thislooks like by design. Naturally dsictinct would not work because not all fields would be identical.

Basically if a user gets invoiced for 2 items in one invoice, he will appear (as will the invoice) twice.

TomTom
Thanks for the reply, TomTom (ha). You are correct about it having a different invoice for the same person but is there anyway to **combine those same named users that do have multiple invoices to only one each?**
StealthRT
You would have to remove the orderdetails join, or if you need to see orderdetail sums or totals add a group by for them.
David
+1  A: 

It looks to me like you are only using the join to OrderDetails for this:

OD.orderdetails AS 'iNum',

As shown in your where clause, you can get this same value from HGP:

AND HGP.invoiceNumber = OD.orderdetails

thus you can eliminate the entire join to OD, and eliminate your duplication due to multiple rows in the details table for an invoice. So, rewritten it should look like this:

  SELECT 
  accountNumber AS 'Member Number',  
  HGP.invoiceNumber AS 'iNum', 
  FirstName AS 'First Name', 
  LastName AS 'Last Name', 
  HGP.email AS 'Email', 
  points AS 'Points -->', 
  '$' + CONVERT(varchar(50),(CONVERT(int,Points) * .1)) AS '<-- Amount', 
  CountryCode AS 'Country', 
  CONVERT(VARCHAR(10), issued, 101) AS 'Order Date', 
  CONVERT(VARCHAR(10), cs.RedeemedDate, 101) AS 'R Date' 
FROM tblHGP HGP, tblInvoices i
JOIN tblCS cs ON i.InvoiceNumber = cs.InvoiceNumber
JOIN tblECI ac ON i.InvoiceNumber = ac.InvoiceNumber 
  AND cs.Sold = ac.ECIID
WHERE 
  i.InvoiceNumber = HGP.invoiceNumber 
  AND HGP.issued BETWEEN '2010-01-01' AND '2010-09-01' 
ORDER BY 
  HGP.issued

I can't tell for sure if the non-table qualified column names (i.e. issued, Points, CountryCode, FirstName, LastName) are coming from OrderDetails or not, though they don't look like they would.

David
Theres no column named invoiceNumber in HGP
StealthRT
According to the question there is, look in the where clause: WHERE _i.InvoiceNumber = HGP.invoiceNumber_ AND HGP.issued BETWEEN '2010-01-01' AND '2010-09-01' _AND HGP.invoiceNumber = OD.orderdetails_
David
Ah, yes. Ok i changed it but i still get the same end results.
StealthRT
I've changed the AND to **AND cs.SoldTo = HGP.ECardInd** which that should keep it down to 1:1 but i am getting an error of **The multi-part identifier "HGP.ECardInd" could not be bound.**?
StealthRT