Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
JJ297
Aged Yak Warrior
940 Posts |
Posted - 2010-01-14 : 09:57:50
|
| This query returns six weeks that's selected from StartDate.I realized I need another field from a different table so I joined a table to get regionname out of that table. When I added that to this query I'm getting 444 rows instead of just six rows that I want. What I've added is in bold. What am I missing?@StartDate datetime,@Region char (1)AsSET NOCOUNT ONSelectCasewhen sort = 'x' then 'Area ' + right(reg,1)else reg end AS reg1, Sort, Reg, Region, n.regionname, Convert(varchar, WKDate,1) as WKDate, co, cr, [Perc Received] = Casewhen co = 0 then '0'when cr = 0 then '0'else convert (varchar,(cr * 100/co * 100)/100) end + '%',Ans, [Perc Ans] = Casewhen cr = 0 then '0'when Ans = 0 then '0'else convert(varchar,(ans * 100/cr * 100)/100) end + '%', OverFlow,[Perc Overflow] = Casewhen cr = 0 then '0'when overflow = 0 then '0'else convert(varchar,(overflow * 100/cr * 100)/100) end + '%',Aband,[Perc Aband] = Casewhen cr = 0 then '0'when Aband = 0 then '0'else convert(varchar,(Aband * 100/cr * 100)/100) end + '%', Busy,[Perc Busy] = Casewhen co = 0 then '0'when Busy = 0 then '0'else convert(varchar,(Busy * 100/co * 100)/100) end + '%' from [dbo].[TSRPTotals] as tinner join natdocfile n on t.region = n.region_ltrwhere(sort = @Region) and (WKDate > DATEADD(wk, -6, @StartDate) AND WKDate <=@StartDate)order by sort, reg, Region, WKDate desc |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-01-14 : 10:00:08
|
quote: Originally posted by JJ297 This query returns six weeks that's selected from StartDate.I realized I need another field from a different table so I joined a table to get regionname out of that table. When I added that to this query I'm getting 444 rows instead of just six rows that I want. What I've added is in bold. What am I missing?@StartDate datetime,@Region char (1)AsSET NOCOUNT ONSelectCasewhen sort = 'x' then 'Area ' + right(reg,1)else reg end AS reg1, Sort, Reg, Region, n.regionname, Convert(varchar, WKDate,1) as WKDate, co, cr, [Perc Received] = Casewhen co = 0 then '0'when cr = 0 then '0'else convert (varchar,(cr * 100/co * 100)/100) end + '%',Ans, [Perc Ans] = Casewhen cr = 0 then '0'when Ans = 0 then '0'else convert(varchar,(ans * 100/cr * 100)/100) end + '%', OverFlow,[Perc Overflow] = Casewhen cr = 0 then '0'when overflow = 0 then '0'else convert(varchar,(overflow * 100/cr * 100)/100) end + '%',Aband,[Perc Aband] = Casewhen cr = 0 then '0'when Aband = 0 then '0'else convert(varchar,(Aband * 100/cr * 100)/100) end + '%', Busy,[Perc Busy] = Casewhen co = 0 then '0'when Busy = 0 then '0'else convert(varchar,(Busy * 100/co * 100)/100) end + '%' from [dbo].[TSRPTotals] as tinner join (select distinct region_ltr,regionname from natdocfile) n on t.region = n.region_ltrwhere(sort = @Region) and (WKDate > DATEADD(wk, -6, @StartDate) AND WKDate <=@StartDate)order by sort, reg, Region, WKDate desc
make the slight tweak above and check |
 |
|
|
JJ297
Aged Yak Warrior
940 Posts |
Posted - 2010-01-14 : 10:04:10
|
| Thanks so I needed to use a subquery just like how you helped me yesterday. How do I know when to use one? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-01-14 : 10:05:55
|
| yup. it depends on relationship existing within table. if you've multiple records existing in table for your interested value then form a derived table to get only distinct ones or apply grouping to get aggregates as in yesterdays example. |
 |
|
|
JJ297
Aged Yak Warrior
940 Posts |
Posted - 2010-01-14 : 10:13:10
|
| Okay thanks I understand now! |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-01-14 : 10:14:56
|
Cool |
 |
|
|
|
|
|
|
|