teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

訂正

2021/11/16 06:52

投稿

sazi
sazi

スコア25430

answer CHANGED
@@ -8,17 +8,19 @@
8
8
  ※検証はしていませんので、あくまでイメージとして考えて下さい。
9
9
  ```SQL
10
10
  SELECT 商品番号
11
- , sum(case when 登録日時 < sysdate then 1 else 0 end) as ①
11
+ , sum(case when not exists(select 1 from 商品 where 商品番号=x.商品番号 and (登録日時 > sysdate or 登録日時 is null) then 1 else 0 end) as ①
12
- , sum(case when 登録日時 > sysdate then 1 else 0 end) as ②
12
+ , sum(case when not exists(select 1 from 商品 where 商品番号=x.商品番号 and (登録日時 < sysdate or 登録日時 is null) then 1 else 0 end) as ②
13
- , sum(case when 登録日時 is not null
13
+ , sum(case when 登録日時 > sysdate
14
14
  and (select count(*) from 商品 where 商品番号=x.商品番号 and 登録日時 > sysdate) > 1
15
15
  and not exists(select 1 from 商品 where 商品番号=x.商品番号 and 登録日時 is null)
16
16
  then 1 else 0 end) as ③
17
+ , sum(case when (登録日時 > sysdate or 登録日時 is null)
17
- , sum(case when exists(select 1 from 商品 where 商品番号=x.商品番号 and 登録日時 > sysdate)
18
+ and exists(select 1 from 商品 where 商品番号=x.商品番号 and 登録日時 > sysdate)
18
19
  and exists(select 1 from 商品 where 商品番号=x.商品番号 and 登録日時 is null)
19
20
  then 1 else 0 end) as ④
20
21
  , sum(case when not exists(select 1 from 商品 where 商品番号=x.商品番号 and 登録日時 is not null) then 1 else 0 end) as ⑤
21
22
  FROM 商品 x
22
23
  GROUP BY 商品番号
23
24
  HAVING COUNT(商品番号) > 1
25
+
24
26
  ```