34,576
社区成员
发帖
与我相关
我的任务
分享
declare @send table(
[serverId] [int] NOT NULL,
[actorId] [bigint] NOT NULL,
[uuid] [bigint] NOT NULL,
[redPackageId] [int] NOT NULL,
[WriteTime] [datetime] NOT NULL
)
insert @send select 1,100,1,2,'2017/12/12 17:45:27'
union all select 1,100,2,2,'2017/12/12 17:45:27'
union all select 1,101,3,2,'2017/12/12 17:45:27'
union all select 1,101,4,2,'2017/12/12 17:45:27'
union all select 2,102,5,2,'2017/12/12 17:45:27'
union all select 2,102,6,2,'2017/12/12 17:45:27'
union all select 2,102,7,2,'2017/12/12 17:45:27'
union all select 3,103,9,2,'2017/12/12 17:45:27'
declare @get table(
[serverId] [int] NOT NULL,
[actorId] [bigint] NOT NULL,
[uuid] [bigint] NOT NULL,
[type] [int] NOT NULL,
[redPackageId] [int] NOT NULL,
[state] [int] NOT NULL,
[money] [int] NOT NULL,
[WriteTime] [datetime] NOT NULL
)
insert @get SELECT 1,100,1,1,0,1,1000,'2017/12/12 17:45:27'
union all select 1,100,2,1,0,1,1000,'2017/12/12 17:45:27'
union all select 1,101,2,1,0,1,1200,'2017/12/12 17:45:27'
union all select 1,101,2,1,0,1,1300,'2017/12/12 17:45:27'
union all select 1,101,2,1,0,1,1400,'2017/12/12 17:45:27'
union all select 1,101,6,1,0,1,1000,'2017/12/12 17:45:27'
union all select 2,102,7,1,0,1,1000,'2017/12/12 17:45:27'
union all select 2,102,8,1,0,1,1000,'2017/12/12 17:45:27'
union all select 2,102,9,1,0,1,1000,'2017/12/12 17:45:27'
union all select 3,104,9,1,0,1,1000,'2017/12/12 17:45:27'
SELECT CASE WHEN get.actorId IS NOT NULL THEN get.actorId
ELSE [send].actorId
END AS actorId ,
ISNULL(getNumber,0) AS getNumber ,
ISNULL(sendNumber,0) AS sendNumber
FROM ( SELECT actorId ,
COUNT(1) AS getNumber
FROM @get
GROUP BY actorId
) get
FULL JOIN ( SELECT actorId ,
COUNT(1) AS sendNumber
FROM @send
GROUP BY actorId
) send ON send.actorId = get.actorId
ORDER BY getNumber + sendNumber DESC
SET NOCOUNT ON
declare @send table(
[serverId] [int] NOT NULL,
[actorId] [bigint] NOT NULL,
[uuid] [bigint] NOT NULL,
[redPackageId] [int] NOT NULL,
[WriteTime] [datetime] NOT NULL
)
insert @send select 1,100,1,2,'2017/12/12 17:45:27'
union all select 1,100,2,2,'2017/12/12 17:45:27'
union all select 1,101,3,2,'2017/12/12 17:45:27'
union all select 1,101,4,2,'2017/12/12 17:45:27'
union all select 2,102,5,2,'2017/12/12 17:45:27'
union all select 2,102,6,2,'2017/12/12 17:45:27'
union all select 2,102,7,2,'2017/12/12 17:45:27'
union all select 3,103,9,2,'2017/12/12 17:45:27'
declare @get table(
[serverId] [int] NOT NULL,
[actorId] [bigint] NOT NULL,
[uuid] [bigint] NOT NULL,
[type] [int] NOT NULL,
[redPackageId] [int] NOT NULL,
[state] [int] NOT NULL,
[money] [int] NOT NULL,
[WriteTime] [datetime] NOT NULL
)
insert @get select 1,100,1,1,0,1,1000,'2017/12/12 17:45:27'
union all select 1,100,2,1,0,1,1000,'2017/12/12 17:45:27'
union all select 1,101,2,1,0,1,1200,'2017/12/12 17:45:27'
union all select 1,101,2,1,0,1,1300,'2017/12/12 17:45:27'
union all select 1,101,2,1,0,1,1400,'2017/12/12 17:45:27'
union all select 1,101,6,1,0,1,1000,'2017/12/12 17:45:27'
union all select 2,102,7,1,0,1,1000,'2017/12/12 17:45:27'
union all select 2,102,8,1,0,1,1000,'2017/12/12 17:45:27'
union all select 2,102,9,1,0,1,1000,'2017/12/12 17:45:27'
union all select 3,104,9,1,0,1,1000,'2017/12/12 17:45:27'
;WITH cte1 AS(
SELECT t.actorId,COUNT(1) AS sendNumber
FROM @send t
GROUP BY t.actorId
)
,cte2 AS (
SELECT t.actorId,COUNT(1) AS getNumber
FROM @get t
GROUP BY t.actorId
)
SELECT
CASE WHEN cte1.actorId IS NOT NULL THEN cte1.actorId ELSE cte2.actorId END
AS actorId
,ISNULL(cte1.sendNumber,0) AS sendNumber
,ISNULL(cte2.getNumber,0) AS getNumber
FROM cte1 FULL JOIN cte2 ON cte1.actorId=cte2.actorId
/*
actorId sendNumber getNumber
-------------------- ----------- -----------
100 2 2
101 2 4
102 3 3
103 1 0
104 0 1
*/