[英]sql server - get count and column value from two tables for a report
I need to select the counts from two tables that share a common column which is the clientId
and group the totals by the clientIds
for a given day and the dateadded
column in both tables is in the datetime format. 我需要从共享一个公共列的两个表中选择计数,这些表是
clientId
并且在给定日期由clientIds
对总计进行分组,并且两个表中的dateadded
列都采用日期时间格式。
For example the result should show: 例如,结果应显示:
ClientId Total1 Total2
aaaa 1 2
bbbbb 43 45
ccccc 123 355
etc. for 2011-03-25 等2011-03-25
what I currently have is 我现在拥有的是什么
select
(select clientid,count(*) as total1 from TFeed where dateadded = getdate()
group by clientId),
(select clientId, count(*) as total2 from WFeed where dateadded = getdate()
group by clientid)
This is of course wrong. 这当然是错的。 ERROR: only one expression can be specified in the select list when the subquery is not introduced with EXISTS.
错误:当未使用EXISTS引入子查询时,只能在选择列表中指定一个表达式。 Also for consideration, these are very large tables - over 3 million records and growing.
同样值得考虑的是,这些是非常大的表 - 超过300万条记录并且还在增长。 Any help appreciated
任何帮助赞赏
EDIT: 编辑:
about the time - if dateadded = '2011-03-25 12:00:34 0011', how can I compare the time to get dateadded = @getdate() and select all the records for today. 关于时间 - 如果dateadded ='2011-03-25 12:00:34 0011',我怎样才能比较获得dateadded = @getdate()的时间并选择今天的所有记录。
while my query is still running - off topic question...since this is a reporting query and I want to run it regularly to update totals so that when client opens web page or clicks on report it would deliver totals without having to run the query and just select the last totals from a database. 虽然我的查询仍在运行 - 关闭主题问题...因为这是一个报告查询,我想定期运行它来更新总计,以便当客户打开网页或点击报告时,它将提供总计而无需运行查询然后从数据库中选择最后一个总计。 do I then need to a have a different query or run this every hour or so.
然后我需要有一个不同的查询或每小时左右运行一次。
You're close. 你很亲密 You want to join the two tables together before you do the aggregation
您希望在进行聚合之前将两个表连接在一起
select
clientid,
sum(case when t.clientid is not null then 1 else 0 end ) as total1
sum(case when w.clientid is not null then 1 else 0 end ) as total1
from TFeed t FULL OUTER JOIN WFeed w
where w.dateadded = getdate() or t.dateadded = getdate()
This might not be quite what you're going for, but that's the general idea. 这可能不是你想要的,但这是一般的想法。 This also handles the case where there's no data in one of the tables for a particular day.
这也处理了特定日期其中一个表中没有数据的情况。
select tf.clientid,
SUM( case when tf.dateadded = getdate() then 1 else 0 end) as Total1,
SUM( case when wf.dateadded = getdate() then 1 else 0 end) as Total2
from tfeed tf full outer join wfeed wf on tf.clientid = wf.clientid
group by tf.clientid
Try some thing like this. 尝试这样的事情。
select clientid, sum(total1) as total1, sum(total2) as total2
from
(select clientid,count(*) as total1, 0 as total2
from TFeed
where dateadded = @someDate
group by clientId
UNION
select clientId, 0 as total1, count(*) as total2
from WFeed
where dateadded = @someDate
group by clientid)
group by clientid
Using a cross apply is really helpful and speeds things up dramatically. 使用交叉应用非常有用,可以大大加快速度。 I'm not positive on your associations and whether or not you've got a one-to-one relationship between clientIDs in your 2 feeds, but this is the direction you would want to go for optimizing your query against so many records.
我对你的关联不是肯定的,你是否在你的2个feed中的clientID之间有一对一的关系,但这是你想要针对这么多记录优化你的查询的方向。
Also, here's a link to another discussion with more information on the topic of cross apply that may be helpful if this exact query isn't working for you: 此外,这里还有另一个讨论的链接,其中包含有关交叉申请主题的更多信息,如果这个确切的查询对您不起作用可能会有所帮助:
When should I use Cross Apply over Inner Join? 什么时候应该使用Cross Apply over Inner Join?
with x as (
select clientID as clientID, COUNT(*) as total1
from TFeed where dateadded = GETDATE()
group by clientID
)
select x.clientID,x.total1,y.total2 from x
cross apply
(
select clientID as clientID, COUNT(*) as total2
from WFeed where dateadded = GETDATE()
group by clientID
) y
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.