我有以下选择语句:
SELECT
l.rowno,
MAX(a.beac) AS AppBeacon,
l.beac AS LoanBeacon
FROM
l
LEFT JOIN
a ON a.rowno = l.rowno
GROUP BY
l.rowno, l.beac
HAVING
rowno = l.rowno
ORDER BY
l.rowno;
这将导致输出
我想做的是将AppBeacon和LoanBeacon合并为1列。我的标准是,如果LoanBeacon为<= 1,则只要AppBeacon不为null,就使用AppBeacon,否则使用LoanBeacon。
通过在这里查看其他问题,我尝试了一个子查询并提出了该查询
SELECT
l.rowno,
CASE
WHEN l.beac <= 1
THEN (SELECT MAX(a.beac)
FROM l
LEFT JOIN a ON a.rowno = l.rowno
GROUP BY rowno, loan.beacon
HAVING rowno = loan.rowno)
ELSE loan.beacon
END as BeaconFinal
FROM
l
LEFT JOIN
a ON a.rowno = l.rowno
ORDER BY
rowno;
这将导致此错误消息
子查询返回了多个值。当子查询遵循=,!=,<,<=,>,> =或将子查询用作表达式时,不允许这样做。
不太确定从这里去哪里,我们将不胜感激!
答案 0 :(得分:2)
似乎应该可以...
select
rowno
,NewColumn = max(case when LoanBeacon <= 1 then coalesce(AppBeacon, LoanBeacon) else LoanBeacon end )
FROM
l
LEFT JOIN
a ON a.rowno = l.rowno
GROUP BY
l.rowno
ORDER BY
l.rowno;
答案 1 :(得分:1)
我认为这是您想要的逻辑:
HAVING
{{1}}子句没有做任何有用的事情,因此我将其删除。