如何通过来自同一个 table 的查询合并两个组?
How to merge two group by queries from the same table?
我有两个来自同一个 table 的查询,我需要合并为一个 table:
SELECT
t.[Origin Terminal Name] as 'Terminal',
COUNT(t.[Pro Number]) as 'Inbound Shipments',
SUM(t.[Total Revenue]) as 'Inbound Revenue'
FROM [AX2cTestStage].[dbo].[AX2cTestAdapter_dbo_AAATRANSPORTTABLE_V] t
GROUP BY t.[Origin Terminal Name]
ORDER BY t.[Origin Terminal Name] ASC
SELECT
t.[Destination Terminal Name] as 'Terminal',
COUNT(t.[Pro Number]) as 'Outbound Shipments',
SUM(t.[Total Revenue]) as 'Outbound Revenue'
FROM [AX2cTestStage].[dbo].[AX2cTestAdapter_dbo_AAATRANSPORTTABLE_V] t
GROUP BY t.[Destination Terminal Name]
ORDER BY t.[Destination Terminal Name] ASC
它们都产生了这些结果:
Terminal Inbound Shipments Inbound Revenue
Abilene 520 75970.08
Albany 877 130560.28
Terminal Outbound Shipments Outbound Revenue
Abilene 2684 419523.83
Albany 2820 407871.89
我需要这个作为输出:
Terminal Inbound Shipments Inbound Revenue Outbound Shipments Outbound Revenue
Abilene 520 75970.08 2684 419523.83
Albany 877 130560.28 2820 407871.89
我试过这个查询:
SELECT
t.[Origin Terminal Name] as 'Terminal',
COUNT(t.[Pro Number]) as 'Inbound Shipments',
SUM(t.[Total Revenue]) as 'Inbound Revenue',
(SELECT
t2.[Destination Terminal Name] as 'Terminal',
COUNT(t2.[Pro Number]) as 'Outbound Shipments',
SUM(t2.[Total Revenue]) as 'Outbound Revenue'
FROM [AX2cTestStage].[dbo].[AX2cTestAdapter_dbo_AAATRANSPORTTABLE_V] t2
GROUP BY t2.[Destination Terminal Name])
FROM [AX2cTestStage].[dbo].[AX2cTestAdapter_dbo_AAATRANSPORTTABLE_V] t
LEFT JOIN [AX2cTestStage].[dbo].[AX2cTestAdapter_dbo_AAATRANSPORTTABLE_V] t2 ON t.[Origin Terminal Name] = t2.[Destination Terminal Name]
GROUP BY t.[Origin Terminal Name]
ORDER BY t.[Origin Terminal Name] ASC
我收到这个错误:
消息 116,级别 16,状态 1,第 22 行
当子查询没有用EXISTS引入时,select列表中只能指定一个表达式。
子查询是解决这个问题的方法吗?我很感激任何建议
如果我没听错,你可以使用cross apply
将列反透视为行,然后聚合:
SELECT
x.terminal,
SUM(CASE WHEN x.which = 'inbound' THEN 1 ELSE 0 END) as inbound_shipments,
SUM(CASE WHEN x.which = 'inbound' THEN x.total_revenue ELSE 0 END) as inbound_revenue,
SUM(CASE WHEN x.which = 'outbound' THEN 1 ELSE 0 END) as outbound_shipments,
SUM(CASE WHEN x.which = 'outbound' THEN x.total_revenue ELSE 0 END) as outbound_revenue
FROM [AX2cTestStage].[dbo].[AX2cTestAdapter_dbo_AAATRANSPORTTABLE_V] t
CROSS APPLY (VALUES
('inbound', [Origin Terminal Name], [Total Revenue]),
('outbound', [Destination Terminal Name], [Total Revenue])
) AS x(which, is_destination, terminal, total_revenue)
GROUP BY x.terminal
ORDER BY x.terminal
SELECT
t.[Origin Terminal Name] as 'Terminal',
COUNT(t.[Pro Number]) as 'Inbound Shipments',
SUM(t.[Total Revenue]) as 'Inbound Revenue',
max(t2.[Outbound Shipments]) as 'Outbound Shipments',
max(t2.[Outbound Revenue]) as 'Outbound Revenue'
FROM [AX2cTestStage].[dbo].[AX2cTestAdapter_dbo_AAATRANSPORTTABLE_V] t
INNER JOIN (SELECT
t2.[Destination Terminal Name] as 'Terminal',
COUNT(t2.[Pro Number]) as 'Outbound Shipments',
SUM(t2.[Total Revenue]) as 'Outbound Revenue'
FROM [AX2cTestStage].[dbo].[AX2cTestAdapter_dbo_AAATRANSPORTTABLE_V] t2
GROUP BY t2.[Destination Terminal Name]) t2 on t.[Origin Terminal Name] = t2.Terminal
GROUP BY t.[Origin Terminal Name]
ORDER BY t.[Origin Terminal Name] ASC
我有两个来自同一个 table 的查询,我需要合并为一个 table:
SELECT
t.[Origin Terminal Name] as 'Terminal',
COUNT(t.[Pro Number]) as 'Inbound Shipments',
SUM(t.[Total Revenue]) as 'Inbound Revenue'
FROM [AX2cTestStage].[dbo].[AX2cTestAdapter_dbo_AAATRANSPORTTABLE_V] t
GROUP BY t.[Origin Terminal Name]
ORDER BY t.[Origin Terminal Name] ASC
SELECT
t.[Destination Terminal Name] as 'Terminal',
COUNT(t.[Pro Number]) as 'Outbound Shipments',
SUM(t.[Total Revenue]) as 'Outbound Revenue'
FROM [AX2cTestStage].[dbo].[AX2cTestAdapter_dbo_AAATRANSPORTTABLE_V] t
GROUP BY t.[Destination Terminal Name]
ORDER BY t.[Destination Terminal Name] ASC
它们都产生了这些结果:
Terminal Inbound Shipments Inbound Revenue
Abilene 520 75970.08
Albany 877 130560.28
Terminal Outbound Shipments Outbound Revenue
Abilene 2684 419523.83
Albany 2820 407871.89
我需要这个作为输出:
Terminal Inbound Shipments Inbound Revenue Outbound Shipments Outbound Revenue
Abilene 520 75970.08 2684 419523.83
Albany 877 130560.28 2820 407871.89
我试过这个查询:
SELECT
t.[Origin Terminal Name] as 'Terminal',
COUNT(t.[Pro Number]) as 'Inbound Shipments',
SUM(t.[Total Revenue]) as 'Inbound Revenue',
(SELECT
t2.[Destination Terminal Name] as 'Terminal',
COUNT(t2.[Pro Number]) as 'Outbound Shipments',
SUM(t2.[Total Revenue]) as 'Outbound Revenue'
FROM [AX2cTestStage].[dbo].[AX2cTestAdapter_dbo_AAATRANSPORTTABLE_V] t2
GROUP BY t2.[Destination Terminal Name])
FROM [AX2cTestStage].[dbo].[AX2cTestAdapter_dbo_AAATRANSPORTTABLE_V] t
LEFT JOIN [AX2cTestStage].[dbo].[AX2cTestAdapter_dbo_AAATRANSPORTTABLE_V] t2 ON t.[Origin Terminal Name] = t2.[Destination Terminal Name]
GROUP BY t.[Origin Terminal Name]
ORDER BY t.[Origin Terminal Name] ASC
我收到这个错误:
消息 116,级别 16,状态 1,第 22 行 当子查询没有用EXISTS引入时,select列表中只能指定一个表达式。
子查询是解决这个问题的方法吗?我很感激任何建议
如果我没听错,你可以使用cross apply
将列反透视为行,然后聚合:
SELECT
x.terminal,
SUM(CASE WHEN x.which = 'inbound' THEN 1 ELSE 0 END) as inbound_shipments,
SUM(CASE WHEN x.which = 'inbound' THEN x.total_revenue ELSE 0 END) as inbound_revenue,
SUM(CASE WHEN x.which = 'outbound' THEN 1 ELSE 0 END) as outbound_shipments,
SUM(CASE WHEN x.which = 'outbound' THEN x.total_revenue ELSE 0 END) as outbound_revenue
FROM [AX2cTestStage].[dbo].[AX2cTestAdapter_dbo_AAATRANSPORTTABLE_V] t
CROSS APPLY (VALUES
('inbound', [Origin Terminal Name], [Total Revenue]),
('outbound', [Destination Terminal Name], [Total Revenue])
) AS x(which, is_destination, terminal, total_revenue)
GROUP BY x.terminal
ORDER BY x.terminal
SELECT
t.[Origin Terminal Name] as 'Terminal',
COUNT(t.[Pro Number]) as 'Inbound Shipments',
SUM(t.[Total Revenue]) as 'Inbound Revenue',
max(t2.[Outbound Shipments]) as 'Outbound Shipments',
max(t2.[Outbound Revenue]) as 'Outbound Revenue'
FROM [AX2cTestStage].[dbo].[AX2cTestAdapter_dbo_AAATRANSPORTTABLE_V] t
INNER JOIN (SELECT
t2.[Destination Terminal Name] as 'Terminal',
COUNT(t2.[Pro Number]) as 'Outbound Shipments',
SUM(t2.[Total Revenue]) as 'Outbound Revenue'
FROM [AX2cTestStage].[dbo].[AX2cTestAdapter_dbo_AAATRANSPORTTABLE_V] t2
GROUP BY t2.[Destination Terminal Name]) t2 on t.[Origin Terminal Name] = t2.Terminal
GROUP BY t.[Origin Terminal Name]
ORDER BY t.[Origin Terminal Name] ASC