sql server 2008 - SQL - Multiple Table Joins -
sql server 2008 - SQL - Multiple Table Joins -
hi maybe can help me here ... have slight problem sql statement. ( on ms - sql server 2008) have 6 tables looking this
id / company / month / closedtimestamp / different info
now need (preferrable in 1 statement :p) count of datasets each table grouped company , month @ time looks this. , there thing not tables need have info company , month there can 0 result count(*)
select count(*) c, month, company table1 closedtimestamp null grouping company, month order company
i can tables , pick out results each company ... if has thought appreciate :)
sorry forgot ... result should this:
company / month / counttable1 / counttable2 / counttable3 / ..... test 02 1 0 50
if it's not possible in 1 statement have create work way. :)
thanks
lim
if db normalized query much simpler.
because, company
, month
spread across 6 tables, need create union
of tables in order distinct dataset of company
+month
, such:
select company, month table1 union select company, month table2 union select company, month table3 union select company, month table4 union select company, month table5 union select company, month table6
note, need union
, not union all
, because don't want same company+month pair repeated.
then, utilize dataset query quantities each table:
select t.company, t.month, (select count(*) table1 company = t.company , month = t.month , closedtimestamp null) qt1, (select count(*) table2 company = t.company , month = t.month , closedtimestamp null) qt2, (select count(*) table3 company = t.company , month = t.month , closedtimestamp null) qt3, (select count(*) table4 company = t.company , month = t.month , closedtimestamp null) qt4, (select count(*) table5 company = t.company , month = t.month , closedtimestamp null) qt5, (select count(*) table6 company = t.company , month = t.month , closedtimestamp null) qt6 ( select company, month table1 union select company, month table2 union select company, month table3 union select company, month table4 union select company, month table5 union select company, month table6 ) t order t.company
sql sql-server-2008
Comments
Post a Comment