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.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 UNION

Author  Topic 

titan_ae78
Starting Member

9 Posts

Posted - 2009-07-02 : 11:40:55
Hi, I've this query in access..

SELECT IDInfo_PianoDeiConti as IDConto, Descrizione,
(CASE WHEN Patrimoniale=1 THEN 'Conto Patrimoniale' ELSE 'Conto Economico' END) as TipoConto,
0 as Tipologia
FROM PianoDeiConti
WHERE Livello = 3 and Descrizione like '1%' AND IDInfo_PianoDeiConti_Nome = 2
order by Patrimoniale
UNION
ALL SELECT IDInfo_Clienti_Fornitori_Agenti as IDConto, Cliente as Descrizione,
(CASE WHEN Tipo='C' THEN 'Cliente' WHEN Tipo='F' THEN 'Fornitore' ELSE 'Agente' END) as Tipoconto,
1 as Tipologia FROM Clienti_Fornitori_Agenti
WHERE Cliente like '1%' order by Descrizione

why it doesn't work in SQL SERVER?

Thanks

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-07-02 : 11:42:52
remove the first ORDER BY before the UNION


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

titan_ae78
Starting Member

9 Posts

Posted - 2009-07-02 : 11:44:43
Thanks..
how does work ordering for UNION? I should set only ORDER clausole for second query?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-07-02 : 13:04:22
yup after secong select query or wrap the union inside a derived table and order outside


SELECT *
FROM
(
SELECT IDInfo_PianoDeiConti as IDConto, Descrizione,
(CASE WHEN Patrimoniale=1 THEN 'Conto Patrimoniale' ELSE 'Conto Economico' END) as TipoConto,
0 as Tipologia
FROM PianoDeiConti
WHERE Livello = 3 and Descrizione like '1%' AND IDInfo_PianoDeiConti_Nome = 2

UNION
ALL SELECT IDInfo_Clienti_Fornitori_Agenti as IDConto, Cliente as Descrizione,
(CASE WHEN Tipo='C' THEN 'Cliente' WHEN Tipo='F' THEN 'Fornitore' ELSE 'Agente' END) as Tipoconto,
1 as Tipologia FROM Clienti_Fornitori_Agenti
WHERE Cliente like '1%'
)t
order by case when Tipologia=0 then Patrimoniale else Descrizione end

Go to Top of Page
   

- Advertisement -