| Author |
Topic |
|
BankOfficerHere
Posting Yak Master
124 Posts |
Posted - 2008-08-15 : 21:28:53
|
| Hi I have two tables and to combine them into 1.Table 1Name EmployeeID ScoresMichael 111 5Michelle 222 10Allan 333 6Gilbert 444 8Table 2Name EmployeeID ScoresMichael 111 9Michelle 222 11Gilbert 444 7I want them to be combined as:Name EmployeeID Scores ScoresMichael 111 5 9Michelle 222 10 11Allan 333 6 Gilbert 444 8 7 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2008-08-15 : 21:44:08
|
have you try using INNER JOIN on it ? KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
jezemine
Master Smack Fu Yak Hacker
2886 Posts |
Posted - 2008-08-15 : 21:44:18
|
select Table1.Name, Table1.EmployeeID, Table1.Scores, Table2.Scoresfrom Table1 join Table2 on Table1.EmployeeID=Table2.EmployeeID elsasoft.org |
 |
|
|
BankOfficerHere
Posting Yak Master
124 Posts |
Posted - 2008-08-15 : 21:53:34
|
| I tried that alredy but the name Allan is not showing on the results. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2008-08-15 : 22:25:40
|
use LEFT JOIN or FULL OUTER JOIN if you have name in 2nd table but not in 1st table KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
BankOfficerHere
Posting Yak Master
124 Posts |
Posted - 2008-08-15 : 23:29:56
|
| okay it's working i have 2 tables. what if I have 4 tables:Name EmployeeID ScoresMichael 111 5Michelle 222 10Allan 333 6Gilbert 444 8 Name EmployeeID ScoresMichael 111 9Michelle 222 11Gilbert 444 7 Name EmployeeID ScoresMichelle 222 11Gilbert 444 7 Name EmployeeID ScoresMichael 111 5Michelle 222 10Allan 333 6Gilbert 444 8The results should be:Name EmployeeID Scores Scores Scores ScoresMichael 111 5 9 5Michelle 222 10 11 11 10Allan 333 6 6Gilbert 444 8 7 7 8 |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2008-08-15 : 23:39:57
|
you can also use FULL OUTER JOIN for that. Another way is to use UNION ALLselect Name, EmployeeID, sum(Scores1), sum(Scores2), sum(Scores3), sum(Scores4)from(select Name, EmployeeID, Scores as Scores1, NULL as Scores2, NULL as Scores3, NULL as Scores4from Table1union allselect Name, EmployeeID, NULL as Scores1, Scores as Scores2, NULL as Scores3, NULL as Scores4from Table2union allselect Name, EmployeeID, NULL as Scores1, NULL as Scores2, Scores as Scores3, NULL as Scores4from Table3union allselect Name, EmployeeID, NULL as Scores1, NULL as Scores2, NULL as Scores3, Scores as Scores4from Table4) as dgroup by Name, EmployeeID EDIT : missed out table alias KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
BankOfficerHere
Posting Yak Master
124 Posts |
Posted - 2008-08-15 : 23:50:49
|
| I tried this:select Name, Employee, sum(Score1), sum(Score2), sum(Score3), sum(Score4)from(select Name, Employee, Score as Score1, NULL as Score2, NULL as Score3, NULL as Score4from Table1union allselect Name, Employee, NULL as Score1, Score as Score2, NULL as Score3, NULL as Score4from Table2union allselect Name, Employee, NULL as Score1, NULL as Score2, Score as Score3, NULL as Score4from Table3union allselect Name, Employee, NULL as Score1, NULL as Score2, NULL as Score3, Score as Score4from Table4)Group by Name, EmployeeBut...I'm getting this message:Server: Msg 156, Level 15, State 1, Line 16Incorrect syntax near the keyword 'Group'. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2008-08-15 : 23:52:51
|
edited the prev post. Missed out the alias KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
BankOfficerHere
Posting Yak Master
124 Posts |
Posted - 2008-08-15 : 23:59:32
|
| I prefer the Outer Join method but I'm getting different results. I resulted this way:Name EmployeeID Scores Expr1 Expr2 Expr3Michael 111 5 9 NULL NULLMichelle 222 10 11 11 10Allan 333 6 NULL NULL NULLGilbert 444 8 7 7 8NULL NULL NULL NULL NULL 5NULL NULL NULL NULL NULL 6 |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2008-08-16 : 00:26:38
|
[code]DECLARE @table1 TABLE( Name varchar(10), EmployeeID int, Scores int)INSERT INTO @table1SELECT 'Michael', 111, 5 UNION ALLSELECT 'Michelle', 222, 10 UNION ALLSELECT 'Allan', 333, 6 UNION ALLSELECT 'Gilbert', 444, 8DECLARE @table2 TABLE( Name varchar(10), EmployeeID int, Scores int)INSERT INTO @table2SELECT 'Michael', 111, 9 UNION ALLSELECT 'Michelle', 222, 11 UNION ALLSELECT 'Gilbert', 444, 7DECLARE @table3 TABLE( Name varchar(10), EmployeeID int, Scores int)INSERT INTO @table3SELECT 'Michelle', 222, 11 UNION ALLSELECT 'Gilbert', 444, 7DECLARE @table4 TABLE( Name varchar(10), EmployeeID int, Scores int)INSERT INTO @table4SELECT 'Michael', 111, 5 UNION ALLSELECT 'Michelle', 222, 10 UNION ALLSELECT 'Allan', 333, 6 UNION ALLSELECT 'Gilbert', 444, 8SELECT [Name] = coalesce(t1.[Name], t2.[Name], t3.[Name], t4.[Name]), EmployeeID = coalesce(t1.EmployeeID, t2.EmployeeID, t3.EmployeeID, t4.EmployeeID), Score1 = t1.Scores, Score2 = t2.Scores, Score3 = t3.Scores, Score4 = t4.ScoresFROM @table1 t1 FULL OUTER JOIN @table2 t2 ON t1.[Name] = t2.[Name] AND t1.EmployeeID = t2.EmployeeID FULL OUTER JOIN @table3 t3 ON t1.[Name] = t3.[Name] AND t1.EmployeeID = t3.EmployeeID FULL OUTER JOIN @table4 t4 ON t1.[Name] = t4.[Name] AND t1.EmployeeID = t4.EmployeeIDSELECT [Name], EmployeeID, Score1 = SUM(Score1), Score2 = SUM(Score2), Score3 = SUM(Score3), Score4 = SUM(Score4)FROM( SELECT [Name], EmployeeID, Score1 = Scores, Score2 = NULL, Score3 = NULL, Score4 = NULL FROM @table1 UNION ALL SELECT [Name], EmployeeID, Score1 = NULL, Score2 = Scores, Score3 = NULL, Score4 = NULL FROM @table2 UNION ALL SELECT [Name], EmployeeID, Score1 = NULL, Score2 = NULL, Score3 = Scores, Score4 = NULL FROM @table3 UNION ALL SELECT [Name], EmployeeID, Score1 = NULL, Score2 = NULL, Score3 = NULL, Score4 = Scores FROM @table4) dGROUP BY [Name], EmployeeID/*Name EmployeeID Score1 Score2 Score3 Score4 ---------- ----------- ----------- ----------- ----------- ----------- Michael 111 5 9 NULL 5 Michelle 222 10 11 11 10 Allan 333 6 NULL NULL 6 Gilbert 444 8 7 7 8 (4 row(s) affected)*/[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
BankOfficerHere
Posting Yak Master
124 Posts |
Posted - 2008-08-16 : 00:44:12
|
| so it's not possible using ountput join format? |
 |
|
|
BankOfficerHere
Posting Yak Master
124 Posts |
Posted - 2008-08-16 : 00:45:58
|
| I got it..looks like the Union All option is easier. Thanks |
 |
|
|
BankOfficerHere
Posting Yak Master
124 Posts |
Posted - 2008-08-16 : 01:38:37
|
| I tried using the actual figures however, it didn't combine. Here's my statement:select Empname, Datereviewed, sum(Average1), sum(Average2), sum(Average3), sum(Average4)from(select Empname, Datereviewed, Average as Average1, NULL as Average2, NULL as Average3, NULL as Average4from QACompleteAverageWK1union allselect Empname, Datereviewed, NULL as Average1, Average as Average2, NULL as Average3, NULL as Average4from QACompleteAverageWK2union allselect Empname, Datereviewed, NULL as Average1, NULL as Average2, Average as Average3, NULL as Average4from QACompleteAverageWK3union allselect Empname, Datereviewed, NULL as Average1, NULL as Average2, NULL as Average3, Average as Average4from QACompleteAverageWK4) as dgroup by Empname, DatereviewedHere are my tables:QACompleteAverageWK3 datereviewed EmpName Average QACompleteAverageWK2 datereviewed EmpName Average7/12/2008 QA2l 0.72072 QACompleteAverageWK3 datereviewed EmpName Average7/26/2008 QA1 0.4324327/26/2008 QA3 0.432432 QACompleteAverageWK4 datereviewed EmpName Average8/4/2008 19:21 QA1 0.9459458/4/2008 19:21 QA3 0.936936The result I'm getting is:Empname Datereviewed Expr1 Expr2 Expr3 Expr4QA1 7/26/2008 0.432432 QA1 8/4/2008 19:21 0.945945QA2l 7/12/2008 0.72072 QA3 7/26/2008 0.432432 QA3 8/4/2008 19:21 0.936936Where it should be:Empname Datereviewed Expr1 Expr2 Expr3 Expr4QA1 7/26/2008 0.432432 0.945945QA2l 7/12/2008 0.72072 QA3 7/26/2008 0.432432 0.936936 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-08-16 : 02:07:53
|
it seems like what you want is thisselect Empname, MIN(Datereviewed), min(Average1), min(Average2), min(Average3), min(Average4)from(select Empname, Datereviewed, Average as Average1, NULL as Average2, NULL as Average3, NULL as Average4from QACompleteAverageWK1union allselect Empname, Datereviewed, NULL as Average1, Average as Average2, NULL as Average3, NULL as Average4from QACompleteAverageWK2union allselect Empname, Datereviewed, NULL as Average1, NULL as Average2, Average as Average3, NULL as Average4from QACompleteAverageWK3union allselect Empname, Datereviewed, NULL as Average1, NULL as Average2, NULL as Average3, Average as Average4from QACompleteAverageWK4) as dgroup by Empname, Datereviewed |
 |
|
|
BankOfficerHere
Posting Yak Master
124 Posts |
Posted - 2008-08-16 : 02:22:03
|
| I found out the problem, however, is it possible that for NULL results, I want to be zero (0) instead. Because it dreamweaver doesn't accepts NULL. Here's my statement:select [Empname], sum(Average1), sum(Average2), sum(Average3), sum(Average4)from(select [Empname], Average as Average1, NULL as Average2, NULL as Average3, NULL as Average4from QACompleteAverageWK1union allselect [Empname], NULL as Average1, Average as Average2, NULL as Average3, NULL as Average4from QACompleteAverageWK2union allselect [Empname], NULL as Average1, NULL as Average2, Average as Average3, NULL as Average4from QACompleteAverageWK3union allselect [Empname], NULL as Average1, NULL as Average2, NULL as Average3, Average as Average4from QACompleteAverageWK4) as dgroup by [Empname] |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-08-16 : 02:24:37
|
quote: Originally posted by BankOfficerHere I found out the problem, however, is it possible that for NULL results, I want to be zero (0) instead. Because it dreamweaver doesn't accepts NULL. Here's my statement:select [Empname], sum(Average1), sum(Average2), sum(Average3), sum(Average4)from(select [Empname], Average as Average1, 0 as Average2, 0 as Average3, NULL as Average4from QACompleteAverageWK1union allselect [Empname], 0 as Average1, Average as Average2, 0 as Average3, 0 as Average4from QACompleteAverageWK2union allselect [Empname], 0 as Average1, 0 as Average2, Average as Average3, NULL as Average4from QACompleteAverageWK3union allselect [Empname], 0 as Average1, 0 as Average2, 0 as Average3, Average as Average4from QACompleteAverageWK4) as dgroup by [Empname]
put 0's instead of NULLs |
 |
|
|
BankOfficerHere
Posting Yak Master
124 Posts |
Posted - 2008-08-16 : 02:32:35
|
| that shouldn't work but i did figure it out..thanks |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-08-16 : 02:44:52
|
quote: Originally posted by BankOfficerHere that shouldn't work but i did figure it out..thanks
why? did you get any error? |
 |
|
|
BankOfficerHere
Posting Yak Master
124 Posts |
Posted - 2008-08-16 : 11:58:21
|
| yes you just can't change it to zero. you need the statement coalesce |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-08-16 : 12:49:31
|
quote: Originally posted by BankOfficerHere yes you just can't change it to zero. you need the statement coalesce
didnt get thet...you're just hardcoding it. then why use coalesce? |
 |
|
|
Next Page
|
|
|