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 |
jkmarvelmani
Starting Member
8 Posts |
Posted - 2008-09-30 : 10:05:16
|
I have a Sql Table with the following columnsUsername -VarcharWeekNumber -VarcharTotalBilledHours -numericCharge_Code -VarcharThe Table contains the employees billed hours for a Charge code.For example Name1 39 40 .netApplName1 38 41 .netApplName2 39 40 .netApplName3 38 41 .netApplI need the output as Name1 39 40 .netApplName2 39 40 .netApplName3 39 0 .netApplPlease help me to get the output |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-09-30 : 10:20:15
|
[code]SELECT t.*FROM YourTable tINNER JOIN (SELECT Username,MAX(WeekNumber) AS MaxWeek FROM YourTable GROUP BY Username)tmpON tmp.Username=t.UsernameAND tmp.MaxWeek=t.WeekNumber[/code] |
 |
|
jkmarvelmani
Starting Member
8 Posts |
Posted - 2008-09-30 : 11:49:34
|
quote: Originally posted by visakh16
SELECT t.*FROM YourTable tINNER JOIN (SELECT Username,MAX(WeekNumber) AS MaxWeek FROM YourTable GROUP BY Username)tmpON tmp.Username=t.UsernameAND tmp.MaxWeek=t.WeekNumber
Thank visakh.But I don't need to select the max. I may need to have the output for any 'N' week. For example i told that. I need to show all the users with the week number i type as input and Billed hours. If they did not have the billed hours for the week i gave i input i need to show zero.please any help |
 |
|
CodesMyBusiness
Starting Member
9 Posts |
Posted - 2008-09-30 : 11:59:06
|
This will pull all the weeks you have, and join it to itself, if the week is not found, then NULL is returned:SELECTa.WeekNumber,b.UserName,b.TotalBilledHours,b.Charge_CodeFROM (SELECT WeekNumberFROM TableNameGROUP BY WeekNumber) aLEFT JOIN TableName bON b.WeekNumber = a.WeekNumberGROUP BYa.WeekNumber,b.UserName,b.TotalBilledHours,b.Charge_Code |
 |
|
jkmarvelmani
Starting Member
8 Posts |
Posted - 2008-09-30 : 12:08:18
|
quote: Originally posted by CodesMyBusiness This will pull all the weeks you have, and join it to itself, if the week is not found, then NULL is returned:SELECTa.WeekNumber,b.UserName,b.TotalBilledHours,b.Charge_CodeFROM (SELECT WeekNumberFROM TableNameGROUP BY WeekNumber) aLEFT JOIN TableName bON b.WeekNumber = a.WeekNumberGROUP BYa.WeekNumber,b.UserName,b.TotalBilledHours,b.Charge_Code
Thanks for the reply. But that thing i got it. For me i need the week number not to be null.That is suppose a user did not have the week number which i given as input,then the output should show the week number and the BilledHours should be 0 for that user.Is it possible to do this. or we need to create any other table to achieve it.I think u understand want I want. |
 |
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2008-09-30 : 13:17:27
|
to change NULLs to 0s use:coalesce(b.TotalBilledHours, 0) as TotalBilledHoursBe One with the OptimizerTG |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-09-30 : 15:47:28
|
[code]DECLARE @Sample TABLE ( userName VARCHAR(20), weekNumber TINYINT, totalBilledHours SMALLMONEY, chargeCode VARCHAR(20) )INSERT @SampleSELECT 'Name1', 39, 40, '.netAppl' UNION ALLSELECT 'Name1', 38, 41, '.netAppl' UNION ALLSELECT 'Name2', 39, 40, '.netAppl' UNION ALLSELECT 'Name3', 38, 41, '.netAppl'SELECT u.userName, w.weekNumber, COALESCE(s.totalBilledHours, 0) AS totalBilledHours, s.chargeCodeFROM ( SELECT MAX(weekNumber) AS weekNumber FROM @Sample ) AS wCROSS JOIN ( SELECT userName FROM @Sample GROUP BY userName ) AS uLEFT JOIN @Sample AS s ON s.userName = u.userName AND s.weekNumber = w.weekNumber[/code] E 12°55'05.63"N 56°04'39.26" |
 |
|
|
|
|
|
|