我有我的代码:
SELECT Y.shipdate[Sent date],
Rate_ = 'price' + Z.Which,
Bill_ = 'bill' + Z.Which,
Rate = CASE Z.Which
WHEN '1' THEN price1
WHEN '2' THEN price2
WHEN '3' THEN price3
WHEN '4' THEN price4
WHEN '5' THEN price5
WHEN '6' THEN price6 END,
Bill = CASE Z.Which
WHEN '1' THEN bill1
WHEN '2' THEN bill2
WHEN '3' THEN bill3
WHEN '4' THEN bill4
WHEN '5' THEN bill5
WHEN '6' THEN billr6 END,
Y.duedate[Due Date], Y.recvdate[Received]
FROM tra Y
CROSS JOIN (SELECT '1' union ALL SELECT '2' UNION ALL SELECT '3' UNION ALL SELECT '4' UNION ALL SELECT '5' UNION ALL SELECT '6') Z (Which)
WHERE name = 'test'
带输出:
Sent Date Rate_ Bill_ Rate Bill Due Date Received
2015-12-22 00:00:00.000 price1 bill1 0.55 300.00 2015-12-16 00:00:00.000 2015-12-01 00:00:00.000
2015-12-22 00:00:00.000 price2 bill2 0.04 2.00 2015-12-16 00:00:00.000 2015-12-01 00:00:00.000
2015-12-22 00:00:00.000 price3 bill3 0.07 43.00 2015-12-16 00:00:00.000 2015-12-01 00:00:00.000
2015-12-22 00:00:00.000 price4 bill4 0.00 0.00 2015-12-16 00:00:00.000 2015-12-01 00:00:00.000
2015-12-22 00:00:00.000 price5 bill5 0.00 0.00 2015-12-16 00:00:00.000 2015-12-01 00:00:00.000
2015-12-22 00:00:00.000 price6 bill6 0.00 0.00 2015-12-16 00:00:00.000 2015-12-01 00:00:00.000
我想要一个只显示价格和账单价值的行的输出。根据我的示例,输出中应该只有3行。感谢。
答案 0 :(得分:1)
它也可以通过其他方式实现,但这是最通用的,并且可以在大多数RDBMS系统中使用。将主查询用作子查询并提供where
条件
SELECT * FROM (
SELECT Y.shipdate[Sent date],
Rate_ = 'price' + Z.Which,
Bill_ = 'bill' + Z.Which,
Rate = CASE Z.Which
WHEN '1' THEN price1
WHEN '2' THEN price2
WHEN '3' THEN price3
WHEN '4' THEN price4
WHEN '5' THEN price5
WHEN '6' THEN price6 END,
Bill = CASE Z.Which
WHEN '1' THEN bill1
WHEN '2' THEN bill2
WHEN '3' THEN bill3
WHEN '4' THEN bill4
WHEN '5' THEN bill5
WHEN '6' THEN bill6 END,
Y.duedate[Due Date], Y.recvdate[Received]
FROM tra Y
CROSS JOIN (SELECT '1' union ALL SELECT '2' UNION ALL SELECT '3' UNION ALL SELECT '4' UNION ALL SELECT '5' UNION ALL SELECT '6') Z (Which)
WHERE name = 'test' ) TMP_TAB
WHERE RATE <> 0 AND BILL <> 0
答案 1 :(得分:0)
试试这个
where name = 'test'
and CASE Z.Which
WHEN '1' THEN price1
WHEN '2' THEN price2
WHEN '3' THEN price3
WHEN '4' THEN price4
WHEN '5' THEN price5
WHEN '6' THEN price6
END <> 0.00
交叉连接生成两个表的笛卡尔积。与其他JOIN运算符不同,它不允许您指定join子句。但在你的情况下,你使用过滤器来消除行。在这种情况下,最好使用Natural Join