I am having some real trouble with this, below is the code, I am try to get counts from two different tables then group them by date.
declare @fromdate as date
declare @todate as date
set @fromdate = CONVERT(date,GETDATE()-3,101)
set @todate = CONVERT(date,GETDATE(),101)
USE database
SELECT
CONVERT(date,created_date,101) as OrdDate,
col1= (SELECT COUNT(distinct cust_id)
FROM table1
WHERE created_date BETWEEN @fromdate and @todate),
col2= (SELECT COUNT(distinct cust_id)
FROM table1
WHERE [status] LIKE 'P%' and created_date BETWEEN @fromdate and @todate),
col3= (SELECT COUNT(distinct cust_id)
FROM table2
WHERE created_date BETWEEN @fromdate and @todate),
col4= (SELECT COUNT(distinct cust_id)
FROM table2
WHERE created_date between @fromdate and @todate and result_error like 'FAIL%')
FROM table1
WHERE created_date BETWEEN @fromdate and @todate
GROUP BY CONVERT(date,created_date,101)
ORDER BY CONVERT(date,created_date,101)
It is counting all of the days together, I have tried adding Group by to each expression, no go. Any suggestions?
OrdDate col1 col2 col3 col4
2011-12-19 5000 4000 3000 2000
2011-12-20 5000 4000 3000 2000
2011-12-21 5000 4000 3000 2000
What I want is something like this
OrdDate col1 col2 col3 col4
2011-12-19 1500 1500 500 750
2011-12-20 2500 750 1000 1000
2011-12-21 1000 1750 1500 250
You have to generate a list of dates first, then JOIN back to that for each count.
This will remove issues with different row counts for each aggregate