选择IF或有更简单的方法吗?

时间:2018-06-13 01:53:54

标签: sql sql-server

我有以下3个表格。

PART

USER

DOCUMENT

       ╔═════════════╦════════════════════╦════════════════╗
       ║ Part Number ║ Keyed Name         ║ Classification ║
       ╠═════════════╬════════════════════╬════════════════╣
       ║ 456789      ║ 1/4" Bolt          ║ Buy            ║
       ╠═════════════╬════════════════════╬════════════════╣
       ║ 224567      ║ 1/4" Washer        ║ Buy            ║
       ╠═════════════╬════════════════════╬════════════════╣
       ║ 119011      ║ Leg Brace Assembly ║ Make           ║
       ╚═════════════╩════════════════════╩════════════════╝


       ╔═════════╦══════════════╦═══════════════════╗
       ║ User ID ║ Keyed Name   ║ Email             ║
       ╠═════════╬══════════════╬═══════════════════╣
       ║ 9999111 ║ Bob Johnson  ║ Bob@comail.com    ║
       ╠═════════╬══════════════╬═══════════════════╣
       ║ 1119999 ║ Lauren Halls ║ LHalls@comail.com ║
       ╠═════════╬══════════════╬═══════════════════╣
       ║ 1199199 ║ Mike Taylor  ║ MTay@comail.com   ║
       ╚═════════╩══════════════╩═══════════════════╝

       ╔═════════════════╦════════════════════╦══════════════╦══════════════╗
       ║ Document Number ║ Keyed Name         ║ Created By   ║ Modified By  ║
       ╠═════════════════╬════════════════════╬══════════════╬══════════════╣
       ║ 123456          ║ Random 1           ║ Bob Johnson  ║ Lauren Halls ║
       ╠═════════════════╬════════════════════╬══════════════╬══════════════╣
       ║ 119011_DDM      ║ Leg Brace Assembly ║ Lauren Halls ║ Lauren Halls ║
       ╠═════════════════╬════════════════════╬══════════════╬══════════════╣
       ║ 123456_DDM      ║ Random 1           ║ Bob Johnson  ║ Mike Taylor  ║
       ╠═════════════════╬════════════════════╬══════════════╬══════════════╣
       ║ 335678          ║ Random 3           ║ Bob Johnson  ║ Mike Taylor  ║
       ╚═════════════════╩════════════════════╩══════════════╩══════════════╝

我的查询如下:

select
d1.KEYED_NAME as "Document Number",
d1.CSH_PLI_DUMMY_REVISION as "Revision",
d1.CREATED_ON as "Created On",
d1.MODIFIED_ON as "Modified On",
d1.RELEASE_DATE as "Release Date",
d1.[STATE] as "State",
p1.KEYED_NAME as "Product",
d1.CLASSIFICATION as "Item Type",
u1.KEYED_NAME as "Created By",
u1.KEYED_NAME as "Modified By",
d1.IS_CURRENT as "Current"

from innovator.DOCUMENT as d1
LEFT JOIN innovator.[USER] as u1 ON (d1.CREATED_BY_ID = u1.id)
LEFT JOIN innovator.PRODUCT as p1 ON (p1.id = d1.CP_PRODUCT)


where d1.IS_CURRENT = '1'
    AND d1.[ITEM_NUMBER] LIKE '%_DDM'
    or EXISTS (select 1 from innovator.DOCUMENT d2 LEFT JOIN innovator.PRODUCT p1 ON (d1.CP_PRODUCT = p1.id)
                where d1.IS_CURRENT = 1 AND d1.[ITEM_NUMBER] + '_DDM' = d2.[ITEM_NUMBER])
order by d1.KEYED_NAME ASC, d1.CSH_PLI_DUMMY_REVISION DESC

现在,查询将找到Document及其对应的_DDM编号。然而,我没有得到的是,如果在Document表中有一个_DDM,查询还需要找到一个PART号。从给定的示例中查询需要查找部件号119011,因为文档表中有一个119011_DDM,它没有自己的文档号119011.

我尝试将上面的所有查询UNION到PART表的select语句。我在NOT EXISTS上尝试了几个变种,但我确信我使用的语法不正确。

基本上我又难过了。

1 个答案:

答案 0 :(得分:0)

你非常接近这一点。您可以将JOIN与OR子句一起使用,因此您可以将部分与文档编号或DDM匹配,并使用part-id。

select
d1.KEYED_NAME as "Document Number",
d1.CSH_PLI_DUMMY_REVISION as "Revision",
d1.CREATED_ON as "Created On",
d1.MODIFIED_ON as "Modified On",
d1.RELEASE_DATE as "Release Date",
d1.[STATE] as "State",
p1.KEYED_NAME as "Product",
d1.CLASSIFICATION as "Item Type",
u1.KEYED_NAME as "Created By",
u1.KEYED_NAME as "Modified By",
d1.IS_CURRENT as "Current"

from innovator.DOCUMENT as d1
LEFT JOIN innovator.[USER] as u1 ON (d1.CREATED_BY_ID = u1.id)
LEFT JOIN innovator.PRODUCT as p1 ON (p1.id = d1.CP_PRODUCT) OR (p1.id + '_DDM' = d1.[ITEM_NUMBER])

where d1.IS_CURRENT = '1'
AND d1.[ITEM_NUMBER] LIKE '%_DDM'
or EXISTS (select 1 from innovator.DOCUMENT d2 where d2.IS_CURRENT = 1 AND d2.[ITEM_NUMBER] + '_DDM' = d1.[ITEM_NUMBER])
order by d1.KEYED_NAME ASC, d1.CSH_PLI_DUMMY_REVISION DESC;