sql server - Combining several query results into one result -
i using sql server. have 5 tables (mtable, c1table, c2table, c3table, c4table).
in mtable have 4 queries , each 1 of them gives correct result:
query 1:
select mtable.cid, mtable.col1, mtable.col2, mtable.col4, mtable.col5, mtable.col6, mtable.col7, mtable.col8, mtable.col9, mtable.col10, mtable.col11, c1table.col12, c1table.col13, c1table.col14, c1table.col15 mtable left outer join c1table on mtable.col2 = c1table.col2 (mtable.col6 = 0) , (mtable.col5 = 0) , (mtable.col4 = 0) , (mtable.col2 > 0)
query 2:
select mtable.cid, mtable.col1, mtable.col2, mtable.col4, mtable.col5, mtable.col6, mtable.col7, mtable.col8, mtable.col9, mtable.col10, mtable.col11, c2table.col12, c2table.col13, c2table.col14, c2table.col15 mtable left outer join c2table on mtable.col4 = c2table.col4 (mtable.col6 = 0) , (mtable.col5 = 0) , (mtable.col4 > 0)
query 3:
select mtable.cid, mtable.col1, mtable.col2, mtable.col4, mtable.col5, mtable.col6, mtable.col7, mtable.col8, mtable.col9, mtable.col10, mtable.col11, c3table.col12, c3table.col13, c3table.col14, c3table.col15 mtable left outer join c3table on mtable.col5 = c3table.col5 (mtable.col6 = 0) , (mtable.col5 > 0)
query 4:
select mtable.cid, mtable.col1, mtable.col2, mtable.col4, mtable.col5, mtable.col6, mtable.col7, mtable.col8, mtable.col9, mtable.col10, mtable.col11, c4table.col12, c4table.col13, c4table.col14, c4table.col15 mtable left outer join c4table on mtable.col6 = c4table.col6 (mtable.col6 > 0)
now should able create query combines results one. gives correct result amount:
select sum(thiscount) expr1 (select count(*) thiscount mtable left outer join c1table on mtable.col1 = c1table.col1 (mtable.col4 = 0) , (mtable.col3 = 0) , (mtable.col2 = 0) , (mtable.col1 > 0) union select count(*) thiscount mtable mtable_3 left outer join c2table on mtable_3.col2 = c2table.col2 (mtable_3.col4 = 0) , (mtable_3.col3 = 0) , (mtable_3.col2 > 0) union select count(*) thiscount mtable mtable_2 left outer join c3table on mtable_2.col3 = c3table.col3 (mtable_2.col4 = 0) , (mtable_2.col3 > 0) union select count(*) thiscount mtable mtable_1 left outer join c4table on mtable_1.col4 = c4table.col4 (mtable_1.col4 > 0) ) x
but need have list of each result (now should have result of 3 000 rows).
i have tried "union all", getting error. doing wrong?
thanks
create temp table each query , combine tables on column need sum based on requirement
select mtable.cid ,mtable.col1 ,mtable.col2 ,mtable.col4 ,mtable.col5 ,mtable.col6 ,mtable.col7 ,mtable.col8 ,mtable.col9 ,mtable.col10 ,mtable.col11 ,c1table.col12 ,c1table.col13 ,c1table.col14 ,c1table.col15 #a mtable left outer join c1table on mtable.col2 = c1table.col2 (mtable.col6 = 0) , (mtable.col5 = 0) , (mtable.col4 = 0) , (mtable.col2 > 0) select mtable.cid ,mtable.col1 ,mtable.col2 ,mtable.col4 ,mtable.col5 ,mtable.col6 ,mtable.col7 ,mtable.col8 ,mtable.col9 ,mtable.col10 ,mtable.col11 ,c2table.col12 ,c2table.col13 ,c2table.col14 ,c2table.col15 #b mtable left outer join c2table on mtable.col4 = c2table.col4 (mtable.col6 = 0) , (mtable.col5 = 0) , (mtable.col4 > 0) select mtable.cid ,mtable.col1 ,mtable.col2 ,mtable.col4 ,mtable.col5 ,mtable.col6 ,mtable.col7 ,mtable.col8 ,mtable.col9 ,mtable.col10 ,mtable.col11 ,c3table.col12 ,c3table.col13 ,c3table.col14 ,c3table.col15 #c mtable left outer join c3table on mtable.col5 = c3table.col5 (mtable.col6 = 0) , (mtable.col5 > 0) select mtable.cid ,mtable.col1 ,mtable.col2 ,mtable.col4 ,mtable.col5 ,mtable.col6 ,mtable.col7 ,mtable.col8 ,mtable.col9 ,mtable.col10 ,mtable.col11 ,c4table.col12 ,c4table.col13 ,c4table.col14 ,c4table.col15 #d mtable left outer join c4table on mtable.col6 = c4table.col6 (mtable.col6 > 0) select sum(value) (select count(*) value #a union select count(*) value #b union select count(*) value #c union select count(*) value #d) c
Comments
Post a Comment