Update the Rank 2 values based on Rank1 value column data - sql-server-2012

Data present in the table is as below:
Query:
select id, actv_ind, valid_from, valid_to, rnk from #ABC
Expected Data is as below:
I want to have the Valid_from date of Rank 2 records to be set as (Valid_from date of Rnk 1 record - 1)
Query tried:
select t1.id, t1.actv_ind, t1.rnk, case when t1.rnk >1 then t1.valid_from end VF1, case when t2.rnk > 1 then t1.valid_from - 1 end VF2
from #ABC t1
inner join #ABC t2
on t1.id = t2.id
and t1.actv_ind = t2.actv_ind

Just use LAG:
SELECT mtrl_id,
actv_ind,
rnk
LAG(DATEADD(DAY, -1, valid_from),1,valid_from) OVER (PARTITION BY mtrl_id ORDER BY Rank) AS valid_from,
valid_to
FROM dbo.YourTable;

Related

Records apart form max date

I have been helped by Metal to write a SQL as below
select id
, OrderDate
, RejectDate
, max(case when RejectDate = '1900-01-01' then '9999-12-31' else RejectDate end) as rSum
from tableA
group by id, OrderDate, RejectDate
Now, I would like to find out all the records for a partcular id below the max reject date to delete them from a transformation table
An option is to use row_number():
select
id,
OrderDate,
RejectDate
from (
select
t.*,
row_number() over(
partition by id
order by case when RejectDate = '1900-01-01' then '9999-12-31' else RejectDate end desc
) rn
from tableA t
) t
where rn > 1
The advantage of this technique is that it avoids aggregation, which may lead to better performance. Also, you can easily turn this into a delete statement by leveraging the concept of updateable CTE, as follows:
with cte as (
select
row_number() over(
partition by id
order by case when RejectDate = '1900-01-01' then '9999-12-31' else RejectDate end desc
) rn
from tableA t
)
delete from cte where rn > 1
This should work...
SELECT *
FROM tableA t1
INNER JOIN (
SELECT ID, MAX(RejectDate) as MaxRejectDate
FROM tableA) t2 ON t1.ID = t2.ID
WHERE t1.RejectDate < t2.MaxRejectDate

penultimate date for each record

I'm struggling with creation of select which shows me penultimate date for each record in my DB.
For example:
id date
1 01.01.2018
1 05.01.2018
1 06.02.2018
2 01.06.2018
2 03.06.2018
3 12.12.2017
Out of this record I need to write select, which shows me following:
ID max_date penultimate
1 06.02.2018 05.01.2018
2 03.06.2018 01.06.2018
3 12.12.2017 NULL
Any idea how to do it? many thanks in advance
Use conditional aggregation and the ANSI-standard row_number() or dense_rank() functions:
select id,
max(date) as max_date,
max(case when seqnum = 2 then date end) as penultimate_date
from (select t.*,
dense_rank() over (partition by id order by date desc) as seqnum
from t
) t
where seqnum in (1, 2)
group by id;
Use row_number() if the dates can be the same in the event of ties.
Use GROUP BY to get the MAX and a correlated subquery with another MAX but this time lower than the former.
SELECT
T.id,
MAX(T.date) max_date,
(
SELECT
MAX(N.date)
FROM
YourTable N
WHERE
N.id = T.id AND
N.date < MAX(T.date)
) penultimate
FROM
YourTable T
GROUP BY
T.id
Just an opitimized query:
;WITH cte AS
(
SELECT id AS ID
,[date] AS max_date
,LEAD ([date], 1, 0) OVER (PARTITION BY id ORDER BY [date] DESC) AS penultimate
,ROW_NUMBER() OVER(PARTITION BY id ORDER BY [date] DESC) AS RN
FROM Table3
)
SELECT ID,max_date,penultimate
FROM cte
WHERE RN=1
SQL Fiddle
I wrote in this way,
SELECT ID
,max(StartDate) MaxDate
,(
SELECT StartDate
FROM YourTable t2
WHERE t2.id = t1.id
ORDER BY StartDate DESC OFFSET 1 ROWS FETCH NEXT 1 ROW ONLY
) penultimate
FROM YourTable t1
GROUP BY id

Cumulative multiplication with window functions, 'exp' is not a valid windowing function

Is it possible doing cumulative multiply(below query) with window functions
select Id, Qty
into #temp
from(
select 1 Id, 5 Qty
union
select 2, 6
union
select 3, 3
)dvt
select
t1.Id
,exp(sum(log( t2.Qty))) CumulativeMultiply
from #temp t1
inner join #temp t2
on t2.Id <= t1.Id
group
by t1.Id
order
by t1.Id
Like:
select
t1.Id
,exp(sum(log( t2.Qty))) over (partition by t1.Id order by t1.Id rows between unbounded preceding and current row ) CumulativeMultiply
from #temp t1
inner join #temp t2
on t2.Id <= t1.Id
But get error:
The function 'exp' is not a valid windowing function, and cannot be used with the OVER clause
Update:
Result that actually I want:
Id CumulativeMultiply
----------- ----------------------
1 5
2 30
3 90
no need of self join for Sum Over(Order by) to find the previous records and multiply it
select
Id
,exp(sum(log( Qty))
over (order by Id )) CumulativeMultiply from #temp
Only aggregation function are valid windowing functions.
I didn't test the code, but you need to separate the 2 in a way:
SELECT Id, exp(cm) CumulativeMultiply
FROM (
select
Id
,sum(log(Qty)) over (partition by Id order by Id rows between unbounded preceding and current row ) cm
from #temp
) d

SQL Joining table with Min and Sec Min row

I want to join table 1 with table2 twice becuase I need to get the first minimum record and the second minimum. However, I can only think of using a cte to get the second minimum record. Is there a better way to do it?
Here is the table table:
I want to join Member with output table FirstRunID whose Output value is 1 and second RunID whose Output value is 0
current code I am using:
select memid, a.runid as aRunid,b.runid as bRunid
into #temp
from FirstTable m inner join
(select min(RunID), MemID [SecondTable] where ouput=1 group by memid)a on m.memid=a.memid
inner join (select RunID, MemID [SecondTable] where ouput=0 )b on m.memid=a.memid and b.runid>a.runid
with cte as
(
select row_number() over(partition by memid, arunid order by brunid ),* from #temp
)
select * from cte where n=1
You can use outer apply operator for this:
select * from t1
outer apply(select top 1 t2.runid from t2
where t1.memid = t2.memid and t2.output = 1 order by t2.runid) as oa1
outer apply(select top 1 t2.runid from t2
where t1.memid = t2.memid and t2.output = 0 order by t2.runid) as oa2
You can do this with conditional aggregation. Based on your results, you don't need the first table:
select t2.memid,
max(case when output = 1 and seqnum = 1 then runid end) as OutputValue1,
max(case when output = 0 and seqnum = 2 then runid end) as OutputValue2
from (select t2.*,
row_number() over (partition by memid, output order by runid) a seqnum
from t2
) t2
group by t2.memid;
declare #FirstTable table
(memid int, name varchar(20))
insert into #firsttable
values
(1,'John'),
(2,'Victor')
declare #secondtable table
(runid int,memid int,output int)
insert into #secondtable
values
(1,1,0),(1,2,1),(2,1,1),(2,2,1),(3,1,1),(3,2,0),(4,1,0),(4,2,0)
;with cte as
(
SELECT *, row_number() over (partition by memid order by runid) seq --sequence
FROM #SECONDTABLE T
where t.output = 1
union all
SELECT *, row_number() over (partition by memid order by runid) seq --sequence
FROM #SECONDTABLE T
where t.output = 0 and
t.runid > (select min(x.runid) from #secondtable x where x.memid = t.memid and x.output = 1 group by x.memid) --lose any O output record where there is no prior 1 output record
)
select cte1.memid,cte1.runid,cte2.runid from cte cte1
join cte cte2 on cte2.memid = cte1.memid and cte2.seq = cte1.seq
where cte1.seq = 1 --remove this test if you want matched pairs
and cte1.output = 1 and cte2.output = 0

need employee details who has only one employment

need employee details who has only one employment
for example this is the table
ID Name StartDate EndDate
1 Fischel 01-May-97 Jan-99
1 Fischel 08-May-92 02-Feb-99
1 Fischel 11-May-92 04-May-10
2 David 10-aug-1980 05-May-1981
3 John 12-sep-1988 06-June-2009
3 John 23-Aug-92 01-Nov-11
Output like this
2 David 10-aug-1980 05-May-1981
Select * from
(select *,row_number() over (partition by id order by StartDate desc ) as rnm
from your table
)Derived_Table
where Derived_Table.rnm=1;
OR
select * from table where id in (
select id
from table group by id having count(*) =1) z;
Use NOT EXISTS to return a user if same user-name doesn't have any other dates.
select ID, Name, StartDate, EndDate
from tablename t1
where not exists (select 1 from tablename t2
where t2.id = t1.id
and (t2.StartDate <> t1.StartDate
or t2.EndDate <> t1.EndDate))
Or, have a sub-query that return all id only appearing once:
select ID, Name, StartDate, EndDate
from tablename t1
where id in (select id from tablename
group by id
having count(*) = 1)