Front Office Football Central  

Go Back   Front Office Football Central > Archives > FOFC Archive
Register FAQ Members List Calendar Mark Forums Read Statistics

Reply
 
Thread Tools
Old 10-09-2008, 05:09 PM   #1
Bonegavel
Awaiting Further Instructions...
 
Join Date: Nov 2001
Location: Macungie, PA
yet more tsql fun

here is the lovely query i've built so far

Quote:
SELECT o.nOrderID, o.dOrderPlaced, v.sVendor, li.nItemID, i.sItemNumber, i.sItemDesc, li.nQty
FROM t_Order o INNER JOIN
t_Vendor v ON o.nVendorID = v.nVendorID INNER JOIN
t_Lineitem li ON o.nOrderID = li.nOrderID INNER JOIN
t_Item i ON li.nItemID = i.nItemID
WHERE (o.nVendorID = 1001) AND (o.dOrderPlaced > '08/31/2007 23:59:59 PM') AND (o.dOrderPlaced <= '09/30/2008 23:59:59 PM')
ORDER BY o.nOrderID DESC

This show me all the data I want. However, I want to sum all the same li.nitemid together. I know I have to utilize group by, but I become retarded when it comes to group by.

i'm getting closer, I figured out I couldn't keep in the norderid or the date since that wouldn't be able to group by that.

I know have:

Quote:

SELECT li.nItemID, i.sItemNumber, i.sItemDesc, li.nQty AS total
FROM t_Order o INNER JOIN
t_Vendor v ON o.nVendorID = v.nVendorID INNER JOIN
t_Lineitem li ON o.nOrderID = li.nOrderID INNER JOIN
t_Item i ON li.nItemID = i.nItemID
WHERE (o.nVendorID = 1001) AND (o.dOrderPlaced > '08/31/2007 23:59:59 PM') AND (o.dOrderPlaced <= '09/30/2008 23:59:59 PM')


I want to do this:

Quote:
SELECT li.nItemID, i.sItemNumber, i.sItemDesc, SUM(li.nQty) AS Expr1
FROM t_Order o INNER JOIN
t_Vendor v ON o.nVendorID = v.nVendorID INNER JOIN
t_Lineitem li ON o.nOrderID = li.nOrderID INNER JOIN
t_Item i ON li.nItemID = i.nItemID
WHERE (o.nVendorID = 1001) AND (o.dOrderPlaced > '08/31/2007 23:59:59 PM') AND (o.dOrderPlaced <= '09/30/2008 23:59:59 PM')
GROUP BY li.nItemID


but I get:

error: column i.sItemNumber is invalid because it is not contained in either an aggregate function or group by clause.

what's screwing me up is that I put everything into separate tables but now they are a pain to query
__________________



Bonegavel is offline   Reply With Quote
Old 10-09-2008, 05:12 PM   #2
Bonegavel
Awaiting Further Instructions...
 
Join Date: Nov 2001
Location: Macungie, PA
oops, I think I answered my own by removing the offending selects

Quote:
SELECT li.nItemID, SUM(li.nQty) AS Expr1
FROM t_Order o INNER JOIN
t_Vendor v ON o.nVendorID = v.nVendorID INNER JOIN
t_Lineitem li ON o.nOrderID = li.nOrderID INNER JOIN
t_Item i ON li.nItemID = i.nItemID
WHERE (o.nVendorID = 1001) AND (o.dOrderPlaced > '08/31/2007 23:59:59 PM') AND (o.dOrderPlaced <= '09/30/2008 23:59:59 PM')
GROUP BY li.nItemID
ORDER BY Expr1 DESC

Anyone have an easy way to think about sum and grouping... always fricks me up.
__________________


Bonegavel is offline   Reply With Quote
Old 10-09-2008, 05:13 PM   #3
Bonegavel
Awaiting Further Instructions...
 
Join Date: Nov 2001
Location: Macungie, PA
One thing, however, is I want to bring the item description over (so they can see what the item is - item number lone doesn't tell much) and I can't becaus of the group by.

Any way to keep in the i.sItemDesc ?
__________________


Bonegavel is offline   Reply With Quote
Old 10-09-2008, 05:15 PM   #4
Bonegavel
Awaiting Further Instructions...
 
Join Date: Nov 2001
Location: Macungie, PA
fucking moron... I just added it to the end

Quote:
SELECT li.nItemID, SUM(li.nQty) AS Expr1, i.sItemDesc
FROM t_Order o INNER JOIN
t_Vendor v ON o.nVendorID = v.nVendorID INNER JOIN
t_Lineitem li ON o.nOrderID = li.nOrderID INNER JOIN
t_Item i ON li.nItemID = i.nItemID
WHERE (o.nVendorID = 1001) AND (o.dOrderPlaced > '08/31/2007 23:59:59 PM') AND (o.dOrderPlaced <= '09/30/2008 23:59:59 PM')
GROUP BY li.nItemID, i.sItemDesc
ORDER BY Expr1 DESC

and it seems to be there now. I just needed to make a sql-ass out of myself to the world so I could figure it out.
__________________


Bonegavel is offline   Reply With Quote
Old 10-09-2008, 05:42 PM   #5
adubroff
High School JV
 
Join Date: Nov 2006
Quote:
Originally Posted by Bonegavel View Post
fucking moron... I just added it to the end



and it seems to be there now. I just needed to make a sql-ass out of myself to the world so I could figure it out.

The simple rule for summing/grouping is that any column which is returned in the recordset which is not being summed, has to in the group by.
adubroff is offline   Reply With Quote
Old 10-10-2008, 09:50 AM   #6
Bonegavel
Awaiting Further Instructions...
 
Join Date: Nov 2001
Location: Macungie, PA
Quote:
Originally Posted by adubroff View Post
The simple rule for summing/grouping is that any column which is returned in the recordset which is not being summed, has to in the group by.

Aaaaaaaah! Thanks. When I added i.sItemDesc, enterprise manager auto-added it to the group by and I didn't see that. Makes total sense and I should probably take a few classes on SQL. Problem is I only use it intensely a few times a year.
__________________


Bonegavel is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is On
Forum Jump


All times are GMT -5. The time now is 01:42 PM.



Powered by vBulletin Version 3.6.0
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.