Custom SORT BY SQL - sql

I'm new to the community but have referenced it many times in the past. I have an issue I'm trying to overcome in Access, specifically with a SORT BY issue in SQL.
Long story short, I need to create a report based on the results of several different queries. I used a Union query to skirt the "Query is too complex" issue. The results of the query aren't in the order I'd like them, though.
Since this UNION query is not based on one specific table, rather the results of many queries, I'm not able to sort by a specific column header.
I want to sort the results by the way they are written in the SQL statement. Can anyone provide some insight to how to do this? I've attempted several different ways but always end up with an error message. Here's the code, and any help is greatly appreciated.
SELECT [Aqua-Anvil_Total].Expr1
FROM [Aqua-Anvil_Total];
UNION SELECT [Aqua-Reslin_Total].Expr1
FROM [Aqua-Reslin_Total];
UNION SELECT [Aqua_Zenivex_Total].Expr1
FROM [Aqua_Zenivex_Total];
UNION SELECT [Aqualuer_20-20_Total].Expr1
FROM [Aqualuer_20-20_Total];
UNION SELECT [Avalon_Total].Expr1
FROM [Avalon_Total];
UNION SELECT [BVA_13_Total].Expr1
FROM [BVA_13_Total];
UNION SELECT [Deltagard_Total].Expr1
FROM [Deltagard_Total];
UNION SELECT [Envion_Total].Expr1
FROM [Envion_Total];
UNION SELECT [Scourge_18-54_Total].Expr1
FROM [Scourge_18-54_Total];
UNION SELECT [Zenivex_E20_Total].Expr1
FROM [Zenivex_E20_Total];

This uses union all instead of union, so if you are using union to remove duplicates, there would be more work to do after this.
select Expr1
from (
select [Aqua-Anvil_Total].Expr1, 0 as sort
from [Aqua-Anvil_Total]
union all select [Aqua-Reslin_Total].Expr1, 1 as sort
from [Aqua-Reslin_Total]
union all select [Aqua_Zenivex_Total].Expr1, 2 as sort
from [Aqua_Zenivex_Total]
union all select [Aqualuer_20-20_Total].Expr1, 3 as sort
from [Aqualuer_20-20_Total]
union all select [Avalon_Total].Expr1, 4 as sort
from [Avalon_Total]
union all select [bva_13_Total].Expr1, 5 as sort
from [bva_13_Total]
union all select [Deltagard_Total].Expr1, 6 as sort
from [Deltagard_Total]
union all select [Envion_Total].Expr1, 7 as sort
from [Envion_Total]
union all select [Scourge_18-54_Total].Expr1, 8 as sort
from [Scourge_18-54_Total]
union all select [Zenivex_E20_Total].Expr1, 9 as sort
from [Zenivex_E20_Total]
) as u
order by u.sort

Related

Best way to UNION ALL several tables [duplicate]

I can unify two tables via:
select * from project.dataset.ourtable1
union all
select * from project.dataset.ourtable2
But what if I have thousands of tables, and I want to unify all which have name starting with ourtable?
I can get all such tables via:
select table_id from project.dataset.__TABLES__
where starts_with(table_id,'ourtable')
which returns a column of tables with table_id starting with ourtable.
How do I perform union all on all of them?
To rephrase the question: I am looking for the equivalent of
select * from project.dataset.ourtable1
union all
select * from project.dataset.ourtable2
union all
.
.
.
union all
select * from project.dataset.ourtable9999
in BigQuery.
A similar thread: here, but it is for SQL-Server, not BQ.
You can use a wildcard:
select * from `project.dataset.ourtable*`

Oracle SQL: Match long, comma separated list

we use Oracle 11g and I am accessing a database from our manufacturing execution system, on which I don't have any write permissions. E.g. I can't use temporary tables. I have to retrieve the data for a long list of production IDs. The format of the production IDs is flexible, so I can also use a different format, but I would expect a comma separated list is rather handy. However, my problem is how to filter my request for a long list? Using in like in
SELECT
productionid,
tool,
proddate
FROM
proddb
WHERE
productionid in ('123','231','312', ...)
is not very fast, limited to 1000 entries and I guess is not designed for this scenario. So what would be the best approach to filter a large list of hundreds or thousand production ids?
As it's already been said, the best way would be findind a table/set of tables that would return the required IDs through simple criteria (grouping data, dates, etc).
But if you absolutely MUST do your query from a huge set of arbitrary IDs,
and if consistency is not an issue for you (i.e., data won't change greatly in a moment), you could also segment the query in 4 or 5 queries with 1000 items each.
Option 1: Use a collection:
CREATE TYPE string_list IS TABLE OF VARCHAR2(20);
Then:
SELECT productionid, tool, proddate
FROM proddb
WHERE productionid MEMBER OF string_list('123','231','312',...);
or, using a the built-in SYS.ODCI*LIST types:
SELECT productionid, tool, proddate
FROM proddb
WHERE productionid IN (SELECT column_value
FROM TABLE(SYS.ODCIVARCHAR2LIST('123','231','312',...)));
Option 2: Use a multi-dimensional IN list to bypass the 1000 item restriction:
SELECT productionid, tool, proddate
FROM proddb
WHERE (productionid, 1) IN ((123,1),(231,1),(321,1),...);
Option 3: Use a subquery:
SELECT productionid, tool, proddate
FROM proddb
WHERE productionid IN (
SELECT 123 FROM DUAL UNION ALL
SELECT 213 FROM DUAL UNION ALL
SELECT 321 FROM DUAL -- UNION ALL ...
)
or:
SELECT productionid, tool, proddate
FROM proddb p
INNER JOIN (
SELECT 123 AS id FROM DUAL UNION ALL
SELECT 213 FROM DUAL UNION ALL
SELECT 321 FROM DUAL -- UNION ALL ...
) i
ON (p.productionid = i.id)
or
WITH filters AS (
SELECT 123 AS id FROM DUAL UNION ALL
SELECT 213 FROM DUAL UNION ALL
SELECT 321 FROM DUAL -- UNION ALL ...
)
SELECT productionid, tool, proddate
FROM proddb p
INNER JOIN filters f
ON (p.productionid = f.id)

stack or union multiple fields in MS Access

Beginner's question here... I have a table of tree measurements being 3 fields: - ID, Diameter_1, Diameter_2
& I wish to get to these 3 fields: - ID, DiameterName, DiameterMeasurement
Input and Desired Output
SELECT DISTINCT ID, Diameter_1
FROM tblDiameters
UNION SELECT DISTINCT ID, Diameter_2
FROM tblDiameters;
Though it results in only 2 fields. How may the field: - DiameterMeasurement be brought in?
Many thanks :-)
You were on the right track to use a union. Here is one viable approach:
SELECT ID, 'Diameter_1' AS DiameterName, Diameter_1 AS DiameterMeasurement
FROM tblDiameters
UNION ALL
SELECT ID, 'Diameter_2', Diameter_2
FROM tblDiameters
ORDER BY ID, DiameterName;

Combing multiple SELECT SQL Queries

I am fairly new to SQL programming and I am trying to combine two queries into one. I have tried multiple times to combine the two, but am coming up short due to syntax issues. Here are the two queries I have:
SELECT
MIN([NewQuery]![TotalBasePay-Amount]) AS 75P
FROM
(SELECT TOP 25 PERCENT [TotalBasePay-Amount]
FROM NewQuery
WHERE RTRIM([TotalBasePay-Amount]) <> ''
ORDER BY [TotalBasePay-Amount] DESC) AS Subquery;
And the other:
SELECT
MIN([NewQuery]![TotalBasePay-Amount]) AS 90P
FROM
(SELECT TOP 10 PERCENT [TotalBasePay-Amount]
FROM NewQuery
WHERE RTRIM([TotalBasePay-Amount]) <> ' '
ORDER BY [TotalBasePay-Amount] DESC) AS Subquery;
Can anyone assist/explain the most effective way to combine these two queries into one?
I appreciate it.
Combine using UNION ALL for all records, UNION for distinct records between the two tables:
eg:
Select something, somethingelse, 'your label1'
FROM table1
UNION ALL
SELECT something, somethingelse, 'your label2'
FROM table2

Fast way to generate concatenated strings in Oracle [duplicate]

This question already has answers here:
SQL Query to concatenate column values from multiple rows in Oracle
(10 answers)
Closed 1 year ago.
Don't we hate when evil coding comes back to haunt?
Some time ago I needed to generate a string concatenating some fields for some more processing later. I thought it would be a good idea to do if straight in the query, and used SO's help to get it. It worked. For a while...
The table got to big and now that trick (which I know is super inefficient) is not exactly viable. This what I'm doing:
with my_tabe as
(
select 'user1' as usrid, '1' as prodcode from dual union
select 'user1' as usrid, '2' as prodcode from dual union
select 'user1' as usrid, '3' as prodcode from dual union
select 'user2' as usrid, '2' as prodcode from dual union
select 'user2' as usrid, '3' as prodcode from dual union
select 'user2' as usrid, '4' as prodcode from dual
)
select
usrid,
ltrim(sys_connect_by_path(prodcode, '|'), '|') as prodcode
from
(
select distinct prodcode, usrid,count(1)
over (partition by usrid) as cnt,
row_number() over (partition by usrid order by prodcode) as rn
from my_tabe
)
where
rn = cnt
start with rn = 1
connect by prior rn + 1 = rn
and prior usrid = usrid
Which nicely yields:
USRID PRODCODE
user1 1|2|3
user2 2|3|4
The evil thing in here, as you might have noticed, is the where rn = cnt, which if you remove you'll see all the work (I suppose) Oracle is really doing:
USRID PRODCODE
user1 1
user1 1|2
user1 1|2|3
user2 2
user2 2|3
user2 2|3|4
I'm actually using this in many places where I have not so many records. It is quite fine up to about a half million records.
Recently I tried the same in a table with ~15Mi records, and well... no good.
Question: is there a way to do this more efficiently on Oracle or is it time bring it down to the actual code?
This is not actual core issue, so I can still afford kludging, as long as it's fast...
Worth mentioning there's a index for the column "usrid" I'm using.
cheers,
Tom Kyte provides a very convenient way to do that, and it works from Oracle 9i, with a custom aggregation function. It aggregates with commas, but you can modify the function body for pipes.
Starting with Oracle 11g, you can do:
SELECT LISTAGG(column, separator) WITHIN GROUP (ORDER BY field)
FROM dataSource
GROUP BY grouping columns
This web page provides additional methods including the one that you listed and which is indeed not really efficient.