0

関連するすべての子レコードのステータスがクローズされているすべてのチケットが必要です。したがって、子供の 1 人が別のものである場合、そのチケットを結果セットに入れたくありません。

アンチ結合パターンを使用してこれを試しましたが、私の問題は、子供たちが異なるテーブルに住んでいることです。

例については、このhttp://sqlfiddle.com/#!3/febde/8を参照してください

t1: チケット t2: 関連レコード t3: child1 t4: child1

select ticket.ticketid, rr.relatedrecordkey, rr.relatedrecordclass, c1.id, c1.status, c2.id, c2.status
from ticket
inner join relatedrecord rr on rr.recordkey = ticket.ticketid and rr.class = ticket.class
left join child1 c1 on c1.id = rr.relatedreckey and c1.class = rr.relatedrecclass
left join child2 c2 on c2.id = rr.relatedreckey and c2.class = rr.relatedrecclass

結果:

+----------+--------+-----------+-------+-----------------+---------------+--------+--------+--------+--------+
| ticketid | status | RECORDKEY | class | RELATEDRECCLASS | relatedreckey |   id   | status |   id   | status |
+----------+--------+-----------+-------+-----------------+---------------+--------+--------+--------+--------+
|     1183 | NEW    |      1183 | SR    | WORKORDER       |          1238 | 1238   | NEW    | (null) | (null) |
|     1183 | NEW    |      1183 | SR    | SR              |          1184 | (null) | (null) | 1184   | NEW    |
|     1185 | NEW    |      1185 | SR    | WORKORDER       |          1239 | 1239   | CLOSE  | (null) | (null) |
|     1185 | NEW    |      1185 | SR    | SR              |          1186 | (null) | (null) | 1186   | CLOSE  |
|     1187 | NEW    |      1187 | SR    | WORKORDER       |          1240 | 1240   | CLOSE  | (null) | (null) |
|     1187 | NEW    |      1187 | SR    | SR              |          1188 | (null) | (null) | 1188   | NEW    |
|     1190 | NEW    |      1190 | SR    | SR              |          1191 | (null) | (null) | 1191   | CLOSE  |
|     1192 | NEW    |      1192 | SR    | WORKORDER       |          1241 | 1241   | CLOSE  | (null) | (null) |
+----------+--------+-----------+-------+-----------------+---------------+--------+--------+--------+--------+

それで

  • チケット 1183 には 2 つの関連レコードがあり、どちらもまだクローズされていません。(拒絶)
  • チケット 1185 には 2 つの関連レコードがあり、両方ともクローズされています (受け入れ)
  • チケット 1187 には 2 つの関連レコードがあり、1 つは新規、もう 1 つはクローズされています。(拒絶)
  • チケット 1190 には、クローズされた関連レコードが 1 つあります (受け入れます)。
  • チケット 1192 には 1 つの関連レコード (別のテーブル) があり、これは閉じられています (受け入れ)
  • チケット 1189 には関連レコードがありません (拒否)

このセットから、結果セットにチケット 1185、1190、1192 のみを表示したい。次のようになります。

+----------+--------+-----------+-------+-----------------+---------------+--------+--------+--------+--------+
| ticketid | status | RECORDKEY | class | RELATEDRECCLASS | relatedreckey |   id   | status |   id   | status |
+----------+--------+-----------+-------+-----------------+---------------+--------+--------+--------+--------+
|     1185 | NEW    |      1185 | SR    | WORKORDER       |          1239 | 1239   | CLOSE  | (null) | (null) |
|     1185 | NEW    |      1185 | SR    | SR              |          1186 | (null) | (null) | 1186   | CLOSE  |
|     1190 | NEW    |      1190 | SR    | SR              |          1191 | (null) | (null) | 1191   | CLOSE  |
|     1192 | NEW    |      1192 | SR    | WORKORDER       |          1241 | 1241   | CLOSE  | (null) | (null) |
+----------+--------+-----------+-------+-----------------+---------------+--------+--------+--------+--------+

私は次のようなものを試しました:

select ticket.ticketid, rr.relatedrecordkey, rr.relatedrecordclass, c1.id, c1.status, c2.id, c2.status
from ticket
inner join relatedrecord rr on rr.recordkey = ticket.ticketid and rr.class = ticket.class
where not exists (
    select 1 from child1 c1
    where c1.id = rr.relatedreckey and c1.class = rr.relatedrecclass
    and c1.status <> 'CLOSE'
) and not exists (
    select 1 from child2 c2
    where c2.id = rr.relatedreckey and c2.class = rr.relatedrecclass
    and c2.status <> 'CLOSE'
)

これにより、チケット 2 の 2 行とチケット 3 の 1 行が作成されます (子が 1 つ閉じられているため)、これを正しく解決する方法が少しわかりません。

4

2 に答える 2

0

あなたが結果のために何を望んでいるのかはまだ完全にはわかりませんが、これがそれかもしれないと思います.

select *
from
(
    select t.ticketid
    from ticket t
    join relatedrecord rr on rr.recordkey = t.ticketid and rr.class = t.class
    join child1 c1 on c1.id = rr.relatedreckey 
        and c1.class = rr.relatedrecclass
        and c1.status = 'CLOSE'
    group by t.ticketid
    having MAX(c1.status) = MIN(c1.status)
    UNION ALL
    select t.ticketid
    from ticket t
    join relatedrecord rr on rr.recordkey = t.ticketid and rr.class = t.class
    join child2 c2 on c2.id = rr.relatedreckey 
        and c2.class = rr.relatedrecclass
        and c2.status = 'CLOSE'
    group by t.ticketid
    having MAX(c2.status) = MIN(c2.status)
) x
group by ticketid
于 2015-10-28T16:21:58.070 に答える
0

私は有効な答えを見つけました。

このsqlfiddleを参照してください:http://sqlfiddle.com/#!3/febde/24

SELECT ticket.ticketid,
   count(rr.relatedreckey) AS children,
   count(wo.id) AS wo,
   count(tt.id) AS sr
FROM ticket
INNER JOIN relatedrecord rr ON rr.recordkey = ticket.ticketid
AND rr.class = ticket.class
LEFT JOIN child1 wo ON wo.id = rr.relatedreckey
AND wo.class = rr.relatedrecclass
AND wo.status = 'CLOSE'
LEFT JOIN child2 tt ON tt.id = rr.relatedreckey
AND tt.class = rr.relatedrecclass
AND tt.status = 'CLOSE'
GROUP BY ticket.ticketid 
HAVING (count(wo.id) + count(tt.id)) = count(rr.relatedreckey);

基本的に、関連するタイプごとにクローズされたレコードの数をカウントし、それを関連するレコードの総数と比較します。それらが同じである場合、関連するすべてのレコードを閉じる必要があります。

于 2015-11-06T14:17:33.030 に答える