How to except columns in join query? - sql

I'm using clickhouse-server.
I have 2 tables
CREATE TABLE table1
(
pid Int64,
timestamp DateTime,
data1 Float32,
data2 Float32
)
ENGINE = MergeTree
PRIMARY KEY (timestamp);
CREATE TABLE table2
(
pid Int64,
timestamp DateTime,
data3 Float32,
data4 Float32
)
ENGINE = MergeTree
PRIMARY KEY (timestamp);
pid and timestamp matches in these tables
I wish to select everything from both tables where timestamp matches.
Here is a query I came up with:
select * from table1 LEFT JOIN table2 ON table1.timestamp = table2.timestamp
It works but selects pid and timestamp twice.
How to exclude unneeded pid and timestamp from table2?
I've tried to EXCEPT timestamp but it seems to be needed for join.
Have to notice that it is simplified example. Real tables have much more columns.

SELECT * EXCEPT (`table2.pid`, `table2.timestamp`)
FROM
(
SELECT *
FROM table1
LEFT JOIN table2 ON table1.timestamp = table2.timestamp
)

I've ended up with this, without second query:
SELECT * EXCEPT `pid` FROM `table1` LEFT JOIN `table2` USING (`pid`,`timestamp`)

Related

New table from two table with max(timestamp) - Bigquery SQL

I have two tables where the combination of retailer and id are the one common between the two. I need to create a new table for all retailer + id combination from the first table and respective data for those from the second table that has the latest timestamp
The first table will have only one record for each retailer, id combination but the second table will have multiple records for each retailer, id combination based on the time it was scraped, I need to create a new table with the latest timestamp data for each combination
input table 1:
input table 2:
output table:
This is basically aggregation and join:
select *
from table1 t1 left join
(select t2.retailer, max(timestamp) as max_timestamp
from table2 t2
group by t2.retailer
) t2
on using (retailer);
If you wanted the entire most recent row, you can use a variant of this:
select *
from table1 t1 left join
(select ( array_agg(t2 order by timestamp desc limit 1) )[safe_ordinal(1)].*
from table2 t2
group by t2.retailer
) t2
on using (retailer);

left join where the primary key has both integer and string

I need to join two tables where the primary keys have both integer and string value. When I use left join using id column as primary key I am getting the records related to the integer only.I would like to get the output for both integer and string as shown in the output. Can anyone assist please.
select t1.*
,t2.Position
from t1
left join t2
on t1.id=t2.id;
I am getting the output which related to id (integer) only.I would like to get the output for both integer id and string id.
how to use UNION DISTINCT here – Nrad
SELECT id FROM table1
UNION DISTINCT
SELECT id FROM table2
or
SELECT CAST(id AS CHAR) AS id FROM table2
UNION DISTINCT
SELECT id FROM table1
DISTINCT keyword is optional and may be skipped in both cases.
If you need the ordering shown then add
ORDER BY id + 0 = 0, id
to the end of a query.
i have some other different columns in both table that I need to take. – Nrad
SELECT id,
COALESCE (t1.name, t2.name) name,
t1.salary,
t2.position,
t1.department
FROM ( SELECT id FROM table1
UNION DISTINCT
SELECT id FROM table2 ) t0
LEFT JOIN table1 t1 USING (id)
LEFT JOIN table2 t2 USING (id)

is there linear interpolation sql query?

I have two tables. Table1 has data recorded at 10 sec intervals and the data in Table2 was recorded at 1 or 2 sec intervals. I want to join these two tables in a way that it will select the whole data from Table1 joined with Table2 where the recording time matches or the recording time in Table two is near to the recording time in Table1.
For example, one row in Table1 was recorded at 21:11:20. This row should be joined with a row in Table2 recorded at 21:11:20 if it exists, otherwise, selects the nearest row, let's say a row at 21:11:19.
Thank you.
Table1
Table2
you could try:
select t1.* from table1 t1 inner join table2 t2 on date_trunc('sec',t1.val) = t2.val;
Databases that allow for function indexes will do slightly better with this query than those without.
It takes a little effort, but it is clearly doable (here: SQL-Server):
-- set upt a test environment with two tables:
create table tbl1 ( i1 int identity primary key, t1 time, v1 float);
create table tbl10 (i10 int identity primary key, t10 time, v10 float);
-- fill them with some test values:
insert into tbl1 (t1,v1) VALUES ('12:00:06',1),('12:00:07',2),('12:00:08',3),('12:00:09',3),('12:00:10',2),('12:00:11',1),('12:00:12',0);
insert into tbl10 (t10,v10) VALUES ('12:00:00',99),('12:00:10',100),('12:00:20',98),('12:00:30',110);
-- and "join" them with interpolation
WITH t10s AS (
SELECT i10, t10, v10, DATEDIFF(second,0,t10) s10 FROM tbl10
)
SELECT t1,v1, v10a*f+v10b*(1.-f) v10int FROM (
SELECT t1, v1, CAST(b-DATEDIFF(second,0.,t1) AS FLOAT)/(b-a) f, ta.v10 v10a, tb.v10 v10b
FROM (
select t1, v1,
(SELECT max(s10) FROM t10s WHERE t10<=t1) a,
(SELECT min(s10) FROM t10s WHERE t10> t1) b
FROM tbl1
) tmp1
INNER JOIN t10s ta ON ta.s10=a
INNER JOIN t10s tb ON tb.s10=b
) tmp2
-- output:
t1 v1 v10int
12:00:06 1 99.6
12:00:07 2 99.7
12:00:08 3 99.8
12:00:09 3 99.9
12:00:10 2 100.0
12:00:11 1 99.8
12:00:12 0 99.6
see the little demo here: https://rextester.com/PQDH85753
In my rudimentary script I had the situation of always having t10 values greater or smaller than the t1 values. To protect against "getting out of range" you could use a COALESCE() function with a default value that applies outside the range.

Combining two tables without losing column or rows

I have two tables:
The first one has the colums "SomeValue" and "Timestamp". The other one has the columns "SomeOtherValue" and also "Timestamp".
What I need as an output is the following:
A table with the three Colums "SomeValue", "SomeOtherValue" and "Timestamp".
When a row in table 1 is like this: [2; 04/07/2017-20:05] and a row in table 2 is like that: [5; 04/07/2017-20:05], I want the combined output row to be [2; 5; 04/07/2017-20:05].
Until that point it would be easy done with a simple join, but I also need all other rows. So for example if we have a row in table 1 like [2; 04/07/2017-20:05] and no matching timestamp in table 2, the output should be like [2; ?; 04/07/2017-20:05]. The '?' stands for undefined or null. It would also be possible to not join two rows with the same timestamp but rather concating both tables, so that every row would have one empty cell with '?'.
I do realize that I didn't use correct Date/Time Format here in that example, but assume that it is used in the database.
I already tried using UNION ALL but it always removes one column.
For my use case it is not possible to query both tables independently. I really need both values in one row/object.
I hope someone can help me with this. Thank you!
What you are describing is a full outer join:
select t1.somevalue, t2.someothervalue, timestamp
from t1
full outer join t2 using (timestamp);
I don't know, however, whether SAP HANA supports the USING clause. Here is the same query with ON instead:
select
t1.somevalue,
t2.someothervalue,
coalesce(t1.timestamp, t2.timestamp) as timestamp
from t1
full outer join t2 on t2.timestamp = t1.timestamp;
Joining on a datetime stamp is not always going to be reliable unless you are setting a datetime variable and writing the value of that to both tables. It's probably not very efficient either.
That said, assuming you want all the results from table 1 and matching table 2 result if it exists then you need a left outer join
Select T1.[SomeValue]
, ISNULL(T2.[SomeOtherValue], '?')
, T1.[TimeStamp]
FROM Table1 T1
LEFT OUTER JOIN Table2 T2
ON T2.[TimeStamp] = T1.[TimeStamp]
Update based on comment from OP
If you need all rows from both tables then you could either do 2 queries as above but interchange T1 and T2 position then union the 2 queries.
SELECT T1.[TimeStamp]
, T1.[SomeValue]
, ISNULL(T2.[SomeOtherValue], '?')
FROM Table1 T1
LEFT OUTER JOIN Table2 T2
ON T2.[TimeStamp] = T2.[TimeStamp]
UNION
SELECT T2.[TimeStamp]
, T2.[SomeValue]
, ISNULL(T1.[SomeOtherValue], '?')
FROM Table2 T2
LEFT OUTER JOIN Table1 T1
ON T1.[TimeStamp] = T2.[TimeStamp]
;
Or you could insert the 1st query results into a table variable then add any missing from T2 rows into that table variable using a where not exists, then select the output.
DECLARE #TempTab TABLE
( [TimeStamp] [datetime] NOT NULL
, [SomeValue] [nvarchar] (MAX) -- or int if this is always an integer
, [SomeOtherValue] [nvarchar] (MAX) -- or int if this is always an integer
)
;
INSERT INTO #TempTab
( [TimeStamp]
, [SomeValue]
, [SomeOtherValue]
)
SELECT T1.[TimeStamp]
, T1.[SomeValue]
, ISNULL(T2.[SomeOtherValue], '?')
FROM Table1 T1
LEFT OUTER JOIN Table2 T2
ON T2.[TimeStamp] = T2.[TimeStamp]
;
INSERT INTO #TempTab
( [TimeStamp]
, [SomeValue]
, [SomeOtherValue]
)
SELECT T2.[TimeStamp]
, T2.[SomeValue]
, ISNULL(T1.[SomeOtherValue], '?')
FROM Table2 T2
LEFT OUTER JOIN Table1 T1
ON T1.[TimeStamp] = T2.[TimeStamp]
WHERE NOT EXISTS
( SELECT 1
FROM #TempTab T
WHERE T.[TimeStamp] = T2.[TimeStamp]
)
;
SELECT T.[TimeStamp]
, T.[SomeValue]
, T.[SomeOtherValue]
FROM #TempTab T
;

Getting the value upon match from the ref table

I have 2 tables
Select distinct
ID
,ValueA
,Place (How to get the Place value from the table 2 based on the Match between 2 columns ValueA and ValueB
Here Table2 is just a ref Table I''m using)
,Getdate() as time
Into #Temp
From Table1
For example when we receive value aa in ValueA column - I want the value of "Place" = "LA"
For example when we receive value bb in ValueA column - I want the value of "Place" = "TN"
Thanks in advance!
SELECT A.ID
, A.ValueA
, B.Place
, GETDATE() INTO #TempTable
FROM Table1 A INNER JOIN Table2 B
ON A.ValueA = B.ValueB
You can do this dude:
Select ID, ValueA, Place, getdate() as Date FROM Table1 INNER JOIN Table2 on Table1.ValueA = table2.ValueB.
Hope this works dude!!!
Regards...
Select
t1.ID
,t1.ValueA
,t2.Place
,Getdate() as time
Into #Temp
From Table1 t1
inner join Table2 t2 on t1.ValueA = t2.ValueB
I believe you are looking to do an inner join:
SELECT Table1.ID, Table1.ValueA, Table2.Place
FROM Table1
INNER JOIN Table2 ON Table1.ValueA = Table2.ValueB
Assumption: ValueB on table 2 is the primary key (or at least UNIQUE and therefore a candidate key).
Also, the DISTINCT is redundant assuming that ID is a primary key. Furthermore, you more than likely do not need a temporary table since a join can be used as an inner SELECT in most databases.
The exact syntax may depend on your particular database engine.