Solved

T-Sql Query problem

Posted on 2010-08-19
8
239 Views
Last Modified: 2012-05-10
Hi, I am having data in a table  as below.

DMDGROUP      WEEKDATE      QTY
9896138670      6/10/2008      5
9896138670      13/10/2008      6
9896138670      27/10/2008      11
9896138670      3/11/2008      8
9896138670      10/11/2008      7
9896138670      1/12/2008      9
9896138670      8/12/2008      6
9885544076      13/10/2008      4
9885544076      3/11/2008      3
9885544076      10/11/2008      8
9885544076      17/11/2008      10
9885544076      1/12/2008      1

Now, I would like to include missing weeks with columns having QTY=0 and also the weekcount starting from 1 for each DMDGROUP. So, it looks like the below table.


DMDGROUP      WEEKDATE      QTY      weekcount
9896138670      6/10/2008      5      1
9896138670      13/10/2008      6      2
9896138670      20/10/2008      0      3
9896138670      27/10/2008      11      4
9896138670      3/11/2008      8      5
9896138670      10/11/2008      7      6
9896138670      17/11/2008      0      7
9896138670      24/11/2008      0      8
9896138670      1/12/2008      9      9
9896138670      8/12/2008      6      10
9885544076      13/10/2008      4      1
9885544076      20/10/2008      0      2
9885544076      27/10/2008      0      3
9885544076      3/11/2008      3      4
9885544076      10/11/2008      8      5
9885544076      17/11/2008      10      6
9885544076      24/11/2008      0      7
9885544076      1/12/2008      1      8



Can someone help me with as soon as possible.

Thanks
0
Comment
Question by:rpk2606
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 53

Expert Comment

by:Huseyin KAHRAMAN
ID: 33480916
i suggest create a table numbers (n:int) and put 0,1,...,9

create view weeks as
select 10*d1.n+d2.n from numbers d2, numbers d1 where 10*d1.n+d2.n < 52

this will give use 0,1,2,...,51
0
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 500 total points
ID: 33480949
;with dg as (select DMDGROUP,MIN(Weekdate) as Week1, MAX(WEEKDATE) as LastWeek from tbl group by DMDGROUP)
select dg.DMDGROUP,wk.WeekDate,isnull(t.QTY,0),wk.WeekNo
FROM dg
cross apply (
      select w.number+1 as WeekNo, dateadd(wk,w.number,dg.Week1) as WeekDate
      from master..spt_values w
      where w.type='P' and w.number<=DATEDIFF(WK,dg.Week1,dg.LastWeek)) wk
left join tbl t on t.DMDGROUP=dg.DMDGROUP and t.WEEKDATE=wk.WeekDate
order by dg.DMDGROUP,wk.WeekDate
0
 
LVL 53

Expert Comment

by:Huseyin KAHRAMAN
ID: 33480998
and this one will give

select min(weekdate) middate, max(weekdate) maxdate, DATEPART(WK , min(weekdate)) minweek, DATEPART(WK , max(weekdate)) maxweek, dmdgroup from dmd group by dmdgroup

2008-10-13 00:00:00      2008-12-01 00:00:00      42      49      9885544076
2008-10-06 00:00:00      2008-12-08 00:00:00      41      50      9896138670
0
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
LVL 53

Expert Comment

by:Huseyin KAHRAMAN
ID: 33481004
i give up ;)
0
 

Author Comment

by:rpk2606
ID: 33481079
awesome man...thanks a lot cyberkiwi
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33481116
You're welcome!
0
 

Author Comment

by:rpk2606
ID: 33484759
Hi Cyberkiwi..I am trying to run this.

select dg.DMDUNIT,dg.DMDGROUP,wk.weekdate,isnull(t.QTY,0),wk.WeekNo
FROM SCADHOC.rpk.dg
cross apply (
      select w.number+1 as WeekNo, dateadd(wk,w.number,dg.Week1) as weekdate
      from master..spt_values w
      where w.type='P' and w.number<=DATEDIFF(WK,dg.Week1,dg.LastWeek)) wk
left join week t on t.dmdunit=dg.dmdunit and t.DMDGROUP=dg.DMDGROUP and t.weekdate=wk.weekdate
order by dg.dmdunit,dg.DMDGROUP,wk.weekdate

But I am getting this error now.

Msg 208, Level 16, State 1, Line 1
Invalid object name 'week'.
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33484875
;with dg as (
    select DMDGROUP,MIN(Weekdate) as Week1, MAX(WEEKDATE) as LastWeek
    from SCADHOC.rpk.dg
    group by DMDGROUP)
select dg.DMDGROUP,wk.WeekDate,isnull(t.QTY,0),wk.WeekNo
FROM dg
cross apply (
      select w.number+1 as WeekNo, dateadd(wk,w.number,dg.Week1) as WeekDate
      from master..spt_values w
      where w.type='P' and w.number<=DATEDIFF(WK,dg.Week1,dg.LastWeek)) wk
left join SCADHOC.rpk.dg t on t.DMDGROUP=dg.DMDGROUP and t.WEEKDATE=wk.WeekDate
order by dg.DMDGROUP,wk.WeekDate

The first 4 lines creates a virtual table named "dg".  It is heavily referenced further down.
The 2nd to last row is where you plug in your actual table name (and aliased at "t")
0

Featured Post

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question