is it possible to concatenate 2 recordsets ?
i have 2 selects and i want to display all results from the first
select and then the results from the second select query
with paging.
if i use union, all the results will be mixed.
thanks for the help!Post the queries you intend to combine and I'd be willing to take a shot at it for you.
-PatP|||if i use union, all the results will be mixed.not if you use UNION ALL with an ORDER BY clause|||if i use UNION ALL i get duplicated rows
here is one example of a full query i need to be gathered:
0 select * from view_veiculos where ativo='1' and ( nome_marc like
'%aa%' or nome_mod like '%aa%' or estado like '%aa%' or cidade like
'%aa%' or ano like '%aa%' ) and ( nome_marc like '%bb%' or nome_mod
like '%bb%' or estado like '%bb%' or cidade like '%bb%' or ano like
'%bb%' ) and ( nome_marc like '%cc%' or nome_mod like '%cc%' or estado
like '%cc%' or cidade like '%cc%' or ano like '%cc%' ) and ( nome_marc
like '%dd%' or nome_mod like '%dd%' or estado like '%dd%' or cidade
like '%dd%' or ano like '%dd%' ) and ( nome_marc like '%ee%' or
nome_mod like '%ee%' or estado like '%ee%' or cidade like '%ee%' or
ano like '%ee%' ) and ( nome_marc like '%ff%' or nome_mod like '%ff%'
or estado like '%ff%' or cidade like '%ff%' or ano like '%ff%' ) union
all
1 select * from view_veiculos where ativo='1' and ( nome_marc like
'%aa%' or nome_mod like '%aa%' or estado like '%aa%' or cidade like
'%aa%' or ano like '%aa%' ) and ( nome_marc like '%bb%' or nome_mod
like '%bb%' or estado like '%bb%' or cidade like '%bb%' or ano like
'%bb%' ) and ( nome_marc like '%cc%' or nome_mod like '%cc%' or estado
like '%cc%' or cidade like '%cc%' or ano like '%cc%' ) and ( nome_marc
like '%dd%' or nome_mod like '%dd%' or estado like '%dd%' or cidade
like '%dd%' or ano like '%dd%' ) and ( nome_marc like '%ee%' or
nome_mod like '%ee%' or estado like '%ee%' or cidade like '%ee%' or
ano like '%ee%' ) union all
2 select * from view_veiculos where ativo='1' and ( nome_marc like
'%aa%' or nome_mod like '%aa%' or estado like '%aa%' or cidade like
'%aa%' or ano like '%aa%' ) and ( nome_marc like '%bb%' or nome_mod
like '%bb%' or estado like '%bb%' or cidade like '%bb%' or ano like
'%bb%' ) and ( nome_marc like '%cc%' or nome_mod like '%cc%' or estado
like '%cc%' or cidade like '%cc%' or ano like '%cc%' ) and ( nome_marc
like '%dd%' or nome_mod like '%dd%' or estado like '%dd%' or cidade
like '%dd%' or ano like '%dd%' ) union all
3 select * from view_veiculos where ativo='1' and ( nome_marc like
'%aa%' or nome_mod like '%aa%' or estado like '%aa%' or cidade like
'%aa%' or ano like '%aa%' ) and ( nome_marc like '%bb%' or nome_mod
like '%bb%' or estado like '%bb%' or cidade like '%bb%' or ano like
'%bb%' ) and ( nome_marc like '%cc%' or nome_mod like '%cc%' or estado
like '%cc%' or cidade like '%cc%' or ano like '%cc%' ) union all
4 select * from view_veiculos where ativo='1' and ( nome_marc like
'%aa%' or nome_mod like '%aa%' or estado like '%aa%' or cidade like
'%aa%' or ano like '%aa%' ) and ( nome_marc like '%bb%' or nome_mod
like '%bb%' or estado like '%bb%' or cidade like '%bb%' or ano like
'%bb%' ) union all
5 select * from view_veiculos where ativo='1' and ( nome_marc like
'%aa%' or nome_mod like '%aa%' or estado like '%aa%' or cidade like
'%aa%' or ano like '%aa%' ) union all
6 select * from view_veiculos where ativo='1' and ( nome_marc like
'%bb%' or nome_mod like '%bb%' or estado like '%bb%' or cidade like
'%bb%' or ano like '%bb%' ) and ( nome_marc like '%cc%' or nome_mod
like '%cc%' or estado like '%cc%' or cidade like '%cc%' or ano like
'%cc%' ) and ( nome_marc like '%dd%' or nome_mod like '%dd%' or estado
like '%dd%' or cidade like '%dd%' or ano like '%dd%' ) and ( nome_marc
like '%ee%' or nome_mod like '%ee%' or estado like '%ee%' or cidade
like '%ee%' or ano like '%ee%' ) and ( nome_marc like '%ff%' or
nome_mod like '%ff%' or estado like '%ff%' or cidade like '%ff%' or
ano like '%ff%' ) union all
7 select * from view_veiculos where ativo='1' and ( nome_marc like
'%bb%' or nome_mod like '%bb%' or estado like '%bb%' or cidade like
'%bb%' or ano like '%bb%' ) and ( nome_marc like '%cc%' or nome_mod
like '%cc%' or estado like '%cc%' or cidade like '%cc%' or ano like
'%cc%' ) and ( nome_marc like '%dd%' or nome_mod like '%dd%' or estado
like '%dd%' or cidade like '%dd%' or ano like '%dd%' ) and ( nome_marc
like '%ee%' or nome_mod like '%ee%' or estado like '%ee%' or cidade
like '%ee%' or ano like '%ee%' ) union all
8 select * from view_veiculos where ativo='1' and ( nome_marc like
'%bb%' or nome_mod like '%bb%' or estado like '%bb%' or cidade like
'%bb%' or ano like '%bb%' ) and ( nome_marc like '%cc%' or nome_mod
like '%cc%' or estado like '%cc%' or cidade like '%cc%' or ano like
'%cc%' ) and ( nome_marc like '%dd%' or nome_mod like '%dd%' or estado
like '%dd%' or cidade like '%dd%' or ano like '%dd%' ) union all
9 select * from view_veiculos where ativo='1' and ( nome_marc like
'%bb%' or nome_mod like '%bb%' or estado like '%bb%' or cidade like
'%bb%' or ano like '%bb%' ) and ( nome_marc like '%cc%' or nome_mod
like '%cc%' or estado like '%cc%' or cidade like '%cc%' or ano like
'%cc%' ) union all
10 select * from view_veiculos where ativo='1' and ( nome_marc like
'%bb%' or nome_mod like '%bb%' or estado like '%bb%' or cidade like
'%bb%' or ano like '%bb%' ) union all
11 select * from view_veiculos where ativo='1' and ( nome_marc like
'%cc%' or nome_mod like '%cc%' or estado like '%cc%' or cidade like
'%cc%' or ano like '%cc%' ) and ( nome_marc like '%dd%' or nome_mod
like '%dd%' or estado like '%dd%' or cidade like '%dd%' or ano like
'%dd%' ) and ( nome_marc like '%ee%' or nome_mod like '%ee%' or estado
like '%ee%' or cidade like '%ee%' or ano like '%ee%' ) and ( nome_marc
like '%ff%' or nome_mod like '%ff%' or estado like '%ff%' or cidade
like '%ff%' or ano like '%ff%' ) union all
12 select * from view_veiculos where ativo='1' and ( nome_marc like
'%cc%' or nome_mod like '%cc%' or estado like '%cc%' or cidade like
'%cc%' or ano like '%cc%' ) and ( nome_marc like '%dd%' or nome_mod
like '%dd%' or estado like '%dd%' or cidade like '%dd%' or ano like
'%dd%' ) and ( nome_marc like '%ee%' or nome_mod like '%ee%' or estado
like '%ee%' or cidade like '%ee%' or ano like '%ee%' ) union all
13 select * from view_veiculos where ativo='1' and ( nome_marc like
'%cc%' or nome_mod like '%cc%' or estado like '%cc%' or cidade like
'%cc%' or ano like '%cc%' ) and ( nome_marc like '%dd%' or nome_mod
like '%dd%' or estado like '%dd%' or cidade like '%dd%' or ano like
'%dd%' ) union all
14 select * from view_veiculos where ativo='1' and ( nome_marc like
'%cc%' or nome_mod like '%cc%' or estado like '%cc%' or cidade like
'%cc%' or ano like '%cc%' ) union all
15 select * from view_veiculos where ativo='1' and ( nome_marc like
'%dd%' or nome_mod like '%dd%' or estado like '%dd%' or cidade like
'%dd%' or ano like '%dd%' ) and ( nome_marc like '%ee%' or nome_mod
like '%ee%' or estado like '%ee%' or cidade like '%ee%' or ano like
'%ee%' ) and ( nome_marc like '%ff%' or nome_mod like '%ff%' or estado
like '%ff%' or cidade like '%ff%' or ano like '%ff%' ) union all
16 select * from view_veiculos where ativo='1' and ( nome_marc like
'%dd%' or nome_mod like '%dd%' or estado like '%dd%' or cidade like
'%dd%' or ano like '%dd%' ) and ( nome_marc like '%ee%' or nome_mod
like '%ee%' or estado like '%ee%' or cidade like '%ee%' or ano like
'%ee%' ) union all
17 select * from view_veiculos where ativo='1' and ( nome_marc like
'%dd%' or nome_mod like '%dd%' or estado like '%dd%' or cidade like
'%dd%' or ano like '%dd%' ) union all
18 select * from view_veiculos where ativo='1' and ( nome_marc like
'%ee%' or nome_mod like '%ee%' or estado like '%ee%' or cidade like
'%ee%' or ano like '%ee%' ) and ( nome_marc like '%ff%' or nome_mod
like '%ff%' or estado like '%ff%' or cidade like '%ff%' or ano like
'%ff%' ) union all
19 select * from view_veiculos where ativo='1' and ( nome_marc like
'%ee%' or nome_mod like '%ee%' or estado like '%ee%' or cidade like
'%ee%' or ano like '%ee%' ) union all
- Hide quoted text -
20 select * from view_veiculos where ativo='1' and ( nome_marc like
'%ff%' or nome_mod like '%ff%' or estado like '%ff%' or cidade like
'%ff%' or ano like '%ff%' )
these numbers are here just to show us how many select statements it has
id like the final result to be ordered w all results from the first query,
then all results from second query and so on
when i use union on it, all results are mixed|||you have 20 select statements that are all doing the same thing!!
i have a feeling they should be just one query, and the WHERE clauses combined with ORs
no wonder UNION ALL returns dupes -- there are lots of dupes, and a given row might satisfy 10 of your queries
however, you forgot to add a row discriminator and an ORDER BY clause
here is an example --select 1 as row_type
, foo
, bar
from table1
union all
select 2
, foo
, bar
from table2
order
by row_type
, foonotice that all the rows from the first select will have 1s in the first column of results, while all the rows from the second select will have 2s in the first column of results, so it is then not possible for a row from the first select to duplicate a row from the second select
this is the true concatenation of result sets
:)|||it gave me duplicated rows.
but thanks for helping
i think i must use one temporary table or a table variable|||In order to avoid many different problems with your original approach, I would suggest using:SELECT *
FROM (SELECT *
, CASE
WHEN 0 < CharIndex('aa', nome_marc + nom_mod + estado + cidade + ano)
AND 0 < CharIndex('bb', nome_marc + nom_mod + estado + cidade + ano)
AND 0 < CharIndex('cc', nome_marc + nom_mod + estado + cidade + ano)
AND 0 < CharIndex('dd', nome_marc + nom_mod + estado + cidade + ano)
AND 0 < CharIndex('ee', nome_marc + nom_mod + estado + cidade + ano)
THEN 1
WHEN 0 < CharIndex('aa', nome_marc + nom_mod + estado + cidade + ano)
AND 0 < CharIndex('bb', nome_marc + nom_mod + estado + cidade + ano)
AND 0 < CharIndex('cc', nome_marc + nom_mod + estado + cidade + ano)
AND 0 < CharIndex('dd', nome_marc + nom_mod + estado + cidade + ano)
THEN 2
WHEN 0 < CharIndex('aa', nome_marc + nom_mod + estado + cidade + ano)
AND 0 < CharIndex('bb', nome_marc + nom_mod + estado + cidade + ano)
AND 0 < CharIndex('cc', nome_marc + nom_mod + estado + cidade + ano)
THEN 3
WHEN 0 < CharIndex('aa', nome_marc + nom_mod + estado + cidade + ano)
AND 0 < CharIndex('bb', nome_marc + nom_mod + estado + cidade + ano)
THEN 4
WHEN 0 < CharIndex('aa', nome_marc + nom_mod + estado + cidade + ano)
THEN 5
WHEN 0 < CharIndex('bb', nome_marc + nom_mod + estado + cidade + ano)
AND 0 < CharIndex('cc', nome_marc + nom_mod + estado + cidade + ano)
AND 0 < CharIndex('dd', nome_marc + nom_mod + estado + cidade + ano)
AND 0 < CharIndex('ee', nome_marc + nom_mod + estado + cidade + ano)
AND 0 < CharIndex('ff', nome_marc + nom_mod + estado + cidade + ano)
THEN 6
WHEN 0 < CharIndex('bb', nome_marc + nom_mod + estado + cidade + ano)
AND 0 < CharIndex('cc', nome_marc + nom_mod + estado + cidade + ano)
AND 0 < CharIndex('dd', nome_marc + nom_mod + estado + cidade + ano)
AND 0 < CharIndex('ee', nome_marc + nom_mod + estado + cidade + ano)
THEN 7
WHEN 0 < CharIndex('bb', nome_marc + nom_mod + estado + cidade + ano)
AND 0 < CharIndex('cc', nome_marc + nom_mod + estado + cidade + ano)
AND 0 < CharIndex('dd', nome_marc + nom_mod + estado + cidade + ano)
THEN 8
WHEN 0 < CharIndex('bb', nome_marc + nom_mod + estado + cidade + ano)
AND 0 < CharIndex('cc', nome_marc + nom_mod + estado + cidade + ano)
THEN 9
WHEN 0 < CharIndex('bb', nome_marc + nom_mod + estado + cidade + ano)
THEN 10
WHEN 0 < CharIndex('cc', nome_marc + nom_mod + estado + cidade + ano)
AND 0 < CharIndex('dd', nome_marc + nom_mod + estado + cidade + ano)
AND 0 < CharIndex('ee', nome_marc + nom_mod + estado + cidade + ano)
AND 0 < CharIndex('ff', nome_marc + nom_mod + estado + cidade + ano)
THEN 11
WHEN 0 < CharIndex('cc', nome_marc + nom_mod + estado + cidade + ano)
AND 0 < CharIndex('dd', nome_marc + nom_mod + estado + cidade + ano)
AND 0 < CharIndex('ee', nome_marc + nom_mod + estado + cidade + ano)
AND 0 < CharIndex('ff', nome_marc + nom_mod + estado + cidade + ano)
THEN 11
WHEN 0 < CharIndex('cc', nome_marc + nom_mod + estado + cidade + ano)
AND 0 < CharIndex('dd', nome_marc + nom_mod + estado + cidade + ano)
AND 0 < CharIndex('ee', nome_marc + nom_mod + estado + cidade + ano)
THEN 12
WHEN 0 < CharIndex('cc', nome_marc + nom_mod + estado + cidade + ano)
AND 0 < CharIndex('dd', nome_marc + nom_mod + estado + cidade + ano)
THEN 13
WHEN 0 < CharIndex('cc', nome_marc + nom_mod + estado + cidade + ano)
THEN 14
WHEN 0 < CharIndex('dd', nome_marc + nom_mod + estado + cidade + ano)
AND 0 < CharIndex('ee', nome_marc + nom_mod + estado + cidade + ano)
AND 0 < CharIndex('ff', nome_marc + nom_mod + estado + cidade + ano)
THEN 15
WHEN 0 < CharIndex('dd', nome_marc + nom_mod + estado + cidade + ano)
AND 0 < CharIndex('ee', nome_marc + nom_mod + estado + cidade + ano)
THEN 16
WHEN 0 < CharIndex('dd', nome_marc + nom_mod + estado + cidade + ano)
THEN 17
WHEN 0 < CharIndex('ee', nome_marc + nom_mod + estado + cidade + ano)
AND 0 < CharIndex('ff', nome_marc + nom_mod + estado + cidade + ano)
THEN 18
WHEN 0 < CharIndex('ee', nome_marc + nom_mod + estado + cidade + ano)
THEN 19
WHEN 0 < CharIndex('ff', nome_marc + nom_mod + estado + cidade + ano)
THEN 20
ELSE 0
END AS DBForums) AS a
WHERE 0 <> DBForums
ORDER BY DBForums
-PatP|||to avoid duplicate rows use just UNION instead of UNION ALL.
Friday, March 9, 2012
how to concatenate 2 recordsets
Labels:
concatenate,
database,
display,
firstselect,
microsoft,
mysql,
oracle,
recordsets,
selects,
server,
sql
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment