Pairs with no duplicates grouped together - sql

I have a table
ID GROUPID NAME
== ======= ========
1 100 A
2 100 B
3 200 C
4 200 D
5 300 E
6 100 F
I would like to create a table containing the permutation pairs within a group without any pairs that are the same on both first and second that looks like this:
PAIRID FIRST SECOND
====== ===== ======
1 1 2
2 1 6
3 2 1
4 2 6
5 3 4
6 4 3
7 6 1
8 6 2
I would like to do it in PL/SQL or straight SQL inserts if possible. I did this through Java already using a recursive function to go through the permutations.

You could self join the table:
SELECT ROW_NUMBER() OVER (ORDER BY a.id, b.id) AS pairid,
a.id AS FIRST, b.id AS second
FROM mytable a
JOIN mytable b ON a.groupid = b.groupid AND a.id <> b.id
ORDER BY 1 ASC;

Related

I have this small table having duplicate id and names and I want to replace null values with names using there ID

I got it with self join:
select a.id, a.name, b.id, b.name, nvl(a.name,b.name)
from names a
join names b
on a.id = b.id
where a.name is null and b.name is not null;
But don't no how do I update it in the table.
I tried update statement but did not work
[]
Problem is that there are no unique [id, name] combinations in rows that don't have empty name columns.
SQL> select * From test;
ID NAME
---------- ------
1 naya
1 naya
2 amar
3 dhruv --> here
4 shyla
4 shyla
3 diya --> here
5 ananya
6 ishaan
5
2
2
1
3
14 rows selected.
SQL>
So, which name would you want to get for ID = 3? You can't have both, so - one option is to select any, e.g. max.
SQL> update test a set
2 a.name = (select max(b.name)
3 from test b
4 where b.id = a.id
5 )
6 where a.name is null;
5 rows updated.
Result:
SQL> select * From test;
ID NAME
---------- ------
1 naya
1 naya
2 amar
3 dhruv
4 shyla
4 shyla
3 diya
5 ananya
6 ishaan
5 ananya
2 amar
2 amar
1 naya
3 diya
14 rows selected.
SQL>
From Oracle 12, you can use:
UPDATE names n
SET name = (SELECT name
FROM names x
WHERE x.id = n.id
ORDER BY name NULLS LAST
FETCH FIRST ROW ONLY)
WHERE name IS NULL
db<>fiddle here

SQL Order By Custom Sequence

I have a data in this order
Id Value
-- ----
1 a
1 b
1 c
2 a
2 c
3 b
4 c
4 b
4 a
I want to sort data in this order
Id Value
-- ----
1 a
2 a
3 b
4 c
1 b
2 c
4 b
1 c
4 a
You seem to want to intersperse the numbers. For this purpose, you can use row_number():
order by row_number() over (partition by id order by value),
id

Update table column that is used for ordering according to alphabetical order on a second table

So I have two tables (A and B) that have a relation of n-n.
So there is a third table (C) that is used to connect both tables.
Table A and B both have an Id and a name.
Table C has IDA, IDB and an Order, the number that is used to sort and that is user given.
My issue is that I need to migrate table C since I just added that order column and so I need to give every line an ordering number, according to the B name.
So if table A has:
Id Name
1 A
2 B
3 C
And Table B has:
Id Name
1 J
2 L
3 M
And table C has:
IdA IdB Order
1 2 0
1 1 0
1 3 0
2 1 0
2 3 0
I need a query that updates table C to be more like:
IdA IdB Order
1 2 2
1 1 1
1 3 3
2 1 1
2 3 2
I have a query that can basically do what i want but it leaves me with "gaps"
reading my results above i get:
IdA IdB Order
1 2 2
1 1 1
1 3 3
2 1 1
2 3 3
I think this should work for what you need:
With ToUpdate As
(
Select C.*,
Row_Number() Over (Partition By C.IdA Order By B.Name) As NewOrder
From C
Join B On B.Id = C.IdB
)
Update C
Set "Order" = U.NewOrder
From ToUpdate U
Where U.IdA = C.IdA
And U.IdB = C.IdB
(In full disclosure, I'm not terribly familiar with postgres, but I think this should be valid).

SQL code to sum duplicate rows and also retain the duplicate rows

How to write a sql query
A 1
B 1
C 1
A 1
A 1
B 1
B 1
C 1
B 1
to make it look like
A 3
B 4
C 2
A 3
A 3
B 4
B 4
C 2
B 4
SELECT field1, SUM(field2) OVER (PARTITION BY field1) as total
FROM table1
More info about window/analytic functions in Oracle : http://docs.oracle.com/cd/E11882_01/server.112/e41084/functions004.htm

SQL Inner Join and Partitioning To obtain RowNumbers when matching

I have 2 tables. The first table 'a' the second 'b'.
I am writing a query that grabs every row in table a (there is 33 rows defined) and inner joins table b where the EnclLocation or the BackPanLoc match the Workcell in table A.
I only want a row from table B where they match based off BackPan and EnclLocation but they are not the same records. table b has a few rows of data that is assigned to the same workcell as table a. I am just trying to retrieve those additional rows and partition it.
I attached table a and table b. I also attached the desired results for this query with respect to Workcell 10 only as an example... As you can see, table B has 4 records that has either the EnclLocation or the BackPanLoc = 10. But my results only show the same DelvNumber 4 times. any help is most appreicated.
Table a
Table b
Incorrect Results
Desired Results (showing only Workcell 10 as an example)
workcell DelvNumber RowNum
1 447910-02 1
2 445710-01 1
2 445710-01 2
3 444291-01 1
3 444291-01 2
4 447910-03 1
4 447910-03 2
5 648020-01 1
6 647800-02 1
7 646920-01 1
7 646920-01 2
8 644830-4-8 1
8 644830-4-8 2
9 443990-01 1
10 645960-01-03 1
10 445710-11 2
10 445710-02 3
10 445710-09 4
Code Used
WITH ss
AS (SELECT a.*,
Row_number()
OVER(
partition BY a.workcell
ORDER BY a.workcell) AS rownum
FROM nwcurrent a
INNER JOIN nwdeliverables b
ON b.encllocation = a.workcell
OR b.backpanloc = a.workcell
WHERE ( b.status < 9
AND ( b.encllocation <> 0
OR b.backpanloc <> 0 )
OR a.delvnumber = '123' ))
SELECT *
FROM ss
copy and paste format
1 447910-02 1
2 445710-01 1
2 445710-01 2
3 444291-01 1
3 444291-01 2
4 447910-03 1
4 447910-03 2
5 648020-01 1
6 647800-02 1
7 646920-01 1
7 646920-01 2
8 644830-4-8 1
8 644830-4-8 2
9 443990-01 1
10 645960-01-03 1
10 445710-11 2
10 445710-02 3
10 445710-09 4
SQLFiddle
http://sqlfiddle.com/#!3/a8682/4
A new try...
SELECT a.workcell
,a.DelvNumber AS A_DelvNumber
,b.DelvNumber AS B_DelvNumber
,CASE WHEN a.DelvNumber<>b.DelvNumber THEN b.DelvNumber ELSE a.DelvNumber END AS DelvNumber_Resolved
,Row_number() OVER(partition BY a.workcell ORDER BY a.workcell) AS rownum
FROM NWCurrent a
INNER JOIN NWDeliverables AS b ON b.EnclLocation=a.WorkCell OR b.BackPanLoc=a.WorkCell
WHERE (b.status <9 AND (b.EnclLocation<>0 OR b.BackPanLoc<>0)OR a.DelvNumber='123')