I have a table like below
ID | s_id | mark
-----------------------
1 | 2 | 10
2 | 5 | 9
3 | 7 | 8
4 | 2 | 8
5 | 2 | 10
6 | 5 | 7
7 | 3 | 7
8 | 2 | 9
9 | 5 | 8
I need to get SQL query for output like:-
mark column need to be in descending order.
Same s_id should not repeat more than 2 times
if same s_id repeats more than 2 times, ignore the 3rd result
ID | s_id | mark
-----------------------
1 | 2 | 10
2 | 2 | 9
3 | 3 | 7
4 | 5 | 9
5 | 5 | 8
6 | 7 | 8
Assuming you're using SQL Server, you can just use ROW_NUMBER() to assign a row number to each s_id group based on a descending order of the mark column. Then, retain only those records where this row number is 1 or 2.
SELECT
t.ID, t.s_id, t.mark
FROM
(
SELECT ID, s_id, mark, ROW_NUMBER() OVER (PARTITION BY s_id ORDER BY mark DESC) rn
FROM yourTable
) t
WHERE t.rn <= 2
ORDER BY t.s_id;
Note: You'll notice that the record (s_id, mark) = (2, 10) appears twice in my result set. Based on your input data, this is what is generated. If you really intended to also remove duplicate (s_id, mark) pairs, then let us know and a small correction can be added to the query.
Output:
Demo here:
Rextester
try this code.
;WITH cte
AS (
SELECT ROW_NUMBER() OVER (PARTITION BY s_id
ORDER BY ( SELECT 0)) RN,ID,s_id,mark
FROM aaa)
select RN,ID,s_id,mark FROM cte
WHERE RN <= 2
order by s_id,mark desc;
Related
how do i do a select where count = select all sequence rows has the same column value as current column value only if there 3 in sequence (row after row with no holes)
NAME | NUM | DATE
---------------------------------
Name 1 | 1 | '2019-01-07 12:11:11:001'
Name 2 | 1 | '2019-01-07 12:11:12:002'
Name 3 | 3 | '2019-01-07 12:11:13:003'
Name 4 | 2 | '2019-01-07 12:11:14:004'
Name 5 | 2 | '2019-01-07 12:11:15:005'
Name 6 | 2 | '2019-01-07 12:11:16:006'
Name 7 | 4 | '2019-01-07 12:11:17:007'
Name 8 | 5 | '2019-01-07 12:11:18:008'
The results should be where count sequence=3
NAME | NUM | DATE
---------------------------------
Name 4 | 2 | '2019-01-07 12:11:14:004'
Name 5 | 2 | '2019-01-07 12:11:15:005'
Name 6 | 2 | '2019-01-07 12:11:16:006'
because 2 appears 3 times in sequence
You can use the following query:
SELECT [NAME], [NUM], [DATE],
ROW_NUMBER() OVER (ORDER BY [DATE]) -
ROW_NUMBER() OVER (PARTITION BY NUM ORDER BY [DATE]) AS grp
FROM mytable
to get:
NAME NUM DATE grp
----------------------------------------
Name 1 1 2019-01-07 12:11:11 0
Name 2 1 2019-01-07 12:11:12 0
Name 4 2 2019-01-07 12:11:13 3
Name 5 2 2019-01-07 12:11:14 3
Name 6 2 2019-01-07 12:11:15 3
Name 3 3 2019-01-07 12:11:16 2
Name 7 4 2019-01-07 12:11:17 6
Name 8 5 2019-01-07 12:11:18 7
As you can see calculated column grp can be used in order to identify islands of consecutive records having the same NUM value.
You can then wrap the above query in a CTE and do:
;WITH GroupCTE AS (
SELECT [NAME], [NUM], [DATE],
ROW_NUMBER() OVER (ORDER BY [DATE]) -
ROW_NUMBER() OVER (PARTITION BY NUM ORDER BY [DATE]) AS grp
FROM mytable
)
SELECT t.*
FROM GroupCTE AS t
JOIN (SELECT NUM, grp
FROM GroupCTE
GROUP BY NUM, grp
HAVING COUNT(*) = 3) AS g ON t.NUM = g.NUM AND t.grp = g.grp
I couldn't find an answer on my question since all questions similar to this one aren't using a nullable int in the max value and getting 1 column out of it.
My table is as follows:
| ContractId | ContractNumber | ContractVersion |
+------------+----------------+-----------------+
| 1 | 11 | NULL |
| 2 | 11 | 1 |
| 3 | 11 | 2 |
| 4 | 11 | 3 | --get this one
| 5 | 24 | NULL |
| 6 | 24 | 1 | --get this one
| 7 | 75 | NULL | --get this one
The first version is NULL and all following versions get a number starting with 1.
So now I only want to get the rows of the latest contracts (as shown in the comments behind the rows).
So for each ContractNumber I want to select the ContractId from the latest ContractVersion.
The MAX() function wont work since it's a nullable int.
So I was thinking to use the ISNULL(ContractVersion, 0) in combination with the MAX() function, but I wouldn't know how.
I tried the following code:
SELECT
ContractNumber,
MAX(ISNULL(ContractVersion, 0))
FROM
Contracts
GROUP BY
ContractNumber
...which returned all of the latest version numbers combined with the ContractNumber, but I need the ContractId. When I add ContractId in the SELECT and the GROUP BY, I'm getting all the versions again.
The result should be:
| ContractId |
+------------+
| 4 |
| 6 |
| 7 |
It's just a simple application of ROW_NUMBER() when you're wanting to select rows based on Min/Max:
declare #t table (ContractId int, ContractNumber int, ContractVersion int)
insert into #t(ContractId,ContractNumber,ContractVersion) values
(1,11,NULL ),
(2,11, 1 ),
(3,11, 2 ),
(4,11, 3 ),
(5,24,NULL ),
(6,24, 1 ),
(7,75,NULL )
;With Numbered as (
select *,ROW_NUMBER() OVER (
PARTITION BY ContractNumber
order by ContractVersion desc) rn
from #t
)
select
*
from
Numbered
where rn = 1
this will work:
select ContractId,max(rank),ContractNumber from(select *,rank() over(partition by
ContractVersion order by nvl(ContractVersion,0)) desc ) rank from tablename) group by
ContractId,max(rank),ContractNumber;
Select * from LoanAccount main INNER JOIN LoanSubAccount sub
WHERE main.LoanAccountID = sub.LoanAccountID
AND sub.LoanStatus = 4
My objective is to retrieve rows with LoanStatus = 4 but replace the amount with records with LoanStatus = 2.
End result expected to be
WITH cte AS (
SELECT *,
ROW_NUMBER() OVER (PARTITION BY LoanAccountID, LoanStatus
ORDER BY LoanSubAccountID) rn
FROM LoanSubAccount
)
SELECT t1.LoanSubAccountID,
t1.LoanAccountID,
t1.LoanStatus,
t1.CommodityType,
t2.Amount
FROM cte t1
INNER JOIN cte t2
ON t1.rn = t2.rn AND
t1.LoanStatus > t2.LoanStatus
Rather than giving a verbose explanation, I would rather show a table representing what the above CTE would look like:
rn | LoanSubAccountID | LoanAccountID | LoanStatus | CommodityType | Amount
1 | 1 | 1 | 2 | 1 | 100
2 | 2 | 1 | 2 | 2 | 200
1 | 3 | 1 | 4 | 3 | 150
2 | 4 | 1 | 4 | 4 | 150
If I read your requirement correctly, you want to connect rows having the same row number from the two different loan statuses. The join query I gave above does this.
I have a table containing employees id, year id, client id, and the number of sales. For example:
--------------------------------------
id_emp | id_year | sales | client id
--------------------------------------
4 | 1 | 14 | 1
4 | 1 | 10 | 2
4 | 2 | 11 | 1
4 | 2 | 17 | 2
For a employee, I want to obtain rows with the minimum sales per year and the minimum sales of the previous year.
One of the queries I tried is the following:
select distinct
id_emp,
id_year,
MIN(sales) OVER(partition by id_emp, id_year) AS min_sales,
LAG(min(sales), 1) OVER(PARTITION BY id_emp, id_year
ORDER BY id_emp, id_year) AS previous
from facts
where id_emp = 4
group by id_emp, id_year, sales;
I get the result:
-------------------------------------
id_emp | id_year | sales | previous
-------------------------------------
4 | 1 | 10 | (null)
4 | 1 | 10 | 10
4 | 2 | 11 | (null)
but I expect to get:
-------------------------------------
id_emp | id_year | sales | previous
-------------------------------------
4 | 1 | 10 | (null)
4 | 2 | 11 | 10
SQL Fiddle
Oracle 11g R2 Schema Setup:
CREATE TABLE EMPLOYEE_SALES ( id_emp, id_year, sales, client_id ) AS
SELECT 4, 1, 14, 1 FROM DUAL
UNION ALL SELECT 4, 1, 10, 2 FROM DUAL
UNION ALL SELECT 4, 2, 11, 1 FROM DUAL
UNION ALL SELECT 4, 2, 17, 2 FROM DUAL;
Query 1:
SELECT ID_EMP,
ID_YEAR,
SALES AS SALES,
LAG( SALES ) OVER ( PARTITION BY ID_EMP ORDER BY ID_YEAR ) AS PREVIOUS
FROM (
SELECT e.*,
ROW_NUMBER() OVER ( PARTITION BY id_emp, id_year ORDER BY sales ) AS RN
FROM EMPLOYEE_SALES e
)
WHERE rn = 1
Query 2:
SELECT ID_EMP,
ID_YEAR,
MIN( SALES ) AS SALES,
LAG( MIN( SALES ) ) OVER ( PARTITION BY ID_EMP ORDER BY ID_YEAR ) AS PREVIOUS
FROM EMPLOYEE_SALES
GROUP BY ID_EMP, ID_YEAR
Results - Both give the same output:
| ID_EMP | ID_YEAR | SALES | PREVIOUS |
|--------|---------|-------|----------|
| 4 | 1 | 10 | (null) |
| 4 | 2 | 11 | 10 |
You mean like this?
select id_emp, id_year, min(sales) as min_sales,
lag(min(sales)) over (partition by id_emp order by id_year) as prev_year_min_sales
from facts
where id_emp = 4
group by id_emp, id_year;
I believe it is because you are using sales column in your group by statement.
Try to remove it and just use
GROUP BY id_emp,id_year
You could get your desired output using ROW_NUMBER() and LAG() analytic functions.
For example,
Table
SQL> SELECT * FROM t;
ID_EMP ID_YEAR SALES CLIENT_ID
---------- ---------- ---------- ----------
4 1 14 1
4 1 10 2
4 2 11 1
4 2 17 2
Query
SQL> WITH DATA AS
2 (SELECT t.*,
3 row_number() OVER(PARTITION BY id_emp, id_year ORDER BY sales) rn
4 FROM t
5 )
6 SELECT id_emp,
7 id_year ,
8 sales ,
9 lag(sales) over(order by sales) previous
10 FROM DATA
11 WHERE rn =1;
ID_EMP ID_YEAR SALES PREVIOUS
---------- ---------- ---------- ----------
4 1 10
4 2 11 10
I need assistance on how to code duplicate Line IDs for the same Purchase Order and assign the additional line IDs with a new number. I would like to use Line ID + 100 for the additional duplicate rows. For example if Purchase Order #11 has three Line ID #5s then the first would stay as 5 and the second would be 501 and the third would be 502, however, I can only get a 1, 2 or 3 or if no duplicate just 1. I am not sure what to use to increment. I am hoping some one can assist or guide. Thank you
PurchaseOrderID LineID PackingList NewLineID
11 1 12323 1
11 1 78786 2
11 2 67523 1
11 3 44559 1
11 4 44559 1
11 5 96545 1
11 5 12323 2
11 5 34569 3
The Packing Slip causes the duplicates for the same line ID.
Below is what I am trying to use which is giving me the above NewLineID:
SELECT
PurchaseOrderID,
LineID,
PackingList,
ROW_NUMBER() over
(
partition by PurchaseOrderID, LineID
order by PurchaseOrderID, LineID
) as NewLineID
FROM PurchaseOrderTransactions
Using ROW_NUMBER and CASE:
WITH Cte AS(
SELECT
PurchaseOrderID,
LineID,
PackingList,
RN = ROW_NUMBER() OVER (PARTITION BY PurchaseOrderID, LineID ORDER BY LineID)
FROM PurchaseOrderTransactions
)
SELECT
PurchaseOrderID,
LineID,
PackingList,
NewLineID = CASE
WHEN RN = 1 THEN LineID
ELSE (LineID * 100) + (RN - 1)
END
FROM Cte
Without using a CTE:
SELECT
PurchaseOrderID,
LineID,
PackingList,
NewLineID =
CASE
WHEN ROW_NUMBER() OVER (PARTITION BY PurchaseOrderID, LineID ORDER BY LineID) = 1 THEN LineID
ELSE (LineID * 100) + (ROW_NUMBER() OVER (PARTITION BY PurchaseOrderID, LineID ORDER BY LineID) - 1)
END
FROM PurchaseOrderTransactions
SQL Fiddle
| PurchaseOrderID | LineID | PackingList | NewLineID |
|-----------------|--------|-------------|-----------|
| 11 | 1 | 12323 | 1 |
| 11 | 1 | 78786 | 101 |
| 11 | 2 | 67523 | 2 |
| 11 | 3 | 44559 | 3 |
| 11 | 4 | 44559 | 4 |
| 11 | 5 | 96545 | 5 |
| 11 | 5 | 12323 | 501 |
| 11 | 5 | 34569 | 502 |