Rank consecutive null values - sql-server

I want to rank consecutive null value for my records. Every record will be rank as 1. For the null value that only appear once, the rank will also be 1. But for the null values that appear in a consecutive way, the rank will be 1 for the first record and 2 for the second record and so on. Here's my code.
CREATE TABLE #my_table
(
id BIGINT IDENTITY PRIMARY KEY
,fruit varchar(100)
);
INSERT INTO #my_table
SELECT 'apple'
UNION ALL SELECT 'apple'
UNION ALL SELECT NULL
UNION ALL SELECT 'pineapple'
UNION ALL SELECT 'banana'
UNION ALL SELECT NULL
UNION ALL SELECT NULL
UNION ALL SELECT 'orange'
select * from #my_table
Intended result
+----+-----------+------+
| id | fruit | rank |
+----+-----------+------+
| 1 | apple | 1 |
| 2 | apple | 1 |
| 3 | NULL | 1 |
| 4 | pineapple | 1 |
| 5 | banana | 1 |
| 6 | NULL | 1 |
| 7 | NULL | 2 |
| 8 | orange | 1 |
+----+-----------+------+
How should I query it?
Please help!

You can use difference of ROW_NUMBER to get the grouping of consecutive NULL values:
WITH Cte AS(
SELECT *,
g = ROW_NUMBER() OVER(ORDER BY id)
- ROW_NUMBER() OVER(PARTITION BY fruit ORDER BY id)
FROM #my_table
)
SELECT
id,
fruit,
CASE
WHEN fruit IS NULL THEN ROW_NUMBER() OVER(PARTITION BY fruit, g ORDER BY id)
ELSE 1
END AS rank
FROM Cte
ORDER BY id;
ONLINE DEMO

CREATE TABLE #my_table
(
id BIGINT IDENTITY PRIMARY KEY
,fruit varchar(100)
);
INSERT INTO #my_table
SELECT 'apple'
UNION ALL SELECT 'apple'
UNION ALL SELECT NULL
UNION ALL SELECT 'pineapple'
UNION ALL SELECT 'banana'
UNION ALL SELECT NULL
UNION ALL SELECT NULL
UNION ALL SELECT 'orange'
;
WITH REC_CTE (id,fruit,ranks)
AS (
-- Anchor definition
SELECT id,
fruit,
1 as ranks
FROM #my_table
WHERE fruit is not null
-- Recursive definition
UNION ALL
SELECT son.id,
son.fruit,
case when son.fruit is null AND father.fruit is null then
father.ranks + 1
else
1
end as ranks
FROM #my_table son INNER JOIN
REC_CTE father
on son.id = father.id +1
WHERE son.fruit is null
--AND father.fruit is null
)
SELECT * from REC_CTE order by id
DROP TABLE #my_table

Following solution doesn't use recursion (limited to 32767 level = ~ rows depending on solution) and also it uses only two agregate/ranking functions (SUM and DENSE_RANK):
;WITH Base
AS (
SELECT *, IIF(fruit IS NULL, SUM(IIF(fruit IS NOT NULL, 1, 0)) OVER(ORDER BY id), NULL) AS group_num
FROM #my_table t
)
SELECT *, IIF(fruit IS NULL, DENSE_RANK() OVER(PARTITION BY group_num ORDER BY id), 1) rnk
FROM Base b
ORDER BY id
Results:
id fruit group_num rnk
--- --------- --------- ---
100 apple NULL 1
125 apple NULL 1
150 NULL 2 1
175 pineapple NULL 1
200 banana NULL 1
225 NULL 4 1
250 NULL 4 2
275 orange NULL 1
300 NULL 5 1
325 NULL 5 2
350 NULL 5 3

Related

How to group parent/child records by flag and collapse to a single record for the combo where the flag is true if one or more expanded was true?

Sorry about the long title it was difficult to describe what I am trying to do. It is simple to understand as you will see below, but difficult to summarise into a title.
I have a query that returns the following data:
| ParentID | ChildID | Flag |
| 100 | 1 | 0 |
| 100 | 1 | 1 |
| 100 | 2 | 0 |
| 100 | 2 | 0 |
| 200 | 1 | 1 |
| 200 | 1 | 1 |
The flag column will only have 1 or 0 in it.
I need to filter the results so that there is only a single row for each parent/child combination.
The flag should be 1 if there were one or more records in the full result set above where it was 1 for the parent/child combo, otherwise it should be zero.
So the result if applied to the above would be:
| ParentID | ChildID | Flag |
| 100 | 1 | 1 |
| 100 | 2 | 0 |
| 200 | 1 | 1 |
I have this working with just the ChildID and Flag columns:
DECLARE #InMemoryResultsFirstPass AS TABLE (ChildID Integer, Flag Integer)
DECLARE #InMemoryResultsRecs AS TABLE (ChildID Integer, Flag Integer)
INSERT INTO #InMemoryResultsFirstPass
SELECT 1 ChildID, 0 Flag
UNION SELECT 1 ChildID, 1 Flag
UNION SELECT 2 ChildID, 0 Flag
UNION SELECT 2 ChildID, 0 Flag
UNION SELECT 1 ChildID, 1 Flag
UNION SELECT 1 ChildID, 1 Flag
select * from #InMemoryResultsFirstPass
INSERT INTO #InMemoryResultsRecs
SELECT DISTINCT
result.*
FROM #InMemoryResultsFirstPass imr
CROSS APPLY (
SELECT TOP 1
*
FROM #InMemoryResultsFirstPass imrfp
WHERE imrfp.ChildID = imr.ChildID
ORDER BY imrfp.Flag DESC
) result
select * from #InMemoryResultsRecs
But have been unable to work out how to do this once I add it in the ParentID column. I have tried a few different approaches trying to do a nested query in the CROSS APPLY with GROUP BY on the ParentID but no matter what I try I lose the ParentID = 200 records:
DECLARE #InMemoryResultsFirstPass AS TABLE (ParentID Integer, ChildID Integer, Flag Integer)
DECLARE #InMemoryResultsRecs AS TABLE (ParentID Integer, ChildID Integer, Flag Integer)
INSERT INTO #InMemoryResultsFirstPass
SELECT 100 ParentID, 1 ChildID, 0 Flag
UNION SELECT 100 ParentID, 1 ChildID, 1 Flag
UNION SELECT 100 ParentID, 2 ChildID, 0 Flag
UNION SELECT 100 ParentID, 2 ChildID, 0 Flag
UNION SELECT 200 ParentID, 1 ChildID, 1 Flag
UNION SELECT 200 ParentID, 1 ChildID, 1 Flag
select * from #InMemoryResultsFirstPass
INSERT INTO #InMemoryResultsRecs
SELECT DISTINCT
result.*
FROM #InMemoryResultsFirstPass imr
CROSS APPLY (
SELECT TOP 1
*
FROM #InMemoryResultsFirstPass imrfp
WHERE imrfp.ChildID = imr.ChildID
ORDER BY imrfp.Flag DESC
) result
select * from #InMemoryResultsRecs
Any assistance would be greatly appreciated.
Thank you for your time.
Actually, I just worked it out:
SELECT ParentID, ChildID, MAX(Flag) AS Flag
FROM #InMemoryResultsFirstPass
GROUP BY ParentID, ChildID
ORDER BY ParentID, ChildID
Hopefully this helps someone else :-)

How to generate an External ID based ID's that has negative value on price

I have this Data set
InvoiceID CDamount companyname
1 2500 NASA
1 -2500 NASA
2 1600 Airjet
3 5000 Boeing
4 -600 EXEarth
5 8000 SpaceX
5 -8000 SpaceX
I want to be able to get that as shown below:
External ID CDamount companyname
1 2500 NASA
1-C -2500 NASA
2 1600 Airjet
3 5000 Boeing
4 -600 EXEarth
5 8000 SpaceX
5-C -8000 SpaceX
I cannot use CASE WHEN CDamount < 0 THEN InvoiceID + '-' + 'C' ELSE InvoiceID END AS "External ID" because some of other companies have negative amount as well that do not fall under this category.
I was wondering how can I say IF InvoiceID is Duplicated AND CDAmount is Negative then Create a new External ID?
Is this something possible?
Below you can create the sample data
Create Table #Incident (
InvoiceID int,
CDamount int,
Companyname Nvarchar(255))
insert into #Incident Values (1,2500,'NASA')
insert into #Incident Values (1,-2500,'NASA')
insert into #Incident Values (2,1600,'Airjet')
insert into #Incident Values (3, 5000, 'Boeing')
insert into #Incident Values (4, -600, 'ExEarth')
insert into #Incident Values (5,8000,'SpaceX')
insert into #Incident Values (5, -8000, 'SpaceX')
Here is What I used but as I mentioned since ID number 4 has negative value as well I get "-C" for it which I do not want to.
Select CASE WHEN T1.CDamount < 0
THEN CAST(T1.InvoiceID AS nvarchar (255)) + '-' + 'C'
ELSE CAST(T1.InvoiceID AS nvarchar (255))
END AS ExternalID,
T1.Companyname
from #Incident AS T1
So I got this based on my knowledge of SQL and that works for my case.
Not sure if it is an smart way to go with but can be a good start for someone who is struggling with a Scenario like this:
;With CTE1 AS (
SELECT Count(*) AS Duplicate, T1.InvoiceID
From #Incident AS T1
Group by T1.InvoiceID
),
Main AS (
Select CASE WHEN T1.CDamount < 0 AND T2.Duplicate > 1
THEN CAST(T1.InvoiceID AS nvarchar (255)) + '-' + 'C'
ELSE CAST(T1.InvoiceID AS nvarchar (255))
END AS ExternalID,
T1.InvoiceID AS count,
T1.CDamount,
T1.Companyname
from #Incident AS T1
Join CTE1 AS T2 ON T1.InvoiceID = T2.InvoiceID
)
SELECT * FROM Main
Alternative solution without CTE, using ROW_NUMBER() function.
SELECT
CASE WHEN CDAmount < 0 AND RowID > 1
THEN InvoiceID + '-C'
ELSE InvoiceID
END AS ExternalID
, CDAmount
, CompanyName
FROM
(
SELECT
CAST(InvoiceID AS NVARCHAR(255)) AS InvoiceID
, CDAmount
, CompanyName
, ROW_NUMBER() OVER (PARTITION BY InvoiceID ORDER BY CompanyName) AS RowID
FROM
#Incident
) AS SourceTable
The trick is using ROW_NUMBER() function to generate a sequence which resets when InvoiceID changes. Here's the subquery and its result. Use CASE statement when CDAmount is negative and RowID greater than 1.
SELECT
CAST(InvoiceID AS NVARCHAR(255)) AS InvoiceID
, CDAmount
, CompanyName
, ROW_NUMBER() OVER (PARTITION BY InvoiceID ORDER BY CompanyName) AS RowID
FROM
#Incident
Subquery result:
+-----------+----------+-------------+-------+
| InvoiceID | CDAmount | CompanyName | RowID |
+-----------+----------+-------------+-------+
| 1 | 2500 | NASA | 1 |
| 1 | -2500 | NASA | 2 |
| 2 | 1600 | Airjet | 1 |
| 3 | 5000 | Boeing | 1 |
| 4 | -600 | ExEarth | 1 |
| 5 | 8000 | SpaceX | 1 |
| 5 | -8000 | SpaceX | 2 |
+-----------+----------+-------------+-------+

Select rows based on count of child table

I have three entities: department, employee, and report. A department has many employees, each of whom has many reports. I want to select the one employee in each department who has the most reports. I have no idea how to even start this query. This question seems very similar, but I can't figure out how to manipulate those answers for what I want.
I have full access to the entire system, so I can make any changes necessary. In the event of a tie, it's safe to arbitrarily pick one of the results.
Department:
ID | Name
----|------
1 | DeptA
2 | DeptB
3 | DeptC
4 | DeptD
Employee:
ID | Name | DeptID
----|------|--------
1 | Joe | 1
2 | John | 1
3 | Emma | 2
4 | Jack | 3
5 | Sven | 3
6 | Axel | 4
7 | Brad | 4
8 | Jane | 4
Report:
ID | EmployeeID
----|------------
1 | 1
2 | 2
3 | 3
4 | 5
5 | 6
6 | 6
7 | 8
Desired result (assuming I queried names only):
Joe OR John (either is acceptable)
Emma
Sven
Axel
How to start this query? Well, get the information about each employee, the department, and the number of reports:
select e.name, e.deptid, count(*) as numreports
from employee e join
reports r
on e.id = r.employeeid
group by e.name, e.deptid;
Now you just want the largest count in each department. I would suggest row_number() or rank() depending on how you want to handle ties:
select er.*
from (select e.name, e.deptid, count(*) as numreports,
row_number() over (partition by e.deptid order by count(*) desc) as seqnum
from employee e join
reports r
on e.id = r.employeeid
group by e.name, e.deptid
) er
where seqnum = 1;
If you want the department name instead of number, you can join that in as well.
From your Question schema will be
SELECT * into #Department FROM(
select 1 ID,'DEPTA' NAME
UNION ALL
select 2,'DEPTB'
UNION ALL
select 3,'DEPTC'
UNION ALL
select 4,'DEPTD')TAB
SELECT * INTO #Employee FROM (
SELECT 1 ID ,'Joe' Name , 1 DeptID
UNION ALL
SELECT 2 , 'John' , 1
UNION ALL
SELECT 3 , 'Emma' ,2
UNION ALL
SELECT 4 ,'Jack' , 3
UNION ALL
SELECT 5 ,'Sven' , 3
UNION ALL
SELECT 6 , 'Axel' , 4
UNION ALL
SELECT 7 ,'Brad' , 4
UNION ALL
SELECT 8 ,'Jane' , 4)AS A
SELECT * INTO #Report FROM(
SELECT 1 ID ,1 EmployeeID
UNION ALL
SELECT 2, 2
UNION ALL
SELECT 3 ,3
UNION ALL
SELECT 4, 5
UNION ALL
SELECT 5, 6
UNION ALL
SELECT 6, 6
UNION ALL
SELECT 7, 8
UNION ALL
SELECT 8, 8
UNION ALL
SELECT 9, 8
)AS A
And you need to apply DENSE_RANK() for giving rank based on no of reports(count)
;WITH CTE AS(
select DEP.ID DEP_ID, DEP.NAME DEP,EMP.ID EMP_ID, EMP.Name EMP
,DENSE_RANK() OVER(PARTITION BY DEP.ID ORDER BY COUNT(REP.ID) DESC) REP_RANK
,COUNT(REP.ID) NO_OF_REP FROM #Department DEP
inner join #Employee emp on emp.deptid=dep.id
inner join #report rep on rep.EmployeeID=emp.id
GROUP BY DEP.ID, DEP.NAME ,EMP.ID, EMP.Name
)
SELECT DEP, EMP, NO_OF_REP FROM CTE WHERE REP_RANK=1
Here in the DEPTA Joe & John both will be picked because both are having 1 report count which is a max count in DEPTA.
And the result will be
+-------+------+-----------+
| DEP | EMP | NO_OF_REP |
+-------+------+-----------+
| DEPTA | Joe | 1 |
| DEPTA | John | 1 |
| DEPTB | Emma | 1 |
| DEPTC | Sven | 1 |
| DEPTD | Jane | 3 |
+-------+------+-----------+
Please try the below code:-
SELECT D.NAME
FROM (
SELECT C.NAME, RANK() OVER (
PARTITION BY C.DEPTID ORDER BY C.COUNTS DESC
) RNK
FROM (
SELECT EMPID, NAME, COUNT(EMPID) AS COUNTS, DEPTID
FROM DBO.REPORT AS A
JOIN DBO.EMPLO AS B ON A.EMPID = B.ID
GROUP BY EMPID, NAME, DEPTID
) AS C
) AS D
WHERE D.RNK = 1

Create the summary table SQL

I have a table of product stock (ProductStockTemp):
ProductStockId | ProductCode | ProductName | ProductStockClosingBalance
-------------------------------------------------------------------------
1 | A1 | A2 | 200
2 | B1 | B2 | 0
3 | A1 | A2 | 100
4 | C1 | C2 | -400
5 | B1 | B2 | 700
6 | C1 | C2 | 0
7 | D1 | D2 | 0
8 | D1 | D2 | 0
I would like to create a table like that
ProductCode | ProductName | TypeA | TypeB
------------------------------------------
A1 | A2 | 200 | 100
B1 | B2 | 0 | 700
C1 | C2 | -400 | 0
D1 | D2 | 0 | 0
It means the first product record will be the product type A and the latest record will be the product type B. I think i have to select the latest product record first. However, the problem arises when the product appears one time then it will choose this record. In this case, I want the product type B will be 0 since the first will be the product type A and the latest will be the type B. My query to select the latest product record in the table
select
p.ProductCode,
p.ProductName,
p.ProductStockClosingBalance
from
ProductStockTemp p
where
p.ProductStockId = (select MAX(q.ProductStockId)
from ProductStockTemp q
where p.ProductCode = q.ProductCode)
order by
p.ProductCode
I'm really stuck right now!
Most obvius in my opinion for this case:
;with cteStock as
(
select
p.ProductCode,
p.ProductName,
p.ProductStockClosingBalance,
row_number() over(partition by p.ProductCode, p.ProductName order by p.ProductStockId asc) rn_asc,
row_number() over(partition by p.ProductCode, p.ProductName order by p.ProductStockId desc) rn_desc,
from ProductStockTemp p
)
select
s.ProductCode, s.ProductName,
sum(case when s.rn_asc = 1 then s.ProductStockClosingBalance else 0) as TypeA,
sum(case when s.rn_desc = 1 and s.rn_asc != s.rn_desc then s.ProductStockClosingBalance else 0) as TypeB
from cteStock s
group by s.ProductCode, s.ProductName
but it's no good to have such a denormalized table with no info about period and names stored right here.
s.rn_asc != s.rn_desc - for the case when there is only one row.
Not sure about default zero but it's up to you and your task definition.
I suggest you to learn SQL GROUP BY Statement
SELECT ProductCode, ProductName, min(ProductStockClosingBalance) as TypeA, Max(ProductStockClosingBalance) as TypeB
FROM ProductStockTemp p
GROUP BY ProductCode, ProductName
This query does what you want.
declare #prod table(ProductStockId int, ProductCode varchar(10), ProductName varchar(10), ProductStockClosingBalance int)
insert #prod values
( 1 , 'A1' , 'A2' , 200),
( 2 , 'B1' , 'B2' , 0),
( 3 , 'A1' , 'A2' , 100),
( 4 , 'C1' , 'C2' , -400),
( 5 , 'B1' , 'B2' , 700),
( 6 , 'C1' , 'C2' , 0),
( 7 , 'D1' , 'D2' , 0),
( 8 , 'D1' , 'D2' , 0 )
;with prod as(
select *
,ROW_NUMBER() over(partition by productcode order by ProductStockId) rn1
,ROW_NUMBER() over(partition by productcode order by ProductStockId desc) rn2
from #prod
)
select productcode,ProductName,
(select ProductStockClosingBalance from prod p1 where p.productcode=p1.ProductCode and p1.rn1=1) TypeA,
ProductStockClosingBalance typeB
from prod p
where rn2=1
Results:
productcode ProductName TypeA typeB
A1 A2 200 100
B1 B2 0 700
C1 C2 -400 0
D1 D2 0 0
use below query
;With ctef as (select ProductCode , ProductName , ProductStockClosingBalance
row_number()over(partition by ProductCode , ProductName ordered by ProductStockId asc) as r from ProductStock)
, ctef1 as ( select * from ctef where r =1)
, ctel as (select ProductCode , ProductName , ProductStockClosingBalance
row_number()over(partition by ProductCode , ProductName ordered by ProductStockId desc)as r2 from ProductStock )
, ctel1 as ( select * from ctel where r =1)
select f.ProductCode , f.ProductName ,f.ProductStockClosingBalance as typea ,nullif(l.ProductStockClosingBalance ,f.ProductStockClosingBalance ) as typeB
from ctef1 f left join ctel1 s
on f.ProductCode =s.ProductCode and f.ProductName =s.ProductName
if using sql servere 2012 or above Analytic Functions FIRST_VALUE (Transact-SQL) and LAST_VALUE (Transact-SQL) can be used .

TSQL UNION GETTING UNIQUE VALUE

I'm trying to get the unique record between two databases based on two criteria. The criteria is:
If the data is found in database 1 (#SCCM in my example below), it
is given preference
Grab the MAX resource id within the selected database
Here is an example, which is half working. The database preference is working, but the maximum resource id WITHIN that database isn't. Right now it's selecting the max between both #SMS and #SCCM
DECLARE #SMS TABLE (
name0 varchar(100),
resid int
)
DECLARE #SCCM TABLE (
name0 varchar(100),
resid int
)
INSERT INTO #SMS
SELECT 'TEST', 1000 UNION
SELECT 'TEST', 1500 UNION
SELECT 'TEST1', 2000 UNION
SELECT 'TEST2', 3000 UNION
SELECT 'TEST3', 4000
INSERT INTO #SCCM
SELECT 'TEST', 100 UNION
SELECT 'TEST', 150 UNION
SELECT 'TEST1', 200 UNION
SELECT 'TEST2', 300
SELECT MIN(SMSDB) as SMSDB, MAX(Resid), Name0 FROM
(
SELECT name0, resid, 2 as SMSDB FROM #SMS
UNION ALL
SELECT name0, resid, 1 as SMSDB FROM #SCCM
) as tbl
GROUP BY NAME0
Expected results:
SMSDB | Resid | Name0
----------------------
1 | 150 | TEST
1 | 200 | TEST1
1 | 300 | TEST2
2 | 4000 | TEST3
You can use partitions:
;WITH tbl as
(
SELECT name0, resid, 2 as SMSDB FROM SMS
UNION ALL
SELECT name0, resid, 1 as SMSDB FROM SCCM
),
t as (
SELECT *,
ROW_NUMBER()
over (partition By name0 order by SMSDB, resid desc )
as rn
FROM tbl
)
SELECT * FROM t
WHERE rn = 1
Results:
| NAME0 | RESID | SMSDB | RN |
------------------------------
| TEST | 150 | 1 | 1 |
| TEST1 | 200 | 1 | 1 |
| TEST2 | 300 | 1 | 1 |
| TEST3 | 4000 | 2 | 1
The partition solution may in fact be better, but it hurts my brain. What about just excluding the values in SMS if they exist in SCCM?
SELECT MIN(SMSDB) as SMSDB, MAX(Resid), Name0 FROM
(
SELECT name0, resid, 2 as SMSDB FROM #SMS SMS
WHERE NOT EXISTS (SELECT * FROM #SCCM WHERE name0 = SMS.name0)
UNION ALL
SELECT name0, resid, 1 as SMSDB FROM #SCCM
) as tbl
GROUP BY NAME0
or even
SELECT 1 as SMSDB, MAX(resid), name0 FROM #SCCM
GROUP BY name0
UNION ALL
SELECT 2 as SMSDB, MAX(resid), name0 FROM #SMS SMS
WHERE NOT EXISTS (SELECT * FROM #SCCM WHERE name0 = SMS.name0)
GROUP BY name0
ORDER BY name0

Resources