IBM i UNION *ALL used here - sql

I have this in CL and would like to make it into a view. I first run 2 queries which are creating these 2 tables. How would the code here for the UNION ALL be? the 2 tables are (Logical) views
CPYF FROMFILE(JETDTA/EODDETAILH) +
TOFILE(JETDTA/EODDETAILS) MBROPT(*ADD) +
FMTOPT(*NOCHK)
MONMSG CPF0000

Besides the structure of the queries result must match, you can do it with:
CREATE VIEW yourViewName AS
SELECT *
FROM JETDTA.EODDETAILH
UNION
SELECT *
FROM JETDTA.EODDETAILS
You can find more information about the CREATE VIEW command here and about the UNION clause here, both of those are part of SQL/400.

Related

Table Value Function and Different Project Queries

I have 2 table value functions in 2 different projects. I can run these 2 individual queries and its working fine.
SELECT *
FROM `project1.analytics_1.TableValueFunc1`('2022-01-01', '2023-02-02')
SELECT *
FROM `project2.analytics_2.TableValueFunc2`('2023-01-01', '2023-01-02')
But when I union these queries, like this:
SELECT *
FROM `project1.analytics_1.TableValueFunc1`('2022-01-01', '2023-02-02')
UNION ALL
SELECT *
FROM `project2.analytics_2.TableValueFunc2`('2023-01-01', '2023-01-02')
I get this error:
Table-valued function not found: project1.analytics_1.TableValueFunc1
I tried this solution but it's not working
BigQuery JOINs between tables in different projects

UNION tables with wildcard in BigQuery

I have over 40 tables I want to append in BigQuery using standard SQL. I have already formatted them to have the exact same schema. When I try to use the '*' wildcard at the end of table name in my FROM clause, I get the following error:
Syntax error: Expected end of input but got "*" at [95:48]
I ended up manually doing a UNION DISTINCT on all my tables. Is this the best way to do this? Any help would be appreciated. Thank you!
CREATE TABLE capstone-320521.Trips.Divvy_Trips_All AS
SELECT * FROM capstone-320521.Trips.Divvy_Trips_*;
--JOIN all 2020-21 trips data
CREATE TABLE capstone-320521.Trips.Divvy_Trips_Raw_2020_2021 AS
SELECT * FROM capstone-320521.Trips.Divvy_Trips_2020_04
UNION DISTINCT
SELECT * FROM capstone-320521.Trips.Divvy_Trips_2020_05
UNION DISTINCT
SELECT * FROM capstone-320521.Trips.Divvy_Trips_2020_06
UNION DISTINCT
Syntax error: Expected end of input but got "*"
I think the problem is in missing ticks around the table references. Try below
CREATE TABLE `capstone-320521.Trips.Divvy_Trips_All` AS
SELECT * FROM `capstone-320521.Trips.Divvy_Trips_*`
Note: The wildcard table name contains the special character (*), which means that you must enclose the wildcard table name in backtick (`) characters. See more at Enclose table names with wildcards in backticks
I am unaware of any such UNION DISTINCT syntax. If your intention is to do a union of the 3 tables and remove any duplicate records in the process, then just using UNION should suffice:
CREATE TABLE capstone-320521.Trips.Divvy_Trips_Raw_2020_2021 AS
SELECT * FROM capstone-320521.Trips.Divvy_Trips_2020_04
UNION
SELECT * FROM capstone-320521.Trips.Divvy_Trips_2020_05
UNION
SELECT * FROM capstone-320521.Trips.Divvy_Trips_2020_06;
Note that in general it is bad practice to use SELECT * with union queries. The reason has to do with that a union between two (or more) tables is generally only valid if the two queries involved have the number and types of columns. Using SELECT * obfuscates what columns are actually being selected, and so it is preferable to always explicitly list out the columns.

Creating a list in SQL and iterating through the list after the FROM line

I want to access some table like Toyota_Corolla, Toyota_Camry, Toyota_Prius, Toyota_Rav4
Instead of typing out multiple SELECT statements like the following:
SELECT * FROM Toyota_Corolla;
SELECT * FROM Toyota_Camry;
SELECT * FROM Toyota_Prius;
SELECT * FROM Toyota_Rav4;
Is there a way to create a list of strings like ['Corolla', 'Camry', 'Prius', Rav4'] and iterate through the list after the FROM line to something similar to:
SELECT * FROM 'Toyota_'` + 'some loop to iterate the list of car model'
I know for my example, it's easier to just type out the whole thing, but what about the situation when Toyota has hundred of models?
This is MS SQL Server DBMS
No. First, you should fix your data model so you have a single table with an additional column for the Toyota model. That is the right way to store the data.
With the data you have, you can emulate this with a view:
create view vw_toyota as
select 'Corolla' as toyota_model, t.* from Toyota_Corolla t union all
select 'Camry' as toyota_model, t.* from Toyota_Camry t union all
select 'Prius' as toyota_model, t.* from Toyota_Prius t union all
select 'Rav4' as toyota_model, t.* from Toyota_Rav4 t;
This also adds the source table information.
And then do:
select *
from vw_toyota;

Oracle: Query identical table in multiple schema in single line

In Oracle, is there a way to query the same, structurally identical table out of multiple schema within the same database in single line? Obviously assuming the user has permissions to access all schema, I could build a query like:
select * from schema1.SomeTable
union all
select * from schema2.SomeTable
But is it possible, given the right permissions to say something like:
select * from allSchema.SomeTable
...and bring back all rows for all the schema? And related to this, is it possible to pick which schema, such as:
select * from allSchema.SomeTable where schemaName in ('schema1','schema2')
The simplest option, as far as I can tell, is to create a VIEW (as UNION of all tables across all those users), and then SELECT FROM VIEW.
For example:
create or replace view my_view as
select 'schema_1' source_schema, id, name from schema_1.table union
select 'schema_2' source_schema, id, name from schema_2.table union
...
-- select all
select * from my_view;
-- select all that belongs to one of schemas
select * from my_view where source_schema = 'schema_1';

Let two table act as one

Is there any way to let two tables act as one? I have two identical tables, the only difference is that one contains recent data and the other one older data. Is there any way to do something like this?
select *
from customers a,
(rentals union archrentals) r
where a.serno = r.custserno
and r.rentaldate > YYYYMMDD;
Why don't you create a view like this?
CREATE VIEW v_allRentals
AS
SELECT * form rentals
UNION ALL
SELECT * FROM archrentals
In this way you can use v_allRentals without worrying every time you create a query that you are forgetting the old ones.
Thanks,
Mucio
Use a temporary table
select *
INTO #allcustomers
from customers a,
(rentals union archrentals) r
where a.serno = r.custserno
and r.rentaldate > YYYYMMDD;
then you can use the temptable to query the results.