我需要将多个查询值获取到一个数据集
Posted
技术标签:
【中文标题】我需要将多个查询值获取到一个数据集【英文标题】:i need to get multiple query values to a one data set 【发布时间】:2018-05-11 14:14:24 【问题描述】:我想从审计表this is the audit table, i need to get the count of address, tele, and AssName(AssociationName) of each and every person specificly创建一个报告
select count(tele),count(AssName),count(Address) from Audit where name='ben'
select count(tele),count(AssName),count(Address) from Audit where name='nik'
select count(tele),count(AssName),count(Address) from Audit where name='josh'
这是我使用的查询,但我需要将这些单独的表合并到一个表中,如果这些单元格中只有“1”,则应计算计数。但是我的表有“0”,但它认为它们是值并且也计算“0”单元格this is the tabel now
【问题讨论】:
“联合所有”会起作用吗? 【参考方案1】:只需使用 GROUP BY
select name, count(tele),count(AssName),count(Address) from Audit group by name
【讨论】:
好的,这行得通。但我需要获取这些 colomns 中有 1 的 colomns tele、AssName、Address 的计数。如果不是那么(如果为空)它不应该被包括在计数中【参考方案2】:改为尝试分组
SELECT
name,
count(tele),
count(AssName),
count(Address)
FROM Audit
GROUP BY name
【讨论】:
好的,这行得通。但我需要获取这些 colomns 中有 1 的 colomns tele、AssName、Address 的计数。如果不是那么(如果为空)它不应该被包括在计数中【参考方案3】:Select name , count (Assname), count (Address), count(tele)
from Audit
GROUP by Name
这将返回您想要的结果。
编辑:我之前误解了你的问题。如果要计算一个人在表中的每个名称的数量,则需要按名称添加组。
【讨论】:
我不这么认为。你错过了GROUP BY
,这也不会给出任何结果:)【参考方案4】:
SELECT
name,
sum(case when tele is not null then 1 else 0 end) tele,
sum(case when Assname is not null then 1 else 0 end) Assname,
sum(case when Address is not null then 1 else 0 end) Address
FROM Audit
GROUP BY name
【讨论】:
以上是关于我需要将多个查询值获取到一个数据集的主要内容,如果未能解决你的问题,请参考以下文章