Hi maybe someone can help me here …
I have a slight problem with an SQL Statement. ( On MS – SQL Server 2008)
So i have 6 Tables looking like this
ID / Company / Month / ClosedTimeStamp / Different Information
Now i need (preferrable in one Statement :P) the count of Datasets from each table grouped by Company and Month at the time it looks something like this.
And there is another thing not all tables need to have data for that Company and that Month so there can be 0 as Result for count(*)
SELECT COUNT(*) as c, Month, Company
FROM Table1 WHERE ClosedTimeStamp IS NULL
GROUP BY Company, Month
ORDER BY Company
I can do this for all the tables and just pick out the results for each company … Well if someone has any Idea i really would appreciate it 🙂
Sorry forgot something … the result should look like this:
Company / Month / CountTable1 / CountTable2 / CountTable3 / …..
Test 02 1 0 50
If it’s not possible in one statement well then i have to make it work another way. 🙂
Thanks
Lim
If your DB was normalized the query would be much simpler.
Because, your
companyandMonthare spread across 6 tables, we need to makeunionof those tables in order to get the distinct dataset of allcompany+month, as such:Note, that we need
union, notunion all, because we don’t want the same company+month pair repeated.Then, just use this dataset to query the quantities for each table: