MSSQL: Create column based on row when using count

How to create columns based on columns.

so for row "EOIVR - SB_Internal_LN - 3 - Operator", it would create a column called "operator" and the value would be the value from "option press"

In this sql fiddle

the following table

CREATE TABLE IVRInterval
    ([cLevelName] varchar(50), [nLevel] FLOAT(20), [I3TimeStampGMT] DATETIME, [cExitPath] varchar(20))
;
INSERT INTO IVRInterval
    ([cLevelName], [nLevel], [I3TimeStampGMT], [cExitPath])
VALUES
    ('EOIVR - SB_Internal_LN - 3 - Operator', '5', '2017-10-05 09:30:00.000', 'Workgroup Queue'),
    ('EOIVR - SB_Internal_LN - 3 - Operator', '5', '2017-10-05 10:00:00.000', 'Workgroup Queue'),
    ('EOIVR - SB_Internal_LN - 3 - Operator', '5', '2017-10-11 11:30:00.000', 'Workgroup Queue'),
    ('EOIVR - SB_Internal_LN - 3 - Operator', '5', '2017-10-11 12:30:00.000', 'Workgroup Queue'),
    ('EOIVR - SB_Internal_LN - 1 - SD', '5', '2017-10-11 13:30:00.000', 'Workgroup Queue'),
    ('EOIVR - SB_Internal_LN - 1 - SD', '5', '2017-10-09 08:30:00.000', 'Workgroup Queue'),
    ('EOIVR - SB_Internal_LN - 1 - SD', '5', '2017-10-09 11:00:00.000', '*'),
    ('EOIVR - SB_Internal_LN - 1 - SD', '5', '2017-10-11 15:00:00.000', 'Workgroup Queue'),
    ('EOIVR - SB_Internal_LN - 1 - SD', '5', '2017-10-06 09:30:00.000', 'Workgroup Queue'),
    ('EOIVR - SB_Internal_LN - 1 - SD', '5', '2017-10-06 11:30:00.000', 'Workgroup Queue'),
    ('EOIVR - SB_Internal_LN - 1 - SD', '5', '2017-10-09 14:30:00.000', '*'),
    ('EOIVR - SB_Internal_LN - 2 - Lobby', '5', '2017-10-06 13:30:00.000', 'Workgroup Queue'),
    ('EOIVR - SB_Internal_LN - 2 - Lobby', '5', '2017-10-09 14:00:00.000', 'Workgroup Queue'),
    ('EOIVR - SB_Internal_LN - 2 - Lobby', '5', '2017-10-04 07:30:00.000', 'Workgroup Queue'),
    ('EOIVR - SB_Internal_LN - 2 - Lobby', '5', '2017-10-04 08:30:00.000', 'Workgroup Queue'),
    ('EOIVR - SB_Internal_LN - 2 - Lobby', '5', '2017-10-10 08:00:00.000', '*')

i run this query

select 

Convert(date,I3TimeStampGMT) as 'Dates',
(select cLevelName) as 'Options Name',
count(I3TimeStampGMT) as 'Option Press'

from IVRInterval

where
I3TimeStampGMT between '2017-10-04 00:00:00' and '2017-10-11 23:59:59'
and cLevelName like '%%EOIVR - SB_Internal_LN -%%'
and nLevel = '5'
and not cExitPath = '*'

group by cLevelName, Convert(date,I3TimeStampGMT)

I get this result

Dates       Options Name                         Option Press
2017-10-04  EOIVR - SB_Internal_LN - 2 - Lobby      2
2017-10-05  EOIVR - SB_Internal_LN - 3 - Operator   2
2017-10-06  EOIVR - SB_Internal_LN - 1 - SD         2
2017-10-06  EOIVR - SB_Internal_LN - 2 - Lobby      1
2017-10-09  EOIVR - SB_Internal_LN - 1 - SD         1
2017-10-09  EOIVR - SB_Internal_LN - 2 - Lobby      1
2017-10-11  EOIVR - SB_Internal_LN - 1 - SD         2
2017-10-11  EOIVR - SB_Internal_LN - 3 - Operator   2

I would like to have my result like this

Date       Lobby    SD    Operator
2017-10-11  0       1       1

I got read-only on the mssql

Upvotes: 1

Views: 240

Answers (2)

mesutoncel
mesutoncel

Reputation: 1

We need to use the '%%' expression, which is meaningful everywhere when using like in the name column. Because if there is a gap at the end of the word, it will misinterpret the desired word and give wrong result when grouping.

create table stack(Dates date,name nvarchar(100))

insert into stack values

('2017-10-04', 'EOIVR - SB_Internal_LN - 2 - Lobby  ') ,
('2017-10-05', 'EOIVR - SB_Internal_LN - 3 - Operator') ,
('2017-10-06', 'EOIVR - SB_Internal_LN - 1 - SD') ,
('2017-10-06', 'EOIVR - SB_Internal_LN - 2 - Lobby') ,
('2017-10-09', 'EOIVR - SB_Internal_LN - 1 - SD') ,
('2017-10-09', 'EOIVR - SB_Internal_LN - 2 - Lobby') ,
('2017-10-11', 'EOIVR - SB_Internal_LN - 1 - SD') ,
('2017-10-11', 'EOIVR - SB_Internal_LN - 3 - Operator')

SELECT Dates as Date,
     COUNT(CASE WHEN name LIKE '%Lobby%' THEN 1 END)    AS Lobby,
     COUNT(CASE WHEN name LIKE '%SD%' THEN 1 END)       AS SD,
     COUNT(CASE WHEN name LIKE '%Operator%' THEN 1 END) AS Operator
FROM stack
GROUP BY Dates

Upvotes: 0

Lukasz Szozda
Lukasz Szozda

Reputation: 175646

You could use conditional aggregation:

SELECT CAST([I3TimeStampGMT]  AS DATE) AS [date],
     COUNT(CASE WHEN [cLevelName] LIKE '%Lobby' THEN 1 END)    AS Lobby,
     COUNT(CASE WHEN [cLevelName] LIKE '%SD' THEN 1 END)       AS SD,
     COUNT(CASE WHEN [cLevelName] LIKE '%Operator' THEN 1 END) AS Operator
FROM IVRInterval
GROUP BY CAST([I3TimeStampGMT]  AS DATE);

Rextester Demo

Upvotes: 1

Related Questions