SQL 查询:每个列值都是基于临时 table 上的不同过滤器的聚合
SQL Query: each column value is an aggergation based on a different filter on a temporary table
我想根据结果 table 中每一列的特定条件从 table 中获取行数。 table 本身是临时的 table.
下面的查询是我想要完成的,但我不知道用什么语法来实现它:
WITH table1 as( a huge inner joins between different tables)
SELECT *
FROM (
(
SELECT COUNT(*)
FROM table1
WHERE
column1 = 'value1' AND column2 > 0
) AS Count1,
(
SELECT COUNT(*)
FROM table1
WHERE
column1 = 'value1' AND column2 = 0
)AS Count2,
(
SELECT COUNT(*)
FROM table1
WHERE
column1 = 'value2' AND column2 > 0
)AS Count3,
(
SELECT COUNT(*)
FROM table1
WHERE
column1 = 'value2' AND column2 = 0
) AS Count4
) CountSummary
这不是正确的语法,但我不确定如何在 sql 服务器中实现这个概念。
如果我使查询的第二部分等于:select * from table1
,这就是我目前遇到的错误
Msg 252, Level 16, State 1, Line 1
Recursive common table expression 'table1' does not contain a top-level UNION ALL operator.
庞大的inner join查询可以运行单独无误
--------------------更新------------------------ -
递归错误是由于命名 'table1' 与内部连接查询部分中的 table 相同,我摆脱了它,错误消失了。然后我尝试了答案,它们都有效。
也许是这样的:-
select
sum(case when column1 = 'value1' and column2 > 0 then 1 else 0 end) as Count1,
sum(case when column1 = 'value1' and column2 = 0 then 1 else 0 end) as Count2,
sum(case when column1 = 'value2' and column2 > 0 then 1 else 0 end) as Count3,
sum(case when column1 = 'value2' and column2 = 0 then 1 else 0 end) as Count4
from table1
这样做:
WITH table1 as( a huge inner joins between different tables)
SELECT * FROM
(
SELECT COUNT(*) AS Count1
FROM table1
WHERE
column1 = 'value1' AND column2 > 0
) AS Count1,
(
SELECT COUNT(*) AS Count2
FROM table1
WHERE
column1 = 'value1' AND column2 = 0
) AS Count2
我想根据结果 table 中每一列的特定条件从 table 中获取行数。 table 本身是临时的 table.
下面的查询是我想要完成的,但我不知道用什么语法来实现它:
WITH table1 as( a huge inner joins between different tables)
SELECT *
FROM (
(
SELECT COUNT(*)
FROM table1
WHERE
column1 = 'value1' AND column2 > 0
) AS Count1,
(
SELECT COUNT(*)
FROM table1
WHERE
column1 = 'value1' AND column2 = 0
)AS Count2,
(
SELECT COUNT(*)
FROM table1
WHERE
column1 = 'value2' AND column2 > 0
)AS Count3,
(
SELECT COUNT(*)
FROM table1
WHERE
column1 = 'value2' AND column2 = 0
) AS Count4
) CountSummary
这不是正确的语法,但我不确定如何在 sql 服务器中实现这个概念。
如果我使查询的第二部分等于:select * from table1
Msg 252, Level 16, State 1, Line 1
Recursive common table expression 'table1' does not contain a top-level UNION ALL operator.
庞大的inner join查询可以运行单独无误
--------------------更新------------------------ -
递归错误是由于命名 'table1' 与内部连接查询部分中的 table 相同,我摆脱了它,错误消失了。然后我尝试了答案,它们都有效。
也许是这样的:-
select
sum(case when column1 = 'value1' and column2 > 0 then 1 else 0 end) as Count1,
sum(case when column1 = 'value1' and column2 = 0 then 1 else 0 end) as Count2,
sum(case when column1 = 'value2' and column2 > 0 then 1 else 0 end) as Count3,
sum(case when column1 = 'value2' and column2 = 0 then 1 else 0 end) as Count4
from table1
这样做:
WITH table1 as( a huge inner joins between different tables)
SELECT * FROM
(
SELECT COUNT(*) AS Count1
FROM table1
WHERE
column1 = 'value1' AND column2 > 0
) AS Count1,
(
SELECT COUNT(*) AS Count2
FROM table1
WHERE
column1 = 'value1' AND column2 = 0
) AS Count2