Sql选择已发送和已接收数据的计数
我需要为已发送和已接收事务的报告编写查询Sql选择已发送和已接收数据的计数,sql,sql-server,tsql,join,subquery,Sql,Sql Server,Tsql,Join,Subquery,我需要为已发送和已接收事务的报告编写查询 Status_Id Status_dt Status 1 4/1/2013 sent 1 4/1/2013 sent 2 4/2/2013 sent 3 4/3/2013 sent 1 4/1/2013 Received 1
Status_Id Status_dt Status
1 4/1/2013 sent
1 4/1/2013 sent
2 4/2/2013 sent
3 4/3/2013 sent
1 4/1/2013 Received
1 4/4/2013 Received
2 4/4/2013 received
在特定日期发送的交易可以在任何日期接收
从上面
2013年4月1日发送的交易有两笔(id为1),该id的wch于2013年4月1日发送,分别于2013年4月1日和2013年4月4日收到
所以o/p应该是
dt sent_count received_count
4/1/2013 2 2
2013年4月2日发送的交易为一笔(对于id 2),对于该id,wch于2013年4月2日发送,并于2013年4月4日收到
所以o/p应该是
dt sent_count received_count
4/2/2013 1 1
dt sent_count received_count
4/3/2013 1 0
2013年4月3日发送的交易为一笔(对于id 3),对于此id,2013年4月3日发送的wch尚未收到
所以o/p应该是
dt sent_count received_count
4/2/2013 1 1
dt sent_count received_count
4/3/2013 1 0
因此,如果我在2013年4月5日运行查询,输出应该是:
dt sent_count received_count
4/1/2013 2 2
4/2/2013 1 1
4/3/2013 1 0
对于已发送计数,我可以将查询写为:
select status_dt, count(*)
from table
where status = 'sent'
group by status_dt
对于接收计数,我应该写什么查询?对于接收计数,您需要返回到发送计数以获取日期。如果状态ID是唯一的,则查询如下:
select t.status_dt, count(*)
from table t join
table tres
on t.status_id = tres.status_id and
t.status = 'sent' and
tres.status = 'received'
group by status_dt;
相反,一种方法是分配一个唯一的id:
select t.status_dt, count(*) as SentCount, count(tres.status_id) as ReceivedCount
from (select t.*,
row_number() over (partition by status_id order by status_dt) as seqnum
from table t
where tres.status = 'sent'
) t join
(select tres.*,
row_number() over (partition by status_id order by status_dt) as seqnum
from table tres
where tres.status = 'received'
) tres
on t.status_id = tres.status_id and
t.seqnum = tres.seqnum
group by status_dt;
此唯一id根据记录中的日期(分别用于发送和接收)枚举具有给定状态\u id
的所有内容。这是可行的,因为接收总是在发送之后。因此,第n次接收总是在第n次发送之后
如果希望在一个查询中同时使用SentCount和ReceivedCount:
select t.status_dt, count(*) as SentCount, count(tres.status_id) as ReceivedCount
from (select t.*,
row_number() over (partition by status_id order by status_dt) as seqnum
from table t
where tres.status = 'sent'
) t left outer join
(select tres.*,
row_number() over (partition by status_id order by status_dt) as seqnum
from table tres
where tres.status = 'received'
) tres
on t.status_id = tres.status_id and
t.seqnum = tres.seqnum
group by status_dt;
试试这个
select t1.Status_dt,
(select count(t2.Status) from table t2 where t2.Status='sent' and t1.Status_dt = t2.Status_dt) as sent_count,
(select count(t2.Status) from table t2 where t2.Status='received' and t1.Status_dt = t2.Status_dt) as received_count
from table t1 group by t1.Sattus_dt
@德姆斯。有一个重要的
status\u id
字段。该问题将其称为“id”,如:“2013年4月3日发送的交易为一笔(对于id 3),对于该id,2013年4月3日发送的wch尚未收到”。因此,它是status\u id
后跟status\u dt
@Dems。非常感谢。“那是我最初的误会留下的碎屑。”@Gordon Linoff。。。你能帮我在上面的查询中再添加一列吗。。此列为“接收的\ U代码”,它是-从接收回的交易中有一个原因代码(Y)。此列将位于另一个名为“Codes”的表中<代码>代码id连接代码id代码状态2 101 Y此连接表是位于b/w代码表和状态中的某个东西(主代码上方)表连接表Junct\u id status\u id code\u id 101 2 201
因此最终结果将是dt sent\u count received\u count received\u status 4/1/2013 2 0 4/2 1 1 1 4/3/2013 1 0 0 0仅status\u id 2具有已接收_状态(代码已接收)Y和其他ID 1和3未收到任何codes@MtKr . . . 您可以通过将代码连接到tres
子查询中,然后将其包含在外部选择中来实现这一点。