select CASE WHEN ( rtt.us_code = 's' )
THEN rtt.name
ELSE '' END AS input_tax_name,
CASE WHEN ( rtt2.us_code = 'r')
THEN rtt2.name
ELSE '' END AS output_tax_name,
CASE WHEN ( rtt.us_code = 's' )
THEN rtt.acc_id
ELSE 0 END AS input_tax_rate,
CASE WHEN ( rtt2.us_code = 'r')
THEN rtt2.acc_id
ELSE 0 END AS output_tax_rate
from supplier_item si
JOIN ret_tx_type rtt
ON si.ret_tx_type_id = rtt.ret_tx_type_id
JOIN ret_tx_type rtt2
ON si.ret_tx_type_id = rtt2.ret_tx_type_id
where si.ret_tx_type_id is not null
我试图在报告的同一行显示输入和输出税取决于us_code是'r'和's'。 但我得到的结果如
如果有输入税...输出税栏将为空 如果有输出税...输入税栏将为空
我希望在同一行中获取两者,而不需要在任何行中使用null
请帮助
答案 0 :(得分:3)
SELECT
MAX(CASE rtt.us_code WHEN 's' THEN rtt.name END) AS input_tax_name,
MAX(CASE rtt.us_code WHEN 'r' THEN rtt.name END) AS output_tax_name,
MAX(CASE rtt.us_code WHEN 's' THEN rtt.acc_id END) AS input_tax_rate,
MAX(CASE rtt.us_code WHEN 'r' THEN rtt.acc_id END) AS output_tax_rate
FROM supplier_item si
INNER JOIN ret_tx_type rtt
ON si.ret_tx_type_id = rtt.ret_tx_type_id
GROUP BY ??? /* here you should supply a column,
presumably in 'si', that is common
to both of the related 'r'- and
's'-tax records */
答案 1 :(得分:2)
您需要在查询中添加GROUP BY
内容(可能GROUP BY si.id
?)
将CASE
语句放入聚合中。 e.g。
MAX(CASE WHEN ( rtt2.us_code = 'r')
THEN rtt2.acc_id
ELSE 0 END)