Author |
Topic |
haroon2k9
Constraint Violating Yak Guru
328 Posts |
Posted - 2010-12-10 : 03:01:41
|
hi,TableAPkid (INT,pRIMARY KEY ) userid(INT)pkidA userid1 1002 1003 1004 2005 200 TableBpKIDB (int,primary key) ,pkidA(fORIGN KEY,INT) Hours (Decimal (9,2) ) CaseType id(int)pkidb pkida hours casetypeid1 1 10 12 2 2 2 Casetypetblcasetypeid(Int,primarykey),casetypename (varchar(50))casetypeid casetypename1 typea2 typpeb3 typec4 typed5 typee Output expected (if parameter passed 100 (Tablea - userid) )totalhrs typea typeb typec typed typee12 10 2 0 0 0am i clear? please reply i mean if parameter passed as 100we need to get the pkidA where userid =100 in Tablea.. (for this case 1,2,3)then we need to sum the hrs where pkida in(1,2,3) in tableb..now see the output please clear now? |
|
haroon2k9
Constraint Violating Yak Guru
328 Posts |
Posted - 2010-12-10 : 03:36:45
|
Am i not clear?..please response |
 |
|
pk_bohra
Master Smack Fu Yak Hacker
1182 Posts |
Posted - 2010-12-10 : 05:23:04
|
Try:Declare @TableA Table( PkidA Int Primary Key, userid INT)Declare @TableB Table( pKIDB Int Primary Key, pKIDA Int, Hours Decimal (9,2), CaseTypeID INT)Declare @Casetypetbl Table( casetypeid Int primary key, casetypename varchar(50)) --totalhrs typea typeb typec typed typee--1--2 10 2 0 0 0Insert Into @TableASelect 1, 100 unionselect 2, 100 unionselect 3, 100 unionselect 4, 200 unionselect 5, 200Insert into @TableBSelect 1,1,10,1 unionSelect 2,2,2,2 unionSelect 3,4,4,4 Insert into @Casetypetbl Select 1,'typea' unionSelect 2, 'typpeb' unionselect 3, 'typec' unionselect 4, 'typed' unionselect 5, 'typee'Select a.userid, sum(b.Hours) as TotalHrs,Max(Case when c.casetypeid =1 then B.Hours else 0 end ) as typea,Max(Case when c.casetypeid =2 then B.Hours else 0 end ) as typeb,Max(Case when c.casetypeid =3 then B.Hours else 0 end ) as typec,Max(Case when c.casetypeid =4 then B.Hours else 0 end ) as typed,Max(Case when c.casetypeid =5 then B.Hours else 0 end ) as typeefrom @TableA Ainner join @TableB B on B.pKIDA = A.pKIDAinner join @Casetypetbl C on B.CaseTypeID = c.CaseTypeIDgroup by A.useridRegards,BohraI am here to learn from Masters and help new bees in learning. |
 |
|
|
|
|