views:

24

answers:

1

Hey all, i am trying to create a report to show how much is spent per HRCode. This is my SQL Query so far:

 SELECT *
 FROM   tblWO as WO, 
        tblWOD as WOD, 
        tblWA as WA 
 WHERE WOD.OrderID = WO.ID 
 AND WA.HRCode  = WO.AdministratorCode 
 AND WO.OrderDate BETWEEN '2010-01-01' AND '2010-08-31' 
 AND Approved = '1' 
 ORDER BY WO.OrderDate

I'm trying to fingure out a way that mash all the same HRCode's together and then SUM() each... But my brain is not working today so i need some help :o)

Here is what the database data looks like:

ID      Total   OrderDate               Approved    HRCode    AdministratorCode    OrderID
3272      25.00   2010-01-04 10:48:57.617 1           RN        RN                   3272
4621      25.00   2010-02-04 11:15:01.600 1           RN        RN                   4621
4899      50.00   2010-02-04 11:55:01.630 1           02        02                   4899
4905      15.00   2010-05-04 11:55:01.190 1           NR        NR                   4905
5001      50.00   2010-06-04 04:11:55.295 1           RN        RN                   5001

Any help would be great! Thanks!

SOLVED

 SELECT SUM(Total) as Total, AdministratorCode 
 FROM   tblWO as WO, 
        tblWOD as WOD
 WHERE WOD.OrderID = WO.ID 
 AND WO.OrderDate BETWEEN '2010-01-01' AND '2010-08-31' 
 AND Approved = '1' 
 ORDER BY WO.AdministratorCode

David

+2  A: 

Remove the OrderDate and ID columns from your select statement:

 SELECT SUM(Total) as Total, HRCode
 FROM   tblWO as WO,  
        tblWOD as WOD,  
        tblWA as WA  
 WHERE WOD.OrderID = WO.ID  
 AND WA.HRCode  = WO.AdministratorCode  
 AND WO.OrderDate BETWEEN '2010-01-01' AND '2010-08-31'  
 AND Approved = '1'  
 GROUP BY HRCode
LittleBobbyTables
I get totals like **11884250.00**???
StealthRT
Also, i would like for all the tables to show up, not just total and HRCode?
StealthRT
What other tables do you want to show up? Do you mean columns? It would help if you edit your question with an example of what output you are looking for.
LittleBobbyTables
Sorry, columns. I would like all the data that i put in my OP just with the addition to the "total" column. I tried SUM(Total) as Total, HRCode, * but it did not work
StealthRT
But that most likely won't sum anything for you, as I'm assuming the ID and the dates are all unique. If you group by the ID and date as well, you'll get the sum of HR values for each order and date.
LittleBobbyTables
Gotcha,.. but the SUM does seem to mess up since the value 11884250.00 does not match what it should be. (a lot less than that)
StealthRT
I would check and make sure that all of your joins are correct. You may not need all the tables in the query, and the `WA.HRCode = WO.AdministratorCode` join seems odd.
LittleBobbyTables
Got it! Check my edit in my OP. Thanks LittleBobbyTables! :o)
StealthRT
Excellent! Glad it works
LittleBobbyTables