如何从多个查询创建 Table

How to create a Table from multiple queries

我正在尝试从两个不同的 table 中创建一个 table 计数项目以获得总计和小计,如下所示:

(select count(*) from ccustomer AS TotalCustomers)    
(select count(*) from ccustomer where floating = 0 AS ActiveCustomers),    
(select count(*) from ccustomer where floating = 1 AS FloatingCustomers),    
(select count(*) from pproperty AS TotalProperties)    
(select count(*) from pproperty where occcustno = 0 and propstat <> 'de' AS VoidProperties),    
(select count(*) from pproperty where occcustno = 0 and propstat = 'de' AS DemolishedProperties),    
(select count(*) from pproperty where occcustno <> 0 AS OccupiedProperties);

首先,第 2+3 行(第 1 行的小计)和 5+6+7(第 4 行的小计)中 'AS' 附近的这些查询 return 语法问题。我无法克服这个问题,因为我每次都尝试重新格式化有或没有括号等。

Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'AS'.
Msg 156, Level 15, State 1, Line 3
Incorrect syntax near the keyword 'AS'.
Msg 156, Level 15, State 1, Line 5
Incorrect syntax near the keyword 'AS'.
Msg 156, Level 15, State 1, Line 6
Incorrect syntax near the keyword 'AS'.
Msg 156, Level 15, State 1, Line 7
Incorrect syntax near the keyword 'AS'.

我需要用这些 headers 和 totals/subtotals 制作一个 table,所以不知道这是否可行。我有另一个查询是一位前同事使用 NumberCheck 创建的 table,但它有点太复杂了我无法复制(他不再在这里寻求帮助)。

如有任何帮助,我们将不胜感激。 谢谢 利亚姆

试试这个:

create table ccustomer(floating int)
create table pproperty(occcustno int, propstat nvarchar(50) )
insert into ccustomer values (1),(0),(1),(0),(1),(0),(1),(0),(1),(1)
insert into pproperty values (0,'de'), (1,'de'), (0,'de'), (0,'us'), (1,'de'), (1,'us'), (1,'de')

select
(select count(*) from ccustomer) AS TotalCustomers,
(select count(*) from ccustomer where floating = 0) AS ActiveCustomers,    
(select count(*) from ccustomer where floating = 1) AS FloatingCustomers,    
(select count(*) from pproperty ) AS TotalProperties,    
(select count(*) from pproperty where occcustno = 0 and propstat <> 'de') AS VoidProperties,    
(select count(*) from pproperty where occcustno = 0 and propstat = 'de') AS DemolishedProperties,    
(select count(*) from pproperty where occcustno <> 0) AS OccupiedProperties;

在脚本中我创建了一个示例输入场景:

结果: