Using Microsoft Access. Is it possible to sort the ORDER BY column as Descending?
SELECT * FROM
(
SELECT table1.* FROM table1 ORDER BY table1.field1
) DUMMY_ALIAS1
UNION ALL
SELECT * FROM
(
SELECT table2.* FROM table2 ORDER BY table2.field1
) DUMMY_ALIAS2
Yes - just add desc after the field name:
SELECT * FROM
(
SELECT table1.* FROM table1 ORDER BY table1.field1 DESC
) DUMMY_ALIAS1
UNION ALL
SELECT * FROM
(
SELECT table2.* FROM table2 ORDER BY table2.field1 DESC
) DUMMY_ALIAS2
Related
Following up from this question Possible to emulate a basic CTE in MySQL by in-lining it, would it be possible to in-line the following query in mysql5.7 without the usage of CTEs?
WITH tbl1 AS (
SELECT 1 AS a
), tbl2 AS (
SELECT 1 AS a UNION ALL SELECT 2
), tbl3 AS (
SELECT * FROM tbl1 JOIN tbl2 USING (a)
) SELECT * FROM tbl3, tbl2
If so, how could that be done?
There's no particular problem with doing that, except that you have to repeat tbl2:
SELECT *
FROM (
SELECT *
FROM (
SELECT 1 AS a
) tbl1
JOIN (
SELECT 1 AS a UNION ALL SELECT 2
) tbl2
USING (a)
) tbl3
CROSS JOIN
(
SELECT 1 AS a UNION ALL SELECT 2
) tbl2
fiddle
This would be similar to substituting the body of a function. Writing it in a similar way (conceptually speaking):
let tbl1 = (SELECT 1 AS a) AS tbl1
let tbl2 = (SELECT 1 AS a UNION ALL SELECT 2) AS tbl2
let tbl3 = (SELECT * FROM tbl1 JOIN tbl2 USING (a)) AS tbl3
-- rewriting tbl1 for tbl1 and tbl2
let tbl3 = (SELECT * FROM (SELECT 1 AS a) AS tbl1
JOIN ((SELECT 1 AS a UNION ALL SELECT 2)) AS tbl1 USING (a)) AS tbl3
SELECT * FROM tbl3, tbl2
-- substitutie for tbl3, tbl2
SELECT * FROM
(SELECT * FROM (SELECT 1 AS a) AS tbl1
JOIN ((SELECT 1 AS a UNION ALL SELECT 2)) AS tbl1 USING (a)) AS tbl3,
(SELECT 1 AS a UNION ALL SELECT 2) AS tbl2
I'm trying to select from table data but i want the result not exist in this select.
(SELECT * FROM data WHERE category = 'A' ORDER BY RAND() LIMIT 4)
UNION ALL
(SELECT * FROM data WHERE category = 'B' ORDER BY RAND() LIMIT 4)`
So there are no row that been selected double.
Fiddle with sample data.
With NOT EXISTS:
SELECT * FROM table1 t1
WHERE NOT EXISTS (
SELECT 1 FROM (
(SELECT * FROM table1 WHERE category = 'A' ORDER BY RAND() LIMIT 4)
UNION ALL
(SELECT * FROM table1 WHERE category = 'B' ORDER BY RAND() LIMIT 4)
) t
WHERE t.id = t1.id AND t.category = t1.category
)
See the demo.
SELECT * FROM table1 where id not in (
select id from (
(SELECT * FROM table1 WHERE table1.category = 'A' ORDER BY RAND() LIMIT 4)
UNION ALL
(SELECT * FROM table1 WHERE table1.category = 'B' ORDER BY RAND() LIMIT 4)
)
as t)
(SELECT *, 0 AS user FROM table1)
UNION
(SELECT * FROM table2 WHERE unix >= {$threemonths})
ORDER BY unix DESC;
I need to add:
WHERE table2.identifier = table1.identifier or something
I want to get all from table1 and only rows from table2 where identifier is found in the results from table1's identifier column.
Please see if this works for you
(SELECT *, 0 AS user FROM table1)
UNION
(SELECT * FROM table2 WHERE unix >= {$threemonths} and exists (select 'Y' from table1 a where a.identifier = table2.identifier))
ORDER BY unix DESC;
Could be
SELECT *, 0 AS user FROM table1
UNION
SELECT * FROM table2 WHERE unix >= {$threemonths}
INNER JOIN table1 on table2.identifier = table1.identifier
ORDER BY unix DESC;
How to get the data that is failed that associate with left(number,7) from sub query count(*) data?
For example I did this:
SELECT * FROM table1 WHERE outcome = 'Fail' AND left(number,7) =
(SELECT count(*) as total, left(number,7) as prefix
FROM table1 where outcome like '%Passed%'
group by prefix order by total desc limit 250)
This wont work because there are two fields in the sub-query.. so how to get around that?
You can use JOIN instead of subquery:
SELECT t1.*, t2.total, ...
FROM table1 AS t1
INNER JOIN
(
SELECT count(*) as total, left(number,7) as prefix
FROM table1
where outcome like '%Passed%' AND outcome = 'Fail'
group by prefix
order by total desc limit 250
) AS t2 ON t2.prefix = left(t1.number,7)
Try this query
SELECT *
FROM
table1 a
INNER JOIN
(SELECT
count(*) as total,
left(number,7) as prefix
FROM
table1
where
outcome like '%Passed%'
group by
prefix
order by
total desc limit 250)b
ON
a.outcome = 'Fail' AND
left(number,7) = b.prefix
I have fairly complicated join query from which I want to select a few rows around a result with a certain id.
The query currently looks something like this:
WITH results AS
(
SELECT t1.id, t1.position, t1.points, t2.name
ROW_NUMBER() OVER(ORDER BY t1.position ASC, t1.points DESC) AS rn
FROM Table1 t1
JOIN Table2 t2 ON t1.id = t2.Table1id
/* Several more joins here, some of which limit the result set */
)
SELECT * FROM results
WHERE rn < ( SELECT rn+3 FROM results WHERE id = #someid )
AND rn > ( SELECT rn-3 FROM results WHERE id = #someid )
Is there a better way to solve this? Most of all I'm worried about performance with these multiple calls to a possibly huge CTE.
The query is run on a SQL 2008 server.
Maybe pull the joins out of the CTE.
That way the query optimizer has a chance filter out rows before processing the joins.
WITH results AS
(
SELECT t1.id, t1.position, t1.points
, ROW_NUMBER() OVER(ORDER BY t1.position ASC, t1.points DESC) AS rn
FROM Table1 t1
)
SELECT results.id, results.position, results.points, t2.name
FROM results
JOIN Table2 t2 ON t2.id = results.Table1id
/* Several more joins here */
WHERE rn < ( SELECT rn+3 FROM results WHERE id = #someid )
AND rn > ( SELECT rn-3 FROM results WHERE id = #someid )
You could use another cte to help form the filter:
WITH results AS (
SELECT
t1.id
, t1.position
, t1.points
, t2.name
, ROW_NUMBER() OVER (ORDER BY t1.POSITION ASC, t1.points DESC) AS rn
FROM Table1 t1
JOIN Table2 t2
ON t1.id = t2.Table1id
/* Several more joins here, some of which limit the result set */
),
filter AS (
SELECT
rn
FROM results
WHERE id = #someid
)
SELECT
*
FROM results
WHERE rn < ( SELECT rn + 3 FROM filter )
AND rn > ( SELECT rn - 3 FROM filter )