[英]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"因此对于行“EOIVR - SB_Internal_LN - 3 - Operator”,它将创建一个名为“operator”的列,其值将是“option press”中的值
In this sql fiddle在这个sql 小提琴中
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我在 mssql 上是只读的
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);
We need to use the '%%' expression, which is meaningful everywhere when using like in the name column.我们需要使用 '%%' 表达式,当在 name 列中使用 like 时,它在任何地方都有意义。 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.