How do I combine 2 select statements into one?
Tag : sql , By : Blaise Roth
Date : March 29 2020, 07:55 AM
I hope this helps . You have two choices here. The first is to have two result sets which will set 'Test1' or 'Test2' based on the condition in the WHERE clause, and then UNION them together: select
'Test1', *
from
TABLE
Where
CCC='D' AND DDD='X' AND exists(select ...)
UNION
select
'Test2', *
from
TABLE
Where
CCC<>'D' AND DDD='X' AND exists(select ...)
select
case
when CCC='D' AND DDD='X' AND exists(select ...) then 'Test1'
when CCC<>'D' AND DDD='X' AND exists(select ...) then 'Test2'
end,
*
from
TABLE
Where
(CCC='D' AND DDD='X' AND exists(select ...)) or
(CCC<>'D' AND DDD='X' AND exists(select ...))
|
Can I combine these two almost identical MERGE statements into one?
Tag : sql , By : CrimsonGore
Date : March 29 2020, 07:55 AM
it should still fix some issue Solved it on my own... figured it was an issue with the AND/NOT logic! MERGE assignment_tbl AS target
USING (SELECT 1 s) AS source
ON target.id = @id
AND target.id1 = @id1
AND target.id2 = @id2
AND target.bActive = 1
WHEN matched AND (NOT (target.iFeeScope = @iFeeScope))
OR (NOT (target.nFeeAmount = @nFeeAmount)) THEN
UPDATE SET target.dLastUpdated = @dNow,
target.dDisabled = @dNow,
target.bActive = 0;
|
Trying to combine three individual SELECT statements into one main SELECT statement
Tag : sql , By : somebody
Date : March 29 2020, 07:55 AM
should help you out You can remove the WHEREs, as well as the non-correlated subqueries in the select list, and probably simplify it to this... SELECT
AID
, EID
, STOREID
, [Language]
, 'BrandLabel' = CASE WHEN BrandLabel LIKE '%"'
THEN LEFT(BrandLabel, LEN(BrandLabel) -1)
ELSE BrandLabel
END
, 'Terms' = CASE WHEN Terms LIKE '%"'
THEN LEFT(Terms, LEN(Terms) -1)
ELSE Terms
END
, 'TrackOrderLbl' = CASE WHEN TrackOrderLbl LIKE '%"'
THEN LEFT(TrackOrderLbl, LEN(TrackOrderLbl) -1)
ELSE TrackOrderLbl
END
FROM parallel_Purchase_Email_Content_OMS WITH (NOLOCK)
|
Combine/Merge two MYSQL SELECT statements into one
Tag : mysql , By : mlapida
Date : March 29 2020, 07:55 AM
help you fix your problem I'm looking for help using a single SELECT statement. Example using two statements: , Why not just use or? SELECT id
FROM sales
WHERE (saledate BETWEEN DATE_ADD(NOW(), INTERVAL 40 DAY) AND DATE_ADD(NOW(), INTERVAL 365 DAY)) OR
(saledate BETWEEN NOW() AND DATE_ADD(NOW(), INTERVAL 40 DAY))
ORDER BY saledate;
SELECT id
FROM sales
WHERE saledate BETWEEN (NOW() AND DATE_ADD(NOW(), INTERVAL 365 DAY)
ORDER BY saledate;
SELECT id
FROM sales
WHERE saledate BETWEEN (NOW() AND DATE_ADD(NOW(), INTERVAL 365 DAY)
ORDER BY (saledate >= DATE_ADD(NOW(), INTERVAL 40 DAY) ) DESC,
saledate;
|
How to combine two select statements into one select statement in MSSQL?
Date : March 29 2020, 07:55 AM
I hope this helps you . Normally, you would use an inner join for things like that. But inner join needs some common columns between the two objects it joins, and you don't have that. Since your queries also does not contain an ORDER BY clause, there is no reliable way to join them so that each row in table1 will always be joined to the same row in table2. ;WITH CTE1 AS
(
SELECT a1,
b1,
c1,
ROW_NUMBER() OVER(ORDER BY [time]) AS rn
FROM Table1
WHERE time between '2018-03-05' and '2018-03-06'
), CTE2 AS
(
SELECT a2,
b2,
c2,
ROW_NUMBER() OVER(ORDER BY [time]) AS rn
FROM Table2
WHERE time between '2018-03-05' and '2018-03-06'
)
SELECT t1.a1, t1.b1, t1.c1, t2.a2, t2.b2, t2.c2
FROM cte1 as t1
INNER JOIN cte2 as t2 ON t1.rn = t2.rn
|