Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
What is the meaning (result) of this SQL statement in layman's term?:
SELECT mainTable.Year FROM mainTable
UNION SELECT mainTable.Title FROM mainTable
In set theory
if A={1,2,3} and B={2,3,4,5} then A union B={1,2,3,4,5} -- No duplicates here!
where as A union all B={1,2,3,2,3,4,5} --with duplicates
similarly in sql
its written like
select * from A
union -- or union all
select * from B
SEE HERE
The UNION combines two results.
When you have two SELECT statements, all of them has a result. If those results contain equivalent number of columns (and those - the ones which are at the same position - column's datatypes are equal or compatible with eachother), you can get those results combined into one result.
The UNION will filter out the duplications. You can use UNION ALL to prevent filtering.
See this SQLFiddle demo: http://sqlfiddle.com/#!2/42569/2
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Write a query using Union all that performs the function of UNION?
UNION ALL returns all the records while UNION returns distinct records.
so you can use the DISTINCT with UNION ALL to make query same as UNION as follows:
select distinct t.* from
(select * from table1
UNION ALL
select * from table2) t
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
sel *A union all sel * B union all sel * C
I need this statement result in a table for calculation
A,B,C contains same columns but different values
I don't know what you mean by volatile but if you are looking for the results of your query in one table you can use something like the below
INSERT INTO TARGET_TABLE
SELECT * FROM A
UNION ALL
SELECT * FROM B
UNION ALL
SELECT * FROM C;
If you dont want duplicate values you can replace UNION ALL by UNION
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am using 11g RDBMS and in one interview interviewer ask me that print 1 to 10 numbers using SQL query don't use loop if you are using PLSQL, means using select query he wants to see the result.
like Table:
1
2
3
Here are two approaches, both are SQL Server syntax, but you will find something similar for other RDBMs:
--ROW_NUMBER()
SELECT TOP 10 ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) FROM sys.objects; --any table with more rows than 10 will do
--recursive CTE
WITH recursiveCounter AS
(
SELECT 1 AS Nr
UNION ALL
SELECT r.Nr+1
FROM recursiveCounter AS r
WHERE r.Nr<10
)
SELECT * FROM recursiveCounter
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am working on Library Management and for making a report I want to get data from 2 tables which are not interlinked i.e issueData and issueRecord.
The Record contains previous book issued data while the Data table contains only the current book issued Report.
Simply, I want to merge these two queries i.e
Select * From issueRecord where issueDate = '19/07/2013'.
Select * From issueData where issueDate = '19/07/2013'.
Please Help.
If your question was more clear we would be able to help more.
From what I understand you are trying for a union all or union. There is also a chance for cross join also, but that may not be the result you wanted.
There are answers for Union and union all, But I would suggest you to use like the below
Select 'Record', Field_1, Field_2, Field_3 From issueRecord where issueDate = '19/07/2013'
UNION
Select 'Data', Field_A, Field_B, Field_C From issueData where issueDate = '19/07/2013'
With this addition you can find which data is from which table.
In addition to this you can also use cross join
select * from issueRecord CROSS JOIN issueData
but check the data what you are getting.
Your question is not very clear but it seems like you are looking for a UNION or UNION ALL query.
Assuming that what you want is a single result set that includes record from each table, but you are not trying to combine the two tables onto a single line, the syntax would be something along the lines of:
Select Field_1, Field_2, Field_3 From issueRecord where issueDate = '19/07/2013'
UNION
Select Field_A, Field_B, Field_C From issueData where issueDate = '19/07/2013'
The type and order of the columns selected from both tables have to match. UNION will collapse identical records into one output row, just like a distinct clause, while UNION ALL will include every record from each query.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Hi I am trying to find duplicates with the same ID. I am not finding all of the duplicates. I would like to find distinct JOIN_FID values, and then duplicate JOIN FID values.
Here is my SQL select statements.
select OBJECTID,
Join_Count,
TARGET_FID,
JOIN_FID,
StrtConcat,
PermID,
Minutes,
FacilityID,
Shape
from
sde.gis.MFD_8_minute_response_ladder
where TARGET_FID
in (
select
JOIN_FID
from
sde.gis.MFD_8_minute_response_ladder
group by
JOIN_FID
having
COUNT(*) > 1
You're missing a closing parenthesis ()), but I assume that's a typo.
I suspect the problem may be that you're comparing two different fields:
where TARGET_FID
in (
select
JOIN_FID
Should that be:
where TARGET_FID
in (
select
TARGET_FID
or
where JOIN_FID
in (
select
JOIN_FID
?
For the query that you added ,an IN subquery can only return one value, so you need something like:
where PermID
in (
select
COUNT(1),
TARGET_FID
making sure you're comparing the right columns.