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.
| Author |
Topic |
|
actsql
Starting Member
34 Posts |
Posted - 2010-04-27 : 16:22:29
|
| I am a newbie and used Access previously, which would allow you to write a query and perform calculations on the queried data, all at the same time. I am sure SQL does the same, however, the SQL code I am getting out of Access is not working. I need to perform the following math in the attached query: [RecvQuant]-[PndRcpt]-[TTQTY] AS FinQty (when I insert this code to the current query I get the error "Invalid column name" for each of the 3 aliases in brackets)For my output I would like to see item_id, RecvQuant,PndRcpt,TTQTY, OpenPO and FinQty.Here is my current code:SELECT DISTINCT t.item_id, ISNULL((SELECT DISTINCT sum(dbo.inventory_receipts_d.received_quantity) FROM dbo.inventory_receipts_d WHERE dbo.inventory_receipts_d.item_id = t.item_id),0) AS RecvQuant, ISNULL((SELECT DISTINCT sum(ISNULL(dbo.inventory_receipts_d.received_quantity,0)) FROM dbo.inventory_receipts_d WHERE dbo.inventory_receipts_d.posted_flag = 'N' AND dbo.inventory_receipts_d.item_id = t.item_id),0) AS PndRcpt, ISNULL((SELECT DISTINCT SUM (ISNULL (CASE WHEN dbo.inventory_activity_transfer_d.transfer_type = 'F' AND dbo.inventory_activity_transfer_d.move_from_to='W' -- AND dbo.inventory_activity_transfer_d.inv_rcpt_trx_id IS NULL AND dbo.inventory_activity_d.item_id = dbo.items.item_id THEN (-1)*isnull(dbo.inventory_activity_transfer_d.stocking_quantity,0) WHEN dbo.inventory_activity_transfer_d.transfer_type = 'T' AND dbo.inventory_activity_transfer_d.move_from_to='W' -- AND dbo.inventory_activity_transfer_d.inv_rcpt_trx_id IS NULL AND dbo.inventory_activity_d.item_id = dbo.items.item_id THEN (1)*isnull(dbo.inventory_activity_transfer_d.stocking_quantity,0) ELSE 0 END,0) ) FROM dbo.items JOIN dbo.inventory_activity_d ON dbo.items.item_id = dbo.inventory_activity_d.item_id JOIN dbo.inventory_activity_transfer_d ON dbo.inventory_activity_d.transaction_no = dbo.inventory_activity_transfer_d.transaction_no AND dbo.inventory_activity_d.line_no = dbo.inventory_activity_transfer_d.line_no WHERE dbo.items.primary_item_category_no<>'NONIN' AND dbo.items.item_id=t.item_id ),0) AS TTQTY, ISNULL((SELECT DISTINCT sum(ISNULL(dbo.po_sub_item_d.stocking_quantity,0)) FROM dbo.po_sub_item_d WHERE dbo.po_sub_item_d.order_completion_date IS NULL AND dbo.po_sub_item_d.item_id = t.item_id),0) AS OpenPO FROM dbo.items t LEFT JOIN dbo.inventory_receipts_d ON t.item_id = dbo.inventory_receipts_d.item_id LEFT JOIN dbo.po_sub_item_d ON t.item_id = dbo.po_sub_item_d.item_id LEFT JOIN dbo.po_sub_change_item_d ON t.item_id = dbo.po_sub_change_item_d.item_id AND dbo.po_sub_change_item_d.po_sub_no = dbo.po_sub_item_d.po_sub_no LEFT JOIN dbo.inventory_activity_d ON t.item_id = dbo.inventory_activity_d.item_id LEFT JOIN dbo.inventory_activity_transfer_d ON dbo.inventory_activity_d.transaction_no = dbo.inventory_activity_transfer_d.transaction_no AND dbo.inventory_activity_d.line_no = dbo.inventory_activity_transfer_d.line_no WHERE t.record_status<>'D' AND t.primary_item_category_no<>'NONIN' GROUP BY t.item_id,t.reorder_point,t.max_stocking_qty,dbo.inventory_receipts_d.item_id, dbo.inventory_activity_transfer_d.transaction_no, dbo.inventory_activity_transfer_d.line_no,dbo.inventory_activity_transfer_d.transfer_type,dbo.inventory_activity_transfer_d.stocking_quantity,dbo.inventory_receipts_d.posted_flag,/*dbo.po_sub_item_d.order_completion_date,*/dbo.inventory_activity_transfer_d.move_from_to,dbo.po_sub_item_d.po_sub_no,dbo.po_sub_item_d.order_completion_date,dbo.inventory_receipts_d.item_id,dbo.inventory_activity_d.transaction_no,dbo.inventory_activity_d.line_no,dbo.inventory_activity_d.item_id,dbo.inventory_activity_transfer_d.inv_rcpt_trx_idORDER BY t.item_id |
|
|
hanbingl
Aged Yak Warrior
652 Posts |
Posted - 2010-04-27 : 16:28:54
|
| [code]select item_id, RecvQuant,PndRcpt,TTQTY, OpenPO, [RecvQuant]-[PndRcpt]-[TTQTY] AS FinQty from (........)torder ty item_id[/code] |
 |
|
|
actsql
Starting Member
34 Posts |
Posted - 2010-04-27 : 16:37:37
|
| I am getting the following error "Msg 1033, Level 15, State 1, Line 74The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP or FOR XML is also specified."Here is the revised code:SELECT item_id, RecvQuant, PndRcpt, TTQTY, OpenPO, [RecvQuant]-[PndRcpt]-[TTQTY] AS FinQty FROM ( SELECT DISTINCT t.item_id, ISNULL((SELECT DISTINCT sum(dbo.inventory_receipts_d.received_quantity) FROM dbo.inventory_receipts_d WHERE dbo.inventory_receipts_d.item_id = t.item_id),0) AS RecvQuant, ISNULL((SELECT DISTINCT sum(ISNULL(dbo.inventory_receipts_d.received_quantity,0)) FROM dbo.inventory_receipts_d WHERE dbo.inventory_receipts_d.posted_flag = 'N' AND dbo.inventory_receipts_d.item_id = t.item_id),0) AS PndRcpt, ISNULL((SELECT DISTINCT SUM (ISNULL (CASE WHEN dbo.inventory_activity_transfer_d.transfer_type = 'F' AND dbo.inventory_activity_transfer_d.move_from_to='W' -- AND dbo.inventory_activity_transfer_d.inv_rcpt_trx_id IS NULL AND dbo.inventory_activity_d.item_id = dbo.items.item_id THEN (-1)*isnull(dbo.inventory_activity_transfer_d.stocking_quantity,0) WHEN dbo.inventory_activity_transfer_d.transfer_type = 'T' AND dbo.inventory_activity_transfer_d.move_from_to='W' -- AND dbo.inventory_activity_transfer_d.inv_rcpt_trx_id IS NULL AND dbo.inventory_activity_d.item_id = dbo.items.item_id THEN (1)*isnull(dbo.inventory_activity_transfer_d.stocking_quantity,0) ELSE 0 END,0) ) FROM dbo.items JOIN dbo.inventory_activity_d ON dbo.items.item_id = dbo.inventory_activity_d.item_id JOIN dbo.inventory_activity_transfer_d ON dbo.inventory_activity_d.transaction_no = dbo.inventory_activity_transfer_d.transaction_no AND dbo.inventory_activity_d.line_no = dbo.inventory_activity_transfer_d.line_no WHERE dbo.items.primary_item_category_no<>'NONIN' AND dbo.items.item_id=t.item_id ),0) AS TTQTY, ISNULL((SELECT DISTINCT sum(ISNULL(dbo.po_sub_item_d.stocking_quantity,0)) FROM dbo.po_sub_item_d WHERE dbo.po_sub_item_d.order_completion_date IS NULL AND dbo.po_sub_item_d.item_id = t.item_id),0) AS OpenPO FROM dbo.items t LEFT JOIN dbo.inventory_receipts_d ON t.item_id = dbo.inventory_receipts_d.item_id LEFT JOIN dbo.po_sub_item_d ON t.item_id = dbo.po_sub_item_d.item_id LEFT JOIN dbo.po_sub_change_item_d ON t.item_id = dbo.po_sub_change_item_d.item_id AND dbo.po_sub_change_item_d.po_sub_no = dbo.po_sub_item_d.po_sub_no LEFT JOIN dbo.inventory_activity_d ON t.item_id = dbo.inventory_activity_d.item_id LEFT JOIN dbo.inventory_activity_transfer_d ON dbo.inventory_activity_d.transaction_no = dbo.inventory_activity_transfer_d.transaction_no AND dbo.inventory_activity_d.line_no = dbo.inventory_activity_transfer_d.line_no WHERE t.record_status<>'D' AND t.primary_item_category_no<>'NONIN' GROUP BY t.item_id,t.reorder_point,t.max_stocking_qty,dbo.inventory_receipts_d.item_id, dbo.inventory_activity_transfer_d.transaction_no, dbo.inventory_activity_transfer_d.line_no,dbo.inventory_activity_transfer_d.transfer_type,dbo.inventory_activity_transfer_d.stocking_quantity,dbo.inventory_receipts_d.posted_flag,/*dbo.po_sub_item_d.order_completion_date,*/dbo.inventory_activity_transfer_d.move_from_to,dbo.po_sub_item_d.po_sub_no,dbo.po_sub_item_d.order_completion_date,dbo.inventory_receipts_d.item_id,dbo.inventory_activity_d.transaction_no,dbo.inventory_activity_d.line_no,dbo.inventory_activity_d.item_id,dbo.inventory_activity_transfer_d.inv_rcpt_trx_idORDER BY t.item_id) tORDER BY item_id |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-04-28 : 05:40:53
|
| No use adding ORDER BY inside view unless you want to restrict result to TOP x rows. you need to use ORDER BY in statement where you retrieve from view to guarantee return order.------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
actsql
Starting Member
34 Posts |
Posted - 2010-04-28 : 07:57:59
|
| I removed the internal ORDER BY but I am getting the error "Msg 156, Level 15, State 1, Line 78 Incorrect syntax near the keyword 'ORDER'."Here is the revised code:SELECT item_id, RecvQuant, PndRcpt, TTQTY, OpenPO, [RecvQuant]-[PndRcpt]-[TTQTY] AS FinQty FROM ( SELECT DISTINCT t.item_id, ISNULL((SELECT DISTINCT sum(dbo.inventory_receipts_d.received_quantity) FROM dbo.inventory_receipts_d WHERE dbo.inventory_receipts_d.item_id = t.item_id),0) AS RecvQuant, ISNULL((SELECT DISTINCT sum(ISNULL(dbo.inventory_receipts_d.received_quantity,0)) FROM dbo.inventory_receipts_d WHERE dbo.inventory_receipts_d.posted_flag = 'N' AND dbo.inventory_receipts_d.item_id = t.item_id),0) AS PndRcpt, ISNULL((SELECT DISTINCT SUM (ISNULL (CASE WHEN dbo.inventory_activity_transfer_d.transfer_type = 'F' AND dbo.inventory_activity_transfer_d.move_from_to='W' -- AND dbo.inventory_activity_transfer_d.inv_rcpt_trx_id IS NULL AND dbo.inventory_activity_d.item_id = dbo.items.item_id THEN (-1)*isnull(dbo.inventory_activity_transfer_d.stocking_quantity,0) WHEN dbo.inventory_activity_transfer_d.transfer_type = 'T' AND dbo.inventory_activity_transfer_d.move_from_to='W' -- AND dbo.inventory_activity_transfer_d.inv_rcpt_trx_id IS NULL AND dbo.inventory_activity_d.item_id = dbo.items.item_id THEN (1)*isnull(dbo.inventory_activity_transfer_d.stocking_quantity,0) ELSE 0 END,0) ) FROM dbo.items JOIN dbo.inventory_activity_d ON dbo.items.item_id = dbo.inventory_activity_d.item_id JOIN dbo.inventory_activity_transfer_d ON dbo.inventory_activity_d.transaction_no = dbo.inventory_activity_transfer_d.transaction_no AND dbo.inventory_activity_d.line_no = dbo.inventory_activity_transfer_d.line_no WHERE dbo.items.primary_item_category_no<>'NONIN' AND dbo.items.item_id=t.item_id ),0) AS TTQTY, ISNULL((SELECT DISTINCT sum(ISNULL(dbo.po_sub_item_d.stocking_quantity,0)) FROM dbo.po_sub_item_d WHERE dbo.po_sub_item_d.order_completion_date IS NULL AND dbo.po_sub_item_d.item_id = t.item_id),0) AS OpenPO FROM dbo.items t LEFT JOIN dbo.inventory_receipts_d ON t.item_id = dbo.inventory_receipts_d.item_id LEFT JOIN dbo.po_sub_item_d ON t.item_id = dbo.po_sub_item_d.item_id LEFT JOIN dbo.po_sub_change_item_d ON t.item_id = dbo.po_sub_change_item_d.item_id AND dbo.po_sub_change_item_d.po_sub_no = dbo.po_sub_item_d.po_sub_no LEFT JOIN dbo.inventory_activity_d ON t.item_id = dbo.inventory_activity_d.item_id LEFT JOIN dbo.inventory_activity_transfer_d ON dbo.inventory_activity_d.transaction_no = dbo.inventory_activity_transfer_d.transaction_no AND dbo.inventory_activity_d.line_no = dbo.inventory_activity_transfer_d.line_no WHERE t.record_status<>'D' AND t.primary_item_category_no<>'NONIN' GROUP BY t.item_id,t.reorder_point,t.max_stocking_qty,dbo.inventory_receipts_d.item_id, dbo.inventory_activity_transfer_d.transaction_no, dbo.inventory_activity_transfer_d.line_no,dbo.inventory_activity_transfer_d.transfer_type,dbo.inventory_activity_transfer_d.stocking_quantity,dbo.inventory_receipts_d.posted_flag,/*dbo.po_sub_item_d.order_completion_date,*/dbo.inventory_activity_transfer_d.move_from_to,dbo.po_sub_item_d.po_sub_no,dbo.po_sub_item_d.order_completion_date,dbo.inventory_receipts_d.item_id,dbo.inventory_activity_d.transaction_no,dbo.inventory_activity_d.line_no,dbo.inventory_activity_d.item_id,dbo.inventory_activity_transfer_d.inv_rcpt_trx_id--ORDER BY-- t.item_id) ORDER BY item_id |
 |
|
|
Ifor
Aged Yak Warrior
700 Posts |
Posted - 2010-04-28 : 09:31:02
|
Does this work?SELECT I.item_id ,COALESCE(D1.RecvQuant, 0) AS RecvQuant ,COALESCE(D1.PndRcpt, 0) AS PndRcpt ,COALESCE(D2.TTQTY, 0) AS TTQTY ,COALESCE(D3.OpenPO, 0) AS OpenPOFROM dbo.items I LEFT JOIN ( SELECT R1.item_id ,SUM(R1.received_quantity) AS RecvQuant ,SUM(CASE WHEN R1.posted_flag = 'N' THEN R1.received_quantity END) AS PndRcpt FROM dbo.inventory_receipts_d R1 GROUP BY R1.item_id ) D1 ON I.item_id = D1.item_id LEFT JOIN ( SELECT A2.item_id ,SUM(T2.stocking_quantity * CASE WHEN T2.transfer_type = 'F' THEN -1 WHEN T2.transfer_type = 'T' THEN 1 END ) AS TTQTY FROM dbo.inventory_activity_d A2 JOIN dbo.inventory_activity_transfer_d T2 ON A2.transaction_no = T2.transaction_no AND A2.line_no = T2.line_no WHERE T2.move_from_to = 'W' GROUP BY A2.item_id ) D2 ON I.item_id = D2.item_id LEFT JOIN ( SELECT S3.item_id ,SUM(S3.stocking_quantity) AS OpenPO FROM dbo.po_sub_item_d S3 WHERE S3.order_completion_date IS NULL ) D3 ON I.item_id = D3.item_idWHERE I.record_status <> 'D' AND I.primary_item_category_no <> 'NONIN'ORDER BY item_id |
 |
|
|
vaibhavktiwari83
Aged Yak Warrior
843 Posts |
Posted - 2010-04-28 : 09:32:17
|
quote: Originally posted by actsql I removed the internal ORDER BY but I am getting the error "Msg 156, Level 15, State 1, Line 78 Incorrect syntax near the keyword 'ORDER'."Here is the revised code:SELECT item_id, RecvQuant, PndRcpt, TTQTY, OpenPO, [RecvQuant]-[PndRcpt]-[TTQTY] AS FinQty FROM ( SELECT DISTINCT t.item_id, ISNULL((SELECT DISTINCT sum(dbo.inventory_receipts_d.received_quantity) FROM dbo.inventory_receipts_d WHERE dbo.inventory_receipts_d.item_id = t.item_id),0) AS RecvQuant, ISNULL((SELECT DISTINCT sum(ISNULL(dbo.inventory_receipts_d.received_quantity,0)) FROM dbo.inventory_receipts_d WHERE dbo.inventory_receipts_d.posted_flag = 'N' AND dbo.inventory_receipts_d.item_id = t.item_id),0) AS PndRcpt, ISNULL((SELECT DISTINCT SUM (ISNULL (CASE WHEN dbo.inventory_activity_transfer_d.transfer_type = 'F' AND dbo.inventory_activity_transfer_d.move_from_to='W' -- AND dbo.inventory_activity_transfer_d.inv_rcpt_trx_id IS NULL AND dbo.inventory_activity_d.item_id = dbo.items.item_id THEN (-1)*isnull(dbo.inventory_activity_transfer_d.stocking_quantity,0) WHEN dbo.inventory_activity_transfer_d.transfer_type = 'T' AND dbo.inventory_activity_transfer_d.move_from_to='W' -- AND dbo.inventory_activity_transfer_d.inv_rcpt_trx_id IS NULL AND dbo.inventory_activity_d.item_id = dbo.items.item_id THEN (1)*isnull(dbo.inventory_activity_transfer_d.stocking_quantity,0) ELSE 0 END,0) ) FROM dbo.items JOIN dbo.inventory_activity_d ON dbo.items.item_id = dbo.inventory_activity_d.item_id JOIN dbo.inventory_activity_transfer_d ON dbo.inventory_activity_d.transaction_no = dbo.inventory_activity_transfer_d.transaction_no AND dbo.inventory_activity_d.line_no = dbo.inventory_activity_transfer_d.line_no WHERE dbo.items.primary_item_category_no<>'NONIN' AND dbo.items.item_id=t.item_id ),0) AS TTQTY, ISNULL((SELECT DISTINCT sum(ISNULL(dbo.po_sub_item_d.stocking_quantity,0)) FROM dbo.po_sub_item_d WHERE dbo.po_sub_item_d.order_completion_date IS NULL AND dbo.po_sub_item_d.item_id = t.item_id),0) AS OpenPO FROM dbo.items t LEFT JOIN dbo.inventory_receipts_d ON t.item_id = dbo.inventory_receipts_d.item_id LEFT JOIN dbo.po_sub_item_d ON t.item_id = dbo.po_sub_item_d.item_id LEFT JOIN dbo.po_sub_change_item_d ON t.item_id = dbo.po_sub_change_item_d.item_id AND dbo.po_sub_change_item_d.po_sub_no = dbo.po_sub_item_d.po_sub_no LEFT JOIN dbo.inventory_activity_d ON t.item_id = dbo.inventory_activity_d.item_id LEFT JOIN dbo.inventory_activity_transfer_d ON dbo.inventory_activity_d.transaction_no = dbo.inventory_activity_transfer_d.transaction_no AND dbo.inventory_activity_d.line_no = dbo.inventory_activity_transfer_d.line_no WHERE t.record_status<>'D' AND t.primary_item_category_no<>'NONIN' GROUP BY t.item_id,t.reorder_point,t.max_stocking_qty,dbo.inventory_receipts_d.item_id, dbo.inventory_activity_transfer_d.transaction_no, dbo.inventory_activity_transfer_d.line_no,dbo.inventory_activity_transfer_d.transfer_type,dbo.inventory_activity_transfer_d.stocking_quantity,dbo.inventory_receipts_d.posted_flag,/*dbo.po_sub_item_d.order_completion_date,*/dbo.inventory_activity_transfer_d.move_from_to,dbo.po_sub_item_d.po_sub_no,dbo.po_sub_item_d.order_completion_date,dbo.inventory_receipts_d.item_id,dbo.inventory_activity_d.transaction_no,dbo.inventory_activity_d.line_no,dbo.inventory_activity_d.item_id,dbo.inventory_activity_transfer_d.inv_rcpt_trx_id--ORDER BY-- t.item_id) A -- Alias nameORDER BY item_id
See the red partVaibhav TTo walk FAST walk ALONE To walk FAR walk TOGETHER |
 |
|
|
actsql
Starting Member
34 Posts |
Posted - 2010-04-28 : 09:53:13
|
| Thank you all. It is now working! |
 |
|
|
|
|
|
|
|