需要每一列的总数,并为空数据点显示 0
Need a total for each column and to show 0 for null data points
我有一个脚本(如下),它有多个连接的列,我需要在底部的最后一行包含每列的总和。我知道 sum 函数,但我不知道将它放在脚本中的什么位置。我还想知道如何或在何处合并此语句 SELECT ISNULL(myColumn, 0 ) FROM myTable
因此空值显示 0.
SELECT A.[start_tran_hour],
[singles picked],
[single packed],
[multis picked],
[units sorted],
[multis packed],
[sa packed],
[sa picked],
[total picked],
[total packed],
[total shipped]
FROM (SELECT Datepart(hour, start_tran_time)start_tran_hour,
Sum(tran_qty) AS 'Total Shipped'
FROM t_tran_log WITH(nolock)
WHERE tran_type IN ( '340', '341' )
AND Cast(start_tran_date AS DATE) = '2021-07-06'
GROUP BY Datepart(hour, start_tran_time))A
LEFT JOIN (SELECT Datepart(hour, start_tran_time)start_tran_hour,
Sum(tran_qty) AS 'Total Picked'
FROM t_tran_log WITH(nolock)
WHERE tran_type = '301'
AND Cast(start_tran_date AS DATE) = '2021-07-06'
GROUP BY Datepart(hour, start_tran_time))B
ON A.start_tran_hour = B.start_tran_hour
LEFT JOIN (SELECT Datepart(hour, start_tran_time)start_tran_hour,
Sum(tran_qty) AS 'Single Packed'
FROM t_tran_log WITH(nolock)
WHERE tran_type = '315'
AND description = 'Single Packing'
AND Cast(start_tran_date AS DATE) = '2021-07-06'
GROUP BY Datepart(hour, start_tran_time))C
ON A.start_tran_hour = C.start_tran_hour
LEFT JOIN (SELECT Datepart(hour, start_tran_time)start_tran_hour,
Sum(tran_qty) AS 'Singles Picked'
FROM t_tran_log WITH(nolock)
LEFT JOIN t_order WITH(nolock)
ON
t_tran_log.control_number = t_order.order_number
WHERE tran_type = '301'
AND t_order.route = 'SINGLE'
AND Cast(start_tran_date AS DATE) = '2021-07-06'
GROUP BY Datepart(hour, start_tran_time))D
ON A.start_tran_hour = D.start_tran_hour
LEFT JOIN (SELECT Datepart(hour, start_tran_time)start_tran_hour,
Sum(tran_qty) AS 'Multis Picked'
FROM t_tran_log WITH(nolock)
LEFT JOIN t_order WITH(nolock)
ON
t_tran_log.control_number = t_order.order_number
WHERE tran_type = '301'
AND t_order.route = 'MULTI'
AND Cast(start_tran_date AS DATE) = '2021-07-06'
GROUP BY Datepart(hour, start_tran_time))E
ON A.start_tran_hour = E.start_tran_hour
LEFT JOIN (SELECT Datepart(hour, start_tran_time)start_tran_hour,
Sum(tran_qty) AS 'Multis Packed'
FROM t_tran_log WITH(nolock)
WHERE tran_type = '315'
AND description = 'Multi Packing'
AND Cast(start_tran_date AS DATE) = '2021-07-06'
GROUP BY Datepart(hour, start_tran_time))F
ON A.start_tran_hour = F.start_tran_hour
LEFT JOIN (SELECT Datepart(hour, start_tran_time)start_tran_hour,
Sum(tran_qty) AS 'SA Picked'
FROM t_tran_log WITH(nolock)
WHERE tran_type = '301'
AND ( location_id LIKE 'PR%'
OR location_id LIKE 'SA%' )
AND Cast(start_tran_date AS DATE) = '2021-07-06'
GROUP BY Datepart(hour, start_tran_time))G
ON A.start_tran_hour = G.start_tran_hour
LEFT JOIN (SELECT Datepart(hour, start_tran_time)start_tran_hour,
Sum(tran_qty) AS 'SA Packed'
FROM t_tran_log WITH(nolock)
WHERE tran_type = '315'
AND ( location_id LIKE 'PACKSA%' )
AND Cast(start_tran_date AS DATE) = '2021-07-06'
GROUP BY Datepart(hour, start_tran_time))H
ON A.start_tran_hour = H.start_tran_hour
LEFT JOIN (SELECT Datepart(hour, start_tran_time)start_tran_hour,
Sum(tran_qty) AS 'Units Sorted'
FROM t_tran_log WITH(nolock)
WHERE tran_type = '311'
AND Cast(start_tran_date AS DATE) = '2021-07-06'
GROUP BY Datepart(hour, start_tran_time))I
ON A.start_tran_hour = I.start_tran_hour
LEFT JOIN (SELECT Datepart(hour, start_tran_time)start_tran_hour,
Sum(tran_qty) AS 'Total Packed'
FROM t_tran_log WITH(nolock)
WHERE tran_type = '315'
AND Cast(start_tran_date AS DATE) = '2021-07-06'
GROUP BY ( Datepart(hour, start_tran_time) ))J
ON A.start_tran_hour = J.start_tran_hour
ORDER BY A.start_tran_hour
对于延误,我们深表歉意。我一直被 Picked and Packed 绊倒。
也许这会让您开始减少条件聚合
您可能注意到我注释掉了 [singles picked]
和 [multis picked]
。看起来需要 JOIN,但我对您的数据了解不足,无法做出假设。
Select start_tran_hour = Datepart(hour, start_tran_time)
--,[singles picked]
,[single packed] = sum( case when tran_type in ('315')
and description = 'Single Packing' then tran_qty else 0 end)
--,[multis picked]
,[units sorted] = sum( case when tran_type in ('311') then tran_qty else 0 end)
,[multis packed] = sum( case when tran_type in ('315')
and description='Multi Packing' then tran_qty else 0 end)
,[sa packed] = sum( case when tran_type in ('315')
and location_id LIKE 'PACKSA%' then tran_qty else 0 end)
,[sa picked] = sum( case when tran_type in ('301')
and left(location_id,2) in ('PR','SA') then tran_qty else 0 end)
,[Total Picked] = sum( case when tran_type in ('301') then tran_qty else 0 end)
,[Total Packed] = sum( case when tran_type in ('315') then tran_qty else 0 end)
,[Total Shipped] = sum( case when tran_type in ('340','341') then tran_qty else 0 end)
From t_tran_log
Where Cast(start_tran_date AS DATE) = '2021-07-06'
Group By Grouping Sets (
(Datepart(hour, start_tran_time))
,()
)
我假设您有几个小时,列中的每个度量值都为空(或者根本没有),所以 SUM(all_null_values)
= NULL 而您希望它为 0。
这将处理空值并添加总计行。
SELECT A.[start_tran_hour],
, sum(coalesce([singles picked], 0)) as 'singles picked'
, sum(coalesce([single packed], 0)) as 'single packed'
, sum(coalesce([multis picked], 0)) as 'multis picked'
, sum(coalesce([units sorted], 0)) as 'units sorted'
, sum(coalesce([multis packed], 0)) as 'multis packed'
, sum(coalesce([sa packed], 0)) as 'sa packed'
, sum(coalesce([sa picked], 0)) as 'sa picked'
, sum(coalesce([total picked], 0)) as 'total picked'
, sum(coalesce([total packed], 0)) as 'total packed'
, sum(coalesce([total shipped], 0)) as 'total shipped'
FROM (
SELECT Datepart(hour, start_tran_time) as start_tran_hour
, Sum(tran_qty) AS 'Total Shipped'
FROM t_tran_log WITH(nolock)
WHERE tran_type IN ( '340', '341' )
AND Cast(start_tran_date AS DATE) = '2021-07-06'
GROUP BY Datepart(hour, start_tran_time)
)A
LEFT JOIN (
SELECT Datepart(hour, start_tran_time) as start_tran_hour
, Sum(tran_qty) AS 'Total Picked'
FROM t_tran_log WITH(nolock)
WHERE tran_type = '301'
AND Cast(start_tran_date AS DATE) = '2021-07-06'
GROUP BY Datepart(hour, start_tran_time)
)B ON A.start_tran_hour = B.start_tran_hour
LEFT JOIN (
SELECT Datepart(hour, start_tran_time) as start_tran_hour
, Sum(tran_qty) AS 'Single Packed'
FROM t_tran_log WITH(nolock)
WHERE tran_type = '315'
AND description = 'Single Packing'
AND Cast(start_tran_date AS DATE) = '2021-07-06'
GROUP BY Datepart(hour, start_tran_time)
)C ON A.start_tran_hour = C.start_tran_hour
LEFT JOIN (
SELECT Datepart(hour, start_tran_time) as start_tran_hour
, Sum(tran_qty) AS 'Singles Picked'
FROM t_tran_log WITH(nolock)
LEFT JOIN t_order WITH(nolock) ON t_tran_log.control_number = t_order.order_number
WHERE tran_type = '301'
AND t_order.route = 'SINGLE'
AND Cast(start_tran_date AS DATE) = '2021-07-06'
GROUP BY Datepart(hour, start_tran_time)
)D ON A.start_tran_hour = D.start_tran_hour
LEFT JOIN (
SELECT Datepart(hour, start_tran_time) as start_tran_hour
, Sum(tran_qty) AS 'Multis Picked'
FROM t_tran_log WITH(nolock)
LEFT JOIN t_order WITH(nolock) ON t_tran_log.control_number = t_order.order_number
WHERE tran_type = '301'
AND t_order.route = 'MULTI'
AND Cast(start_tran_date AS DATE) = '2021-07-06'
GROUP BY Datepart(hour, start_tran_time)
)E ON A.start_tran_hour = E.start_tran_hour
LEFT JOIN (
SELECT Datepart(hour, start_tran_time) as start_tran_hour
, Sum(tran_qty) AS 'Multis Packed'
FROM t_tran_log WITH(nolock)
WHERE tran_type = '315'
AND description = 'Multi Packing'
AND Cast(start_tran_date AS DATE) = '2021-07-06'
GROUP BY Datepart(hour, start_tran_time)
)F ON A.start_tran_hour = F.start_tran_hour
LEFT JOIN (
SELECT Datepart(hour, start_tran_time) as start_tran_hour
, Sum(tran_qty) AS 'SA Picked'
FROM t_tran_log WITH(nolock)
WHERE tran_type = '301'
AND ( location_id LIKE 'PR%'
OR location_id LIKE 'SA%' )
AND Cast(start_tran_date AS DATE) = '2021-07-06'
GROUP BY Datepart(hour, start_tran_time)
) G ON A.start_tran_hour = G.start_tran_hour
LEFT JOIN (
SELECT Datepart(hour, start_tran_time) as start_tran_hour
, Sum(tran_qty) AS 'SA Packed'
FROM t_tran_log WITH(nolock)
WHERE tran_type = '315'
AND ( location_id LIKE 'PACKSA%' )
AND Cast(start_tran_date AS DATE) = '2021-07-06'
GROUP BY Datepart(hour, start_tran_time)
) H ON A.start_tran_hour = H.start_tran_hour
LEFT JOIN (
SELECT Datepart(hour, start_tran_time) as start_tran_hour
, Sum(tran_qty) AS 'Units Sorted'
FROM t_tran_log WITH(nolock)
WHERE tran_type = '311'
AND Cast(start_tran_date AS DATE) = '2021-07-06'
GROUP BY Datepart(hour, start_tran_time)
) I ON A.start_tran_hour = I.start_tran_hour
LEFT JOIN (
SELECT Datepart(hour, start_tran_time) as start_tran_hour
, Sum(tran_qty) AS 'Total Packed'
FROM t_tran_log WITH(nolock)
WHERE tran_type = '315'
AND Cast(start_tran_date AS DATE) = '2021-07-06'
GROUP BY ( Datepart(hour, start_tran_time) )
) J ON A.start_tran_hour = J.start_tran_hour
group by rollup (A.start_tran_hour)
ORDER BY A.start_tran_hour
但是,它仍然包含来自您的原始查询的 LEFT INNER JOIN
(不是真实的东西。我编造了这个词。)。
FROM t_tran_log
LEFT JOIN t_order ON t_tran_log.control_number = t_order.order_number
WHERE tran_type = '301'
AND t_order.route = 'MULTI'
在 WHERE
子句中包含 t_order.route = 'MULTI'
会导致 LEFT JOIN
成为 INNER JOIN
。所以我们要解决这个问题。
不过约翰·卡佩莱蒂的建议也不错。让我们尝试在 FROM
子句中引用一次 table 并计算 SELECT
子句中的每一列。
SELECT Datepart(hour, start_tran_time) as start_tran_hour,
, sum(
case
when tran_type = '301'
AND t_order.route = 'SINGLE'
then [singles picked]
else 0
end) as 'singles picked'
, sum(
case
when tran_type = '315'
AND description = 'Single Packing'
then [single packed]
else 0
end) as 'single packed'
, sum(
case
when tran_type = '301'
AND t_order.route = 'MULTI'
then [multis picked]
else 0
end) as 'multis picked'
, sum(
case
when tran_type = '311'
then [units sorted]
else 0
end) as 'units sorted'
, sum(
case
when tran_type = '315'
AND description = 'Multi Packing'
then [multis packed]
else 0
end) as 'multis packed'
, sum(
case
when tran_type = '315'
AND location_id LIKE 'PACKSA%'
then [sa packed]
else 0
end) as 'sa packed'
, sum(
case
when tran_type = '301'
AND ( location_id LIKE 'PR%'
OR location_id LIKE 'SA%' )
then [sa picked]
else 0
end) as 'sa picked'
, sum(
case
when tran_type = '301'
then [total picked]
else 0
end) as 'total picked'
, sum(
case
when tran_type = '315'
then [total packed]
else 0
end) as 'total packed'
, sum(
case
when tran_type IN ( '340', '341' )
then [total shipped]
else 0
end) as 'total shipped'
from t_tran_log WITH(nolock)
LEFT JOIN t_order WITH(nolock) ON t_tran_log.control_number = t_order.order_number
AND t_order.route in ('SINGLE', 'MULTI')
where Cast(start_tran_date AS DATE) = '2021-07-06'
and tran_type IN ('340', '341', '301', '315', '311')
group by rollup (Datepart(hour, start_tran_time))
ORDER BY Datepart(hour, start_tran_time)
如果没有数据库架构和示例数据,我无法确定其中任何一项是否有效。下次,创建并引用一个 dbfiddle。
我有一个脚本(如下),它有多个连接的列,我需要在底部的最后一行包含每列的总和。我知道 sum 函数,但我不知道将它放在脚本中的什么位置。我还想知道如何或在何处合并此语句 SELECT ISNULL(myColumn, 0 ) FROM myTable
因此空值显示 0.
SELECT A.[start_tran_hour],
[singles picked],
[single packed],
[multis picked],
[units sorted],
[multis packed],
[sa packed],
[sa picked],
[total picked],
[total packed],
[total shipped]
FROM (SELECT Datepart(hour, start_tran_time)start_tran_hour,
Sum(tran_qty) AS 'Total Shipped'
FROM t_tran_log WITH(nolock)
WHERE tran_type IN ( '340', '341' )
AND Cast(start_tran_date AS DATE) = '2021-07-06'
GROUP BY Datepart(hour, start_tran_time))A
LEFT JOIN (SELECT Datepart(hour, start_tran_time)start_tran_hour,
Sum(tran_qty) AS 'Total Picked'
FROM t_tran_log WITH(nolock)
WHERE tran_type = '301'
AND Cast(start_tran_date AS DATE) = '2021-07-06'
GROUP BY Datepart(hour, start_tran_time))B
ON A.start_tran_hour = B.start_tran_hour
LEFT JOIN (SELECT Datepart(hour, start_tran_time)start_tran_hour,
Sum(tran_qty) AS 'Single Packed'
FROM t_tran_log WITH(nolock)
WHERE tran_type = '315'
AND description = 'Single Packing'
AND Cast(start_tran_date AS DATE) = '2021-07-06'
GROUP BY Datepart(hour, start_tran_time))C
ON A.start_tran_hour = C.start_tran_hour
LEFT JOIN (SELECT Datepart(hour, start_tran_time)start_tran_hour,
Sum(tran_qty) AS 'Singles Picked'
FROM t_tran_log WITH(nolock)
LEFT JOIN t_order WITH(nolock)
ON
t_tran_log.control_number = t_order.order_number
WHERE tran_type = '301'
AND t_order.route = 'SINGLE'
AND Cast(start_tran_date AS DATE) = '2021-07-06'
GROUP BY Datepart(hour, start_tran_time))D
ON A.start_tran_hour = D.start_tran_hour
LEFT JOIN (SELECT Datepart(hour, start_tran_time)start_tran_hour,
Sum(tran_qty) AS 'Multis Picked'
FROM t_tran_log WITH(nolock)
LEFT JOIN t_order WITH(nolock)
ON
t_tran_log.control_number = t_order.order_number
WHERE tran_type = '301'
AND t_order.route = 'MULTI'
AND Cast(start_tran_date AS DATE) = '2021-07-06'
GROUP BY Datepart(hour, start_tran_time))E
ON A.start_tran_hour = E.start_tran_hour
LEFT JOIN (SELECT Datepart(hour, start_tran_time)start_tran_hour,
Sum(tran_qty) AS 'Multis Packed'
FROM t_tran_log WITH(nolock)
WHERE tran_type = '315'
AND description = 'Multi Packing'
AND Cast(start_tran_date AS DATE) = '2021-07-06'
GROUP BY Datepart(hour, start_tran_time))F
ON A.start_tran_hour = F.start_tran_hour
LEFT JOIN (SELECT Datepart(hour, start_tran_time)start_tran_hour,
Sum(tran_qty) AS 'SA Picked'
FROM t_tran_log WITH(nolock)
WHERE tran_type = '301'
AND ( location_id LIKE 'PR%'
OR location_id LIKE 'SA%' )
AND Cast(start_tran_date AS DATE) = '2021-07-06'
GROUP BY Datepart(hour, start_tran_time))G
ON A.start_tran_hour = G.start_tran_hour
LEFT JOIN (SELECT Datepart(hour, start_tran_time)start_tran_hour,
Sum(tran_qty) AS 'SA Packed'
FROM t_tran_log WITH(nolock)
WHERE tran_type = '315'
AND ( location_id LIKE 'PACKSA%' )
AND Cast(start_tran_date AS DATE) = '2021-07-06'
GROUP BY Datepart(hour, start_tran_time))H
ON A.start_tran_hour = H.start_tran_hour
LEFT JOIN (SELECT Datepart(hour, start_tran_time)start_tran_hour,
Sum(tran_qty) AS 'Units Sorted'
FROM t_tran_log WITH(nolock)
WHERE tran_type = '311'
AND Cast(start_tran_date AS DATE) = '2021-07-06'
GROUP BY Datepart(hour, start_tran_time))I
ON A.start_tran_hour = I.start_tran_hour
LEFT JOIN (SELECT Datepart(hour, start_tran_time)start_tran_hour,
Sum(tran_qty) AS 'Total Packed'
FROM t_tran_log WITH(nolock)
WHERE tran_type = '315'
AND Cast(start_tran_date AS DATE) = '2021-07-06'
GROUP BY ( Datepart(hour, start_tran_time) ))J
ON A.start_tran_hour = J.start_tran_hour
ORDER BY A.start_tran_hour
对于延误,我们深表歉意。我一直被 Picked and Packed 绊倒。
也许这会让您开始减少条件聚合
您可能注意到我注释掉了 [singles picked]
和 [multis picked]
。看起来需要 JOIN,但我对您的数据了解不足,无法做出假设。
Select start_tran_hour = Datepart(hour, start_tran_time)
--,[singles picked]
,[single packed] = sum( case when tran_type in ('315')
and description = 'Single Packing' then tran_qty else 0 end)
--,[multis picked]
,[units sorted] = sum( case when tran_type in ('311') then tran_qty else 0 end)
,[multis packed] = sum( case when tran_type in ('315')
and description='Multi Packing' then tran_qty else 0 end)
,[sa packed] = sum( case when tran_type in ('315')
and location_id LIKE 'PACKSA%' then tran_qty else 0 end)
,[sa picked] = sum( case when tran_type in ('301')
and left(location_id,2) in ('PR','SA') then tran_qty else 0 end)
,[Total Picked] = sum( case when tran_type in ('301') then tran_qty else 0 end)
,[Total Packed] = sum( case when tran_type in ('315') then tran_qty else 0 end)
,[Total Shipped] = sum( case when tran_type in ('340','341') then tran_qty else 0 end)
From t_tran_log
Where Cast(start_tran_date AS DATE) = '2021-07-06'
Group By Grouping Sets (
(Datepart(hour, start_tran_time))
,()
)
我假设您有几个小时,列中的每个度量值都为空(或者根本没有),所以 SUM(all_null_values)
= NULL 而您希望它为 0。
这将处理空值并添加总计行。
SELECT A.[start_tran_hour],
, sum(coalesce([singles picked], 0)) as 'singles picked'
, sum(coalesce([single packed], 0)) as 'single packed'
, sum(coalesce([multis picked], 0)) as 'multis picked'
, sum(coalesce([units sorted], 0)) as 'units sorted'
, sum(coalesce([multis packed], 0)) as 'multis packed'
, sum(coalesce([sa packed], 0)) as 'sa packed'
, sum(coalesce([sa picked], 0)) as 'sa picked'
, sum(coalesce([total picked], 0)) as 'total picked'
, sum(coalesce([total packed], 0)) as 'total packed'
, sum(coalesce([total shipped], 0)) as 'total shipped'
FROM (
SELECT Datepart(hour, start_tran_time) as start_tran_hour
, Sum(tran_qty) AS 'Total Shipped'
FROM t_tran_log WITH(nolock)
WHERE tran_type IN ( '340', '341' )
AND Cast(start_tran_date AS DATE) = '2021-07-06'
GROUP BY Datepart(hour, start_tran_time)
)A
LEFT JOIN (
SELECT Datepart(hour, start_tran_time) as start_tran_hour
, Sum(tran_qty) AS 'Total Picked'
FROM t_tran_log WITH(nolock)
WHERE tran_type = '301'
AND Cast(start_tran_date AS DATE) = '2021-07-06'
GROUP BY Datepart(hour, start_tran_time)
)B ON A.start_tran_hour = B.start_tran_hour
LEFT JOIN (
SELECT Datepart(hour, start_tran_time) as start_tran_hour
, Sum(tran_qty) AS 'Single Packed'
FROM t_tran_log WITH(nolock)
WHERE tran_type = '315'
AND description = 'Single Packing'
AND Cast(start_tran_date AS DATE) = '2021-07-06'
GROUP BY Datepart(hour, start_tran_time)
)C ON A.start_tran_hour = C.start_tran_hour
LEFT JOIN (
SELECT Datepart(hour, start_tran_time) as start_tran_hour
, Sum(tran_qty) AS 'Singles Picked'
FROM t_tran_log WITH(nolock)
LEFT JOIN t_order WITH(nolock) ON t_tran_log.control_number = t_order.order_number
WHERE tran_type = '301'
AND t_order.route = 'SINGLE'
AND Cast(start_tran_date AS DATE) = '2021-07-06'
GROUP BY Datepart(hour, start_tran_time)
)D ON A.start_tran_hour = D.start_tran_hour
LEFT JOIN (
SELECT Datepart(hour, start_tran_time) as start_tran_hour
, Sum(tran_qty) AS 'Multis Picked'
FROM t_tran_log WITH(nolock)
LEFT JOIN t_order WITH(nolock) ON t_tran_log.control_number = t_order.order_number
WHERE tran_type = '301'
AND t_order.route = 'MULTI'
AND Cast(start_tran_date AS DATE) = '2021-07-06'
GROUP BY Datepart(hour, start_tran_time)
)E ON A.start_tran_hour = E.start_tran_hour
LEFT JOIN (
SELECT Datepart(hour, start_tran_time) as start_tran_hour
, Sum(tran_qty) AS 'Multis Packed'
FROM t_tran_log WITH(nolock)
WHERE tran_type = '315'
AND description = 'Multi Packing'
AND Cast(start_tran_date AS DATE) = '2021-07-06'
GROUP BY Datepart(hour, start_tran_time)
)F ON A.start_tran_hour = F.start_tran_hour
LEFT JOIN (
SELECT Datepart(hour, start_tran_time) as start_tran_hour
, Sum(tran_qty) AS 'SA Picked'
FROM t_tran_log WITH(nolock)
WHERE tran_type = '301'
AND ( location_id LIKE 'PR%'
OR location_id LIKE 'SA%' )
AND Cast(start_tran_date AS DATE) = '2021-07-06'
GROUP BY Datepart(hour, start_tran_time)
) G ON A.start_tran_hour = G.start_tran_hour
LEFT JOIN (
SELECT Datepart(hour, start_tran_time) as start_tran_hour
, Sum(tran_qty) AS 'SA Packed'
FROM t_tran_log WITH(nolock)
WHERE tran_type = '315'
AND ( location_id LIKE 'PACKSA%' )
AND Cast(start_tran_date AS DATE) = '2021-07-06'
GROUP BY Datepart(hour, start_tran_time)
) H ON A.start_tran_hour = H.start_tran_hour
LEFT JOIN (
SELECT Datepart(hour, start_tran_time) as start_tran_hour
, Sum(tran_qty) AS 'Units Sorted'
FROM t_tran_log WITH(nolock)
WHERE tran_type = '311'
AND Cast(start_tran_date AS DATE) = '2021-07-06'
GROUP BY Datepart(hour, start_tran_time)
) I ON A.start_tran_hour = I.start_tran_hour
LEFT JOIN (
SELECT Datepart(hour, start_tran_time) as start_tran_hour
, Sum(tran_qty) AS 'Total Packed'
FROM t_tran_log WITH(nolock)
WHERE tran_type = '315'
AND Cast(start_tran_date AS DATE) = '2021-07-06'
GROUP BY ( Datepart(hour, start_tran_time) )
) J ON A.start_tran_hour = J.start_tran_hour
group by rollup (A.start_tran_hour)
ORDER BY A.start_tran_hour
但是,它仍然包含来自您的原始查询的 LEFT INNER JOIN
(不是真实的东西。我编造了这个词。)。
FROM t_tran_log
LEFT JOIN t_order ON t_tran_log.control_number = t_order.order_number
WHERE tran_type = '301'
AND t_order.route = 'MULTI'
在 WHERE
子句中包含 t_order.route = 'MULTI'
会导致 LEFT JOIN
成为 INNER JOIN
。所以我们要解决这个问题。
不过约翰·卡佩莱蒂的建议也不错。让我们尝试在 FROM
子句中引用一次 table 并计算 SELECT
子句中的每一列。
SELECT Datepart(hour, start_tran_time) as start_tran_hour,
, sum(
case
when tran_type = '301'
AND t_order.route = 'SINGLE'
then [singles picked]
else 0
end) as 'singles picked'
, sum(
case
when tran_type = '315'
AND description = 'Single Packing'
then [single packed]
else 0
end) as 'single packed'
, sum(
case
when tran_type = '301'
AND t_order.route = 'MULTI'
then [multis picked]
else 0
end) as 'multis picked'
, sum(
case
when tran_type = '311'
then [units sorted]
else 0
end) as 'units sorted'
, sum(
case
when tran_type = '315'
AND description = 'Multi Packing'
then [multis packed]
else 0
end) as 'multis packed'
, sum(
case
when tran_type = '315'
AND location_id LIKE 'PACKSA%'
then [sa packed]
else 0
end) as 'sa packed'
, sum(
case
when tran_type = '301'
AND ( location_id LIKE 'PR%'
OR location_id LIKE 'SA%' )
then [sa picked]
else 0
end) as 'sa picked'
, sum(
case
when tran_type = '301'
then [total picked]
else 0
end) as 'total picked'
, sum(
case
when tran_type = '315'
then [total packed]
else 0
end) as 'total packed'
, sum(
case
when tran_type IN ( '340', '341' )
then [total shipped]
else 0
end) as 'total shipped'
from t_tran_log WITH(nolock)
LEFT JOIN t_order WITH(nolock) ON t_tran_log.control_number = t_order.order_number
AND t_order.route in ('SINGLE', 'MULTI')
where Cast(start_tran_date AS DATE) = '2021-07-06'
and tran_type IN ('340', '341', '301', '315', '311')
group by rollup (Datepart(hour, start_tran_time))
ORDER BY Datepart(hour, start_tran_time)
如果没有数据库架构和示例数据,我无法确定其中任何一项是否有效。下次,创建并引用一个 dbfiddle。