简体   繁体   中英

SQL need to repress one type of row

I have some data being used in a crystal report. the data looks like this. There is some redundancy in the data. the only difference is the Tran_code

Sales_Rep   Tran_code   Order#   Amt
------------------------------------
Joe         001         1234  100.00
Joe         WOC         1234  100.00

Jon         001         5555   50.00

Sue         001         7777   70.00
Sue         WOC         7777   70.00
Sue         QUE         7777   70.00

If an order has only the 001 Tran_code , we want to show that 001 , along with the other columns.

But if it has other codes, then we don't want to show the 001 even in the case of Sue, we would want to show WOC and QUE .

I am looking for SQL that will present the data as needed to the CR. as far as redundant I can do that in the Crystal grouping.

Assuming your desired result was:

Sales_Rep   Tran_code   Order#   Amt
------------------------------------
<row omitted>
Joe         WOC         1234  100.00

Jon         001         5555   50.00

<row omitted>
Sue         WOC         7777   70.00
Sue         QUE         7777   70.00

Try:

SELECT *
FROM table1 AS t1
WHERE
( Tran_code = '001' 
  AND 0 = (
    SELECT COUNT(*) 
    FROM table1 AS t2
    WHERE t1.Sales_Rep = t2.Sales_Rep
    AND   t1.Order  = t2.Order
    AND Tran_code <> '001'))
OR
Tran_code <> '001'

There may be more efficient ways to write this, but I'm going for obvious here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM