| Author |
Topic |
|
leifthoreson
Starting Member
16 Posts |
Posted - 2005-10-25 : 15:08:51
|
Hi all, and thank you in advance.I have 2 views that each return 3 columnsview1-------------Description , repairs , codeview2----------------Description , repairs, codeDescription is char(10)repairs is intcode is char(5)data looks like thisview1-----------Test1failed ,10, F28/F10Test2failed ,5, F23/F10Test1failed ,10, F28/F10Test3failed ,15, F24/F10view2-----------Test1failed ,10, F28/F10Test2failed ,9, F23/F10Test1failed ,11, F28/F10Test3failed ,5, F24/F10the base quiry I started with isselect Distinct [Description], sum(repairs) , code from view1group by [Description],codewhich returns Test1failed ,20, F28/F10Test2failed ,5, F23/F10Test3failed ,15, F24/F10and the base quiry for view2 I started with isselect Distinct [Description], sum(repairs) , code from view2group by [Description],codewhich returns Test1failed ,21, F28/F10Test2failed ,9, F23/F10Test3failed ,5, F24/F10I need to cobine the data returned into one sumsomething like select Distinct [Description], sum(repairs) , code from (select [Description], repairs , code from view1union allselect [Description], repairs , code from view2) of course this doesn't workdata should look likeTest1failed ,41, F28/F10Test2failed ,14, F23/F10Test3failed ,20, F24/F10 I've been trying for two days to figure out how to select from the returned dataset... Please help!Leif |
|
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2005-10-25 : 15:44:54
|
You seemed to be pretty close... biggest thing you needed was a Group By clauseCreate Table #View1( [Description] varchar(100), Repairs int, Code varchar(100))Insert Into #View1Select 'Test1failed', 10, 'F28/F10' Union AllSelect 'Test2failed', 5, 'F23/F10' Union AllSelect 'Test1failed', 10, 'F28/F10' Union AllSelect 'Test3failed', 15, 'F24/F10'Create Table #View2( [Description] varchar(100), Repairs int, Code varchar(100))Insert Into #View2Select 'Test1failed', 10, 'F28/F10' Union AllSelect 'Test2failed', 9, 'F23/F10' Union AllSelect 'Test1failed', 11, 'F28/F10' Union AllSelect 'Test3failed', 5, 'F24/F10'Select [Description], Repairs = sum(repairs), code from ( select [Description], repairs , code from #view1 union all select [Description], repairs , code from #view2 ) AGroup By [Description], CodeOrder By [Description]Drop Table #View1Drop Table #View2/*Test1failed ,41, F28/F10Test2failed ,14, F23/F10Test3failed ,20, F24/F10*/ Corey Co-worker on children "...when I have children, I'm going to beat them. Not because their bad, but becuase I think it would be fun ..." |
 |
|
|
leifthoreson
Starting Member
16 Posts |
Posted - 2005-10-25 : 17:33:07
|
quote: Originally posted by Seventhnight You seemed to be pretty close... biggest thing you needed was a Group By clauseCreate Table #View1( [Description] varchar(100), Repairs int, Code varchar(100))Insert Into #View1Select 'Test1failed', 10, 'F28/F10' Union AllSelect 'Test2failed', 5, 'F23/F10' Union AllSelect 'Test1failed', 10, 'F28/F10' Union AllSelect 'Test3failed', 15, 'F24/F10'Create Table #View2( [Description] varchar(100), Repairs int, Code varchar(100))Insert Into #View2Select 'Test1failed', 10, 'F28/F10' Union AllSelect 'Test2failed', 9, 'F23/F10' Union AllSelect 'Test1failed', 11, 'F28/F10' Union AllSelect 'Test3failed', 5, 'F24/F10'Select [Description], Repairs = sum(repairs), code from ( select [Description], repairs , code from #view1 union all select [Description], repairs , code from #view2 ) AGroup By [Description], CodeOrder By [Description]Drop Table #View1Drop Table #View2/*Test1failed ,41, F28/F10Test2failed ,14, F23/F10Test3failed ,20, F24/F10*/ Corey Co-worker on children "...when I have children, I'm going to beat them. Not because their bad, but becuase I think it would be fun ..." 
Thanks   wow... I was just missing a little bit.... Your sqlest Thanks again |
 |
|
|
|
|
|