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 |
Raoulh79
Starting Member
24 Posts |
Posted - 2008-01-15 : 07:35:01
|
Dear All,I want to join this 2 queries in order to get the result i want:select t1.sz_item_ref_no, t1.sz_description, sum(t1.n0_quantity)from rdb_log_item as t1wheret1.n0_period_no = '200712310000' and t1.bl_voided =0 group by t1.sz_item_ref_no, t1.sz_descriptionorder by t1.sz_item_ref_noRESULTSET[sz_item_ref_n0], [sz_description], [n0_column_name] 1, double espresso, 55 2, espresso fredo , 34 3, coca cola, 265This are the items soldselect t1.sz_item_ref_no, t1.sz_description, sum(t1.n0_quantity)from rdb_log_item as t1wheret1.n0_period_no = '200712310000' and t1.bl_voided =1 group by t1.sz_item_ref_no, t1.sz_descriptionorder by t1.sz_item_ref_noI get the result:RESULTSETsz_item_ref_n0 , sz_description , n0_column_name 1 , double espresso , 9 2 , espresso fredo , 4 3 , coca cola , 3This are the item's VOIDED (canceled) I want to display the Items sold minus the items canceled (which means join the two queries???)RESULTSETsz_item_ref_n0, sz_description, n0_column_name 1 , double espresso , 45 2 , espresso fredo , 30 3 , coca cola , 262 i am trying several query types i think the one more close to result i want should be like this, but when i run it i get an error:select t1.sz_item_ref_no, t1.sz_description, sum(t1.n0_quantity)from rdb_log_item as t1wheret1.n0_period_no = '200712310000' and t1.bl_voided =1 andt1.bl_voided not in(select * from rdb_log_item as t2 wheret2.bl_voided =0 andt1.n0_xact_no= t2.n0_xact_no)group by t1.sz_item_ref_no, t1.sz_descriptionorder by t1.sz_item_ref_noServer: Msg 116, Level 16, State 1, Line 1Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.Any help for the query is appreciated, i am on a hurry...Thanks in advance! |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-01-15 : 08:10:41
|
As a continuation from herehttp://www.sqlteam.com/forums/topic.asp?TOPIC_ID=95539SELECT t1.sz_item_ref_no, t1.sz_description, SUM(CASE WHEN t1.bl_voided = 0 THEN t1.n0_quantity ELSE 0 END), SUM(CASE WHEN t1.bl_voided = 1 THEN t1.n0_quantity ELSE 0 END)FROM RDB_LOG_ITEM AS t1WHERE t1.n0_period_no = '200712310000' AND GROUP BY t1.sz_item_ref_no, t1.sz_descriptionORDER BY t1.sz_item_ref_no E 12°55'05.25"N 56°04'39.16" |
 |
|
imrul
Starting Member
36 Posts |
Posted - 2008-01-16 : 04:30:47
|
select t1.sz_item_ref_no, t1.sz_description, sum(t1.n0_quantity-IsNull(t2.n0_quantity,0))from(select x.sz_item_ref_no, x.sz_description, sum(x.n0_quantity) As n0_quantity from rdb_log_item As xwherex.n0_period_no = '200712310000' andx.bl_voided =0group by s.sz_item_ref_no, x.sz_description) as t1Left Outer Join(select y.sz_item_ref_no, y.sz_description, sum(y.n0_quantity) as n0_quantityfrom rdb_log_item as ywherey.n0_period_no = '200712310000' andy.bl_voided =1group by y.sz_item_ref_no, y.sz_descriptionorder by y.sz_item_ref_no) As t2Group By t1.sz_item_ref_no, t1.sz_description, |
 |
|
|
|
|
|
|