How to join a grouped table in sql? - sql

Novice in SQL here but hopefully someone can help. I have two tables. For the simplicity here is how the tables are structured.
Table 1:
+------------+-------+-----------+------------+
| department | sales | date | sales_code |
+------------+-------+-----------+------------+
| 1 | 50 | 5/26/2021 | A |
+------------+-------+-----------+------------+
| 2 | 150 | 5/26/2021 | B |
+------------+-------+-----------+------------+
| 1 | 200 | 5/25/2021 | C |
+------------+-------+-----------+------------+
| 2 | 250 | 5/24/2021 | D |
+------------+-------+-----------+------------+
Table 2:
+------+------------+-------+-----------+-----------------------+
| item | department | sales | date | column I want to join |
+------+------------+-------+-----------+-----------------------+
| 31 | 1 | 50 | 5/26/2021 | x |
+------+------------+-------+-----------+-----------------------+
| 30 | 2 | 150 | 5/26/2021 | x |
+------+------------+-------+-----------+-----------------------+
| 29 | 1 | 200 | 5/25/2021 | x |
+------+------------+-------+-----------+-----------------------+
| 28 | 2 | 250 | 5/24/2021 | x |
+------+------------+-------+-----------+-----------------------+
I need to join table 2 to table 1 - however it needs to be aggregated by department sales first, this is because table 2 is already aggregated by department sales. Here is what I was thinking but cannot seem to get it to work.
SELECT t1.*, t2.*
FROM table1 as t1
JOIN (
SELECT department, date, column_i_want, sum(sales)
FROM table2
GROUP BY department ) as t2
ON t2.department = t1.department AND t1.date = t2.date
Desired Output:
+------------+-------+-----------+------------+-----------------------+
| department | sales | date | sales_code | column I want to join |
+------------+-------+-----------+------------+-----------------------+
| 1 | 50 | 5/26/2021 | A | x |
+------------+-------+-----------+------------+-----------------------+
| 2 | 150 | 5/26/2021 | B | x |
+------------+-------+-----------+------------+-----------------------+
| 1 | 200 | 5/25/2021 | C | x |
+------------+-------+-----------+------------+-----------------------+
| 2 | 250 | 5/24/2021 | D | x |
+------------+-------+-----------+------------+-----------------------+
Any help would be appreciated.

There are several ways to go about doing that, the easiest one is to create a view
CREATE VIEW t2 AS
SELECT department, date, column_i_want, sum(sales)
FROM table2
GROUP BY department;
then it's easier to join them (you can also use a With clause instead of a view but it can get messy)
SELECT *
FROM table1 NATURAL JOIN t2

here is what you want:
select t2.*, t1.sales_code
from table2 t2
join table1 t1
on t1.department = t2.department
and t1.date = t2.date

Related

SELECT DISTINCT on multiple columns with new value

I have these two tables.
Table #1:
+----+------+-----+
| ID | Y | AGE |
+----+------+-----+
| 1 | 2022 | a |
| 1 | 2022 | b |
| 3 | 2021 | a |
| 4 | 2021 | a |
| 4 | 2021 | b |
| 4 | 2021 | c |
| 7 | 2021 | a |
| 7 | 2022 | b |
+----+-------+----+
Table #2:
+----+------+-----------+
| ID | num | something |
+----+------+-----------+
| 1 | 10 | a1221 |
| 3 | 30 | a4342 |
| 4 | 40 | bdssd |
| 7 | 70 | asdsds |
+----+-----+------------+
and I would like to merge them into this result set:
+----+-------+-----+
| ID | Y | num |
+----+-------+-----+
| 1 | 2022 | 10 |
| 3 | 2021 | 30 |
| 4 | 2021 | 40 |
| 7 | 2021 | 70 |
| 7 | 2022 | 70 |
+----+-------+-----+
That means I would like take unique pairs of T1.ID and T1.Y ignoring column AGE and them INNER JOIN resulting table with T2 on T1.ID = T2.ID.
I know I can do this in steps as
WITH cte AS
(
SELECT ID, Y
FROM T1
GROUP BY ID, Y
)
SELECT cte.ID, cte.Y, T2.num
FROM cte
INNER JOIN T2 ON cte.ID = T2.ID
but is there any better way without creating a temporary table? Because simple
SELECT T1.ID, T1.Y, T2.num
FROM T1
INNER JOIN T2 ON T1.ID = T2.ID
will result with duplicates that comes from T1.AGE even tho I'm not using it
I think it's better to use views for this:
CREATE VIEW dbo.view1
AS
SELECT
ID
,Y
FROM T1
GROUP BY ID
,Y
GO
And call it wherever needed like tables:
SELECT v1.ID, v1.Y, T2.num
FROM view1 v1
INNER JOIN T2 ON v1.ID = T2.ID

SQL : automatically fill price between dates

I'm trying to write a view from two tables, one referential table that contains products ID and weeks :
+------------+------+
| Product_id | week | t1
+------------+------+
| 1 | 1 |
| 2 | 1 |
| 1 | 2 |
| 2 | 2 |
| 1 | 3 |
| 2 | 3 |
+------------+------+ etc...
the other one contains Products ID, weeks when the product's price changed and the price
+------------+------+-------+
| Product_id | week | price | t2
+------------+------+-------+
| 1 | 1 | 70 |
| 1 | 2 | 50 |
| 2 | 2 | 70 |
| 1 | 4 | 30 |
| 2 | 4 | 40 |
+------------+------+-------+
I know how to achieve easily this by joining the two tables :
+------------+------+-------+
| Product_id | week | price |
+------------+------+-------+
| 1 | 1 | 70 |
| 1 | 2 | 50 |
| 1 | 3 | |
| 1 | 4 | 30 |
| 1 | 5 | |
| 2 | 1 | |
| 2 | 2 | 70 |
| 2 | 3 | |
| 2 | 4 | 40 |
| 2 | 5 | |
+------------+------+-------+
But my goals would rather be to fill in the gaps and have the price for each week (without creating any new table), as such :
+------------+------+-------+
| Product_id | week | price |
+------------+------+-------+
| 1 | 1 | 70 |
| 1 | 2 | 50 |
| 1 | 3 | 50 |
| 1 | 4 | 30 |
| 1 | 5 | 30 |
| 2 | 1 | |
| 2 | 2 | 70 |
| 2 | 3 | 70 |
| 2 | 4 | 40 |
| 2 | 5 | 40 |
+------------+------+-------+ (product 2 isn't sold yet at week 1, so it doesn't have a price).
I can't see how I would do this in SQL. I haven't used PARTITION BY or LAG yet, and it might be what I'm looking for. If anyone can push me in the right direction, I would appreciate it :)
You can use window functions - the ignore nulls clause, which teradata supports, comes handy here:
select
t1.product_id,
t1.week,
coalesce(
t2.price,
lag(t2.price ignore nulls) over(partition by t1.product_id order by t1.week)
) price
from t1
left join t2
on t2.product_id = t1.product_id
and t2.week = t1.week
Or better yet, as suggested by dnoeth, you can use last_value(), which avoids the need for coalesce():
select
t1.product_id,
t1.week,
last_value(t2.price ignore nulls) over(partition by t1.product_id order by t1.week) price
from t1
left join t2
on t2.product_id = t1.product_id
and t2.week = t1.week
Use a cross join to generate the rows, then left join and window functions:
with weeks as (
select row_number() over (order by product_id) as n
from table1
)
select t1.product_id, w.n as week,
coalesce(t2.price, lag(t2.price ignore nulls) over (partition by p.product_id order by w.n)
) as price
from (select distinct product_id
from table1 t1
) p cross join
weeks w left join
table2 t2
on t2.product_id = p.product_id and t2.week = w.week
where w.n <= 5
You can do this with a LEFT JOIN.
SELECT t1.Product_id, t1.week, tmp.price
FROM t1
LEFT JOIN t2 tmp ON tmp.Product_id = t1.Product_id AND
tmp.week = (SELECT MAX(week) FROM t2
WHERE Product_id = tmp.Product_id AND week <= t1.week)
ORDER BY t1.Product_id, t1.week
I would argue it's cleaner yet with OUTER APPLY, but I don't know if that's supported by teradata.
SELECT t1.Product_id, t1.week, oa.price
FROM t1
OUTER APPLY (SELECT TOP 1 price FROM t2
WHERE Product_id = t1.Product_id AND week <= t1.week
ORDER BY week DESC) oa
ORDER BY t1.Product_id, t1.week

Join two tables to get both matching & unmatching rows in SQL Server

Table scenario
TABLE1
TRAN_NO | SR_NO |MAT_NAME
---------------------------
001 | 1 |Material 1
001 | 2 |Material 2
TABLE2
TRAN_NO | SR_NO | TEMP
-------------------------
001 | 1 | 10
001 | 2 | 20
001 | 3 | 30
001 | 4 | 40
I want output like this
TRAN_NO | SR_NO |MAT_NAME | TRAN_NO | SR_NO | TEMP
001 | 1 | Material 1 | 001 | 1 | 10
001 | 2 | Material 2 | 001 | 2 | 20
NULL | NULL | NULL | 001 | 3 | 30
NULL | NULL | NULL | 001 | 4 | 40
I tried all types of join including LEFT, RIGHT, CROSS but all of them either give matching rows or Cartesian product of them.
try this.
select *
from table1 a
right join table2 b on a.sr_no = b.sr_no;
select a., b.
from Table_2 b left join Table_1 a on a.TRAN_NO = b.TRAN_NO and a.sr_no = b.sr_no
use left join like below
select t1.*,t2.* from TABLE2 t2 left join TABLE1 t1
on t2.TRAN_NO=t1.TRAN_NO and t2.SR_NO=t1.SR_NO

Access Multiple SQL Connection

I have two queries in Access which are returning two tables like:
(The tables have both about 1000 lines)
SELECT
(select count(*)
from Table1 T2
where T1.Name=T2.Name and T1.Variable1 >= T2.Variable1) as Rank,
T1.Name,
T1.Variable1
FROM Table1 T1
Results:
+-------+---------+------------+
| Rank | Name | Variable1 |
+-------+---------+------------+
| 1 | Tim | x |
| 2 | Tim | y |
| 3 | Tim | z |
| 1 | Susan | x |
| 2 | Susan | w |
+-------+---------+------------+
Second query:
SELECT (select count(*)
from Table2 T2
where T1.Name=T2.Name and T1.Variable2 >= T2.Variable2) as Rank,
T1.Name,T1.Variable2
FROM Table2 T1
Results:
+--------+---------+------------+
| Ran | Name | Variable2 |
+--------+---------+------------+
| 1 | Tim | a |
| 2 | Tim | b |
| 3 | Tim | c |
| 1 | Susan | a |
| 2 | Susan | c |
+--------+---------+------------+
I want to link them:
Select distinct Table1.Name, Table1.Variable1, Table2.Variable2
from Table1, Table2
where Table1.Name=Table2.Name and Table1.Rank=Table2.Rank
Results:
+-----------+---------+-------------+------------+
| Rank | Name | Variable1 | Variable2 |
+-----------+---------+-------------+------------+
| 1 | Tim | x | a |
| 2 | Tim | y | b |
| 3 | Tim | z | c |
| 1 | Susan | x | a |
| 2 | Susan | w | b |
+-----------+---------+-------------+------------+
But that link isn't performing well in access.
I also tried to link them via "join" but the performance isnt getting better.
These ranking queries are expensive (the subquery has to be executed for each row of the main table).
Stacking / cascading expensive queries in Access often performs badly.
Your best option is to change your 1st and 2nd query into "Create table" (SELECT INTO) queries, storing the results in intermediate tables.
E.g.
SELECT
(select count(*)
from Table1 T2
where T1.Name=T2.Name and T1.Variable1 >= T2.Variable1) as Rank,
T1.Name,
T1.Variable1
INTO Result1
FROM Table1 T1
Then use these tables (Result1, Result2) as input for the JOIN.

SQL Query to get results that match between three tables, or a single result for no match

Is there a way to use a where clause to check if there were zero matches between tables for a record from the first table, and produce one row or results reflecting that?
I'm trying to get results that look like this:
+----------+----------+-----------+----------+-------------+
| Results |
+----------+----------+-----------+----------+-------------+
| Date | Queue ID | From Date | To Date | Campaign ID |
| 3/1/2014 | 1 | 2/24/2014 | 3/2/2014 | 1 |
| 3/1/2014 | 2 | (NULL) | (NULL) | (NULL) |
+----------+----------+-----------+----------+-------------+
From a combination of tables that look like this:
+----------+-------+ +-------+----+ +----+-----------+-----------+----------+
| Table 1 | | Table 2 | | Table 3 |
+----------+-------+ +-------+----+ +----+-----------+-----------+----------+
| Date | Queue | | Queue | SP | | SP | From Date | To Date | Campaign |
| | ID | | ID | ID | | ID | | | ID |
+----------+-------+ +-------+----+ +----+-----------+-----------+----------+
| 3/1/2014 | 1 | | 1 | 1 | | 1 | 2/24/2014 | 3/2/2014 | 1 |
| 3/1/2014 | 2 | | 1 | 2 | | 2 | 3/3/2014 | 3/9/2014 | 5 |
| | | | 1 | 3 | | 3 | 3/10/2014 | 3/16/2014 | 1 |
| | | | 1 | 4 | | 4 | 3/17/2014 | 3/23/2014 | 1 |
| | | | 1 | 5 | | 5 | 3/24/2014 | 3/30/2014 | 4 |
| | | | 2 | 6 | | 6 | 3/3/2014 | 3/9/2014 | 5 |
| | | | 2 | 7 | | 7 | 3/10/2014 | 3/16/2014 | 5 |
| | | | 2 | 8 | | 8 | 3/17/2014 | 3/23/2014 | 5 |
| | | | 2 | 9 | | 9 | 3/24/2014 | 3/30/2014 | 5 |
+----------+-------+ +-------+----+ +----+-----------+-----------+----------+
I'm joining Table 1 to Table 2 on QUEUE ID,
and Table 2 to Table 3 on SP ID,
and DATE from Table 1 should fall between Table 3's FROM DATE and TO DATE.
I want a single record returned for each queue, including if there were no date matches.
Unfortunately any combinations of joins or where clauses I've tried so far only result in either one record for Queue ID 1 or multiple records for each Queue ID.
I would suggest this:
SELECT
t1.Date,
t1.QueueID,
s.FromDate,
s.ToDate,
s.CampaignID
FROM
Table1 t1
LEFT JOIN
(
SELECT
t2.QueueID,
t3.FromDate,
t3.ToDate,
t3.CampaignID
FROM
Table2 t2
INNER JOIN
Table3 t3 ON
t2.SPID = t3.SPID
) s ON
t1.QueueID = s.QueueID AND
t1.Date BETWEEN s.FromDate AND s.ToDate
SQL Fiddle here with an abbreviated dataset
A trivial amendment to AHiggins code. Using the CTE makes it a little easier to read perhaps.
With AllDates as
(
SELECT
t2.QueueID,
t3.FromDate,
t3.ToDate,
t3.CampaignID
FROM Table2 t2
INNER JOIN Table3 t3 ON
t2.SPID = t3.SPID
)
SELECT
t1.Date,
t1.QueueID,
s.FromDate,
s.ToDate,
s.CampaignID
FROM Table1 t1
LEFT JOIN AllDates s ON
t1.QueueID = s.QueueID AND
t1.Date BETWEEN s.FromDate AND s.ToDate
You want something like:
select distinct t1.date, t1,queue_id IFNULL(t3.from_date,'NULL'),
IFNULL(t3.to_date,'NULL'), IFNULL(t3.campaign,'NULL')
FROM table1 t1
LEFT OUTER JOIN table2 t2 on t1.queue_id = t2.queue_id
left outer join table3 t3 on t2.sp_id = t3.sp_id
where t3.from_date <= t1.date
AND t3.to_date >= t1.date
This will select dsitinct records from the table (eliminating null duplicates and replacing them with NULL)
SELECT t1.[Date], t1.[Queue ID], s.[From Date], s.[To Date], s.[Campaign ID]
FROM table1 t1
LEFT JOIN (SELECT t3.*, t2.[Queue ID] FROM table3 t3 JOIN table2 t2 ON t2.[SP ID] = t3.[SP ID]) s
ON s.[Queue ID] = t1.[Queue ID] AND t1.[Date] BETWEEN s.[From Date] AND s.[To Date]
SQL Fiddle