How to combine two tables so the new one to have only new records from the first and all the rest from the second? - google-bigquery

I have table A with 1 million records and a table B with 10 million records. Table A can have new records not present in table B. How I can combine the two tables so the table C to have all the records from A and all the records from B but with updated records from A. Both tables A and B have a column ID by which both can be joined.

You have to run a query and follow these instructions.
I would construct the query using the Join clause. For example:
SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
FROM Orders
INNER JOIN Customers ON Orders.CustomerID=Customers.CustomerID;
Find a detailed explanation for the different types of Joins in BigQuery here.

Related

How to return no duplicates for records that have many-to-many values

I am trying to find the correct join construction to join together the relevant customer info from the Rentals table with the Accidents table. I often run into this issue where my joining fields aren't unique but not sure what else to join on. The accidents table only has about 1500 records but when I join it to pull in more customer data, I get like 35k records. I can do some joins but frequently have joins like this at work and I feel like a dummy because I am not sure how to troubleshoot...
SELECT a.*,
r.market,
r.date_of_birth,
r.is_blocked,
r.rate_type
FROM `accidents` a
LEFT JOIN `rentals` r -- I also tried an INNER JOIN
USING (customer_id) -- Other fields I tried to match on: Full Name
ORDER BY accident_dt DESC

SQL Join two tables connected by a third cross table

I have two tables, one table bill, and one table log.
Each bill has one log. Therefore there is a third table bill_log with two columns bill_id and log_id.
I want to select all columns from bill and logs in one statement with
bill_log.bill_id=bill.id and bill_log.log_id=log.id
I tried to join these tables, but it didn't work. Can anyone help?
select bill.*,log.* from bill
inner join bill_log on bill_log.bill_id=bill.id
inner join log on bill_log.log_id=log.id

how to join one table to multiple other tables

i have a query which is used to generate reports. There are multiple fields to be displayed. One requirement is such that i need to join one table to different tables with different aliases for data. e.g., table 1 employee id with employee table for knowing the full name. similarly table 2 employee id with employee table for table 2 employee id full name. PFB the query:
select * from office o
left join employee e
on e.id=o.id
left join master m
on m.id=o.id
left join student s1
on e.id=s1.id
left join student s2
on m.id=s2.id
Can we optimize this query to use only one join statement of student table instead of multiple table join statement? I need to reduce the number of tables used in the query since i'm getting the error as too many tables in the query maximum allowed is 50. Please help. Appreciate.
Can we optimize this query to use only one join statement of student
table instead of multiple table join statement?
No, I would not call it query optimization. However, for reporting purposes you reduce the joins by creating views.
http://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.infocenter.dc00801.1510/html/iqrefso/X315714.htm

Joining two tables to update results in third table using like

I am trying to join two tables and update the results in the third table.
So table A is the results table and it has the columns customer number and score.
Table B has customer number and ind_code and table C has ind_code and ind_score.
So the output of the query should be such that the ind_code in table B and C should join together based on the first two digits and ind_score should be updated in Table A in the score column. Table A and Table B should be joined on the basis of customer number.
Could anyone please help. I tried multiple queries but nothing seems to work. i am using oracle sql developer
Generally, the JOIN operation mustn't cut field information but if your structure (for me not correct) is that ...
If I understand better:
UPDATE TableA
SET score =
(SELECT MAX(C.ind_score)
FROM TableC C
JOIN TableB B
ON C.ind_code = SUBSTRING(B.ind_code, 1, 2)
WHERE B.customernumber = TableA.customernumber)
I use on subquery MAX aggregate function, because I don't know if your cut of ind_code of TableB can be not unique (i.e. you have ind_code 5555 and 5554)

SQL queries with different results

I have two tables that I try to join over one field and it gives me different results in two queries that should give same results. Queries are:
SELECT * FROM tblCustomer tca
WHERE tca.PhoneNumber IN(
SELECT ts.SubscriptionNumber FROM sub.tblSubscription ts
WHERE ts.ServiceTypeID=4
AND ts.SourceID=-1
)
and
SELECT tca.*
FROM sub.tblSubscription ts
inner JOIN tblCustomer tca
ON ts.SubscriptionNumber = tca.PhoneNumber
WHERE ts.ServiceTypeID = 4
AND ts.SourceID = -1
How is this possible?
I'm assuming a customer can have multiple subscriptions, right? Let's assume you have 5 customers, each with 2 subscriptions...
When doing a SELECT ... FROM Customer WHERE IN (Subscription), you will receive 5 customer records, because each of those 5 customers are in fact in the subscription table, even though the subscription table will have 10 records. You are inherently asking the database for the data from one table, where the value of one of it's fields exists in another table. So it will only return the distinct records in the FROM table, irrespective of the amount of data in the WHERE IN table.
On the other hand, INNER JOINing the Customer table with the subscription table will return 5 customers x 2 subscriptions each = 10 records. By JOINing the tables you are asking the database for ALL the data in each table, where the data is matched up against specific fields.
So yes, the 2 queries will definitely give you different results.