I have the following table:
Code ParentCode oItem
-----------------------------
A null Item 001
B A Item 002
C A Item 003
D C Item 004
E B Item 005
Now, I want to have the row number then the result should be:
Rn Code ParentCode oItem
------------------------------------------
1 A null Item 001
2 B A Item 002
3 C A Item 003
4 D C Item 004
5 E B Item 005
My question is, How's the query to get the following result:
Rn Code RnParent ParentCode oItem
--------------------------------------------------------
1 A null null Item 001
2 B 1 A Item 002
3 C 1 A Item 003
4 D 3 C Item 004
5 E 2 B Item 005
If you see on the result table, parentcode is actually the code and I need to know the id of the parentcode base on the code of Rn.
Please advise.
Thank you.
If I understand correctly, you just want the rn for the parentcode. You can get this with a join:
with t as (
select row_number() over (order by code) as rn, t.*
from t
)
select t.*, tp.rn as parentrn
from t left join
t tp
on t.parentcode = tp.code
Related
I want to split a row into multiple rows according to a column named "Qta" and incrementing a serial number named "S/N", so:
Name S/N Qta
A 004 1
B 005 1
C 007 3
D 004 1
Will become:
Name S/N Qta
A 004 1
B 005 1
C 007 1
C 008 1
C 009 1
D 004 1
I'm using Microsoft Access
I managed to split the rows, but i see no way to increment serial number
Here's my query:
SELECT *
FROM Num AS N INNER JOIN MergedQueries1 AS t ON n.numbers<=t.Qta;
Obviously i created another table containing numbers
Consider:
SELECT T.Name, Format(T.SN + N.Num - 1,"000") AS SerNum, 1 AS Q
FROM MergedQueries1 AS T, (SELECT Num FROM Numbers) AS N
WHERE N.Num<=T.Qta ORDER BY T.Name, T.SN + N.Num - 1;
i'm trying to realize a query that find duplicates in a table and increment by 1 their s/n value.
Example:
Name S/N id
A 004 1
B 005 2
C 007 3
C 007 4
C 007 5
D 004 6
Will be:
Name S/N id
A 004 1
B 005 2
C 007 3
C 008 4
C 009 5
D 004 6
I'm using ms access.
Okay then assuming your table is called Table1, you could use the following code:
select
base.[Name]
, base.[S/N]
+ (
select
count(*)
from Table1 as a
where 1=1
and a.[S/N] = base.[S/N]
and a.[Name] = base.[Name]
and base.id >= a.id
)
- 1 as [S/N]
, base.id
from Table1 base
This solution is not performant on huge datasets, but unfortunately there is no row_number() in Access (it's a terrible tool). Basically what it does is create an artificial row number with a partition by Name,S/N and then adds the row number minus 1 to the S/N. So if there is only one entry, then 0 will be added, otherwise the first duplicate gets +1, second +2 etc.
You don't need the 1=1 of course, but it's a habit :)
I have the below 2 tables
Table Name: Port
City Code
------------------
City A 001
City B 002
City C 003
City D 004
City E 005
City F 006
City G 007
Table Name: Shipments
Code Shipments
------------------
001 5
001 4
002 2
003 4
003 3
003 4
004 1
005 1
006 1
007 2
I want to get the list of Cities where maximum shipments take place.
Answer:
City Total Shipments
------------------------
City C 11
City D 1
City E 1
City F 1
Can someone help me with the SQL query for this.
select c.city as City, sum(s.shipments) as Shipments
from
port c
inner join
shipments s on s.code = c.code
group by c.code
order by sum(s.shipments) asc;
Check this out and let me know.
You can use a Common Table Expression (CTE) to first aggregate the shipments for each City. The min and max records can then be returned as follows:
WITH cte AS ( SELECT P.city AS City,
SUM(S.shipments) AS TotalShipments
FROM Port P
INNER JOIN Shipments S ON S.code = P.code
GROUP BY P.city )
SELECT *
FROM cte c
WHERE c.TotalShipments IN ( SELECT MAX(c2.TotalShipments) RequiredTotalShipments
FROM cte c2
UNION
SELECT MIN(c3.TotalShipments)
FROM cte c3 )
This one is support hard for me. I can do inner join with first result only, but if exist I want take 2nd result.
THIS IS MY TABLE A
ID NAME VALUE
1 A 123
2 B 456
3 C 789
4 A 456
TABLE B
BID BNAME BVALUE
1 A ABC
2 A CDE
3 B 845
4 C 1234
MY SELECT SQL:
SELECT * FROM A
CROSS APPLY (
SELECT TOP 1 *
FROM B
WHERE A.Name = B.BName
) BB
It return
1 A 123 1 A ABC
2 B 456 3 B 845
3 C 789 4 C 1234
4 A 456 1 A ABC
Please help, I want this result:
1 A 123 1 A ABC
2 B 456 3 B 845
3 C 789 4 C 1234
4 A 456 2 A CDE
I accept tmp table and any kind of query :(
Following clarification in the comments that both tables will always have matching rows.
WITH A
AS (SELECT *,
ROW_NUMBER() OVER (PARTITION BY NAME ORDER BY ID) AS RN
FROM TableA),
B
AS (SELECT *,
ROW_NUMBER() OVER (PARTITION BY BNAME ORDER BY BID) AS RN
FROM TableB)
SELECT A.ID,
A.NAME,
A.VALUE,
B.BID,
B.BNAME,
B.BVALUE
FROM A
JOIN B
ON A.NAME = B.BNAME
AND A.RN = B.RN
I have a TblA
ID Match Code Status
1 001 A
2 001 B
3 002 A
4 003 A
5 003 V
6 004 A
7 004 B
I want to populate Status with 'FAIL' according to :
Code "A" and "B" should both exist for every match number. For 001,002,003 both A, B should exist. if not, FAIL the whole Match. Expected table:
ID Match Code Status
1 001 A NULL
2 001 B NULL
3 002 A FAIL
4 003 A FAIL
5 003 V FAIL
6 004 A NULL
7 004 B NULL
Thanks !
Here you go:
update [TblA]
set [Status] = 'FAIL' where
Match NOT in
(select match from tblA where Code = 'A'
intersect
select match from tblA where Code = 'B');