데이터터는...
no |
name |
start_date |
end_date |
1 |
홍길동 |
2013-05 |
|
2 |
유관순 |
2011-03 |
2014-08 |
3 |
한석봉 |
2014-12 |
|
4 |
김유신 |
2015-02 |
|
대략 이런식으로.. 되있습니다.
입학일과 졸업일 이라고 보면 되고요.
이런 데이터에서
2014-01 ~ 2014-10 까지의 현원 수 를 구하라.... 는것이 문제인데...
mdate |
now_cnt |
2014-01 |
4 |
2014-02 |
4 |
2014-03 |
4 |
2014-04 |
4 |
2014-05 |
4 |
2014-06 |
4 |
2014-07 |
4 |
2014-08 |
4 |
2014-09 |
3 |
2014-10 |
3 |
이런식으로 end_date 가 존재하는 날짜 이후는 현원에서 빠지게끔의 count 를... 매기게끔...
어떻게 해야되는지;;
답변 부탁드립니다.
Comment 3
-
Terry
2015.03.02 19:49
-
웬리
2015.03.03 16:05
DECLARE @tbl TABLE (no int IDENTITY(1,1),name varchar(20),start_date date,end_date date NULL)INSERT INTO @tbl values ('홍길동','2013-05-01',NULL)INSERT INTO @tbl values ('유관순','2011-03-01','2014-08-01')INSERT INTO @tbl values ('한석봉','2014-12-01',NULL)INSERT INTO @tbl values ('김유신','2015-02-01',NULL)DECLARE @datetbl TABLE(mdate date)DECLARE @date DATESET @date = '2013-01-01'WHILE @date < '2015-03-01'BEGININSERT @datetblSELECT @dateSET @date = DATEADD(MONTH,1,@date)ENDSELECT mdate, COUNT(*) as cntFROM @tbl aINNER JOIN @datetbl b ON b.mdate BETWEEN a.start_date AND ISNULL (end_date,'2999-12-31')GROUP BY mdate -
한태
2016.04.14 16:20
CREATE TABLE mancnt(no int IDENTITY(1,1),name varchar(20),start_date date,end_date date NULL)INSERT INTO mancnt values ('홍길동','2013-05-01',NULL)INSERT INTO mancnt values ('유관순','2011-03-01','2014-08-01')INSERT INTO mancnt values ('한석봉','2014-12-01','2015-08-01')INSERT INTO mancnt values ('김유신','2015-02-01',NULL)declare @sdate datetime = '2014-01-01'declare @edate datetime = '2015-10-01';with cte as(select @sdate sd,start_date,end_date,case when start_date<=@sdate then 1 else 0 end s,case when end_date<@sdate then -1 else 0 end efrom mancntunion allselect dateadd(mm,1,sd) sd,start_date,end_date,case when start_date<=dateadd(mm,1,sd) then 1 else 0 end s,case when end_date<dateadd(mm,1,sd) then -1 else 0 end efrom ctewhere sd<@edate)SELECT sd mdate, SUM(s+e) now_cntFROM cteGROUP BY sdOPTION (MAXRECURSION 0)
일단 결과만 나오게..
쿼리 자체는 단순무식합니다 -_-;;;
----------쿼리시작
Declare @as_from_yymm Char(7)
Declare @as_to_yymm Char(7)
Select @as_from_yymm = '2014-01'
Select @as_to_yymm = '2014-10'
with table_a As
(
Select '홍길동' as name, '2013-05' As start_date, '' As end_date Union All
Select '유관순' as name, '2011-03' As start_date, '2014-08' As end_date Union All
Select '한석봉' as name, '2014-12' As start_date, '' As end_date Union All
Select '김유신' as name, '2015-02' As start_date, '' As end_date
)
,month_t As
(
Select '2011-01' As mon Union All
Select '2011-02' As mon Union All
Select '2011-03' As mon Union All
Select '2011-04' As mon Union All
Select '2011-05' As mon Union All
Select '2011-06' As mon Union All
Select '2011-07' As mon Union All
Select '2011-08' As mon Union All
Select '2011-09' As mon Union All
Select '2011-10' As mon Union All
Select '2011-11' As mon Union All
Select '2011-12' As mon Union All
Select '2012-01' As mon Union All
Select '2012-02' As mon Union All
Select '2012-03' As mon Union All
Select '2012-04' As mon Union All
Select '2012-05' As mon Union All
Select '2012-06' As mon Union All
Select '2012-07' As mon Union All
Select '2012-08' As mon Union All
Select '2012-09' As mon Union All
Select '2012-10' As mon Union All
Select '2012-11' As mon Union All
Select '2012-12' As mon Union All
Select '2013-01' As mon Union All
Select '2013-02' As mon Union All
Select '2013-03' As mon Union All
Select '2013-04' As mon Union All
Select '2013-05' As mon Union All
Select '2013-06' As mon Union All
Select '2013-07' As mon Union All
Select '2013-08' As mon Union All
Select '2013-09' As mon Union All
Select '2013-10' As mon Union All
Select '2013-11' As mon Union All
Select '2013-12' As mon Union All
Select '2014-01' As mon Union All
Select '2014-02' As mon Union All
Select '2014-03' As mon Union All
Select '2014-04' As mon Union All
Select '2014-05' As mon Union All
Select '2014-06' As mon Union All
Select '2014-07' As mon Union All
Select '2014-08' As mon Union All
Select '2014-09' As mon Union All
Select '2014-10' As mon Union All
Select '2014-11' As mon Union All
Select '2014-12' As mon Union All
Select '2015-01' As mon Union All
Select '2015-02' As mon Union All
Select '2015-03' As mon Union All
Select '2015-04' As mon Union All
Select '2015-05' As mon Union All
Select '2015-06' As mon Union All
Select '2015-07' As mon Union All
Select '2015-08' As mon Union All
Select '2015-09' As mon Union All
Select '2015-10' As mon Union All
Select '2015-11' As mon Union All
Select '2015-12' As mon
)
Select
a.mon
,(
Case When ISNULL(a.end_date,'') = ''
Then a.cnt
Else a.cnt - 1
End
) As cnt
From
(
Select
a.mon As mon
,MAX(a.cnt) As cnt
,Max(b.end_date) As end_date
From
(
Select
b.mon As mon
,(
Select COUNT(1)
From table_a b
Where b.start_date <= a.start_date
) As cnt
From table_a a
Left Outer Join
month_t b
On a.start_date <= b.mon
Where b.mon Between @as_from_yymm And @as_to_yymm
) a
Left Outer Join
table_a b
On a.mon > b.end_date
Group By a.mon
) a
----------쿼리 끝
결과값
|----------|--------|
|mon | cnt |
|----------|--------|
|2014-01 | 2 |
|2014-02 | 2 |
|2014-03 | 2 |
|2014-04 | 2 |
|2014-05 | 2 |
|2014-06 | 2 |
|2014-07 | 2 |
|2014-08 | 2 |
|2014-09 | 1 |
|2014-10 | 1 |
|----------|--------|