iT邦幫忙

0

SQL資料篩選時間區間問題

大家好第一次新手發問
想問一個SQL資料篩選時間區間問題

這是我串出來的資料
emp_no SD ED
A001 2019/12/23 2020/01/03
A001 2020/05/19 2020/05/25
B002 2020/03/01 2020/03/10
B002 2020/04/05 2020/04/15

然後我的主表Table M 如下
emp_no Date Val
A001 2020/05/18 100
A001 2020/05/23 200
A001 2020/05/24 200
B002 2020/03/05 300
B002 2020/04/05 400

我想得到的資料如下,主表日期符合員編&SD~ED的資料
emp_no Date Val CHK
A001 2020/05/18 100 0
A001 2020/05/23 200 1
A001 2020/05/24 200 1
B002 2020/02/20 300 0
B002 2020/03/05 300 1
B002 2020/04/05 400 1

我是寫with N 包住串出來的資料,但是有出錯好像不能這樣引用with N
如下:
with N as (
串出來資料
)
select emp_no,Date,val,
case
when emp_no=N.emp_no and Date Between N.SD and N.ED then '1'
else '0'
end as 'CHK'
From M

請問有更好得解法嗎?謝謝

2 個回答

0
一級屠豬士
iT邦大師 1 級 ‧ 2020-06-29 22:53:28
-- 使用 PostgreSQL

create table ithelp200629 (
  emp_no text not null
, idate date not null
, val int not null
);

insert into ithelp200629 values
('A001', date '2020/05/18', 100),
('A001', date '2020/05/23', 200),
('A001', date '2020/05/24', 200),
('B002', date '2020/03/05', 300),
('B002', date '2020/04/05', 400);


with n (emp_no, sd, ed) as (
select 'A001', date '2019/12/23', date '2020/01/03'
union all
select 'A001', date '2020/05/19', date '2020/05/25'
union all
select 'B002', date '2020/03/01', date '2020/03/10'
union all
select 'B002', date '2020/04/05', date '2020/04/15'
)
select m.emp_no
     , m.idate
     , m.val
     , case
        when m.idate >= sd and m.idate <= ed then 1
        else 0
       end as chk
     , n.sd
     , n.ed
  from n
     , ithelp200629 m
 where n.emp_no = m.emp_no;
 
+--------+------------+-----+-----+------------+------------+
| emp_no |   idate    | val | chk |     sd     |     ed     |
+--------+------------+-----+-----+------------+------------+
| A001   | 2020-05-18 | 100 |   0 | 2020-05-19 | 2020-05-25 |
| A001   | 2020-05-18 | 100 |   0 | 2019-12-23 | 2020-01-03 |
| A001   | 2020-05-23 | 200 |   1 | 2020-05-19 | 2020-05-25 |
| A001   | 2020-05-23 | 200 |   0 | 2019-12-23 | 2020-01-03 |
| A001   | 2020-05-24 | 200 |   1 | 2020-05-19 | 2020-05-25 |
| A001   | 2020-05-24 | 200 |   0 | 2019-12-23 | 2020-01-03 |
| B002   | 2020-03-05 | 300 |   0 | 2020-04-05 | 2020-04-15 |
| B002   | 2020-03-05 | 300 |   1 | 2020-03-01 | 2020-03-10 |
| B002   | 2020-04-05 | 400 |   1 | 2020-04-05 | 2020-04-15 |
| B002   | 2020-04-05 | 400 |   0 | 2020-03-01 | 2020-03-10 |
+--------+------------+-----+-----+------------+------------+
(10 rows)

1
rogeryao
iT邦高手 2 級 ‧ 2020-06-29 22:54:36
CREATE TABLE TempA
(
	emp_no nvarchar(20),
	SD nvarchar(10),
	ED nvarchar(10)
);

insert into TempA
values
('A001','2019/12/23','2020/01/03'),
('A001','2020/05/19','2020/05/25'),
('B002','2020/03/01','2020/03/10'),
('B002','2020/04/05','2020/04/15');

select *
from TempA;
CREATE TABLE TableM 
(
	emp_no nvarchar(20),
	Date nvarchar(10),
	Val int
);

insert into TableM
values
('A001','2020/05/18',100),
('A001','2020/05/23',200),
('A001','2020/05/24',200),
('B002','2020/02/20',300),
('B002','2020/03/05',300),
('B002','2020/04/05',400);

select *
from TableM;
select TableM.emp_no,TableM.Date,TableM.Val,
sum(case when TableM.Date>=TempB.SD and TableM.Date<=TempB.ED then 1 else 0 end) as CHK
from TableM
left join (select emp_no,SD,ED from TempA) as TempB on TempB.emp_no=TableM.emp_no 
group by TableM.emp_no,TableM.Date,TableM.Val

select emp_no,SD,ED from TempA
改成你原本的 SQL

Demo

我蠻佩服,發問的人,表達的不清晰,你都能想到要group by 聚合.
這段我就懶得替他想了.
另外你的這部分, (select emp_no,SD,ED from TempA) as TempB , 就直接 用 TempA 就好了啦.

rogeryao iT邦高手 2 級 ‧ 2020-06-30 00:41:08 檢舉

他已特別強調..."第一次新手發問" ,
所以我才沒直接用 TempA ,
會上來這邊問的人 , 很多都是新手 .

就扶他一把了 , 太簡潔可能會看不懂 .

我要發表回答

立即登入回答