SQL query which select the record from three tables and there is no relation between these tables. Actually I want to make it a VIEW.
suppose there are three tales Table1, Table2, Table3
I want to show records of Table1 first with some filter criteria
and then the records from Table2
and in last from Table3 as when we execute the view it show like the records like a Table.
There can be any number of rows but the records must be in this sequence.
I would suggest using UNION ALL instead of union if you want all the records from each of the tables. UNION will use a distinct to filter out duplicates. If you don't need tht it is just slowing down the query.
A further explanation here:
http://wiki.lessthandot.com/index.php/Union_All
To show you how to handle when you don't have all the columns in each table:
select
1 as seq,col1, col2, col3, cast(null as varchar (40)) as col4
FROM Table1
where ...
UNION ALL
select
2 as seq,'Unknown', col2, null, col4
FROM Table2
where ...
UNION ALL
select
3 as seq ,col1, col2, col3, cast(null as varchar (40)) as col4
FROM Table3
where ...
ORDER BY seq
try:
select
1,col1, col2, col3
FROM Table1
where ...
UNION ALL
select
2,col1, col2, col3
FROM Table2
where ...
UNION ALL
select
3,col1, col2, col3
FROM Table3
where ...
ORDER BY 1
please note that each of the three queries needs to have the same number of columns and that the data types should be consistent also. Also, I used UNION ALL to speed up the query, since there is no use eliminating duplicates between the three queries because the sequence table will guarantee no dups.
to not have the sequence column in the result set try:
SELECT
col1,col2,col3
FROM (select
1 as seq,col1, col2, col3
FROM Table1
where ...
UNION ALL
select
2 as seq,col1, col2, col3
FROM Table2
where ...
UNION ALL
select
3 as seq,col1, col2, col3
FROM Table3
where ...
) dt
ORDER BY seq
you can use a UNION query:
SELECT Field1, Field2, Field3, '1' as Sequence FROM Table1 WHERE SomeCriteria
UNION
SELECT Field7, Field5, Field6, '2' FROM Table2 WHERE SomeCriteria
UNION
SELECT Field4, Field8, Field9, '3' FROM Table3 WHERE SomeCriteria
How about:
create view AZ_VIEW as
select 1 as orderby, tbl1Col1 as col1, tbl1Col2 as col2, tbl1col3 as col3 from Table1 where criteria1='val'
union
select 2, tbl2Col1, tbl2Col2, tbl2col3 from Table2 where criteria2='anotherval'
union
select 3, tbl3Col1, tbl3Col2, tbl3col3 from Table3 where criteria3='athirdval'
;
If your tables share the same columns, you can use Union All:
Select col1, col2, 1 As seq
From table1
Union All
Select col1, col2, 2 As seq
From table1
Union All
Select col1, col2, 3 As seq
From table1
Order By seq
You can UNION the three tables, taking care to ensure that they all return the same number of fields. There is a simple cheat to control the order (seen below):
SELECT * FROM
(
SELECT a, b, c, 1 as ListOrder FROM table1
UNION
SELECT a, b, c, 2 as ListOrder FROM table2
UNION
SELECT a, b, c, 3 as ListOrder FROM table3
)
ORDER BY ListOrder
You can do something like this - WHERE ID = 34 is just a sample filter:
create view vAllRecords as
select 1 as Rank, Field1, Field2 from Table1 where ID = 34
UNION
select 2 as Rank, Field1, Field2 from Table2
UNION
select 3 as Rank, Field1, Field2 from Table3
The use of UNION will remove any duplicates. If you know there will be no duplicates, or you want to see them, the query will run faster with UNION ALL instead.
ORDER BY is not allowed in views, so you'll need to order by Rank when you select from the view:
select *
from vAllRecords
order by Rank
Related
I have a table similar to this
Can I get help writing up a query which will join col1, col2 & col3 and give me a result as below
I've spent an hour trying to figure it out with my mediocre skills and have got to some point.
select col1, count(*)
from tableName
group by col1
But I can't figure out how to join all three cols.
try this one
select
col,
count(*)
from
(select
id,
col1 as col
from
<table_name>
union all
select
id,
col2
from
<table_name>
union all
select
id,
col3
from
<table_name>)
group by
col
You need to group by col of the union of the 3 columns:
select t.col, count(*)
from (
select col1 col from tablename
union all
select col2 from tablename
union all
select col3 from tablename
) t
group by t.col
You should use UNION to group values from all columns to one column. After that, you can count values
SELECT
col,
count(*) as cnt
FROM
(SELECT col1 as col FROM table1
UNION ALL
SELECT col2 as col FROM table1
UNION ALL
SELECT col2 as col FROM table1) as t
GROUP BY col
I am currently using UNION on two queries (see psuedo-code below):
query1
UNION
query2
I want to add an additional column to my results that says the source of the data. The new column called "Source" would return one of the following: "1", "2", or "both".
Being able to handle "both" is very important because query1 and query2 will have similar results and many overlapping records. If anyone could help point me in the right direction, especially with how to handle the "both" case, that would be greatly appreciated!
Sample:
If query1 has a row "Apple,Yellow,Bob" and query2 has the same row, then the result I'm hoping for is:
"Apple,Yellow,Bob,Both"
The individual queries themselves will not have duplicates, but there may be the same row both in query1 and query2 (as seen above).
you can make use of an additional column col4 like this
select col1,col2,col3,sum(col4)
from(
Select col1, col2, col3, 1 as col4 from table1
UNION
Select col1,col2,col3, 2 as col4 from table4
)
group by col1,col2,col3
The records with col4=1 only exist in table1.
The records with col4=2 only exist in table2.
The records with col4=3 exist in both table1+table
add a Source field to both query 1 and query 2:
select 1 as source, ...
from table1
union
select 2 as source, ...
from table2
Here's one way
WITH T
AS (SELECT '1' AS Source,
Col1,
Col2,
Col3
FROM table1
UNION ALL
SELECT '2' AS Source,
Col1,
Col2,
Col3
FROM table2)
SELECT CASE
WHEN MAX(Source) = MIN(Source) THEN Source
ELSE 'Both'
END AS Source,
Col1,
Col2,
Col3
FROM T
GROUP BY Col1,
Col2,
Col3
One more approach
SELECT col1
,col2
,source = CASE
WHEN count(DISTINCT source) > 1
THEN 'Both'
ELSE max(source)
END
FROM (
SELECT col1 ,col2, source = 'source1'
FROM source1
UNION ALL
SELECT col1, col2, source = 'source2'
FROM source2
) u
GROUP BY col1, col2
You can try this
SELECT
a.col1 , a.col2,
CASE WHEN MAX(a.Source) <> MIN(a.Source)
THEN 'BOTH'
ELSE MAX(a.Source) END
FROM
(
SELECT
col1, col2 ,'Source2' AS Source
FROM Table1
UNION ALL
SELECT
col1, col2 ,'Source1' AS Source
FROM Table2
) a
GROUP BY
a.col1 , a.col2
Link to the Sample
I have the following sql at the begnging.
select col1, col2 from table1
union
select col1, col2 from table2
Now I want to able to do a count the total number of rows in the union set from above, and order by col2. How should I do this?
with a as (
select col1, col2 from table1
union
select col1, col2 from table2
)
select *,count(1) over()
from a
order by col2
;
I have 12 tables in SQL Server with the exact same columns that I would like to combine into one brand new table. I don't want any data/rows deleted.
Thanks
Use union all:
insert into NewTable(col1, col2)
select col1, col2
from(
select col1, col2 from Table1
union all
select col1, col2 from Table2
union all
select col1, col2 from Table3
.....
)t
You can create new table while selecting like:
select col1, col2
into NewTable
from(
select col1, col2 from Table1
union all
select col1, col2 from Table2
union all
select col1, col2 from Table3
.....
)t
I am reworking some tables from a screwed up database. A few of the tables had the same data with different table names, and each one of them also had similar data but different column names. Anyway, this is a weird request but this has to be down like this.
I need to pivot rows up to simulate one row so I can create one record from two different tables.
I have attached a photo. The table on the left will pull a single row and the table on the left will supply 1 - n rows based on the id from the left table. I need to pivot the rows up to simulate one row and create one record with the two results.
From my checking online the pivot seems to be the way to go but it seems to want me to group or do some type of aggregating.
What is the best way to go about doing this?
table1 ---Produces one row
table1id | col1 | col2 | col3
1 Wow Wee Zee
table2 ---Produces 1 - n rows
table2id | table1id | col1 | col2 | col3
1 1 sock cloth sup
2 1 bal baa zak
3 1 x y fooZ
needs to look like this (the below is not column names, they're the result set)
Woo,wee,zee,sock,cloth,sup,bla,baaa,zak,x,y,fooZ
If using MySQL:
SELECT a.table1id, GROUP_CONCAT(a.col) AS col_values
FROM
(
SELECT table1id, col1 col FROM table1 UNION ALL
SELECT table1id, col2 FROM table1 UNION ALL
SELECT table1id, col3 FROM table1 UNION ALL
SELECT table1id, col1 FROM table2 UNION ALL
SELECT table1id, col2 FROM table2 UNION ALL
SELECT table1id, col3 FROM table2
) a
GROUP BY a.table1id
SQLFiddle Demo
If using SQL-Server:
SELECT a.table1id, b.colnames
FROM table1 a
CROSS APPLY
(
SELECT STUFF((
SELECT ',' + aa.col
FROM
(
SELECT table1id, col1 col FROM table1 UNION ALL
SELECT table1id, col2 FROM table1 UNION ALL
SELECT table1id, col3 FROM table1 UNION ALL
SELECT table1id, col1 FROM table2 UNION ALL
SELECT table1id, col2 FROM table2 UNION ALL
SELECT table1id, col3 FROM table2
) aa
WHERE aa.table1id = a.table1id
FOR XML PATH('')
), 1, 1, '') AS colnames
) b
SQLFiddle Demo