Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Consecutive Queries

$
0
0

Hi,  I would like to perform the following query eleven times and put the results in a single result set.  Two values will change with each itteration: s_id, '5.9.2013' (staff ID and a particular date).

 

WITH CTE AS
(
SELECT s_fname, s_lname, s_id, MONTH(e_date) AS [MONTH], ROW_NUMBER() OVER(PARTITION BY DAY(e_date) ORDER BY staff.s_id) AS Row
FROM client INNER JOIN events ON client.c_id = events.e_case_no
INNER JOIN staff ON staff.s_id = events.e_staff
WHERE e_date BETWEEN '05.09.2013' AND '08.31.2013'
AND s_id = '7660'
)
SELECT s_fname + ' ' + s_lname AS 'Staff', s_id, [MONTH], COUNT(*) AS [Events]
FROM CTE
GROUP BY s_fname, s_lname,  s_id,[MONTH]
What is the best practice?  Thank you.

Viewing all articles
Browse latest Browse all 23857

Trending Articles