How to get count of last 30 days and total of count of rows in a single query

Hi All,

My requirement is to write a sql query with the following requirement

I have a table TEST which has 6500 rows out of which the created date > 30 which means records from last 30 days are like 60.So if i write a query like this 

Select Count(*) from TEST where CreatedDdate > 30

This one would give me last 30 days count but in the same query i also want the count(*) which would be 6500

So my output should be one row with 6500 in one column and 65 in second column

Can someone please help me on how to do this?

If you have any questions or if i am unclear please let me know.

Thanks

April 19th, 2015 7:08pm

Turn the filter in to a case

select count(*), count(case when created date > 30 then 1 else null end)

You need all rows back else you can't count the total so you need to pseudo-filter post retrieval

  • Marked as answer by SqlDev12 7 hours 23 minutes ago
Free Windows Admin Tool Kit Click here and download it now
April 19th, 2015 7:25pm

declare @sdate table(sdate date)
insert into @sdate 
values('20150418'),('20140418')

Select Count(*) as [total],count(case when(datediff(day,sdate,getdate()))<30 then 1 else null end) as [Last30days] from @sdate

April 19th, 2015 7:35pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics