Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I want to execute two queries having same number of columns. Here is my query, please help me to get the result:
SELECT A.*
FROM (SELECT *
FROM yh5lw_onlinecourse
WHERE state = 1
ORDER BY id DESC) A
union
SELECT A1.*
FROM (SELECT *
FROM yh5lw_practicetest
WHERE state = 1
ORDER BY id DESC) A1
I think you want this:
SELECT * FROM
(SELECT A.id as id, A.field1 as outputname1, A.field2 as outputname2, ... and so on
FROM yh5lw_onlinecourse A
WHERE state = 1
UNION
SELECT A1.*
FROM yh5lw_practicetest A1
WHERE state = 1) myUnion
order by myUnion.id DESC
Pay attention:
If you use UNION instead UNION ALL you discard all duplicated value-
First query must have alias because in this way you named yuor output table. The second query (after UNION) can use * wildcard (but the number of fields must be the same)
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last month.
The community reviewed whether to reopen this question last month and left it closed:
Original close reason(s) were not resolved
Improve this question
I have a table tbl and I want to extract count of some data from it.
As simplified, I have two choices:
select count(if(c10=7,1,null)) as recordCount
from tbl
where cId in (
select max(cId)
from tbl
group by c37
);
and
select count(*) as recordCount
from (
select max(cId)
from tbl
where c10 = 7
group by c37
) t1;
The first query returns 151 and the second returns 156.
Why do these two queries have different results?
Select Count(if(c10=7,1,null)) as recordCount
from tbl
where cId in (select max(cId)
from tbl
group by c37);
In English -- for every grouping of c37 take the one with max id -- from records that match those ids if c10 = 7 then count them
Select Count(*) as recordCount
from (Select max(cId)
from tbl
where c10=7
group by c37) t1;
In English -- for every grouping of c37 with c10 is 7 take the max id -- count those.
I wrote the above to try and figure out what the queries are doing. Then I saw the issue as described below.
As for your question, it makes no sense the numbers would be different until you consider that max(cID) could be null in the second one but can't be in the first one. -> You can have null in a row in a table, but you can't have it when you use an IN statement.
To show this is the case run the following query
SELECT X
FROM (
Select max(cId) as X
from tbl
where c10=7
group by c37
) Z
WHERE X IS NULL
I'd expect you to get 4 rows back.
To get the same result for both queries use the following for the 2nd query
Select Count(X) as recordCount
from (Select max(cId) X
from tbl
where c10=7
group by c37) t1;
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
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.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to use multiple CTEs in my query, but I haven't been able to get it working. The following is an example of what I would like to do:
WITH tbl1 AS
(SELECT *
FROM tblStuff)
WITH tbl2 AS
(SELECT tbl1.fldStuff1 ...
FROM tbl1, tblStuff2)
SELECT *
FROM tbl2
You only need to specify WITH once. Then you can list additional CTEs separated by a comma. E.g.:
WITH cte1 AS
(
...
),
cte2 AS
(
...
)
SELECT ...;
WITH tbl1 AS
(SELECT *
FROM tblStuff),
tbl2 AS
(SELECT tbl1.fldStuff1, tbl1.fldStuff2, tblStuff2.fldStuff1, tblStuff2.fldStuff2
FROM tbl1, tblStuff2)
SELECT *
FROM tbl2
When you use multiple CTEs, no need for the extra WITH, seperate with a comma.