从基于另一列

时间:2018-05-08 22:00:20

标签: sql tsql sql-server-2012

如何根据另一个字段的特定值排除字段中具有特定值的行?深入探讨:

我有一个名为" rule_name"的列这是从案例陈述中得出的。

我有另一个名为" DeliveryTermsIdentifier"那是从另一张桌子拉出来的(见下面查询结果的图片)。

我尝试删除带有rule_name "需要传递事件" 的行,仅当 DeliveryTermsIdentifier为" PP" ," DA"等。我被抛弃了,因为我的rule_name列已基于条件了。

image of query result

以下是用于派生这两列的SQL:

  ,case when s.ShipmentNo is not null then cast(dqr.rulename as varchar(30))
        else cast(dqc.dataqualityruleid as varchar(30)) end as rule_name

  ,case when (dqd.SuccessFlag = 0) then 'Missing'
        when (dqd.SuccessFlag = 1) then 'Reported'
        when (s.ShipmentNo is null) then 'Missing' end reporting_result

  ,s.DeliveryTermsIdentifier

from CustomerMasterList cml

join organization o on o.organizationid = cml.senderid
inner join DataQualityConfig dqc on dqc.DomainId = cml.DomainID
left outer join shipment s on s.shipmentno = cml.shipmentno
left outer join dataqualityresult dqd on dqd.shipmentid = s.shipmentid 
left outer join dataqualityrule dqr on dqr.dataqualityruleid 
=dqd.dataqualityruleid 

2 个答案:

答案 0 :(得分:0)

一种方法是将您拥有的内容放在子查询中,并使用外部查询中的where子句过滤掉。 E.g。

0000-00d0:  xx xx xx xx xx xx xx xx-ff c4 00 1f-00 00 01 05  !....... ........
0000-00e0:  01 01 01 01-01 01 00 00-00 00 00 00-00 00 01 02  ........ ........
0000-00f0:  03 04 05 06-07 08 09 0a-0b ff c4 00-b5 10 00 02  ........ ........
0000-0100:  01 03 03 02-04 03 05 05-04 04 00 00-01 7d 01 02  ........ .....}..
0000-0110:  03 00 04 11-05 12 21 31-41 06 13 51-61 07 22 71  ......!1 A..Qa."q
0000-0120:  14 32 81 91-a1 08 23 42-b1 c1 15 52-d1 f0 24 33  .2....#B ...R..$3
0000-0130:  62 72 82 09-0a 16 17 18-19 1a 25 26-27 28 29 2a  br...... ..%&'()*
0000-0140:  34 35 36 37-38 39 3a 43-44 45 46 47-48 49 4a 53  456789:C DEFGHIJS
0000-0150:  54 55 56 57-58 59 5a 63-64 65 66 67-68 69 6a 73  TUVWXYZc defghijs
0000-0160:  74 75 76 77-78 79 7a 83-84 85 86 87-88 89 8a 92  tuvwxyz. ........
0000-0170:  93 94 95 96-97 98 99 9a-a2 a3 a4 a5-a6 a7 a8 a9  ........ ........
0000-0180:  aa b2 b3 b4-b5 b6 b7 b8-b9 ba c2 c3-c4 c5 c6 c7  ........ ........
0000-0190:  c8 c9 ca d2-d3 d4 d5 d6-d7 d8 d9 da-e1 e2 e3 e4  ........ ........
0000-01a0:  e5 e6 e7 e8-e9 ea f1 f2-f3 f4 f5 f6-f7 f8 f9 fa  ........ ........
0000-01b0:  ff c4 00 1f-01 00 03 01-01 01 01 01-01 01 01 01  ........ ........
0000-01c0:  00 00 00 00-00 00 01 02-03 04 05 06-07 08 09 0a  ........ ........
0000-01d0:  0b ff c4 00-b5 11 00 02-01 02 04 04-03 04 07 05  ........ ........
0000-01e0:  04 04 00 01-02 77 00 01-02 03 11 04-05 21 31 06  .....w.. .....!1.
0000-01f0:  12 41 51 07-61 71 13 22-32 81 08 14-42 91 a1 b1  .AQ.aq." 2...B...
0000-0200:  c1 09 23 33-52 f0 15 62-72 d1 0a 16-24 34 e1 25  ..#3R..b r...$4.%
0000-0210:  f1 17 18 19-1a 26 27 28-29 2a 35 36-37 38 39 3a  .....&'( )*56789:
0000-0220:  43 44 45 46-47 48 49 4a-53 54 55 56-57 58 59 5a  CDEFGHIJ STUVWXYZ
0000-0230:  63 64 65 66-67 68 69 6a-73 74 75 76-77 78 79 7a  cdefghij stuvwxyz
0000-0240:  82 83 84 85-86 87 88 89-8a 92 93 94-95 96 97 98  ........ ........
0000-0250:  99 9a a2 a3-a4 a5 a6 a7-a8 a9 aa b2-b3 b4 b5 b6  ........ ........
0000-0260:  b7 b8 b9 ba-c2 c3 c4 c5-c6 c7 c8 c9-ca d2 d3 d4  ........ ........
0000-0270:  d5 d6 d7 d8-d9 da e2 e3-e4 e5 e6 e7-e8 e9 ea f2  ........ ........
0000-0280:  f3 f4 f5 f6-f7 f8 f9 fa-xx xx xx xx xx xx xx xx  ........ .....(..

答案 1 :(得分:0)

您还可以将case语句封装到外部apply中,并使用如下别名:

SELECT * ,
   rules.rule_name ,
   RESULT.reporting_result ,
   s.DeliveryTermsIdentifier
FROM   CustomerMasterList cml
   JOIN organization o ON o.organizationid = cml.senderid
   INNER JOIN DataQualityConfig dqc ON dqc.DomainId = cml.DomainID
   LEFT OUTER JOIN shipment s ON s.shipmentno = cml.shipmentno
   LEFT OUTER JOIN dataqualityresult dqd ON dqd.shipmentid = s.shipmentid
   LEFT OUTER JOIN dataqualityrule dqr ON dqr.dataqualityruleid = dqd.dataqualityruleid
   OUTER APPLY (   SELECT CASE WHEN ( dqd.SuccessFlag = 0 ) THEN 'Missing'
                               WHEN ( dqd.SuccessFlag = 1 ) THEN 'Reported'
                               WHEN ( s.ShipmentNo IS NULL ) THEN 'Missing'
                          END reporting_result ) RESULT
   OUTER APPLY (   SELECT CASE WHEN s.ShipmentNo IS NOT NULL THEN
                                   CAST(dqr.rulename AS VARCHAR(30))
                               ELSE
                                   CAST(dqc.dataqualityruleid AS VARCHAR(30))
                          END AS rule_name ) rules
WHERE  s.DeliveryTermsIdentifier NOT IN ( 'PP', 'DA' )
   AND rules.rule_name <> 'Delivery event required';