Sum on Multiple Left Joins - sql

I am trying to sum the total value of a column for a specific ID after multiple left joins.
The below code gives me what I am looking for but across multiple rows, I need the value for T3.C_Amt and T4.E_Amt to be totaled.
SELECT
T1.ID,
T2.Unique_ID,
T3.C_Date,
T3.C_Amount,
T4.D_Date,
T4.D_Amount
FROM
TABLE_1 T1
LEFT JOIN DATABASE1.TABLE_2 T2
ON T1.ID = T2.UNIQUE_ID
LEFT JOIN DATABASE1.TABLE_3 T3
ON T2.Unique_ID = T3.Unique_ID
AND T3.C_Date = '2019-04-11'
LEFT JOIN DATABASE1.TABLE_4 T4
ON T2.Unique_ID = T4.Unique_ID
AND T4.D_Date= '2019-04-11'
--this needs to be summed to have the total amount
I want it to return one row for the Unique ID with total C_Amount and total D_Amount for the specific date

Use aggregation with group by
SELECT T2.Unique_ID,T3.C_Date,sum(T3.C_Amount),
T4.D_Date,sum(T4.D_Amount)
FROM TABLE_1 T1
LEFT JOIN DATABASE1.TABLE_2 T2
ON T1.ID = T2.UNIQUE_ID
LEFT JOIN DATABASE1.TABLE_3 T3
ON T2.Unique_ID = T3.Unique_ID AND T3.C_Date = '2019-04-11'
LEFT JOIN DATABASE1.TABLE_4 T4
ON T2.Unique_ID = T4.Unique_ID AND T4.D_Date= '2019-04-11'
group by T2.Unique_ID,T3.C_Date,T4.D_Date

I would do it this way. Since Teradata is a MPP, there should not be much of a performance impact as well.
SELECT Unique_ID,C_Date,sum(C_Amount),D_Date,sum(D_Amount)
FROM
(
SELECT
T1.ID ID,
T2.Unique_ID Unique_ID,
T3.C_Date C_Date,
T3.C_Amount C_Amount,
T4.D_Date D_Date,
T4.D_Amount D_Amount
FROM
TABLE_1 T1
LEFT JOIN DATABASE1.TABLE_2 T2
ON T1.ID = T2.UNIQUE_ID
LEFT JOIN DATABASE1.TABLE_3 T3
ON T2.Unique_ID = T3.Unique_ID
AND T3.C_Date = '2019-04-11'
LEFT JOIN DATABASE1.TABLE_4 T4
ON T2.Unique_ID = T4.Unique_ID
AND T4.D_Date= '2019-04-11'
) ABC
GROUP BY Unique_ID,C_Date,D_Date

I would add a concern of 1-to-many causing a false total. What if table 3 has 10 records for a given T2.UniqueID and another 5 for the T4 table. You have just compounded your total completely out of range.
As such, I would pre-aggregate from the child tables grouped by the unique ID filtered on the date. Also, you can remove the T2 table due to associative properties.
T1.ID = T2.Unique_ID = T3.Unique_ID = T4.Unique_ID
to T1.ID = T3.Unique_ID = T4.Unique_ID
SELECT
T1.ID,
T3.C_Date,
T3.C_Amount,
T4.D_Date,
T4.D_Amount
FROM
TABLE_1 T1
LEFT JOIN
( Select Unique_ID, sum( C_Amount ) as T3Sum
from DATABASE1.TABLE_3
where T3.C_Date = '2019-04-11'
group by Unique_ID ) T3
ON T1.ID = T3.Unique_ID
LEFT JOIN
( select Unique_ID, sum( D_Amount ) T4Sum
from DATABASE1.TABLE_4
where D_Date= '2019-04-11'
group by Unique_ID ) T4
ON T1.ID = T4.Unique_ID
Now, your ambiguity on table names might help being more real-life descriptive. Your summary amounts are based on a single date, but how many records in T1 that are applicable? If you have 5k rows in T1 and only 450 entries total between tables T3 and T4, your result set would still give you all the rows. That being said, you probably dont want the bloat of records that don't have any such details in the secondary sum subqueries. I would add a WHERE clause at the end
WHERE
T3.Unique_ID IS NOT NULL
OR T4.Unique_ID IS NOT NULL

Related

PostgreSQL multiple join

I would like to make multiple joins in one query, I have four tables for that with the following structure:
Table 1 fields, t1: did (int), finished (datetime), userid (int)
Table 2 fields, t2: userid (int), name
Table 3 fields, t3: blid (int), did (int), count (int), btid(int)
Table 4 fields, t4: btid(int), denom (int)
I want to make a query where I would get all fields of t1 except for that one joined with t2 in that case I would like to show t2.name when t1.userid = t2.userid This is fair simple:
select t1.did, t1.finished, t2.name, t2.userid from t1 inner join on t1.userid=t2.userid
But I also want to add a column composed by the following query:
select sum(t3.count*t4.denom) from t3 inner join t4 on t3.btid=t4.btid
Only when t3.did=t1.did I don't know if it would be an outer join or otherwise, but I want to add the previous query as a column in the first query and it would be something like:
t1.did, t1.finished, t2.name, t2.userid, "total"?
The real-world problem is that users make money deposits t1 users information are held in t2. The amount deposited is described in t3 for every deposit t1.did and related denominations of each deposit breakdown, in t3 are described in t4.
You might find this easiest as a correlated subquery:
select t1.did, t1.finished, t2.name, t2.userid,
(select sum(t3.count * t4.denom)
from t3 inner join
t4
on t3.btid = t4.btid
where t3.did = t1.did
) as value
from t1 inner join t2
on t1.userid = t2.userid;
For this version, you want indexes on t3(did, btid, count) and t4(btid, denom).
You can also phrase this as a join and group by:
select t1.did, t1.finished, t2.name, t2.userid,
sum(t3.count * t4.denom)
from t1 inner join t2
on t1.userid = t2.userid left join
t3
on t3.did = t1.did left join
t4
on t3.btid = t4.btid
group by t1.did, t1.finished, t2.name, t2.userid;
You can use group by clause:
select t1.did, t1.finished, t2.name, t2.userid, sum(t3.count*t4.denom)
from t1
inner join t2 on t1.userid=t2.userid
inner join t3 on t1.did = t3.did
inner join t4 on t3.btid = t4.btid
group by t1.did, t1.finished, t2.name, t2.userid

SQL Multiple subquery issue

Currently struggling with a second subquery (t3) which is giving me a syntax error. The query works fine if t3 is excluded.
SELECT switch(LEFT(t1.[treatment],1)='C',"Complaint",LEFT(t1.[treatment],1)='P',"Post") AS Treatment, count(t1.[ID]) AS Total_Vol, count(t2.[event]) AS Total_Posted, format(count(t2.[event]) / count(t1.[ID]),"0.00%") AS Percentage, COUNT(IIF(t1.[requirements]='1',1,)) AS Special _Population,count(t3.[approved]) as Approved_vol
FROM Main_audit_table_v3 AS t1 LEFT JOIN (SELECT t2.[ID], t2.[event] FROM Main _table AS t2 WHERE t2.event Not Like ('NA')) AS t2 ON t1.[ID] = t2.[ID]
LEFT JOIN (SELECT t3.[ref],t3.[requirement],t3.[approved] from Main_table AS t3 where
t3.[requirement] = '1' and t3.[approved] not like ('NA')) as t3
t2.[ID] = t3.[ID]
GROUP BY LEFT(t1.[treatment],1);
The expected output is that the [approved] column will provide a count of records where requirement = 1 and approved not like NA.
In MS Access you need additional parentheses for JOINs. And you are missing ON:
SELECT . . .
FROM (Main_audit_table_v3 AS t1 LEFT JOIN
(SELECT t2.[ID], t2.[event]
FROM Main _table AS t2
WHERE t2.event Not Like ('NA')
) AS t2 ON t1.[ID] = t2.[ID]
) LEFT JOIN
(SELECT t3.[ref], t3.[requirement], t3.[approved]
FROM Main_table AS t3
WHERE t3.[requirement] = '1' and t3.[approved] not like 'NA'
) as t3
ON t2.[ID] = t3.[ID]
GROUP BY LEFT(t1.[treatment], 1);

Joining tbl1 to select statement twice with join to tbl2 that also joins to tbl3

I'm using SQL server manger.
I have 3 tables
I need a query that pulls t1 ands add an Origin Basin and a Destination Basin.
So far I have the following:
select T1.[Country (destination)], T3.AreaName
From T1
left outer join T2 on
T1.[Country (destination)] = T2.CountryName
inner join T3 on
T2.AreaID = T3.AreaID
inner join T3 on
T2.AreaID = T3.AreaID
Which returns:
Country | Area
However, I'm having trouble doing this for the second country column. I believe you use aliases. I've tried:
select (select AreaName
FROM T3
where T3.AreaID = T2.AreaID) as 'Area Imp',
(select AreaID
From T2
where T2.CountryName = T1.[Country (origin)]) as 'x',
(select AreaID
From T2
where T2.CountryName = T1.[Country (destination)]) as 'y'
FROM T1
But I can't get it to work.
This is what you need to do:
select t1.date, t1.country_destination, t1.country_origin, destination_area.AreaName as area_destination, origin_area.AreaName as area_origin
from t1 as t1 join t2 as destination on t1.country_destination = destination.countryname
join t2 as origin on t1.country_origin = origin.countryname
join t3 as destination_area on t2.areaid = destination_area.areaid
join t3 as origin_area on t2.areaid = origin_area.areaid
You will need to join with the same table twice, both for t2 and t3 so that you get the matching records for your needs.
It helps usually to put aliases that match the purpose of the join (in this case, destination and origin) when writing the query.
I think what you're trying to do is something like this:
select T1.*, T3dest.AreaName, T3orig.AreaName
From
T1
inner join
T2 T2dest on
T1.[Country (destination)] = T2dest.CountryName
inner join
T3 T3dest on
T2dest.AreaID = T3dest.AreaID
inner join
T2 T2orig on
T1.[Country (origin)] = T2orig.CountryName
inner join
T3 T3orig on
T2orig.AreaID = T3orig.AreaID
Note that I've switched to inner joins throughout, at the moment. If you do want left join semantics, you either need to use those for all of the joins to the T2 and T3 tables or you need to change the join order (so that the relevant T3 joins to the T2 tables occur before the attempted join with T1). It's not clear from the sample data if that's required, however.
Try this, You would still want to join on area id's
select T1.Date,T1.[Country (destination)], null [Country (origin)], T3.AreaName [AreaName(Destination)], null [AreaName(Origin)]
From T1
left outer join T2 on
T1.[Country (destination)] = T2.CountryName
inner join T3 on
T2.AreaID = T3.AreaID
union all
select T1.Date,null [Country (destination)], t1.[Country (origin)], Null [AreaName(Destination)], t3. [AreaName(Origin)]
From T1
left outer join T2 on
T1.[Country (Origin)] = T2.CountryName
inner join T3 on
T2.AreaID = T3.AreaID

Multiple Full Outer Joins

I want to use the result of a FULL OUTER JOIN as a table to FULL OUTER JOIN on another table. What is the syntax that I should be using?
For eg: T1, T2, T3 are my tables with columns id, name. I need something like:
T1 FULL OUTER JOIN T2 on T1.id = T2.id ==> Let this be named X
X FULL OUTER JOIN T3 on X.id = t3.id
I want this to be achieved so that in the final ON clause, I want the T3.id to match either T1.id or T2.id. Any alternative way to do this is also OK.
SELECT COALESCE(X.id,t3.id) AS id, *-- specific columns here instead of the *
FROM
(
SELECT COALESCE(t1.id,t2.id) AS id, * -- specific columns here instead of the *
FROM T1 FULL OUTER JOIN T2 on T1.id = T2.id
) AS X
FULL OUTER JOIN T3 on X.id = t3.id
Often, chains of full outer joins don't behave quite as expected. One replacements uses left join. This works best when a table has all the ids you need. But you can also construct that:
from (select id from t1 union
select id from t2 union
select id from t3
) ids left join
t1
on ids.id = t1.id left join
t2
on ids.id = t2.id left join
t3
on ids.id = t3.id
Note that the first subquery can often be replaced by a table. If you have such a table, you can select the matching rows in the where clause:
from ids left join
t1
on ids.id = t1.id left join
t2
on ids.id = t2.id left join
t3
on ids.id = t3.id
where t1.id is not null or t2.id is not null or t3.id is not null
You can do it like you suggested, using IN()
FROM T1
FULL OUTER JOIN T2
ON(T1.id = T2.id)
FULL OUTER JOIN T3
ON(T3.ID IN(T2.id,T1.id))
or I think you can do it with a UNION (depends on what you need) :
SELECT * FROM
(SELECT name,id from T1
UNION
SELECT name,id from T2) x
FULL OUTER JOIN T3
ON(t3.id = x.id)

SQL: Calculating the Max date across multiple tables

I am trying to find the max date across multiple columns (in multiple tables). I have an SQL query that I think is almost workable, but am having a hard time debugging it. The problem is that it never returns anything. Any ideas on what I'm doing incorrectly? I don't get any errors, just an empty row "maxdate."
Here's my query:
SELECT
(
SELECT MAX(dates) FROM
(
SELECT dates = t1.UpdateDate
UNION ALL
SELECT dates = t2.UpdateDate
UNION ALL
SELECT dates = t3.UpdateDate
UNION ALL
SELECT dates = t4.UpdateDate
) as dateAcrossColumns
) as maxdate
FROM table1 as t1
join table2 as t2 on t1.ID = t2.ID
join table3 as t3 on t1.ID = t3.ID
join table4 as t4 on t1.ID = t4.ID
join table5 as t5 on t1.Status = t5.Status
WHERE t1.id = #param and t5.status <> 3
First join you join to table 2 using table 1's ID and table 3's ID. I presume you mean table 1's ID and table 2's ID?
join table2 as t2 on t1.ID = t3.ID
Should be:
join table2 as t2 on t1.ID = t2.ID
Now that this has been changed;
I tried your query and it works fine, are you sure that your joins are bringing back any rows? To test it replace your SELECT statement with SELECT *, if you get no rows back then they are being filtered out somewhere in your joins / WHERE.
If it's possible for any of the tables to not have any rows for the given #param, the (inner) joins will filter out all of the other tables.
Try unioning the dates and get the max of the unions
WITH allDates AS (
SELECT UpdateDate
FROM Table1
WHERE ID = #param
UNION
SELECT UpdateDate
FROM Table2
WHERE ID = #param
UNION
SELECT UpdateDate
FROM Table3
WHERE ID = #param
UNION
SELECT UpdateDate
FROM Table4
WHERE ID = #param
UNION
SELECT t5.UpdateDate
FROM Table5 AS t5
JOIN Table1 AS t1
ON t5.Status = t1.Status
WHERE t1.ID = #param
)
SELECT MAX( UpdateDate ) AS MaxDate
FROM allDates
Cant think of a simpler way of doing this :)
SELECT MAX(Dates) AS RequiredDate
FROM (
SELECT t1.UpdateDate AS DateOne
,t2.UpdateDate AS DateTwo
,t3.UpdateDate AS DateThree
,t4.UpdateDate AS DateFour
FROM table1 as t1
join table2 as t2 on t1.ID = t2.ID
join table3 as t3 on t1.ID = t3.ID
join table4 as t4 on t1.ID = t4.ID
join table5 as t5 on t1.[Status] = t5.[Status]
WHERE t1.id = #param and t5.[Status] <> 3
)t
UNPIVOT (Dates FOR DateCols
IN (DateOne, DateTwo,DateThree,DateFour))up
I was trying to solve a similar problem to find the last modified date across a set of joined tables in MySQL. The following worked for me using a combination of GREATEST and MAX:
SELECT GREATEST(MAX(t1.modified), MAX(t2.modified), MAX(t3.modified),
MAX(t4.modified)) AS lastmodified
FROM `table1` AS t1
LEFT JOIN `table2` AS t2 ON t1.id = t2.t1_id
LEFT JOIN `table3` AS t3 ON t2.id = t3.t2_id
LEFT JOIN `table4` AS t4 ON t3.id = t4.t3_id
WHERE t1.id = #param