conditional-aggregation

Multi-column Conditional Aggregation

孤者浪人 提交于 2020-01-15 15:27:55
问题 In SQL Server 2008. I have things that have components in one of two states, and the table looks like this: create table Things ( ThingName varchar(10), ItemNumber INT, ItemStatus varchar(10)); INSERT INTO Things ( ThingName, ItemNumber, ItemStatus) VALUES ('a', 1, 'red'), ('a', 2, 'red'), ('a', 3, 'blue'), ('b', 1, 'red'), ('b', 2, 'red'), ('b', 3, 'red'), ('b', 4, 'red'), ('c', 1, 'blue'), ('c', 2, 'blue'), ('c', 3, 'red'); The result I need for each Thing is 1) total number of items 2)

Conditional aggregation performance

我的未来我决定 提交于 2019-11-27 13:12:06
Let us have the following data IF OBJECT_ID('dbo.LogTable', 'U') IS NOT NULL DROP TABLE dbo.LogTable SELECT TOP 100000 DATEADD(day, ( ABS(CHECKSUM(NEWID())) % 65530 ), 0) datesent INTO [LogTable] FROM sys.sysobjects CROSS JOIN sys.all_columns I want to count the number of rows, the number of last year rows and the number of last ten years rows. This can be achieved using conditional aggregation query or using subqueries as follows -- conditional aggregation query SELECT COUNT(*) AS all_cnt, SUM(CASE WHEN datesent > DATEADD(year,-1,GETDATE()) THEN 1 ELSE 0 END) AS last_year_cnt, SUM(CASE WHEN

Conditional aggregation performance

ε祈祈猫儿з 提交于 2019-11-26 22:23:20
问题 Let us have the following data IF OBJECT_ID('dbo.LogTable', 'U') IS NOT NULL DROP TABLE dbo.LogTable SELECT TOP 100000 DATEADD(day, ( ABS(CHECKSUM(NEWID())) % 65530 ), 0) datesent INTO [LogTable] FROM sys.sysobjects CROSS JOIN sys.all_columns I want to count the number of rows, the number of last year rows and the number of last ten years rows. This can be achieved using conditional aggregation query or using subqueries as follows -- conditional aggregation query SELECT COUNT(*) AS all_cnt,