Question

I am trying to combine 2 results from the table using the following SQL:

select * (
  SELECT Datepart(mm,starttime) as Month, COUNT(*) TotalCountSub
  from data 
  where usertype='subscriber'
  group by Datepart(mm,starttime)
  having count(*) > 1
 ) a
 LEFT JOIN (
   SELECT Datepart(mm,starttime) as Month, COUNT(*) TotalCountCust
   from data
   where usertype='Customer'
   group by Datepart(mm,starttime)
   having count(*) > 1
 ) b on a.Month = b.Month
order by Datepart(mm,starttime) ASC

What i am trying to achieve here is to combine both results into 1 table:

Table1

Month|TotalCountSub
1    |50
2    |123
3    |14
4    |91

Table2

Month|TotalCountCust
1    |80
2    |465
3    |79
4    |84

Results should be:

Month|TotalCountSub|TotalCountCust
1    |50           |80
2    |123          |465
3    |14           |79
4    |91           |84
Était-ce utile?

La solution

Hope this works:

select a.Month, a.TotalCountSub, b.TotalCountCust from (
  SELECT Datepart(mm,starttime) as Month, COUNT(*) TotalCountSub
  from data 
  where usertype='subscriber'
  group by Datepart(mm,starttime)
  having count(*) > 1
 ) a
 INNER JOIN (
   SELECT Datepart(mm,starttime) as Month, COUNT(*) TotalCountCust
   from data
   where usertype='Customer'
   group by Datepart(mm,starttime)
   having count(*) > 1
 ) b 
ON a.Month = b.Month
order by a.Month ASC
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top