How to select records of a table with last related record on another table in T-sql - sql

I have two tables and i want to use some records from first table and get last related record from another one.
You can see my tables
I want to join table 1 with last record of table 2. (creationDate = 2018-07-20)

If you simply want to get the latest record in table 2 for every ID in table one then this will work:
select t1.ID, t1.Name, q.ID, q.CreationDate
from table1 t1
outer apply
(
select top 1 t2.ID, t2.CreationDate
from table2 t2
where t2.tbl_1_Id = t1.ID
order by t2.CreationDate desc
)q

Related

Group using data from one query into another

I have a table that looks like below. It is created using a query -
NPI Other_Columns
123 Several_Other_Columns
456 Several_Other_Columns
How do I take every NPI from this table and get a count of the number of times they appeared in another table? The structure of the other table is like so -
Claim_id NPI1 NPI2 NPI3 NPI4 NPI5 NPI6 NPI7 NPI8
If NPIs in the first table, show in any field in the second table, we want to count that claim.s
The first task is the join
SELECT
t1.npi,
t1.other_columns,
t2.claim_id
FROM table1 as t1
JOIN table2 as t2 ON t1.npi in (t2.np1,t2.np2,t2.np3,t2.np4,t2.np5,t2.np6,t2.np7,t2.np8)
that gets you all the things joined.
Now count those..
SELECT
count(t2.claim_id)
FROM table1 as t1
JOIN table2 as t2 ON t1.npi in (t2.np1,t2.np2,t2.np3,t2.np4,t2.np5,t2.np6,t2.np7,t2.np8)

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);

Sql query with join on table with ID not match

I have two tables.
Table 1
Id
UpdateId
Name
Table 2
Table1ID
UpdateID
Address
Each time user update, system will insert record to table1. But for table2, system only insert record when there is update in address.
Sample data
Table 1
1,1,name1
1,2,name1
1,3,name1update
1,4,name1update
1,5,name1
1,6,name2
Table 2
1,1,address
1,4,addressupdate
I want to get the result as following
1,1,name1,address
1,2,name1,address
1,3,name1update,address
1,4,name1update,addressupdate
1,5,name1,addressupdate
1,6,name2,addressupdate
How to make use of join condition to achieve as above?
You can use a correlated subquery. Here is standard syntax, but it can be easily adapted to any database:
select t1.*,
(select t2.addressid
from table2 t2
where t2.table1id = t1.id and
t2.updateid <= t1.updateid
order by t2.updateid desc
fetch first 1 row only
) as addressid
from table1 t1;
you can use left join when you want to take all columns from left table t1 even though it doesn't match with the other table with column updateid on t2 table.
select t1.id,t1.updateid,t1.name,t2.address from table1 t1
left join table2 t2
on t2.updateid= t1.updateid
you can read more about joins here

Comparing base table value with second table's sum of value with group by

I have two tables:
One is base table and second is transaction table. I want to compare base table value with second table's sum of value with group by.
Table1(T1Id,Amount1,...)
Tabe2(T2Id,T1ID,Amount2)
I want those rows from table 1 WHere SUM of Table2's SUM( Amount2) is greater or equal table1's Amount1.
*T1ID is in relation with both tables
* The SQL query have many joins with other table for data retriving.
One approach uses a join:
SELECT t1.T1Id, t1.Amount1
FROM Table1 t1
INNER JOIN Table2 t2
ON t1.T1Id = t2.T1ID
GROUP BY
t1.T1Id, t1.Amount1
HAVING
SUM(t2.Amount2) >= t1.Amount1;
We can also try doing this via a correlated subquery:
SELECT t1.T1Id, t1.Amount1
FROM Table1 t1
WHERE t1.Amount1 <= (SELECT SUM(t2.Amount2) FROM Table2 t2
WHERE t1.T1Id = t2.T1ID);
I would use something similar to the query below:
SELECT
a.T1Id, a.Amount1, SUM(b.Amount2)
FROM Table1 a
INNER JOIN Table2 b on b.T1Id = a.T1Id
GROUP BY a.T1Id, a.Amount1
HAVING SUM(b.Amount2) >= a.Amount1;
Basically what the query above does is give you the ID, Amount from table 1 and the summed amount from table 2. The HAVING clause at the end of query filters out those records where the summed amount from the second table is smaller than the amount from the first one.
If you want to add further table joins to the query, you can do so by adding as many joins as you wish. I would recommend having a referenced ID for each table you are joining in the Table1 table.

SQL - Update last occurrence with select in clause

In Oracle, I would like do an update on a table for the last occurrence based on select in list, something like :
UPDATE table t1
set t1.fieldA = 0
where t1.id in (
select t2.id, max(t2.TIMESTAMP)
from table t2
where t2.id in (1111,2222,33333)
group by t2.id
);
This query does not works, I received an error "too many values".
Any ideas?
Thanks
Your subselect has 2(too many) fields so it cant't be compared with the id.
compare the timestamp with the max timestamp.
UPDATE table t1 set t1.fieldA = 0
where t1.TIMESTAMP = (select max(t2.TIMESTAMP) from table t2 where t2.id = t1.id )
and id in (1111,2222,33333)
I believe oracle supports multiple columns IN, so you could try:
UPDATE table t1
set t1.fieldA = 0
where
(t1.id,t1.timestamp) in ( select t2.id, max(t2.TIMESTAMP) from table t2 where t2.id in (1111,2222,33333) group by t2.id );
Essentially if you're providing a subquery to the right side of the IN that has a result set with two columns, then you have to provide the names of both the columns in brackets on the left side of the IN
Incidentally, I've never liked updating based on a max value if the requirement is that only one row be updated, as two rows with the same max value will both be updated. This kind of query is better:
Update table
Set fieldA=0
Where rowid in(
select rowid from (
select rowid, row_number() over(partition by id order by timestamp desc
) as rown from table
) where rown=1)
If, however, your primary key on the table includes the time stamp as part of the compound, then there is no concern of here being two rows with the same ID/timestamp.. it's just rarely the case!