I want to join two tables, where the first table has more entries than the second, such that rows from each are joined in order. Maybe a little example would be helpful:
Table T:
| tid | sid | ron | val | seqno |
| --- | --- | --- | --- | --- |
| 1 | a | x1 | 15 | 1 |
| 2 | b | x2 | 10 | 3 |
| 2 | b | x3 | 20 | 4 |
| 3 | a | x5 | 10 | 5 |
| 4 | c | x9 | 15 | 7 |
| 4 | c | x9 | 15 | 8 |
| 4 | c | x9 | 20 | 10 |
| 4 | c | x9 | 15 | 11 |
| 6 | b | x11 | 22 | 12 |
| 7 | b | x12 | 10 | 14 |
| 7 | b | x13 | 10 | 16 |
| 7 | b | x13 | 10 | 17 |
| 7 | b | x14 | 10 | 19 |
The second table (Table C) is as follows (in reality, more columns):
| tid | sid | ron | val | fid |
| --- | --- | --- | --- | --- |
| 2 | b | x3 | 20 | 54 |
| 4 | c | x9 | 15 | 12 |
| 4 | c | x9 | 15 | 14 |
| 4 | c | x9 | 20 | 15 |
| 4 | c | x9 | 15 | 20 |
| 7 | b | x13 | 10 | 112 |
| 7 | b | x13 | 10 | 113 |
seqNo and fid are there in each table to provide ordering within the groups formed by (tid, sid, ron), and that is the ordering I'd like to maintain.
How can I get from these two tables to something like the following table?
| tid | sid | ron | val | fid | seqno |
| --- | --- | --- | --- | --- | --- |
| 2 | b | x3 | 20 | 54 | 4 |
| 4 | c | x9 | 15 | 12 | 7 |
| 4 | c | x9 | 15 | 14 | 8 |
| 4 | c | x9 | 20 | 15 | 10 |
| 4 | c | x9 | 15 | 20 | 11 |
| 7 | b | x13 | 10 | 112 | 16 |
| 7 | b | x13 | 10 | 113 | 17 |
I can't assign a rank to each element in the group and use that for matching inside of a LEFT JOIN, since there are cases where matching doesn't begin at the end of the group (for example tid=7). Also, because val in the same group may have repeated values, I can't blindly match on it either, as that may blow up the number of rows.
This is what I managed to get late last night, seems to be working correctly:
WITH
table_t AS (
SELECT *
FROM (VALUES
(1,'a','x1',15,1),
(2,'b','x2',10,3),
(2,'b','x3',20,4),
(3,'a','x5',10,5),
(4,'c','x9',15,7),
(4,'c','x9',15,8),
(4,'c','x9',20,10),
(4,'c','x9',15,11),
(6,'b','x11',22,12),
(7,'b','x12',10,14),
(7,'b','x13',10,16),
(7,'b','x13',10,17),
(7,'b','x14',10,19)
) AS c(tid, sid, ron, val, seq)
),
table_t_ranked AS (
SELECT *
, DENSE_RANK() OVER (PARTITION BY tid, sid, ron ORDER BY seq ASC) AS ranking
FROM table_t
),
table_c AS (
SELECT *
FROM (VALUES
(2,'b','x3',20,54),
(4,'c','x9',15,12),
(4,'c','x9',15,14),
(4,'c','x9',20,15),
(4,'c','x9',15,20),
(7,'b','x13',10,112),
(7,'b','x13',10,113)
) AS c(tid, sid, ron, val, fid)
),
table_c_ranked AS (
SELECT *
, DENSE_RANK() OVER (PARTITION BY tid, sid, ron ORDER BY fid ASC) AS ranking
FROM table_c
),
foo AS (
SELECT c.*
, t.seq
, t.ranking as ranking_t
FROM table_c_ranked c
LEFT JOIN table_t_ranked t
ON c.tid = t.tid
AND c.sid = t.sid
AND c.ron = t.ron
AND c.val = t.val
)
SELECT tid, sid, ron, val, fid, seq
FROM foo
WHERE ranking = ranking_t
ORDER BY tid, seq
I have a Microsoft Access table with the following values:
id | C | D | ED | T |
---+-------+-----+------------+---+
1 | 33105 | ABC | 2020/01/04 | 1 |
2 | 33105 | ABC | 2020/01/08 | 2 |
3 | 33102 | DEF | 2020/02/01 | 2 |
4 | 34145 | GHI | 2020/02/09 | 1 |
5 | 34145 | GHI | 2020/02/10 | 2 |
6 | 34162 | JKL | 2020/02/08 | 1 |
I would like to extract the rows with the same C but lowest T (with this precedence) and finally sort the results by date (ED) descending. So my expected result is the following:
id | C | D | ED | T |
---+-------+-----+------------+---+
4 | 34145 | GHI | 2020/02/09 | 1 |
6 | 34162 | JKL | 2020/02/08 | 1 |
3 | 33102 | DEF | 2020/02/01 | 2 |
1 | 33105 | ABC | 2020/01/04 | 1 |
What's the fastest way in SQL to do so (the table is actually pretty large)?
You can do it with NOT EXISTS:
SELECT t.*
FROM tablename AS t
WHERE NOT EXISTS (SELECT 1 FROM tablename WHERE C = t.C AND T < t.T)
Or with a correlated subquery:
SELECT t.*
FROM tablename AS t
WHERE t.T = (SELECT MIN(T) FROM tablename WHERE C = t.C)
I have a table as such (tbl):
+----+-----+------+-----+
| pk | grp | attr | val |
+----+-----+------+-----+
| 0 | 0 | ohif | 4 |
| 1 | 0 | foha | 56 |
| 2 | 0 | slns | 2 |
| 3 | 1 | faso | 11 |
| 4 | 1 | tepj | 4 |
| 5 | 2 | bnda | 12 |
| 6 | 2 | ojdf | 9 |
| 7 | 2 | anaw | 1 |
+----+-----+------+-----+
I would like to select one row from each group, in particular that with the maximum val for each group.
I can easily select grp and val:
SELECT grp, MAX(val)
FROM tbl
GROUP BY grp
Yielding this table (tbl2):
+-----+-----+
| grp | val |
+-----+-----+
| 0 | 56 |
| 1 | 11 |
| 2 | 12 |
+-----+-----+
However, I want this table:
+----+-----+------+-----+
| pk | grp | attr | val |
+----+-----+------+-----+
| 1 | 0 | foha | 56 |
| 3 | 1 | faso | 11 |
| 5 | 2 | bnda | 12 |
+----+-----+------+-----+
Since (grp, val) constitutes a key, I could left-join tbl2 with tbl on same grp and val.
However, I was wondering if there was any other solution: in my real-world situation tbl is a pretty complex and heavy derived table, and I have the design constrain of not being able to use temp tables. Is there any way to order the rows inside each group according to val and to then take the first record for each group?
I'm on PostgreSQL 10, but a standard SQL solution would be the best.
In Postgres, the best approach is distinct on:
SELECT DISTINCT ON (t.grp) t.*
FROM tbl
ORDER BY grp, val DESC;
In particular, this can take advantage of an index on (grp, val desc).
I have two tables - a header and a matrix/details.
*Header Table* *Matrix / Details Table*
+----+--------+-----+ +----+--------+------+
| ID | Parent | Qty | | ID | Child | Qty |
+----+--------+-----+ +----+--------+------+
| 1 | A | 10 | | 1 | X | 100 |
| 2 | B | 20 | | 1 | Y | 1000 |
| 3 | C | 30 | | 2 | X | 200 |
+----+--------+-----+ | 2 | Y | 2000 |
| 3 | X | 30 |
| 3 | Y | 300 |
| 3 | Z | 3000 |
+----+--------+------+
I'm Joining these two tables based on ID.
I don't want the result to have duplicated values from header table.
I expect a result like following:
*Current Result* *Expected Result*
+----+--------+-----+-------+------+ +----+--------+-----+-------+------+
| ID | Parent | Qty | Child | Qty | | ID | Parent | Qty | Child | Qty |
+----+--------+-----+-------+------+ +----+--------+-----+-------+------+
| 1 | A | 10 | X | 100 | | 1 | A | 10 | X | 100 |
| 1 | A | 10 | Y | 1000 | | | | | Y | 1000 |
| 2 | B | 20 | X | 200 | | 2 | B | 20 | X | 200 |
| 2 | B | 20 | Y | 2000 | | | | | Y | 2000 |
| 3 | C | 30 | X | 30 | | 3 | C | 30 | X | 30 |
| 3 | C | 30 | Y | 300 | | | | | Y | 300 |
| 3 | C | 30 | Z | 3000 | | | | | Z | 3000 |
+----+--------+-----+-------+------+ +----+--------+-----+-------+------+
Is this possible? If not any, alternate solution available?
Thanks in advance...
If you are using SQL Server,Try with the below query.
;WITH CTE_1
AS
(SELECT *,ROW_NUMBER()OVER(PARTITION BY ID,Parent,Quantity ORDER BY ID ) RNO
FROM Header H
JOIN [Matrix / Details] M
ON H.ID=M.ID)
SELECT CASE WHEN RNO=1 THEN CAST(ID as VARCHAR(50)) ELSE '' END ID,
CASE WHEN RNO=1 THEN Parent ELSE '' END Parent,
CASE WHEN RNO=1 THEN cast(Quantity as VARCHAR(50)) ELSE '' END Quantity,
Child,Qty
FROM CTE_1
ORDER BY ID,Parent,Quantity
I try to do a Rollup over multiple columns and then apply a ranking on each stage/section of the rollup process. The result should look somewhat like the following:
| ColA | ColB | ColC | RankingCriteria | Ranking |
|------|------|------|-----------------|---------|
| - | - | - | 10 | 1 |
|------|------|------|-----------------|---------|
| A | - | - | 10 | 1 |
| B | - | - | 8 | 2 |
|------|------|------|-----------------|---------|
| A | a | - | 9 | 1 |
| A | b | - | 7 | 2 |
| A | c | - | 5 | 3 |
| A | d | - | 2 | 4 |
|------|------|------|-----------------|---------|
| B | a | - | 8 | 1 |
| B | c | - | 7 | 2 |
| B | b | - | 2 | 3 |
|------|------|------|-----------------|---------|
| A | a | x | 7 | 1 |
| A | a | y | 5 | 2 |
| A | a | z | 4 | 3 |
|------|------|------|-----------------|---------|
| A | b | y | 6 | 1 |
|------|------|------|-----------------|---------|
| A | c | w | 10 | 1 |
| A | c | y | 10 | 1 |
| A | c | z | 8 | 2 |
| A | c | x | 6 | 3 |
|------|------|------|-----------------|---------|
| A | d | y | 4 | 1 |
|------|------|------|-----------------|---------|
| B | a | w | 10 | 1 |
| B | a | x | 8 | 2 |
|------|------|------|-----------------|---------|
| B | b | y | 6 | 1 |
| B | b | z | 5 | 2 |
| B | b | w | 4 | 3 |
|------|------|------|-----------------|---------|
| B | c | x | 6 | 1 |
|------|------|------|-----------------|---------|
So as you can see each grouping set has it's own ranking.
The basic Rollup-Query for this is simple but the ranking is giving me headaches and I am running out of ideas on how to achieve this.
Select ColA, ColB, ColC, RankingCriteria
From table
Group By Rollup(ColA, ColB, ColC)
The problem is that I cannot use a normal Rank() over (Partition by ...) because there is no partition I could use that'd work on the whole thing.
I think this will produce what you want:
SELECT r.*,
row_number() over (partition by (case when colb is null and colc is null and cola is not null
then 1 else 0 end),
(case when colb is null and colc is null and cola is not null
then NULL else A end),
(case when colb is null and colc is null and cola is not null
then NULL else B end)
order by RankingCriteria desc) as seqnum
FROM (Select ColA, ColB, ColC, RankingCriteria
From table
Group By Rollup(ColA, ColB, ColC)
) r;
The way I read the logic is that partitioning by A and B works for all but the second group. That is why this uses the three case statements.