Solved

T-Sql Query problem

Posted on 2010-08-19
8
235 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 51

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 51

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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
LVL 51

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Job - date manual 1 41
SQL Server Configuration Manager WMI Error 11 40
How can I use this function? 3 32
the way to learn Microsoft BI 13 45
by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
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…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

740 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