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 |
|
CLages
Posting Yak Master
116 Posts |
Posted - 2005-03-15 : 17:56:28
|
| hi, its me again with another troublei have this simple select belowonce i put top 20 in each select and once when i use unioni can not use (order by for in each select) I MUST USE "order by only in the end of script) my Select works wrong because when i use "order by cli_uf , ParticUF DESC " SELECT top 20 brings me up 20 record for each Group by (in this case)UF (state in english) something like this ( wrong)UF bla bla bla %ParticUFBA --- --- --- 12 BA ---- --- --- 08 BA ---- --- --- 06 RJ ---- --- --- 05 RJ ---- --- --- 04 RJ ---- --- --- 02 the right one would beUF bla bla bla %ParticUFBA --- --- --- 12 BA ---- --- --- 08 BA ---- --- --- 06 RJ ---- --- --- 33 RJ ---- --- --- 27 RJ ---- --- --- 12 I want to get the 20 rows of each Group from the Biggest to smallest using %ParticUFif i dont use TOP 20 works fine but i get too many rows tks C. LagesSelect top 20 cli_uf, cli_codigo, cli_fantasia, sum(mov_qt) as Qt, sum(mov_mercadoria ) as Valor, VltotalUF =( select sum(mov_mercadoria ) from moviment , natureza , clientes where mov_codfis = nat_codigo and mov_cliente = cli_codigo and mov_flag = 2 and nat_grupo = 'A' and cli_uf = 'BA') , particUF =( sum(mov_mercadoria) / (select sum(mov_mercadoria ) from moviment , natureza , clientes where mov_codfis = nat_codigo and mov_cliente = cli_codigo and mov_flag = 2 and nat_grupo = 'A' and cli_uf = 'BA') ) * 100 from moviment, clientes, naturezawhere (mov_cliente = cli_codigo )and (mov_codfis = nat_codigo)and (mov_flag = 2)and (nat_grupo = 'A')and (cli_uf = 'BA')group bycli_uf, cli_codigo, cli_fantasia union allselect top 20 cli_uf, cli_codigo, cli_fantasia, sum(mov_qt) as Qt, sum(mov_mercadoria ) as Valor, VltotalUF =( select sum(mov_mercadoria ) from moviment , natureza , clientes where mov_codfis = nat_codigo and mov_cliente = cli_codigo and mov_flag = 2 and nat_grupo = 'A' and cli_uf = 'RJ') , particUF =( sum(mov_mercadoria ) / (select sum(mov_mercadoria ) from moviment , natureza , clientes where mov_codfis = nat_codigo and mov_cliente = cli_codigo and mov_flag = 2 and nat_grupo = 'A' and cli_uf = 'RJ' ) ) * 100 from moviment, clientes, naturezawhere (mov_cliente = cli_codigo )and (mov_codfis = nat_codigo)and (mov_flag = 2)and (nat_grupo = 'A')and (cli_uf = 'RJ')group bycli_uf, cli_codigo, cli_fantasia order by cli_uf , ParticUF DESC GO |
|
|
gpl
Posting Yak Master
195 Posts |
Posted - 2005-03-15 : 19:41:48
|
How about this, it puts your unioned selects into a derived table, then sorts and tops off the 20 wanted rows (I havent tried this as I dont have the table structures, but in theory it would work)GrahamSelect top 20 cli_uf,cli_codigo,cli_fantasia, Qt,Valor,VltotalUFfrom(Select cli_uf,cli_codigo,cli_fantasia, sum(mov_qt) as Qt,sum(mov_mercadoria ) as Valor,VltotalUF =( select sum(mov_mercadoria ) from moviment , natureza , clienteswhere mov_codfis = nat_codigoand mov_cliente = cli_codigoand mov_flag = 2and nat_grupo = 'A'and cli_uf = 'BA') ,particUF =( sum(mov_mercadoria) /(select sum(mov_mercadoria ) from moviment , natureza , clienteswhere mov_codfis = nat_codigoand mov_cliente = cli_codigoand mov_flag = 2and nat_grupo = 'A'and cli_uf = 'BA') ) * 100from moviment, clientes, naturezawhere (mov_cliente = cli_codigo )and (mov_codfis = nat_codigo)and (mov_flag = 2)and (nat_grupo = 'A')and (cli_uf = 'BA')group bycli_uf, cli_codigo, cli_fantasiaunion allselect cli_uf,cli_codigo,cli_fantasia,sum(mov_qt) as Qt,sum(mov_mercadoria ) as Valor,VltotalUF =( select sum(mov_mercadoria ) from moviment , natureza , clienteswhere mov_codfis = nat_codigoand mov_cliente = cli_codigoand mov_flag = 2and nat_grupo = 'A'and cli_uf = 'RJ') ,particUF =( sum(mov_mercadoria ) /(select sum(mov_mercadoria ) from moviment , natureza , clienteswhere mov_codfis = nat_codigoand mov_cliente = cli_codigoand mov_flag = 2and nat_grupo = 'A'and cli_uf = 'RJ' ) ) * 100from moviment, clientes, naturezawhere (mov_cliente = cli_codigo )and (mov_codfis = nat_codigo)and (mov_flag = 2)and (nat_grupo = 'A')and (cli_uf = 'RJ')group bycli_uf, cli_codigo, cli_fantasia) As Derivedorder by cli_uf , ParticUF DESC |
 |
|
|
|
|
|
|
|