下面的Oracle查询给出error_message
和Serial_num
是否存在任何不同的错误。
如果计数为零或没有Different error
而不显示空白/空结果。我怎么能看到这样的输出?我尝试使用NVL(error_message,0)
和COALESCE (Sum(total),0)
,但未获得所需的输出。
预期输出:
1 Different Errors: 0
Oracle SQL查询:
SELECT
1 as Index_Num,
CONCAT('Different Errors: ', error_message || '# ' || serial_num),
SUM(total)
FROM (
SELECT error_message, serial_num, COUNT(*) total
FROM Table1
WHERE error_message NOT LIKE '%INVALID%'
GROUP BY error_message, serial_num
)
GROUP BY error_message, serial_num
答案 0 :(得分:1)
为子查询创建UNION ALL
,并将NOT EXISTS
与CTE
一起使用,以解决WITH cte AS (
SELECT error_message, serial_num, COUNT(*) total
FROM Table1
WHERE error_message NOT LIKE '%INVALID%'
GROUP BY error_message, serial_num
)
SELECT
1 as Index_Num,
CONCAT(
'Different Errors: ',
list_agg(error_message || '# ' || serial_num) within group (order by error_message)
),
SUM(total)
FROM cte
UNION ALL
SELECT 1, 'Different Errors: ', 0
FROM dual
WHERE NOT EXISTS (SELECT 1 FROM cte)
不返回任何行的情况:
"LogGroupName": {
"Fn::Join": [
"",
[
"/aws/lambda/",
{
"Ref": "MyLambdaFunction"
}
]
]
}
答案 1 :(得分:0)
这并不是您所要的,但可能会有用。您可以使用grouping sets
轻松添加包含错误总数的行:
SELECT 1 as Index_Num,
('Different Errors: ' || error_message || '# ' || serial_num),
COUNT(*) as total
FROM Table1
WHERE error_message NOT LIKE '%INVALID%'
GROUP BY GROUPING SETS ( (error_message, serial_num), () );
A,即使有错误,这也会产生摘要行。在我看来,您可能会发现这很有用。
答案 2 :(得分:0)
D'哦!看来我花了太长时间。这是后代的另一个选择:
SELECT
1,
CONCAT(
'Different Errors: ',
CASE
WHEN src.error_message IS NULL THEN ''
ELSE src.error_message || ' # ' || src.serial_num
END
) Summary,
COALESCE(src.total, 0) AS total
FROM dual -- Get a seed row (in case there are no rows in error table)
LEFT JOIN (
SELECT error_message, serial_num, COUNT(*) total
FROM Table1
WHERE error_message NOT LIKE '%INVALID%'
GROUP BY error_message, serial_num
) src ON 0=0