由于我缺乏SQL知识并且需要帮助,因此我不知道如何获得正确的结果。
我了解ORDER BY命令的作用;我只是不知道如何处理查询返回的数据,以使数据保持在我需要的顺序。
下面是SQL字符串:
SELECT cfcif# AS "CIF Number",cfna1 AS "Customer Name" FROM cncttp08.jhadat842.cfmast cfmast WHERE cfcif#='T000714'
UNION
SELECT cfcif# AS "CIF Number",cfna1 AS "Customer Name" FROM cncttp08.jhadat842.cfmast cfmast WHERE cfcif#='T000713'
UNION
SELECT cfcif# AS "CIF Number",cfna1 AS "Customer Name" FROM cncttp08.jhadat842.cfmast cfmast WHERE cfcif#='T000715'
ORDER BY "CIF Number"
如您所见,“ CIF编号”是乱序的,这是它们的处理方式,并且使用ORDER BY数据是按CIF#排序的,但是我实际上需要使数据乱序
以下是当前字符串如何返回数据:
T000713 | ROGER RABBITT
T000714 | JESSICA RABBIT
T000715 | JAMES KIRK
以下是我需要返回的数据的方式:
T000714 | JESSICA RABBITT
T000713 | ROGER RABBITT
T000715 | JAMES KIRK
当我删除ORDER BY时,返回的结果是:
T000714 | JAMES KIRK
T000713 | JESSICA RABBITT
T000715 | ROGER RABBITT
答案 0 :(得分:3)
我不明白您为什么使用UNION。您可以这样做:
SELECT
cfcif# AS "CIF Number",
cfna1 AS "Customer Name"
FROM cncttp08.jhadat842.cfmast cfmast
WHERE cfcif# IN ('T000714', 'T000713', 'T000715')
ORDER BY
CASE cfcif#
WHEN 'T000714' THEN 1
WHEN 'T000713' THEN 2
WHEN 'T000715' THEN 3
END
答案 1 :(得分:0)
简单!
从order by子句中删除引号。它应该工作。您的代码应如下所示。
`SELECT cfcif# AS "CIF Number",cfna1 AS "Customer Name" FROM
cncttp08.jhadat842.cfmast cfmast WHERE cfcif#='T000714'
UNION
SELECT cfcif# AS "CIF Number",cfna1 AS "Customer Name" FROM
cncttp08.jhadat842.cfmast cfmast WHERE cfcif#='T000713'
UNION
SELECT cfcif# AS "CIF Number",cfna1 AS "Customer Name" FROM
cncttp08.jhadat842.cfmast cfmast WHERE cfcif#='T000715'
ORDER BY CIF Number`