1

I am looking to merge the following two queries into one:

select top 100 date, count(*) from sections
where content not like '%some condition%'
group by date
order by date;

select top 100 date, count(*) from sections
group by date
order by date;

It is like this question, LEFT JOIN after GROUP BY? except that I need this to work for MS SQL Server, not MySQL (the difference being that MSSQL does not allow subqueries in the from clause).

I am looking for a way to have the result set have three columns, date, the first count(*), and the second count(*).

My current solution is:

select top 100 date, 
(select count(*) from sections s1
where content not like '%some condition%' 
and s1.date = s2.date),
(select count(*) from sections s1
where s1.date=s2.date) from sections as s2
group by date
order by date;

Is there a better way to do this?

Community
  • 1
  • 1
soandos
  • 4,978
  • 13
  • 62
  • 96
  • Does your current solution works in SSMS? – Paresh J Nov 05 '14 at 11:24
  • @PareshJadhav Yes (SQL server 2014 if it makes a difference) – soandos Nov 05 '14 at 11:24
  • On merging, you have added condition, s1.date = s2.date which will eliminate rows from both tables and shows result that are match. I will suggest you to use Union of two queries. Also, add a hardcoded column 'S1' and 'S2' to identify which row belongs to which table after union. – Paresh J Nov 05 '14 at 11:27
  • @PareshJadhav So I considered using union, but then I'd get two rows for each data, and then i'd need an extra column in each, do a group by, with a group concat. Is there a way to do it without all of that? – soandos Nov 05 '14 at 11:28
  • Then Solution by Thiago is good. – Paresh J Nov 05 '14 at 11:33

2 Answers2

2

Try this:

with q1 as (select top 100 date, count(*) total from sections
            where content not like '%some condition%'
            group by date),
q2 as (select top 100 date, count(*) total from sections
       group by date)
select q1.date, q1.total total1, q2.total total2
  from q1
  join q2 on q1.date = q2.date
  order by q1.date

UPDATE:

Or this:

select date,
       count(*) total,
       sum(has_condition) total_condition
from (select top 100 
             date, 
             case when content not like '%some condition%' then 1
                  else 0 end has_condition
        from sections ) t
group by date
order by date;

I did not do any triout, but that is the idea.

Thiago Lunardi
  • 749
  • 1
  • 5
  • 19
1

This is the query that do the job with just one select:

select top 100 date, 
count(*) as count_all,
sum (
  case 
    when content not like '%some condition%' then 1
    else 0
  end
) as count_condition
from sections
group by date
order by date

I am also pasting a working snippet from AdventureWorks2012 database

select top 100 
ModifiedDate, 
count(*) as count_all,
sum (
case when CarrierTrackingNumber not like '4911%' then 1
else 0
end
) as count_condition
from [Sales].[SalesOrderDetail]
group by ModifiedDate
order by ModifiedDate

For you reference you can use subqueries after FROM clause in SQL Server.

scar80
  • 1,642
  • 2
  • 18
  • 36