Getting an element and the next from a table - sql

I have a table with ids, cities and some sequence number, say:
ID CITY SEQ_NO
1 Milan 123
2 Paris 124
1 Rome 125
1 Naples 126
1 Strasbourg 130
3 London 129
3 Manchester 132
2 Strasbourg 128
3 Rome 131
2 Rome 127
4 Moscow 135
5 New York 136
4 Helsinki 137
I want to get the city that comes after Rome for the same id, in this case, I can order them by doing something like:
SELECT ROW_NUMBER() OVER (PARTITION BY ID ORDER BY SEQ_NO) as rownum,
id,
city,
seq_no
FROM mytable
I get:
rownum ID CITY SEQ_NO
1 1 Milan 123
2 1 Rome 125
3 1 Naples 126
4 1 Strasbourg 130
1 2 Paris 124
2 2 Rome 127
3 2 Strasbourg 128
1 3 London 129
2 3 Rome 131
3 3 Manchester 132
1 4 Moscow 135
2 4 Helsinki 137
1 5 New York 136
and, I want to get
ID CITY SEQ_NO
1 Rome 125
1 Naples 126
2 Rome 127
2 Strasbourg 128
3 Rome 131
3 Manchester 132
How do I proceed?

Hmmm . . . I might suggest window functions:
select t.*
from (select t.*,
lag(city) over (partition by id order by seq_no) as prev_city
from mytable t
) t
where 'Rome' in (city, prev_city)

Related

How to change the Row Number behavior in my T-SQL Query?

I am using SQL Server 2014 and I have the following T-SQL query running against a table (tbl1).
Extract of tbl1:
emp_code Name Address Company
---------------------------------------
100 Peter London ABC
125 Allan Cambridge DCE
125 Allan Cambridge DCE
115 John Suffolk ABC
115 John Suffolk XYZ
154 Mary Highlands ABC
154 Mary Bristol ABC
124 Mary Chester ABC
My T-SQL query stands as follows:
SELECT
[ID],
[Name],
[Address],
[Company],
ROW_NUMBER() OVER (PARTITION BY [emp_code] ORDER BY [Address]) AS RowNumber
FROM
[tbl1]
Output from above query:
emp_code Name Address Company RowNumber
--------------------------------------------------------
100 Peter London ABC 1
125 Allan Cambridge DCE 1
125 Allan Cambridge DCE 2
115 John Suffolk ABC 1
115 John Suffolk XYZ 2
154 Mary Highlands ABC 1
154 Mary Bristol ABC 2
154 Mary Chester ABC 3
Output I'm after:
emp_code Name Address Company RowNumber
---------------------------------------------------------
100 Peter London ABC 1
125 Allan Cambridge DCE 1
125 Allan Cambridge DCE 1
115 John Suffolk ABC 1
115 John Suffolk XYZ 1
154 Mary Highlands ABC 1
154 Mary Bristol ABC 2
154 Mary Chester ABC 3
I want my RowNumber (or change the column name if need be) to change based on the [Address] column for each [emp_code]. If the employee has the SAME address, it should have the same value (that is, 1). Else, it should give the values as in the case of employee "Mary" (above output).
I am assuming the Row_Number() function is not the right one to be used for what I'm after.
Any help would be appreciated.
I think you want DENSE_RANK here rather than ROW_NUMBER():
SELECT [ID], [Name], [Address], [Company],
DENSE_RANK() OVER (PARTITION BY [emp_code]
ORDER BY [Address]) AS DenseRank
FROM [tbl1];
Demo

Join on same column multiple times

how do I join on the same column multiple times, one of the tables is a configuration table with values and other is the data table.
T1:
ID Seq Code1 Code2 Code3
1 001 101 203 305
1 002 107
2 001 103
3 005 213
4 009 320
5 001 101 314
T2 (Config table):
Value Description
101 Strawberry
103 Raspberry
107 Blueberry
111 Banana
203 Cashews
213 Almonds
305 Bellpepper
320 Tomatoes
314 Potatoes
I need to display the code and its relevant description for each ID.
Expected Output:
ID Code1 Description Code2 Description Code3 Description
1 101 Strawberry 203 Cashews 305 Bellpepper
1 107 Blueberry
2 103 Raspberry
3 213 Almonds
4 320 Tomatoes
5 101 Strawberry 314 Potatoes
This is what I have tried so far, however, it is not giving me the desired output:
select distinct ID,code1, T2.description,
code2, T2.description, code3, T2.description
from T1,T2
where (T1.Code1=T2.Value OR T1.Code2=T2.Value or T1.Code3=T2.Value)
How can I achieve this? Sorry if my question is confusing, I can provide more details if needed.
That's multiple outer join with the t2 table:
select a.id, a.seq,
a.code1, d1.description,
a.code2, d2.description,
a.code3, d3.description
from t1 a left join t2 d1 on a.code1 = d1.value
left join t2 d2 on a.code2 = d2.value
left join t2 d3 on a.code3 = d3.value
order by a.id, a.seq;
ID SEQ CODE1 DESCRIPTIO CODE2 DESCRIPTIO CODE3 DESCRIPTIO
----- --- ---------- ---------- ---------- ---------- ---------- ----------
1 001 101 Strawberry 203 Cashews 305 Bellpepper
1 002 107 Blueberry
2 001 103 Raspberry
3 005 213 Almonds
4 009 320 Tomatoes
5 001 101 Strawberry 314 Potatoes

How to eliminate duplicates in a SQL table with the following values?

S_No. Name HRA
1 SS 123
2 SS 123
3 SS 123
4 SS 124
5 SA 222
6 SA 222
7 SA 221
8 SE 222
9 SE 123
10 SE 123
Desired Result
S_No. Name HRA
1 SS 123
4 SS 124
5 SA 222
7 SA 221
8 SE 222
9 SE 123
select min(s_no), name, hra
from table_name
group by name, hra

Counting the duplicate and setting the record in calculated column in SQL

I have a table Student_Information, with columns and data like:
ID StudentName FatherName NIC No_of_Childrens Date_of_Birth Date_of_Admission
1 Mark John 85 2010-04-01 2015-04-19
2 Akbar Aslam 89 2009-05-01 2015-04-19
3 Percul John 85 2010-04-01 2015-04-19
4 Ali Aslam 89 2009-05-01 2015-04-19
5 Diglor John 85 2010-04-01 2015-04-19
6 Sabi Aslam 89 2009-05-01 2015-04-19
I want to count the NIC column for duplicates and give numbers to no of childs column. Like this:
ID StudentName FatherName NIC No_of_Childrens Date_of_Birth Date_of_Admission
1 Mark John 85 1 2010-04-01 2015-04-19
2 Akbar Aslam 89 1 2009-05-01 2015-04-19
3 Percul John 85 2 2010-04-01 2015-04-19
4 Ali Aslam 89 2 2009-05-01 2015-04-19
5 Diglor John 85 3 2010-04-01 2015-04-19
6 Sabi Aslam 89 3 2009-05-01 2015-04-19
Try the below snippet -
SELECT ID, StudentName, FatherName, NIC,No_of_Childrens,Date_of_Birth, Date_of_Admission
FROM
(
SELECT ID, StudentName, FatherName, NIC,Date_of_Birth, Date_of_Admission,No_of_Childrens = RANK() OVER(PARTITION BY NIC ORDER BY ID)
FROM Student_Information) A ORDER BY ID

How to extract info based on the latest row

I have two tables:-
TABLE A :-
ORNO DEL PONO QTY
801 123 1 80
801 123 2 60
801 123 3 70
801 151 1 95
801 151 3 75
802 130 1 50
802 130 2 40
802 130 3 30
802 181 2 55
TABLE B:-
ORNO PONO STATUS ITEM
801 1 12 APPLE
801 2 12 ORANGE
801 3 12 MANGO
802 1 22 PEAR
802 2 22 KIWI
802 3 22 MELON
I wish to extract the info based on the latest DEL (in Table A) using SQL. The final output should look like this:-
OUTPUT:-
ORNO PONO STATUS ITEM QTY
801 1 12 APPLE 95
801 2 12 ORANGE 60
801 3 12 MANGO 75
802 1 22 PEAR 50
802 2 22 KIWI 55
802 3 22 MELON 30
Thanks.
select b.*, y.QTY
from
(
select a.ORNO, a.PONO, MAX(a.DEL) [max]
from #tA a
group by a.ORNO, a.PONO
)x
join #tA y on y.ORNO = x.ORNO and y.PONO = x.PONO and y.DEL = x.max
join #tB b on b.ORNO = y.ORNO and b.PONO = y.PONO
Output:
ORNO PONO STATUS ITEM QTY
----------- ----------- ----------- ---------- -----------
801 1 12 APPLE 95
801 2 12 ORANGE 60
801 3 12 MANGO 75
802 1 22 PEAR 50
802 2 22 KIWI 55
802 3 22 MELON 30