Solved

T-Sql Query problem

Posted on 2010-08-19
8
212 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
  • 3
  • 3
  • 2
8 Comments
 
LVL 51

Expert Comment

by:HainKurt
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 51

Expert Comment

by:HainKurt
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
 
LVL 51

Expert Comment

by:HainKurt
ID: 33481004
i give up ;)
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

867 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now