Reputation: 61
select Executive, count([Cat A]) AS [1-3], count([Cat B]) AS [4-6],count([Cat C]) AS [7-10],count([Cat D]) AS [11-15],count([Cat E]) AS [16+]
from (
select
Executive,
case when [Next Follow Up Date] < GETDATE() and (DATEDIFF(d,[next follow up date],getdate()) > 0 and DATEDIFF(d,[next follow up date],getdate()) < 4)
then 'A'
end as [Cat A],
case when [Next Follow Up Date] < GETDATE() and (DATEDIFF(d,[next follow up date],getdate()) > 3 and DATEDIFF(d,[next follow up date],getdate()) < 7)
then 'B'
end as [Cat B],
case when [Next Follow Up Date] < GETDATE() and (DATEDIFF(d,[next follow up date],getdate()) > 6 and DATEDIFF(d,[next follow up date],getdate()) < 11)
then 'C'
end as [Cat C],
case when [Next Follow Up Date] < GETDATE() and (DATEDIFF(d,[next follow up date],getdate()) > 10 and DATEDIFF(d,[next follow up date],getdate()) < 16)
then 'D'
end as [Cat D],
case when [Next Follow Up Date] < GETDATE() and (DATEDIFF(d,[next follow up date],getdate()) > 15)
then 'E'
end as [Cat E]
from vw_FollowUps)
as Table1
group by Executive, [Cat A], [Cat B], [Cat C], [Cat D], [Cat E]
output is coming like below
Executive 1-3 4-6 7-10 11-15 16+
Rani 0 0 0 0 0
Rani 0 1 0 2 0
Rani 0 0 1 0 0
but i need like this below output
Executive 1-3 4-6 7-10 11-15 16+
Rani 0 1 1 2 0
Upvotes: 4
Views: 125
Reputation: 16310
You can use Common Table Expression(CTE)
to get above result...You can have new query with CTE as following:(Not tested but the way of implementation is right)
WITH CTETABLE AS
(
select Executive, count([Cat A]) AS [1-3], count([Cat B]) AS [4-6],count([Cat C]) AS [7-10],count([Cat D]) AS [11-15],count([Cat E]) AS [16+]
from (
select
Executive,
case when [Next Follow Up Date] < GETDATE() and (DATEDIFF(d,[next follow up date],getdate()) > 0 and DATEDIFF(d,[next follow up date],getdate()) < 4)
then 'A'
end as [Cat A],
case when [Next Follow Up Date] < GETDATE() and (DATEDIFF(d,[next follow up date],getdate()) > 3 and DATEDIFF(d,[next follow up date],getdate()) < 7)
then 'B'
end as [Cat B],
case when [Next Follow Up Date] < GETDATE() and (DATEDIFF(d,[next follow up date],getdate()) > 6 and DATEDIFF(d,[next follow up date],getdate()) < 11)
then 'C'
end as [Cat C],
case when [Next Follow Up Date] < GETDATE() and (DATEDIFF(d,[next follow up date],getdate()) > 10 and DATEDIFF(d,[next follow up date],getdate()) < 16)
then 'D'
end as [Cat D],
case when [Next Follow Up Date] < GETDATE() and (DATEDIFF(d,[next follow up date],getdate()) > 15)
then 'E'
end as [Cat E]
from vw_FollowUps)
as Table1
group by Executive, [Cat A], [Cat B], [Cat C], [Cat D], [Cat E]
)
SELECT Executive, MAX([1-3]),MAX([4-6]),MAX([7-10]),MAX([11-15],MAX([16+]) FROM CTETABLE GROUP BY Executive;
Upvotes: 1
Reputation: 107716
An additional SELECT (the other two answers) is extraneous.
Simple GROUP BY only the Executive
column.
select Executive, count([Cat A]) AS [1-3], count([Cat B]) AS [4-6],count([Cat C]) AS [7-10],count([Cat D]) AS [11-15],count([Cat E]) AS [16+]
from (
select
Executive,
case when [Next Follow Up Date] < GETDATE() and (DATEDIFF(d,[next follow up date],getdate()) > 0 and DATEDIFF(d,[next follow up date],getdate()) < 4)
then 'A'
end as [Cat A],
case when [Next Follow Up Date] < GETDATE() and (DATEDIFF(d,[next follow up date],getdate()) > 3 and DATEDIFF(d,[next follow up date],getdate()) < 7)
then 'B'
end as [Cat B],
case when [Next Follow Up Date] < GETDATE() and (DATEDIFF(d,[next follow up date],getdate()) > 6 and DATEDIFF(d,[next follow up date],getdate()) < 11)
then 'C'
end as [Cat C],
case when [Next Follow Up Date] < GETDATE() and (DATEDIFF(d,[next follow up date],getdate()) > 10 and DATEDIFF(d,[next follow up date],getdate()) < 16)
then 'D'
end as [Cat D],
case when [Next Follow Up Date] < GETDATE() and (DATEDIFF(d,[next follow up date],getdate()) > 15)
then 'E'
end as [Cat E]
from vw_FollowUps)
as Table1
group by Executive;
Upvotes: 2
Reputation: 1062
select Executive, SUM([1-3]), SUM([4-6]), SUM([7-10]), SUM([11-15]), SUM([16+]) FROM
(
select Executive, count([Cat A]) AS [1-3], count([Cat B]) AS [4-6],count([Cat C]) AS [7-10],count([Cat D]) AS [11-15],count([Cat E]) AS [16+]
from (
select
Executive,
case when [Next Follow Up Date] < GETDATE() and (DATEDIFF(d,[next follow up date],getdate()) > 0 and DATEDIFF(d,[next follow up date],getdate()) < 4)
then 'A'
end as [Cat A],
case when [Next Follow Up Date] < GETDATE() and (DATEDIFF(d,[next follow up date],getdate()) > 3 and DATEDIFF(d,[next follow up date],getdate()) < 7)
then 'B'
end as [Cat B],
case when [Next Follow Up Date] < GETDATE() and (DATEDIFF(d,[next follow up date],getdate()) > 6 and DATEDIFF(d,[next follow up date],getdate()) < 11)
then 'C'
end as [Cat C],
case when [Next Follow Up Date] < GETDATE() and (DATEDIFF(d,[next follow up date],getdate()) > 10 and DATEDIFF(d,[next follow up date],getdate()) < 16)
then 'D'
end as [Cat D],
case when [Next Follow Up Date] < GETDATE() and (DATEDIFF(d,[next follow up date],getdate()) > 15)
then 'E'
end as [Cat E]
from vw_FollowUps)
as Table1
group by Executive, [Cat A], [Cat B], [Cat C], [Cat D], [Cat E] ) AS T
group by Executive
Upvotes: 0
Reputation: 65496
Add a select around the entire thing:
SELECT Executive SUM([1-3]), SUM([4-6]), SUM([7-10]), SUM([11-15]), SUM([16+])
FROM
(
....YOUR BIG SELECT....
)
GROUP BY Executive
So like this:
SELECT Executive SUM([1-3]), SUM([4-6]), SUM([7-10]), SUM([11-15]), SUM([16+])
FROM
(
select Executive, count([Cat A]) AS [1-3], count([Cat B]) AS [4-6],count([Cat C]) AS [7-10],count([Cat D]) AS [11-15],count([Cat E]) AS [16+]
from (
select
Executive,
case when [Next Follow Up Date] < GETDATE() and (DATEDIFF(d,[next follow up date],getdate()) > 0 and DATEDIFF(d,[next follow up date],getdate()) < 4)
then 'A'
end as [Cat A],
case when [Next Follow Up Date] < GETDATE() and (DATEDIFF(d,[next follow up date],getdate()) > 3 and DATEDIFF(d,[next follow up date],getdate()) < 7)
then 'B'
end as [Cat B],
case when [Next Follow Up Date] < GETDATE() and (DATEDIFF(d,[next follow up date],getdate()) > 6 and DATEDIFF(d,[next follow up date],getdate()) < 11)
then 'C'
end as [Cat C],
case when [Next Follow Up Date] < GETDATE() and (DATEDIFF(d,[next follow up date],getdate()) > 10 and DATEDIFF(d,[next follow up date],getdate()) < 16)
then 'D'
end as [Cat D],
case when [Next Follow Up Date] < GETDATE() and (DATEDIFF(d,[next follow up date],getdate()) > 15)
then 'E'
end as [Cat E]
from vw_FollowUps)
as Table1
group by Executive, [Cat A], [Cat B], [Cat C], [Cat D], [Cat E]
)
GROUP BY Executive
Upvotes: 2