sql 组合 2 个不同顺序的查询
sql combining 2 queries with different order by group by
我有一个查询,我在其中计算数据库中最频繁的响应并按最高数量对它们进行排名,因此使用分组依据和排序依据。
下面展示了如何为一个人做:
select health, count(health) as count
from [Health].[Questionaire]
group by Health
order by count(Health) desc
输出如下:
Health Count
----------- -----
Very Good 6
Good 5
Poor 4
我想在同一个 table 上处理另一列,另一个查询类似于以下内容,因此两个查询使用一个 sql 语句,如下所示:
Health Count Diet Count
----------- ----- ----- -----
Very Good 6 Very Good 6
Good 5 Good 4
Poor 4 Poor 3
更新!!
你好,这就是 table 目前的样子
ID Diet Health
----------- ----- -------
101 Very Good Very Good
102 Poor Good
103 Poor Poor
我想在同一个 table 上处理另一列,另一个查询类似于以下内容,因此两个查询使用一个 sql 语句,如下所示:
Health Count Diet Count
----------- ----- ----- -----
Very Good 2 Very Good 1
Poor 1 Good 1
Good 0 Poor 1
谁能帮我解决这个问题?
如果需要,可以提供进一步的说明!
这里有两种不同的方法,注意我删除了多余的列:
测试数据:
DECLARE @t table(Health varchar(20), Diet varchar(20))
INSERT @t values
('Very good', 'Very good'),
('Poor', 'Good'),
('Poor', 'Poor')
查询 1:
;WITH CTE1 as
(
SELECT Health, count(*) CountHealth
FROM @t --[Health].[Questionaire]
GROUP BY health
), CTE2 as
(
SELECT Diet, count(*) CountDiet
FROM @t --[Health].[Questionaire]
GROUP BY Diet
)
SELECT
coalesce(Health, Diet) Grade,
coalesce(CountHealth, 0) CountHealth,
coalesce(CountDiet, 0) CountDiet
FROM CTE1
FULL JOIN
CTE2
ON CTE1.Health = CTE2.Diet
ORDER BY CountHealth DESC
结果 1:
Grade CountHealth CountDiet
Poor 2 1
Very good 1 1
Good 0 1
混合这样的结果确实不是一个好的做法,所以这里有一个不同的解决方案
查询 2:
SELECT Health, count(*) Count, 'Health' Grade
FROM @t --[Health].[Questionaire]
GROUP BY health
UNION ALL
SELECT Diet, count(*) CountDiet, 'Diet'
FROM @t --[Health].[Questionaire]
GROUP BY Diet
ORDER BY Grade, Count DESC
结果 2:
Health Count Grade
Good 1 Diet
Poor 1 Diet
Very good 1 Diet
Poor 2 Health
Very good 1 Health
您需要将 table 连接到自身,但(如您的示例数据所示)处理特定值的实际数据中的差距。
如果您的 table 具有 health/diet 个值的范围:
select
v.value Status,
count(a.id) healthCount,
count(b.id) DietCount
from health_diet_values v
left join Questionaire a on a.health = v.value
left join Questionaire b on b.diet = v.value
group by v.value
或者如果您没有这样的 table,您需要手动生成值列表并从中加入:
select
v.value Status,
count(a.id) healthCount,
count(b.id) DietCount
from (select 'Very Good' value union all
select 'Good' union all
select 'Poor') v
left join Questionaire a on a.health = v.value
left join Questionaire b on b.diet = v.value
group by v.value
如果值没有匹配数据,这两个查询都会生成零。
请注意,在您想要的输出中有一个冗余列 - 您重复了值列。上面的查询产生如下所示的输出:
Status HealthCount DietCount
-------------------------------
Very Good 2 1
Good 1 1
Poor 0 1
我有一个查询,我在其中计算数据库中最频繁的响应并按最高数量对它们进行排名,因此使用分组依据和排序依据。
下面展示了如何为一个人做:
select health, count(health) as count
from [Health].[Questionaire]
group by Health
order by count(Health) desc
输出如下:
Health Count
----------- -----
Very Good 6
Good 5
Poor 4
我想在同一个 table 上处理另一列,另一个查询类似于以下内容,因此两个查询使用一个 sql 语句,如下所示:
Health Count Diet Count
----------- ----- ----- -----
Very Good 6 Very Good 6
Good 5 Good 4
Poor 4 Poor 3
更新!!
你好,这就是 table 目前的样子
ID Diet Health
----------- ----- -------
101 Very Good Very Good
102 Poor Good
103 Poor Poor
我想在同一个 table 上处理另一列,另一个查询类似于以下内容,因此两个查询使用一个 sql 语句,如下所示:
Health Count Diet Count
----------- ----- ----- -----
Very Good 2 Very Good 1
Poor 1 Good 1
Good 0 Poor 1
谁能帮我解决这个问题? 如果需要,可以提供进一步的说明!
这里有两种不同的方法,注意我删除了多余的列:
测试数据:
DECLARE @t table(Health varchar(20), Diet varchar(20))
INSERT @t values
('Very good', 'Very good'),
('Poor', 'Good'),
('Poor', 'Poor')
查询 1:
;WITH CTE1 as
(
SELECT Health, count(*) CountHealth
FROM @t --[Health].[Questionaire]
GROUP BY health
), CTE2 as
(
SELECT Diet, count(*) CountDiet
FROM @t --[Health].[Questionaire]
GROUP BY Diet
)
SELECT
coalesce(Health, Diet) Grade,
coalesce(CountHealth, 0) CountHealth,
coalesce(CountDiet, 0) CountDiet
FROM CTE1
FULL JOIN
CTE2
ON CTE1.Health = CTE2.Diet
ORDER BY CountHealth DESC
结果 1:
Grade CountHealth CountDiet
Poor 2 1
Very good 1 1
Good 0 1
混合这样的结果确实不是一个好的做法,所以这里有一个不同的解决方案
查询 2:
SELECT Health, count(*) Count, 'Health' Grade
FROM @t --[Health].[Questionaire]
GROUP BY health
UNION ALL
SELECT Diet, count(*) CountDiet, 'Diet'
FROM @t --[Health].[Questionaire]
GROUP BY Diet
ORDER BY Grade, Count DESC
结果 2:
Health Count Grade
Good 1 Diet
Poor 1 Diet
Very good 1 Diet
Poor 2 Health
Very good 1 Health
您需要将 table 连接到自身,但(如您的示例数据所示)处理特定值的实际数据中的差距。
如果您的 table 具有 health/diet 个值的范围:
select
v.value Status,
count(a.id) healthCount,
count(b.id) DietCount
from health_diet_values v
left join Questionaire a on a.health = v.value
left join Questionaire b on b.diet = v.value
group by v.value
或者如果您没有这样的 table,您需要手动生成值列表并从中加入:
select
v.value Status,
count(a.id) healthCount,
count(b.id) DietCount
from (select 'Very Good' value union all
select 'Good' union all
select 'Poor') v
left join Questionaire a on a.health = v.value
left join Questionaire b on b.diet = v.value
group by v.value
如果值没有匹配数据,这两个查询都会生成零。
请注意,在您想要的输出中有一个冗余列 - 您重复了值列。上面的查询产生如下所示的输出:
Status HealthCount DietCount
-------------------------------
Very Good 2 1
Good 1 1
Poor 0 1