How to only have 1 query in SQL Server -
i created 3 queries in sql server, want know if can have 1 query instead of these 3 queries?
these 3 queries:
select count(jonumber) january joborders jodate between '01/01/2017' , '01/31/2017' select count(jonumber) february joborders jodate between '02/01/2017' , '02/28/2017' select count(jonumber) total joborders jodate between '01/01/2017' , '02/28/2017'
without knowing specific flavor of sql, can offer following single query uses conditional aggregation arrive @ same results:
select sum(case when jodate between '2017-01-01' , '2017-01-31' 1 else 0 end) jancount, sum(case when jodate between '2017-02-01' , '2017-02-28' 1 else 0 end) febcount, count(*) totalcount joborders
since using sql server, better approach group by
month (and year), , let database worry counting records:
select cast(month(jodate) varchar(2)) + '-' + cast(year(jodate) varchar(4)) monthyear, count(*) numorders joborders jodate between '2017-01-01' , '2017-02-28' group cast(month(jodate) varchar(2)) + '-' + cast(year(jodate) varchar(4)) rollup
Comments
Post a Comment