Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Outputting a DISTINCT value

Author  Topic 

speedy_gonzalos
Starting Member

10 Posts

Posted - 2009-06-01 : 08:23:33
Hi,

I have the following query in Access 2000 Reports:

SELECT DISTINCT [Orders].[ShippingEnteredDate]
, [Orders].[DealerID]
, [Orders].[OrderID]
, [Orders].[OrderDate]
, [Orders].[PurchaseOrderNumber]
, [Orders].[CustomerRef]
, [Orders].[ShipName]
, [Orders].[ShipDate]
, [Orders].[TrackingNumber]
, [Orders].[MethodofPayment]
, [Orders].[Amount]
, [Orders].[CurrencyType]
, [Orders].[EnquiryID]
, [OrderDetails].[SerialNumber]
, [Orders].[SaleType]
, [Orders].[OrderDespatchedFrom] AS DespatchedFrom
FROM Orders LEFT JOIN OrderDetails ON [Orders].[OrderID]=[OrderDetails].[OrderID]
WHERE ((([Orders].[ShippingEnteredDate]) Between @startdate And @enddate) And (([Orders].[Cancelled])<>1))
ORDER BY [Orders].[OrderID], [Orders].[OrderDate] DESC;

I need to output the Total for each SaleType and then a Grand Total for the whole report.
The problem that i have is that the Amount field is like the total value for an OrderID, the above query outputs the Amount value for each order line item, which will always be the same for each OrderID. When i sum the Amount field it multiplies the Amount value by the number of order lines that OrderID has.

For example if an OrderID has 2 order lines, the total for that OrderID is the vlaue in the Amount field. So for this example, if i say the Amount value is 200, then when i sum the Amount in the report it will output 200*2 = 400 for that OrderID. BUT the answer should be 200 not 400.

If i do distinct it doesnt work as the serial number is not unique for each OrderID. If i take the serial number field out then it works fine, however i need this field in the report as it outputs this field.

I have been going round and round in circles, so please any help would be most appreciated.

speedy_gonzalos
Starting Member

10 Posts

Posted - 2009-06-01 : 10:06:56
Hi,

Im just wondering why i havent received any replies as yet? is it because you dont understand what im asking or is it something else?
its just that i would appreciate any hints or tips as soon as possible.

Thanks
Shabana
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-06-01 : 13:17:50
quote:
Originally posted by speedy_gonzalos

Hi,

Im just wondering why i havent received any replies as yet? is it because you dont understand what im asking or is it something else?
its just that i would appreciate any hints or tips as soon as possible.

Thanks
Shabana


One reason is because you're in wrong forum. this forum is for sql server. you had to post this in access forum. in any case, the following will work in sql server. you can try this in acces too. if it still doesnt work please post this in access forum here.


SELECT DISTINCT [Orders].[ShippingEnteredDate]
, [Orders].[DealerID]
, [Orders].[OrderID]
, [Orders].[OrderDate]
, [Orders].[PurchaseOrderNumber]
, [Orders].[CustomerRef]
, [Orders].[ShipName]
, [Orders].[ShipDate]
, [Orders].[TrackingNumber]
, [Orders].[MethodofPayment]
, [Orders].[Amount]
, [Orders].[CurrencyType]
, [Orders].[EnquiryID]
, [OrderDetails].[SerialNumber]
, [Orders].[SaleType]
, [Orders].[OrderDespatchedFrom] AS DespatchedFrom
, CASE WHEN od1.SerialNumber IS NULL THEN 0 ELSE 1 END AS MinLine
FROM Orders
INNER JOIN OrderDetails
ON [Orders].[OrderID]=[OrderDetails].[OrderID]
LEFT JOIN (SELECT OrderID,MIN(SerialNumber) AS MinDetail
FROM OrderDetails
GROUP BY OrderID)od1
ON od1.[OrderID]=[OrderDetails].[OrderID]
AND od1.MinDetail=[OrderDetails].SerialNumber
WHERE ((([Orders].[ShippingEnteredDate]) Between @startdate And @enddate) And (([Orders].[Cancelled])<>1))
ORDER BY [Orders].[OrderID], [Orders].[OrderDate] DESC;

then for getting correct total group by SaleType and take sum as

SUM(CASE WHEN MinDetail=1 THEN Amount ELSE 0 END) AS Total
Go to Top of Page

tosscrosby
Aged Yak Warrior

676 Posts

Posted - 2009-06-01 : 14:55:33
And also, many of us have jobs (this is a free site, unless Visakh is getting paid???) and cannot always get to the posts as quickly as the posters may like. But we do come along at some point.

Terry

-- Procrastinate now!
Go to Top of Page

speedy_gonzalos
Starting Member

10 Posts

Posted - 2009-06-02 : 04:50:16
Thanks for the reply, Visakh, i will defo try that.
And Terry, i only asked the question as i was curious why i hadnt got a reply, because other people who had posted after me had more interaction (replies). Since i am fairly new to this, i thought id done something wrong. But as Visakh replied, it was because i posted it in the wrong forum. I understand poeple have jobs, including me, and i was not insinuating anything at all so no need to get harsh so quickly...take a chill pill, :)

Until the next time...laters
And thanks for your assistance!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-06-02 : 11:48:01
quote:
Originally posted by tosscrosby

And also, many of us have jobs (this is a free site, unless Visakh is getting paid???) and cannot always get to the posts as quickly as the posters may like. But we do come along at some point.

Terry

-- Procrastinate now!


Just to confirm, I'm not
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-06-02 : 11:49:35
quote:
Originally posted by speedy_gonzalos

Thanks for the reply, Visakh, i will defo try that.
And Terry, i only asked the question as i was curious why i hadnt got a reply, because other people who had posted after me had more interaction (replies). Since i am fairly new to this, i thought id done something wrong. But as Visakh replied, it was because i posted it in the wrong forum. I understand poeple have jobs, including me, and i was not insinuating anything at all so no need to get harsh so quickly...take a chill pill, :)

Until the next time...laters
And thanks for your assistance!


Good luck
Go to Top of Page
   

- Advertisement -