create a view from two tables in athena query - mysql

Basically my use case is that I am creating a view in athena but i want to select the data from two tables depending on the date (snapshot_date) which is given by a column in the tables. How to do it? I am not finding the syntax for that
SELECT
baseline_year
, marketplace
, locale
, snapshot_time
, ...
FROM
table1
I want to achieve something like if snapshot time is less than 2022-05-01, then use table 2 otherwise use table 1.
Can we any kind of conditioning in FROM? I have explored that we can use CASE_WHEN for performing conditions on columns, but not sure that can be used in FROM?

If I am not mistaken, you meant to use table2 if the snapshot_time is earlier than 2022-05-01 and use table1 if the snapshot_time is greater than or equal to 2022-05-01 REGARDLESS of what shapshot_time those two tables have.
SELECT ... FROM table1 where date(snapshot_time)>= '2022-05-01'
union
SELECT ... FROM table2 where date(snapshot_time) < '2022-05-01' ;

You can proceed with a parametrized view, try to create a function:
CREATE FUNCTION func() RETURNS DATE
RETURN #var;
Then create the view :
CREATE VIEW view1 AS
SELECT
T1.baseline_year
, T1.marketplace
, T1.locale
, T1.snapshot_time
, ...
FROM table1 AS T1
WHERE DATE(T1.snapshot_time) >= func()
UNION
SELECT
T2...,
..
FROM
table1 AS T2
WHERE DATE(T2.snapshot_time) <= func()

Related

TSQL: Join columns, but rows in one column have multiple values

I have a query where I have to join two tables. Lets say T1 and T2.
T1 has a column with an Id. And every row has only one Id value. However, the second table is where i'm struggling. in T2 there is a column with the Id's but it can be possible that one row has multiple of those Id's.
So as example T1.col1 has this value: 737382. But T2.col2 can have this entries in one row: 737382;239112;2838210;9923834;2388342;...
I know that this structure violates the 1NF and stuff. But i cannot change anything in the data or structure of the data.
now what i want to do is to join this two tables. Please let me know if i'm missing to tell any relevant information in order to answer my question. its late and my brain is exhausted ~.~
try
select tab2.* -- whatever
from t1 tab1
inner join t2 tab2 on ( ';'||tab2.col2||';' like '%;'||tab1.col1||';%' )
;
the extra affixed ; characters serve to avoid disjunctions in the join condition.
You could use regular expressions in your join, your regular expression can check for your T1.col1 in T2.col2. The regular expression should check for the value from the begining of the string (i.e. T2.col2) or being preceeded by ';' and always followed by ';'
Have you tried something like:
select
a.column1,
a.column2,
b.column1,
b.column2
from table a
inner join table b on a.column1 = b.column1
Since one T2.Col2 can hold n entries, you need to parse them to rows using a table-valued function and then using CROSS APPLY
BEWARE if the T2 is big this solution will hang for quite long time
something like this:
;WITH IDSplitted AS
(
SELECT *
FROM T2
CROSS APPLY dbo.[StringSplit](col2, ';')
)
SELECT * -- or whatever columns you need
FROM T1
INNER JOIN IDSplitted ON IDSplitted.val = t1.col1
having StringSplit:
CREATE FUNCTION [dbo].[StringSplit]
(
#delimited nvarchar(max),
#delimiter nvarchar(100)
) RETURNS #t TABLE
(
-- Id column can be commented out, not required for sql splitting string
id int identity(1,1), -- I use this column for numbering splitted parts
val nvarchar(max)
)
AS
BEGIN
declare #xml xml
set #xml = N'<root><r>' + replace(#delimited,#delimiter,'</r><r>') + '</r></root>'
insert into #t(val)
select
r.value('.','varchar(max)') as item
from #xml.nodes('//root/r') as records(r)
RETURN
END
After using the solution of collapstar (which was correct) i encountered performance issues. so what i did is to create a mapping table so that when i have to run the query again, i dont have to wait so long. so i have a scheduled job which does the join and writes the output into a mapping table over night

Get a list of ids not present in a table

I have a list of ids, and I want to query a mysql table for ids not present in the table.
e.g.
list_of_ids = [1,2,4]
mysql table
id
1
3
5
6
..
Query should return [2,4] because those are the ids not in the table
since we cant view ur code i can only work on asumption
Try this anyway
SELECT id FROM list_of_ids
WHERE id NOT IN (SELECT id
FROM table)
I hope this helps
There is a horrible text-based hack:
SELECT
substr(result,2,length(result)-2) AS notmatched
FROM (
SELECT
#set:=replace(#set,concat(',',id,','),',') AS result
FROM (
select #set:=concat(',',
'1,2,4' -- your list here
,',')
) AS setinit,
tablename --Your tablename here
) AS innerview
ORDER BY LENGTH(result)
LIMIT 1;
If you represent your ids as a derived table, then you can do this directly in SQL:
select list.val
from (select 1 as val union all
select 2 union all
select 4
) list left outer join
t
on t.id = list.val
where t.id is null;
SQL doesn't really have a "list" type, so your question is ambiguous. If you mean a comma separated string, then a text hack might work. If you mean a table, then something like this might work. If you are constructing the SQL statement, I would advise you to go down this route, because it should be more efficient.

CREATE VIEW WHERE SELECTid = VIEWrowID

Warning: vagueness & unclear questioning will abound because I know squat about databases.
I just discovered that I need to use views as surrogates for a cronned update statement. I can somewhat get the view to work, but I'm having trouble with rows.
This post helped me to bang out the update I need, but now that I know that views can run that update whenever it's needed rather than on a cron schedule, how can I set the view's column value based upon the view's row id or equivalent?
I've got the select I need:
SELECT SUM( table2.column1/ (
SELECT table2constant
FROM table3
)
FROM table2
WHERE table2table1id = table1id
table1id is the AI id column for table1. table2table1id is PKd to table1id. I'd like the view to have a column PKd to table1id like with table2, and the view needs to have every distinct table1id represented.
I'm sure the jargon's way off, but hopefully you can see what I need.
Will provide as many edits as necessary for clarity.
Many thanks in advance!
EDIT1
Should I create a trigger that creates the view upon insert to table1? Just found about materialization which is what I need/want?
Clarity
I need a summed value for each table1.table1id
Progress
With this code, I'm getting the first id from table1 and only the total sum. I need a sum for each table1.id.
CREATE VIEW db1.sums as
SELECT SUM( table2.column1/ (
SELECT table2constant
FROM table3
) as theSum, table1id
FROM table1, table2
WHERE table2.table2table1id = table1.table1id
To be clear I'm still not sure what you're trying to accomplish here but if what you posted works, try
SELECT table1.table1id,
SUM( table2.collumn1 ) / (SELECT table2constant FROM table3 ) as theSum
FROM table1, table2
WHERE table2.table2table1id = table1.table1id GROUP BY table1.table1id
you can replace (SELECT table2constant FROM table3 ) with your constant if it has no reason to otherwise be in the database (if it's not updated)
Its actually very simple. Here is an example of how you can do it.
SELECT SUM( table1.column / table2.column ), table1.*, table2.*
FROM table1, table2
WHERE table1.id = table2.column_id

mysql query with when statement comparing

I want to write mysql when statement and i can't do it.
explanation => localParty is column in table "data", loc is column from table "o", and i want to compare localParty to loc, if their values are equal then i want to retrieve information from loc_m column (this column is from table "o"), and if not equal then from localParty column (from "data" table)
Please help how to write this script in mysql query ? Thanks
with this script
select (case when data.localparty = o.loc then o.loc_m else data.localparty end)
as customdata from data, o
it is working but it is missing exactly three result ( I mean that then data.localparty equal to o.loca it is giving result from data.localparty 3 times and after it one time it is giving result from loc_m and it is going like so .
You could modify the query in the following way:
SELECT IF(t1.Column1 = t2.Column2,t2.Column1,t1.Column3) FROM TABLE1 AS t1, Table2 AS t2
Try This:
select (case when data.localparty = o.loc then o.loc_m else data.localparty end)
as customdata from data, o
you can use following query
Select O.loc_m as local
from Data
inner join on O on data.localparty=O.loc
UNION
Select data.loacalparty as local
from Data
where data.localparty is not in (select loc from O )
You should use control-flow functions to achieve that goal:
SELECT IF(Column1 = Column2,Column1,Column3) FROM TABLE1

How to run a case sensative comparison on text fields Access

I am trying to run a query to remove a set of ID's from a table when they are present in a field from another table.
The problem is both ID fields are of type text and the search does not appear to be case sensitive (but I need it to be). (i.e. ABC123 is different than abc123)
I am running a query similar to Select myID from table1 where myID NOT IN (Select otherID from table2)
What modification do I need to make in my Access query to make the results case sensitive when running comparison?
Try this:
SELECT a.*
FROM table1 a LEFT JOIN table2 b
ON a.myID = b.otherID
WHERE StrComp(IIF(IsNull(b.otherID ), a.myID , b.otherID), a.myID, 0) <> 0
OR IsNull(b.otherID)