I need help joining these two datasets to produce the results on this exercise - sql

I'm trying to join table 1 and table 2 to create table 3 with using SQL. How would I be able to do this?

you need a join, count and group by
select a.region, b.partnerType, count(*)
from table1 a
inner join table2 b on a.DistrectID = b.DistrectID
group by a.region, b.partnerType

Related

Getting column names of a query in Teradata

I'm trying to get only columns names of a query result without the need of running all the query.
for example, if table a columns are: id, price, txn_date,city_id and table b columns are: city_id,city_name, country
I want a query that will output only column names of the following query:
Select a.*,b.* from a left join b on a.city_id=b.city_id
without using CPU to compute all the results.
desired output:
id, price, txn_date, city_id, city_name, country
try add condiniion ever false and use an inner join
Select a.*,b.* from a inner join b on a.city_id=b.city_id and 1= 2
this should return a result without rows
or dorectly
Select a.*,b.* from a inner join b on 1= 2

Delete records from Postgresql

I need to delete records from one table based on inner join condition on other two tables, however query runs for ages.
DELETE FROM public.blacklist WHERE subject_id NOT IN(
SELECT DISTINCT a.id FROM public.subject a
INNER JOIN stg.blacklist_init b ON a.subject_id=b.customer_code);
Any ideas how to achieve this?
Thank you.
You can use NOT EXISTS instead of NOT IN, and I think you don't need a DISTINCT
DELETE FROM public.blacklist bl
WHERE NOT EXISTS (
SELECT 0
FROM public.subject a
INNER JOIN stg.blacklist_init b
ON a.subject_id=b.customer_code
WHERE a.id = bl.subject_id
);

How to take distinct values in hive join

I need to take the distinct values from Table 2 while joining with Table 1 in Hive. Because the table 2 has duplicate records.
Considering below join condition is it possible to take only distinct key_col from table 2? i dont want to use select distinct * from ...
select * from Table_1 a left join Table_2 b on a.key_col = b.key_col
Note: This is in Hive
Use Left semi join. This will give you all the record in table1 which exist in table2(duplicate record) without duplicates.
select a.* from Table_1 a left semi join Table_2 b on a.key_col = b.key_col

SQL join including all rows from one table irrespective of how many are represented in the other

I have two tables:
I want to output the following:
I tried this statement:
SELECT TableA.bu_code
, SUM(TableB.count_invalid_date) AS TotalInvDate
FROM TableA
LEFT JOIN TableB ON TableA.bu_code = TableB.bu_code
GROUP BY TableA.bu_code
But it doesn't show every row represented in TableA, instead it does this:
Is there a single SQL statement that can output what I want?
You could use a left join after performing the group by:
SELECT a.bu_code, COALESCE (TotalInvDate, 0)
FROM TableA a
LEFT JOIN (SELECT bu_code, SUM(count_invalid_date) AS TotalInvDate
FROM TableB
GROUP BY bu_code) b ON a.bu_code = b.bu_code
There may be orphaned TableB rows without a parent row in TableA.
If so, use this GROUP BY syntax to see them.
GROUP BY ROLLUP(TableA.bu_code)
Refer to this Microsoft SQL-Server page on the GROUP BY clause for more details on the ROLLUP option.
SELECT A.bu_code AS [bu_code],
ISNULL(sum( B.count_invalid_date),0) AS [TotalInvDate]
FROM
TableA A
left JOIN
TableB B
ON
A.bu_code=B.bu_code
GROUP BY
A.bu_code

MySQL: Multi-column join on several tables

I have several tables that I am joining that I need to add another table to and I can't seem to get the right query. Here is what I have now -
Table 1
carid, catid, makeid, modelid, caryear
Table 2
makeid, makename
Table 3
modelid, modelname
Table 4
catid, catname
The query I am using to join these is:
SELECT * FROM table1 a
JOIN table2 b on a.makeid=b.makeid
JOIN table3 c on a.modelid=c.modelid
JOIN table4 d on a.catid=d.catid
WHERE a.carid = $carid;
Now I need to add a 5th table that I am getting from a 3rd party that I am having a hard time adding to my existing query. The new table has these fields -
Table 5
id, year, make, model, citympg, hwympg
I need the citympg and hwympg based on caryear from table 1, makename from table 2, and modelname from table 3. I know I can do a second query with those values, but I would prefer to do a single query and have all of the data in a single row. Can this be done in a single query? If so, how?
it's possible to have more than condition in a join.
does this work?
SELECT a.*, e.citympg, e.hwympg
FROM table1 a
JOIN table2 b on a.makeid=b.makeid
JOIN table3 c on a.modelid=c.modelid
JOIN table4 d on a.catid=d.catid
Join table5 e on b.makename = e.make
and c.modelname = e.model
and a.caryear = e.year
WHERE a.carid = $carid;
...though your question is not clear. Did you only want to join table5 to the others, or was there something else you wanted to do with table5?
Without indexes, It won't be efficient, but you can do
LEFT JOIN table5 ON (table2.make = table5.make AND table3.model = table5.model AND table1.caryear = table5.caryear)
This also assumes the make and models and years strings match exactly.