0

このSQL選択を改良する必要があります:

SELECT ch_num, payment_type, Total FROM transactions WHERE ( ch_num ) 
IN (
SELECT ch_num FROM transactions GROUP BY ch_num HAVING count('ch_num') > 1 
)
ORDER BY `ch_num`

私が得る結果は次のとおりです:

ch_num  payment_type    Total 
20001   visa        36.60
20001   visa        36.60
20001   mc          30.60
50019   cash        9.00
50019   mc          18.95
50023   cash        2.70
50023   visa        7.00

ただし、「cash」payment_typeがある場合にのみ結果行が必要なので、「ch_no」20001は省略してください。正しい結果は次のようになります。

ch_num  payment_type    Total 
50019   cash        9.00
50019   mc          18.95
50023   cash        2.70
50023   visa        7.00
4

2 に答える 2

1
SELECT ch_num, payment_type, Total
FROM transactions 
WHERE ch_num IN 
(
       SELECT ch_num 
       FROM transactions 
       GROUP BY ch_no
       HAVING count('ch_num') > 1 
       and sum(payment_type='cash') >= 1
)
ORDER BY `ch_num`
于 2012-10-17T00:22:50.837 に答える
0

これは完全で実績のあるコード例であり、最後にテスト結果があります。Oracleを使用しましたが、SQLSELECTの構文は同じである必要があります。

create table transactions (ch_num int, payment_type varchar2(100), total float);

insert into transactions values(20001,'visa',36.60);
insert into transactions values(20001,'mc',30.60);
insert into transactions values(50019,'cash',9.00);
insert into transactions values(50019,'mc',18.95);
insert into transactions values(50023,'cash',2.70);
insert into transactions values(50023,'visa',7.00);

SELECT ch_num, payment_type, Total FROM transactions a WHERE ( ch_num ) 
IN (
SELECT ch_num FROM transactions  GROUP BY ch_num HAVING count(ch_num) > 1 
)
AND EXISTS
(SELECT  ch_num FROM transactions b where payment_type = 'cash' and a.ch_num = b.ch_num)
ORDER BY ch_num

結果:

    CH_NUM  PAYMENT_TYPE    TOTAL
1   50019   cash    9
2   50019   mc  18.95
3   50023   cash    2.7
4   50023   visa    7
于 2012-10-17T00:22:45.320 に答える