我是sql的新手,我需要你的帮助。非常感谢你的时间!
Locprog表列出了在不同位置启动程序的日期(每个位置由唯一的指示符LOC标识)
LOC PROG DATE_STARTED
121 P 10-JUL-95
131 P 11-JUL-95
141 P 18-APR-00
151 P 19-APR-00
161 P 26-MAY-06
171 P 28-MAY-06
181 P 30-APR-15
191 P 31-APR-15
192 P 19-JAN-16
195 P 20-JAN-16
DTWK表格给出了与特定日期相关的周数
D_IND W_IND
09-JUL-95 199501
10-JUL-95 199502
11-JUL-95 199502
12-JUL-95 199502
13-JUL-95 199502
14-JUL-95 199502
15-JUL-95 199502
16-JUL-95 199502
17-JUL-95 199502
... ...
18-APR-00 200005
19-APR-00 200005
26-MAY-06 200602
28-MAY-06 200602
30-APR-15 201504
31-APR-15 201504
19-JAN-16 201603
20-JAN-16 201603
问题:我想在200602和201603周之间找到该计划有效的地点数
我试过了
select count(*) from (
select distinct A.LOC, A.Date_started, B.w_ind as wk from Locprog A, DTWK B
where a.date_started=B.d_ind) aa
where aa.wk<=200602
答案:6
我试图在&lt; =运算符之后包含周范围(200602和201603)并且然后按 所需范围的w_id
select count(*), bb.wek from (
select distinct A.LOC, A.Date_started, B.w_ind as wk from Locprog A, DTWK B
where a.date_started=B.d_ind) aa
where aa.wk<= ( select distinct W_ind as wek fromDTWK where W_ind between 200602 and 201603) bb
group by bb.wk
order by bb.wk
查询不起作用。你能帮忙吗?谢谢!
所需的输出
wk count
200602 6
201504 8
201603 10
答案 0 :(得分:0)
SELECT COUNT(DISTINCT lp.loc)
FROM LogProg lp JOIN DTWK dw on lp.date_started = dw.d_ind
WHERE dw.w_ind >= 200602
AND dw.w_ind <= 201603;
答案 1 :(得分:0)
DTWK表每周的编号都有很多记录 以下查询给出了每周数字的开始和结束日期:
SELECT min( D_IND ) start_date,
max( D_IND ) end_date
FROM DTWK d
WHERE d.W_IND between 200602 and 201603
GROUP BY W_IND
并且可以将此查询的结果连接到LOC表以计算计数:
SELECT count( distinct loc )
FROM LOC l
JOIN (
SELECT min( D_IND ) start_date,
max( D_IND ) end_date
FROM DTWK d
WHERE d.W_IND between 200602 and 201603
GROUP BY W_IND
) x
ON l.date_started between x.start_date and x.end_date
;