I’m really not good with SQL and i just want to write a query that shows some log data in a chart.
I have a SQL Server table where i log requests made to a web service.
ID RequestedFunction RequestDateTime
1 'Confirm()' 2010-12-15 13:27:01.234
2 'Register()' 2010-12-16 12:27:00.678
3 'Confirm()' 2010-12-16 21:00:05.456
Now i’d like write a query that return me the number of requests made per day grouped by RequestedFunction; that return me a dataset like this.
Day ConfirmFunctionRequests RegisterFunctionRequests
15 1 0
16 1 1
Is this possible? All i could come up with so far is something like this,
;WITH CTE AS
(
SELECT id,
[Day] = CAST(day(requestDateTime) AS INT),
RequestedFunction
FROM [Log]
WHERE month(RequestDateTime)=12 AND
year(RequestDateTime)=2010
)
SELECT [Day],COUNT(ID) AS [#Requests] FROM CTE GROUP BY [Day]
Which returns the number of requests per day, without differentiating between different types of requests. How can i write a query that counts the requests in to two different columns based on the value in ‘RequestedFunction’?
This can be done using a PIVOT but a quick and simple solution would be something like