sql server - mssql join criteria with the same table -
just need little help. have calendar table here sqlfiddle.com/#!3/5d8a9 there 2 flags (m, w) indicating month or week. need join tbl2 (below) has weekly date field. join tbl2 on weekstartdate cal table on start date (with w flag) need return startdate 'm' flag if weekstartdate falls between start , end date. make sense?
tbl2: prod_id weeknum wkstartdate postingdate qty ---------------------------------------------------------------- 1043890 5 2015-01-25 2016-01-18 3 1043890 6 2015-02-01 2016-01-18 6 1043890 7 2015-02-08 2016-01-18 2 1043890 8 2015-02-15 2016-01-18 0 ... 1043890 50 2015-12-06 2016-01-18 1 1043890 51 2015-12-13 2016-01-18 2 1043890 52 2015-12-20 2016-01-18 7 desired result: ================== prod_id weeknum wkstartdate postingdate qty period ---------------------------------------------------------------------------- 1043890 5 2015-01-25 2016-01-18 3 2015-01-25 1043890 6 2015-02-01 2016-01-18 6 2015-01-25 1043890 7 2015-02-08 2016-01-18 2 2015-01-25 1043890 8 2015-02-15 2016-01-18 0 2015-01-25 1043890 10 2015-03-01 2016-01-18 0 2015-03-01 ... 1043890 50 2015-12-06 2016-01-18 1 2015-11-22 1043890 51 2015-12-13 2016-01-18 2 2015-11-22 1043890 52 2015-12-20 2016-01-18 7 2015-11-22 if makes sense, i'm trying check if weekstartdate falls between range of dates (startdate-enddate) flag of m, bring startdate , either update record in temp table or create temp table new column added each record.
thanks.
what understood trying join table2 on weekstarttable cal table on startdate , want modify flag if week start date falls between start date , end date. if got correct, try following query.
select t2.prod_id,t2.weeknum,c.startdate ,case when t2.wkstartdate between c.startdate , c.enddate 'm' else null end modifiedflag, c.flag originalflag cal c,table2 t2 c.flag ='w' , t2.wkstartdate = c.startdate , t2.wkstartdate between c.startdate , c.enddate;
Comments
Post a Comment