I would like to ask a little help on using dynamic sql date header, i have data that i count transaction group by date then by hours. date range would be entered start date and end date.
my data is simple just date and time:
created_Date
'2020-01-14 13:25:20.147'
'2020-01-14 13:23:15.639'
'2020-01-14 12:27:48.896'
'2020-01-09 20:03:06.713'
'2020-01-09 19:33:05.032'
'2020-01-09 19:16:35.590'
'2020-01-09 19:08:19.788'
'2020-01-09 13:02:03.543'
'2020-01-09 12:23:12.595'
'2020-01-08 15:29:52.262'
'2020-01-08 15:17:31.247'
'2020-01-08 15:16:51.499'
'2020-01-08 13:29:47.661'
'2020-01-06 20:19:30.173'
currently found this code:
ALTER PROCEDURE "DBA"."test_trancountdaily"(@sdate datetime, @edate datetime)
BEGIN
create table #trantable(TDate varchar(100),Hour varchar(2), count varchar(1000));
insert #trantable
SELECT CAST(created_date as date) AS ForDate,
DATEPART(hour,created_date) AS OnHour,
COUNT(*) AS Totals
FROM prescription
WHERE created_date >= @sdate and created_date <= @edate
GROUP BY CAST(created_date as date),
DATEPART(hour,created_date)
ORDER BY CAST(created_date as date),
DATEPART(hour,created_date) asc;
select * from #trantable;
END
my data are created_date datetime and would count how many transaction that is inside a Hour
but would like an output like this:
HR | 2020-01-01 | 2020-01-02 | 2020-01-03 etc |
---|---|---|---|
1 | 1 | 0 | 3 |
2 | 0 | 1 | 1 |
3 | 1 | 1 | 1 |
4 | 1 | 0 | 2 |
thanks
bolivar1985