简体   繁体   English

sql server - 从报告的两个表中获取计数和列值

[英]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.

相关问题 SQL Server从两个表的字符串中获取最大值整数 - sql server get max value integer in a string from two tables 如何从SQL Server中的两个表获取值 - How to get value from two tables in sql server 获取表SQL Server中每列的每个值的计数 - get a count of each value from every column in a table SQL Server sql server - 如何从两个表中逐列获取所有不同的值,并从另一个表中为每个值计数 - sql server - How to Get all distinct value in group by column from two table and count from another table for each value 使用两个表在SQL Server中获取一个报告 - Using two tables to get one report in SQL Server SQL Server:匹配两个表并获取值 - SQL Server: Match two tables and get value 如何从SQL Server中两个不同的表中获取两列的总数? - How to get the total count of two columns from two different tables in SQL Server? 如何从记录总数中获取特定列值的计数并在 SQL 服务器中的两个不同列中显示差异 - How to get count of particular column value from total number of records and display difference in two different columns in SQL Server SQL查询 - 从两个表中获取两列的计数 - SQL Query - Get count of two columns from two tables SQL 服务器:如何从两个表中的 select 列显示第二个表中最接近日期的相同值 - SQL Server: How to select column from two tables and show the same value of the closest date from the second table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM