我有一个像这样的表
________________________________________________________
| ID | ADDRESS_LINE1 | CITY | STATE | ZIP | TYPE |
________________________________________________________
| 12345 | abcd st | city1 | CA | zip1 | 1 |
| 12345 | abcd st | city1 | CA | zip1 | 2 |
| 12345 | efgh st | city2 | CA | zip2 | 3 |
________________________________________________________
在此表中,我需要检查Type是1还是3而不是2。
我需要类似的输出:
对于指定的表记录
_______________________________________________________________
| ID | RESIDENTIAL_ADDRESS | MAILING_ADDRESS |
_______________________________________________________________
| 12345 | abcd st, city1, CA, zip1 | efgh st, city2, CA ,zip2 |
_______________________________________________________________
如果表中没有类型1
_______________________________________________________________
| ID | RESIDENTIAL_ADDRESS | MAILING_ADDRESS |
_______________________________________________________________
| 12345 | | efgh st, city2, CA ,zip2 |
_______________________________________________________________
如果表中没有类型3
_______________________________________________________________
| ID | RESIDENTIAL_ADDRESS | MAILING_ADDRESS |
_______________________________________________________________
| 12345 | abcd st, city1, CA, zip1 | |
_______________________________________________________________
如果类型1和类型3都不存在
_______________________________________________________________
| ID | RESIDENTIAL_ADDRESS | MAILING_ADDRESS |
_______________________________________________________________
| 12345 | | |
_______________________________________________________________
我在尝试什么:
SELECT DISTINCT ID,
ADDRLINE1 || ',' || CITY || ',' || STATE || ',' || ZIP (CASE WHEN TYPE = 1 THEN 1 ELSE 0 END) AS RESIDENTIAL_ADDRESS,
ADDRLINE1 || ',' || CITY || ',' || STATE || ',' || ZIP (CASE WHEN TYPE = 3 THEN 1 ELSE 0 END) AS MAILING_ADDRESS
FROM TABLE
WHERE ID = 12345;
这会引发错误
ORA-00904:ZIP:无效的标识符
我知道我想念一些东西。请帮助构建查询。
答案 0 :(得分:2)
ZIP列与其余代码之间缺少分隔符
如果案例内容是其他列,则需要为逗号分隔的RESIDENTIAL_ADDRESS和MAILING_ADDRESS列ZIP
SELECT DISTINCT ID,
ADDRLINE1 || ',' || CITY || ',' || STATE || ',' || ZIP, (CASE WHEN TYPE = 1 THEN 1 ELSE 0 END) AS RESIDENTIAL_ADDRESS,
ADDRLINE1 || ',' || CITY || ',' || STATE || ',' || ZIP, (CASE WHEN TYPE = 3 THEN 1 ELSE 0 END) AS MAILING_ADDRESS
FROM TABLE
WHERE ID = 12345;
如果案例内容是要连接的其他列,则||您需要用逗号分隔RESIDENTIAL_ADDRESS和MAILING_ADDRESS的单独列ZIP
SELECT DISTINCT ID,
ADDRLINE1 || ',' || CITY || ',' || STATE || ',' || ZIP || (CASE WHEN TYPE = 1 THEN 1 ELSE 0 END) AS RESIDENTIAL_ADDRESS,
ADDRLINE1 || ',' || CITY || ',' || STATE || ',' || ZIP || (CASE WHEN TYPE = 3 THEN 1 ELSE 0 END) AS MAILING_ADDRESS
FROM TABLE
WHERE ID = 12345;
或
SELECT DISTINCT ID,
ADDRLINE1 || ',' || CITY || ',' || STATE || ',' || ZIP || ',' || (CASE WHEN TYPE = 1 THEN 1 ELSE 0 END) AS RESIDENTIAL_ADDRESS,
ADDRLINE1 || ',' || CITY || ',' || STATE || ',' || ZIP || ',' || (CASE WHEN TYPE = 3 THEN 1 ELSE 0 END) AS MAILING_ADDRESS
FROM TABLE
WHERE ID = 12345;
如果在同一行上需要两个不同的行,则需要在表上为每个地址两次联接,例如:
SELECT DISTINCT a.ID,
a.ADDRLINE1 || ',' || a.CITY || ',' || a.STATE || ',' || a.ZIP AS RESIDENTIAL_ADDRESS,
b.ADDRLINE1 || ',' || b.CITY || ',' || b.STATE || ',' || b.ZIP AS MAILING_ADDRESS
FROM TABLE a
INNER JOIN TABLE b ON a.ID = b.ID
AND a.TYPE = 1
AND b.TYPE = 3
WHERE a.ID = 12345;