按组获取SQL Server表的摘要
我有一个表,用户可以在其中设置对给定事件的反应/投票,我希望能够创建一个摘要视图,以查看哪种反应在大多数情况下被投票,除以每个事件的组 样本数据如下:按组获取SQL Server表的摘要,sql,sql-server,Sql,Sql Server,我有一个表,用户可以在其中设置对给定事件的反应/投票,我希望能够创建一个摘要视图,以查看哪种反应在大多数情况下被投票,除以每个事件的组 样本数据如下: DECLARE @voteTable AS TABLE ( id INT IDENTITY(1,1) NOT NULL PRIMARY KEY CLUSTERED, eventId VARCHAR(255) NOT NULL , isGroupA INT NOT NULL , isG
DECLARE @voteTable AS TABLE (
id INT IDENTITY(1,1) NOT NULL PRIMARY KEY CLUSTERED,
eventId VARCHAR(255) NOT NULL
, isGroupA INT NOT NULL
, isGroupB INT NOT NULL
, userVote VARCHAR(255) NOT NULL
);
INSERT INTO @voteTable (eventId, isGroupA, isGroupB, userVote)
VALUES
('event1','0','0','fantastic'),
('event1','0','0','fantastic'),
('event1','0','0','fantastic'),
('event1','0','0','fantastic'),
('event1','0','0','fantastic'),
('event1','0','0','fantastic'),
('event1','0','0','meh'),
('event1','0','0','meh'),
('event1','1','0','fine'),
('event1','1','0','fine'),
('event1','1','0','great'),
('event1','1','0','ok'),
('event1','1','0','ok'),
('event1','1','0','ok'),
('event1','0','1','fine'),
('event1','0','1','great'),
('event1','0','1','great'),
('event1','0','1','ok'),
('event1','1','1','bad'),
('event1','1','1','bad'),
('event1','1','1','horrible'),
('event1','1','1','horrible'),
('event1','1','1','horrible'),
('event1','1','1','horrible'),
('event1','1','1','horrible'),
('event1','1','1','ok'),
('event2','0','0','fantastic'),
('event2','0','0','fantastic'),
('event2','0','0','fantastic'),
('event2','0','0','horrible'),
('event2','0','0','fantastic'),
('event2','0','0','fantastic'),
('event2','0','0','fine'),
('event2','0','0','great'),
('event2','1','0','meh'),
('event2','1','0','meh'),
('event2','1','0','ok'),
('event2','1','0','ok'),
('event2','1','0','ok'),
('event2','1','0','ok'),
('event2','0','1','bad'),
('event2','0','1','bad'),
('event2','0','1','bad'),
('event2','0','1','bad'),
('event2','1','1','fine'),
('event2','1','1','fine'),
('event2','1','1','great'),
('event2','1','1','great'),
('event2','1','1','ok'),
('event2','1','1','bad'),
('event2','1','1','ok'),
('event2','1','1','ok')
我想要得到的输出应该是:
eventId | groupA | groupB | everyone
----------------------------------------
event1 | horrible | horrible | fantastic
event2 | ok | bad | ok
原因是对于事件1:
- 那些对iGroupa投1票的人投了5次“糟糕”票,比任何人都多 其他投票类型
- 那些对isGroupB投1票的人也投了5次“糟糕”票,比其他人多 任何其他投票
- 无论是哪一组,event1的得票最多 “太棒了。”
- groupA中的人投了7次赞成票,比其他任何一票都多
- B组的人投了5次“不好”票,比其他任何一票都多李>
- 无论是哪一组,频率最高的投票类型为“ok”
计数(isGroupA)作为aVotes
,根据eventId和userVote进行分组,并使用RANK()
函数,但我似乎不知道如何构造整个查询
提前感谢您的帮助 您正在寻找模式。获取此信息的一种方法使用聚合和窗口函数:
select eventid,
max(case when grp = 'A' then uservote end) as groupA,
max(case when grp = 'B' then uservote end) as groupB,
max(case when grp = 'Both' then uservote end) as both
from (select eventid, uservote, grp, count(*) as cnt,
row_number() over (partition by eventid, grp order by count(*) desc) as seqnum
from votetable vt cross apply
(values (case when isGroupA = '1' then 'A' end),
(case when isGroupB = '1' then 'b' end),
('Both')
) v(grp)
where grp is not null
group by eventid, uservote, grp
) eg
where seqnum = 1
group by eventId;
是一个SQL FIDLE。使用Cte表
;
with CteCount as(
Select eventId, isGroupA, null as isGroupB,
ROW_NUMBER() over (Partition by eventId, isGroupA, UserVote Order by eventId, isGroupA, UserVote) as isGroupACount,
null as isGroupBCount,
null as isAllCount,
uservote
from @voteTable
where isGroupA = 1
UNION ALL
Select eventId, null as isGroupA, isGroupB,
null,
ROW_NUMBER() over (Partition by eventId, isGroupB, UserVote Order by eventId, isGroupB, UserVote) as isGroupBCount,
null,
uservote
from @voteTable
where isGroupB = 1
UNION ALL
Select eventId, null as isGroupA, null as isGroupB,
null,
null,
ROW_NUMBER() over (Partition by eventId, UserVote Order by eventId, UserVote) as isAllCount,
uservote
from @voteTable
),
CteSummary as(
Select eventId,
max(isGroupACount) as GroupA,
max(isGroupBCount) as GroupB,
max(isAllCount) as isAll
from CteCount
Group by eventId
)
Select
*,
(Select a.userVote from CteCount a where a.isGroupA = 1 and isGroupACount = GroupA and a.eventId = CteSummary.eventId) as GroupAvote,
(Select a.userVote from CteCount a where a.isGroupB = 1 and isGroupBCount = GroupB and a.eventId = CteSummary.eventId) as GroupBvote,
(Select a.userVote from CteCount a where a.isAllCount = isAll and a.eventId = CteSummary.eventId) as Allvote
from CteSummary
工作真的很好!只要在底部添加一个顶部(1),就可以在出现平局时进行选择=)