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 |
|
trallador
Starting Member
13 Posts |
Posted - 2009-07-24 : 03:54:56
|
I have a problem with a 2 sum colums in different tables:the two separate querys:SELECT articulo, definicion, SUM(unidades) AS 'u_comp', AVG(precio) AS 'avg_precio' FROM d_albcom GROUP BY articuloSELECT articulo, definicion, SUM(unidades) AS 'u_vent', AVG(precio) AS 'avg_precio' FROM d_albven GROUP BY articulo If i use UNION works, but i need the results in colums not in rows :&and i tried something like this:SELECT article, definicio, SUM(u_comp), SUM(u_vent), AVG(avg_preu_comp), AVG(avg_preu_vent)FROM ( SELECT articulo AS 'article', definicion AS 'definicio', unidades AS 'u_comp', precio AS 'avg_preu_comp' FROM d_albcom UNION ALL SELECT articulo AS 'article', definicion AS 'definicio', unidades AS 'u_vent', precio AS 'avg_preu_vent' FROM d_albven)GROUP BY articleORDER BY article not works :(Any can help me? Thanks for all. |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-07-24 : 04:15:14
|
Try thisselect articulo, definicion, sum(case when sno=1 then unidades else 0 end) as u_comp, sum(case when sno=2 then unidades else 0 end) as u_vent, avg(case when sno=1 then precio else 0 end) as avg_comp, avg(case when sno=2 then precio else 0 end) as avg_compfrom(SELECT 1 as sno,articulo, definicion, (unidades) AS 'u_comp', (precio) AS 'avg_precio' FROM d_albcom GROUP BY articulounion allSELECT 2 , articulo, definicion, (unidades) AS 'u_vent', (precio) AS 'avg_precio' FROM d_albven GROUP BY articulo) as tgroup by articulo, definicion MadhivananFailing to plan is Planning to fail |
 |
|
|
trallador
Starting Member
13 Posts |
Posted - 2009-07-24 : 04:40:55
|
Thx madhivanan for fast response but not work :(unfortunately got the same result with this:select articulo, definicion, sum(case when sno=1 then unidades else 0 end) as u_comp, sum(case when sno=2 then unidades else 0 end) as u_vent, avg(case when sno=1 then precio else 0 end) as avg_precio_comp, avg(case when sno=2 then precio else 0 end) as avg_precio_ventfrom((SELECT 1 as sno, articulo, definicion, (unidades) AS 'u_comp', (precio) AS 'avg_precio_comp' FROM d_albcom GROUP BY articulo)union all(SELECT 2 as sno, articulo, definicion, (unidades) AS 'u_vent', (precio) AS 'avg_precio_vent' FROM d_albven GROUP BY articulo) )group by articulo, definicion |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-07-24 : 05:57:43
|
Wrong use of column namesselect articulo, definicion, sum(case when sno=1 then unidades else 0 end) as u_comp, sum(case when sno=2 then unidades else 0 end) as u_vent, avg(case when sno=1 then precio else 0 end) as avg_precio_comp, avg(case when sno=2 then precio else 0 end) as avg_precio_ventfrom((SELECT 1 as sno, articulo, definicion, unidades , precio FROM d_albcom GROUP BY articulo)union all(SELECT 2 as sno, articulo, definicion, unidades, precio FROM d_albven GROUP BY articulo) )group by articulo, definicion MadhivananFailing to plan is Planning to fail |
 |
|
|
trallador
Starting Member
13 Posts |
Posted - 2009-07-24 : 06:11:52
|
| This produce the same error: Incorrect syntax near 'group' (last group by) :& |
 |
|
|
ayamas
Aged Yak Warrior
552 Posts |
Posted - 2009-07-24 : 06:17:27
|
quote: Originally posted by madhivanan Wrong use of column namesselect articulo, definicion, sum(case when sno=1 then unidades else 0 end) as u_comp, sum(case when sno=2 then unidades else 0 end) as u_vent, avg(case when sno=1 then precio else 0 end) as avg_precio_comp, avg(case when sno=2 then precio else 0 end) as avg_precio_ventfrom((SELECT 1 as sno, articulo, definicion, unidades , precio FROM d_albcom GROUP BY articulo)union all(SELECT 2 as sno, articulo, definicion, unidades, precio FROM d_albven GROUP BY articulo) )as tgroup by articulo, definicion MadhivananFailing to plan is Planning to fail
|
 |
|
|
trallador
Starting Member
13 Posts |
Posted - 2009-07-24 : 06:52:28
|
now works :Dselect articulo, definicion, sum(case when sno=1 then unidades else 0 end) as u_comp, sum(case when sno=2 then unidades else 0 end) as u_vent, avg(case when sno=1 then (precio) else NULL end) as avg_precio_comp, avg(case when sno=2 then (precio) else NULL end) as avg_precio_ventfrom((SELECT 1 as sno, articulo, definicion, unidades , precio FROM d_albcom )union all(SELECT 2 as sno, articulo, definicion, unidades, precio FROM d_albven ) )as tgroup by articulo, definicionorder by articulo I use the NULL because AVG uses the 0 to calculate the average, a more efficient option?Big thanks to both for the help :) |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-07-24 : 06:53:17
|
quote: Originally posted by ayamas
quote: Originally posted by madhivanan Wrong use of column namesselect articulo, definicion, sum(case when sno=1 then unidades else 0 end) as u_comp, sum(case when sno=2 then unidades else 0 end) as u_vent, avg(case when sno=1 then precio else 0 end) as avg_precio_comp, avg(case when sno=2 then precio else 0 end) as avg_precio_ventfrom((SELECT 1 as sno, articulo, definicion, unidades , precio FROM d_albcom GROUP BY articulo)union all(SELECT 2 as sno, articulo, definicion, unidades, precio FROM d_albven GROUP BY articulo) )as tgroup by articulo, definicion MadhivananFailing to plan is Planning to fail
Thanks. I just copied from OP's query where alias was missingMadhivananFailing to plan is Planning to fail |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-07-24 : 06:54:19
|
quote: Originally posted by trallador now works :Dselect articulo, definicion, sum(case when sno=1 then unidades else 0 end) as u_comp, sum(case when sno=2 then unidades else 0 end) as u_vent, avg(case when sno=1 then (precio) else NULL end) as avg_precio_comp, avg(case when sno=2 then (precio) else NULL end) as avg_precio_ventfrom((SELECT 1 as sno, articulo, definicion, unidades , precio FROM d_albcom )union all(SELECT 2 as sno, articulo, definicion, unidades, precio FROM d_albven ) )as tgroup by articulo, definicionorder by articulo I use the NULL because AVG uses the 0 to calculate the average, a more efficient option?Big thanks to both for the help :)
Yes. For count/Avg better to use NULLMadhivananFailing to plan is Planning to fail |
 |
|
|
ayamas
Aged Yak Warrior
552 Posts |
Posted - 2009-07-24 : 07:23:56
|
quote: Originally posted by madhivanan
quote: Originally posted by ayamas
quote: Originally posted by madhivanan Wrong use of column namesselect articulo, definicion, sum(case when sno=1 then unidades else 0 end) as u_comp, sum(case when sno=2 then unidades else 0 end) as u_vent, avg(case when sno=1 then precio else 0 end) as avg_precio_comp, avg(case when sno=2 then precio else 0 end) as avg_precio_ventfrom((SELECT 1 as sno, articulo, definicion, unidades , precio FROM d_albcom GROUP BY articulo)union all(SELECT 2 as sno, articulo, definicion, unidades, precio FROM d_albven GROUP BY articulo) )as tgroup by articulo, definicion MadhivananFailing to plan is Planning to fail
Thanks. I just copied from OP's query where alias was missingMadhivananFailing to plan is Planning to fail
You are welcome |
 |
|
|
|
|
|
|
|