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.
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed last year.
Improve this question
INSERT INTO your_table_name (your_column_name, your_id )
VALUES (select your_column_name from another_table, 'abc');
the above logic doesn't seem to work, can anyone suggest a better idea?
Your syntax is incorrect. I believe you want:
INSERT INTO your_table_name (your_column_name, your_id )
(select your_column_name, 'abc' as your_id from another_table);
At a minimum, you can either do an insert ... select or a insert ... values but you can't combine the two. If you want to do a select, you'd need an insert ... select
INSERT INTO your_table_name (your_column_name, your_id )
select your_column_name, 'abc'
from another_table;
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 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)
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.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
I have a SQL query where I want to order the database on type, order and category. Can I have query having all these three?
select * from t_person where name=xyz order by type,category,order
try this:
Use "" (double quotes) to escape the reserved keyword order
select * from t_person where name='xyz'
order by type,category,"order"
Use double quote for correct query:
select * from t_person where name='xyz' order by type,category,"order"