I have two tables as follows
TABLE_1
PERSON_ID
LDTS
45
2022-03-03 15:41:05.685
72
2022-03-03 15:42:08.203
15
2022-06-08 21:57:07.909
36
2022-06-28 21:58:43.558
TABLE_2
PERSON_ID
LDTS
CURRENCY
34
2022-03-03 15:00:21.814
US
28
2022-03-03 15:02:05.963
CA
52
2022-03-03 15:02:05.963
US
10
2022-06-08 14:40:13.762
US
11
2022-06-08 14:40:13.762
CA
19
2022-06-14 16:10:19.005
US
I am trying to join these tables and order by timestamp in order to get a result such as
PERSON_ID
TABLE_1.LDTS
TABLE_2.LDTS
CURRENCY
34
NULL
2022-03-03 15:00:21.814
US
28
NULL
2022-03-03 15:02:05.963
CA
52
NULL
2022-03-03 15:02:05.963
US
45
2022-03-03 15:41:05.685
NULL
NULL
72
2022-03-03 15:42:08.203
NULL
NULL
10
NULL
2022-06-08 14:40:13.762
US
11
NULL
2022-06-08 14:40:13.762
CA
15
2022-06-08 21:57:07.909
NULL
NULL
19
NULL
2022-06-14 16:10:19.005
US
36
2022-06-28 21:58:43.558
NULL
NULL
Would this just be a left join on LDTS? I am not sure how to get the resulting table such that the timestamps are ordered in this way and the columns that are not shared contain nulls if their values are not in the other table. When I try to do a full outer join, it looks like rows are duplicated for LDTS and LDTS becomes a singular column while the values for the other columns are all null. Thanks!
Getting the rows where the key in the other table is null reciprocally could be handled as a set-based issue:
select T1.PERSON_ID, T1.LDTS as T1_LDTS, T2.LDTS as T2_LDTS, CURRENCY from TABLE_1 T1 left join TABLE_2 T2 on T1.LDTS = T2.LDTS
union
select T2.PERSON_ID, T1.LDTS as T1_LDTS, T2.LDTS as T2_LDTS, CURRENCY from TABLE_2 T2 left join TABLE_1 T1 on T1.LDTS = T2.LDTS
order by nvl(T1_LDTS, T2_LDTS)
;
In response to the question in the comments, if TABLE_2 does not have a PERSON_ID column, then simply specify that it's null:
select T1.PERSON_ID, T1.LDTS as T1_LDTS, T2.LDTS as T2_LDTS, CURRENCY from TABLE_1 T1 left join TABLE_2 T2 on T1.LDTS = T2.LDTS
union
select NULL as PERSON_ID, T1.LDTS as T1_LDTS, T2.LDTS as T2_LDTS, CURRENCY from TABLE_2 T2 left join TABLE_1 T1 on T1.LDTS = T2.LDTS
order by nvl(T1_LDTS, T2_LDTS)
;
Another one - similar to #Greg -
with cte(person_id, ldts) as
(select person_id,ldts from table_1
union all
select person_id,ldts from table_2)
select t3.person_id, t1.ldts, t2.ldts, t2.currency from
cte t3 left join table_1 t1
on t3.person_id = t1.person_id
left join table_2 t2
on t3.person_id = t2.person_id
order by t3.ldts;
PERSON_ID
LDTS
LDTS
CURRENCY
34
NULL
2022-03-03 15:00:21.814
US
28
NULL
2022-03-03 15:02:05.963
CA
52
NULL
2022-03-03 15:02:05.963
US
45
2022-03-03 15:41:05.685
NULL
NULL
72
2022-03-03 15:42:08.203
NULL
NULL
10
NULL
2022-06-08 14:40:13.762
US
11
NULL
2022-06-08 14:40:13.762
CA
15
2022-06-08 21:57:07.909
NULL
NULL
19
NULL
2022-06-14 16:10:19.005
US
36
2022-06-28 21:58:43.558
NULL
NULL
Or were you trying to do something like this
SELECT COALESCE(T1.PERSON_ID,T2.PERSON_ID),
COALESCE(T1.LDTS,T2.LDTS) AS T1_T2_LDTS,
CURRENCY
FROM TABLE_1 AS T1
FULL OUTER JOIN TABLE_2 AS T2
ON T1.PERSON_ID = T2.PERSON_ID
ORDER BY T1_T2_LDTS;
IF you really wanted in the format you posted, you can also do this
SELECT COALESCE(T1.PERSON_ID,T2.PERSON_ID),
T1.LDTS,T2.LDTS ,
CURRENCY
FROM TABLE_1 AS T1
FULL OUTER JOIN TABLE_2 AS T2
ON T1.PERSON_ID = T2.PERSON_ID
ORDER BY COALESCE(T1.LDTS,T2.LDTS);
Related
Scenario:
Have got two bigquery tables with same columns. Have to compare these two tables w.r.t. Category and Article,
i) if same present in table_2, have to fetch 'Flow' column from table_2
ii) otherwise, retain the same Flow column from Table_1 itself.
Table_1:
Category Article Flow
AA 11 Apple
AA 12 Orange
BB 13 Lemon
CC 14
Table_2:
Category Article Flow
AA 11 Melon
BB 13 Pine
Resultant Table:
Category Article Flow
AA 11 Melon
AA 12 Orange
BB 13 Pine
CC 14
Tried_Out Query:
select t1.Category, t1.Article, t2.Flow
from t1 left join t2
on t1.Category=t2.Category and t1.Article=t2.Article
Help me resolve this issue. Thanks in Advance!
Try left join:
with table_1 as (
select 'AA' as category, 11 as article, 'Apple' as flow UNION ALL
select 'AA', 12, 'Orange' UNION ALL
select 'BB', 13, 'Lemon' UNION ALL
select 'CC', 14, null
),
table_2 as (
select 'AA' as category, 11 as article, 'Melon' as flow UNION ALL
select 'BB', 13, 'Pine'
)
select
table_1.category,
table_1.article,
ifnull(table_2.flow, table_1.flow) as flow
from table_1 left join table_2 using(category, article)
Let's say I have
table 1:
ID
Date
1
july 10
2
aug 4
3
feb 20
table 2:
ID
Date
Name
Address
1
july 10
joe
123 Howard way
2
aug 4
kate
456 king ave
3
feb 20
lisa
789 giuldford way
4
march 1
jake
145 smith street
5
dec 16
robert
6784 apple street
I want the final table to pull all columns from table 2 but only the rows that have the same ID number and Date as table 1 therefore:
final table:
ID
Date
Name
Address
1
july 10
joe
123 Howard way
2
aug 4
kate
456 king ave
3
feb 20
lisa
789 giuldford way
How would I do this?
I tried using an INNER JOIN and ON with a WHERE clause and that didn't work out. I received duplicates of everything. Also tried a subquery as well. Please help. I am using Standard SQL
Simple JOIN should work!
SELECT t2.*
FROM t1
JOIN t2
USING (ID, Date)
In case if Table 1 really has just those two columns that are part of Using clause - you can use also below (simply * instead of t2.*)
SELECT *
FROM t1
JOIN t2
USING (ID, Date)
Try with Left Join with below query:
SELECT t1.Id,t1.Date,t2.Name,t2.Address
FROM Table1 t1
LEFT JOIN TABLE2 t2 ON t2.Id=t1.Id AND t2.Date=t1.Date
Try RIGHT JOIN with query below
SELECT t2.* FROM t2
RIGHT JOIN t1 USING (ID, Date)
my starting table looks similar to the following
Person 1, 75
Person 2, 48
Person 3,
Person 4, 82
Person 5,
Person 6, 93
...
I now try to include in following query a "where" statement to exclude entities that have no numeric value. This is what I currently have to show me the lowest 5 values of the set above and it works so far
=QUERY('DPS Transpose Tables'!D1:E29;"select D, max(E) group by D order by max(E) asc limit 5 label max(E) ''";0)
How can I add something like this that works
=QUERY('DPS Transpose Tables'!D1:E29;"select D where (E<>"" OR Is not NULL), max(E) group by D order by max(E) asc limit 5 label max(E) ''";0)
Thanks a million in advance!
Try 'where E >=0' like this:
=QUERY('DPS Transpose Tables'!D1:E29,"select D, max(E) where E >=0 group by D order by max(E) asc limit 5 label max(E) ''",0)
You can also try 'WHERE IS NOT NULL'
Sometimes I find using the >= 0 does not get the desired results
=QUERY('DPS Transpose Tables'!D1:E29,"select D, max(E) where E IS NOT NULL group by D order by max(E) asc limit 5 label max(E) ''",0)
Use NOT Equal To and '' empty string with single '' combination
Where E <> ''
I have two tables that look like this
Date ID Date ID Value
2017-01-01 1 2017-01-01 1 10.0
2017-01-01 2 2017-01-01 2 15.0
2017-01-01 3 2017-01-02 3 20.0
2017-01-01 4 2017-01-02 4 50.0
2017-01-02 1
2017-01-02 2
2017-01-02 3
2017-01-02 4
I want to join the in a way to get
Date ID Value
2017-01-01 1 10.0
2017-01-01 2 15.0
2017-01-01 3 NULL
2017-01-01 4 NULL
2017-01-02 1 NULL
2017-01-02 2 NULL
2017-01-02 3 20.0
2017-01-02 4 50.0
I tried left joining T2 on T1 by using ID and Date which results always just in only the records that matched. If I only join on ID I get multiple entries (each Value) for each date.
SELECT
t1.Date,
t1.ID,
t2.Value
FROM table1 t1
left join table2 t2 using (Date,ID)
Here's another way of phrasing it:
SELECT
t1.Date,
t1.ID,
t2.Value
FROM
table1 t1
LEFT OUTER JOIN table2 t2 ON
t1.Date = t2.Date
AND t1.ID = t2.ID
How to convert columns of one row into one row each?
Having data in table as below:
+------+------+------+------+
| col1 | col2 | col3 | col4 |
+------+------+------+------+
| a1 | a2 | a3 | a4 |
+------+------+------+------+
required output: all values in the row should go in single column as below.
+---------------+
| All_cols |
+---------------+
| a1,a2,a3,a4 |
+---------------+
Like in image: Here mytable always have one record, so I need the output as below.
want to avoid below sql as it requires multiple joins:
select a1.name from accounts a1 where a1.id='658f3b73-8260-5a7a-ae7e-54c25deded36'
union
Select a2.name from accounts a1
left join accounts a2 on a1.id=a2.parent_id where a1.id='658f3b73-8260-5a7a-ae7e-54c25deded36'
union
Select a3.name from accounts a1
left join accounts a2 on a1.id=a2.parent_id
left join accounts a3 on a2.id=a3.parent_id where a1.id='658f3b73-8260-5a7a-ae7e-54c25deded36'
union
Select a4.name from accounts a1
left join accounts a2 on a1.id=a2.parent_id
left join accounts a3 on a2.id=a3.parent_id
left join accounts a4 on a3.id=a4.parent_id where a1.id='658f3b73-8260-5a7a-ae7e-54c25deded36'
union
Select a5.name from accounts a1
left join accounts a2 on a1.id=a2.parent_id
left join accounts a3 on a2.id=a3.parent_id
left join accounts a4 on a3.id=a4.parent_id
left join accounts a5 on a4.id=a5.parent_id where a1.id='658f3b73-8260-5a7a-ae7e-54c25deded36'
You could make a UNION on four single calls
SELECT col1 FROM mytable
UNION
SELECT col2 FROM mytable
UNION
SELECT col3 FROM mytable
UNION
SELECT col4 FROM mytable
will output
You say that
mytable always have one record.
Be aware that if you have more than one record, it's wise to use the same WHERE condition on each SELECT statement above.