Rewrite SQL with LEFT JOIN INSTEAD OF OUTER APPLY - sql

CREATE TABLE #ledgertxn (
txnno int,
lid int,
flid int,
txndate date,
lname varchar(50),
debit int,
credit int,
ledgername varchar(50),
drcr varchar(2),
txntype varchar(30)
)
SELECT
Limit1.Txnno,
Limit1.Txndate,
Limit1.Particulars,
Limit1.Debit,
Limit1.Credit
FROM
(SELECT DISTINCT
txnno
FROM
#ledgertxn ) Distinct1
OUTER APPLY
(SELECT TOP 1
Project2.Txnno,
Project2.Txndate,
Project2.Particulars,
Project2.Debit,
Project2.Credit
FROM
( SELECT
Extent2.txnno,
Extent2.txndate,
Extent2.ledgername AS Particulars,
Extent2.debit,
Extent2.credit,
Extent2.lid
FROM
#ledgertxn Extent2
WHERE
Distinct1.txnno = Extent2.txnno ) Project2
ORDER BY
Project2.Lid desc ) AS Limit1

Related

Trying to create a temp table in Microsoft SQL Server but keep getting hit with an error

create table #PercentofPopulationVaccinated
(
continent nvarchar(255),
location nvarchar(255),
date datetime,
Population numeric,
people_fully_vaccinated numeric,
[%_of_pop_vaxxxed] numeric,
rn int
)
insert into #PercentofPopulationVaccinated
select
cd.continent, cd.location, cd.date, cd.population,
vac.people_fully_vaccinated,
(cast(vac.people_fully_vaccinated as int) / cd.population) * 100 as [%_of_pop_vaxxxed],
rn = row_number() over (partition by cd.Location order by (vac.people_fully_vaccinated / cd.population) * 100 desc, cd.Date)
from
coviddeaths as cd
join
covidvaccinations vac on cd.location = vac.location
and cd.date = vac.date
where
cd.continent is not null
select *
from #PercentofPopulationVaccinated
Error
Column name or number of supplied values does not match table definition
This error is odd; I'm sure it has to do with row number
First if that is your temporary table, you should first check if that table exist and drop it.
if object_id('tempdb..#PercentofPopulationVaccinated') is not null drop table #PercentofPopulationVaccinated
After that table definition should be:
create table #PercentofPopulationVaccinated
(
continent nvarchar(20),
data_location nvarchar(255),
data_date datetime,
total_population float,
people_fully_vaccinated float,
[%_of_pop_vaxxxed] decimal(8,4),
rn
)
and your select:
insert into #PercentofPopulationVaccinated (
continent,
data_location,
data_date,
total_population,
people_fully_vaccinated,
[%_of_pop_vaxxxed],
rn int
)
select
cd.continent,
cd.location, cd.date, cd.population,
vac.people_fully_vaccinated,
(vac.people_fully_vaccinated / cd.population) * 100 as [%_of_pop_vaxxxed],
rn = row_number() over (partition by cd.Location order by
(vac.people_fully_vaccinated / cd.population) * 100 desc, cd.Date)
from
coviddeaths as cd
join
covidvaccinations vac on cd.location = vac.location
and cd.date = vac.date
where
cd.continent is not null
select *
from #PercentofPopulationVaccinated

Preparing Rules Based List for Promotions

I am trying to put together a promotions list based on a set of rules
In table #productdetail I need to pick up items that have available =1 or available =2 and expected <=08052019 and not_sellable =0
From #product table I need to join id with pid in #productdetail and coalesce (vendor_id to get the vendor_id
Using the vendor_id that I have got from the step above i need to get the vendorname from #vendor table..
From the #Asin table I need to select the latest lastconfirmedasin from the list of items selected in step 1 and also ensure that I should not pick up data from rows where disable =1
From the #pageviews table I need to get the number of pageviews perday for every item based on the lastconfirmedasin for the list of items selected in step 1
Here for every item if I have datetype =day and also datetype =month then I need to select data from datetype =day and select sum(pageviews)/count of days to get the sum of pageviews per day
Also if I have only datetype =month then I need to select (sum(pageviews)(count of rows that have month)/30
Then in the promo table I need to select the average promo% got for the items selected in step 1..The average promo% is calculated as avg((pre_promo_price-promo_price)/promo_price*100))...This will be calculated at the vendor level...So if the vendor has 3 items then the average promo% is calculated as avg([avg((pre_promo_price-promo_price)/promo_price*100)
Then the cost is taken based on if in the table #promo has the date in between or included in promo_start and promo_end (for example today's date being 08/16/2019 is included then the pre promo cost is to taken as the cost for that item otherwise the cost from #product_detail table should be taken
Then I am calculating the priority of the items by vendor(meaning within each vendor ) prioritizing the items based on the power(calculated as pageviewsperday*price)-Higher value of an item within a vendor gets the first priority
The tables:
create table #productdetail
(
itemid int,
available int,
expected date,
isnotsellable int,
pid int,
vendor_id varchar(50),
cost float,
price float)
insert into #productdetail values
('123','1',NULL,'1','1','201','180','200'),
('125','2','08/05/2019','0','1','NULL','40','60'),
('127','1',NULL,'0','1','201','60','80'),
('129','2',NULL,'0','2','203','80','100'),
('131','1',NULL,'0','2','203','70','90'),
('133','1',NULL,'1','2','203','90','110'),
('135','1',NULL,'0','7','206','110','130'),
('137','1',NULL,'0','8','207','120','140'),
('139','1',NULL,'0','8','207','30','50'),
('141','1',NULL,'0','8','207','40','60'),
('143','1',NULL,'0','11','210','60','80'),
('145','1',NULL,'0','11','210','70','90'),
('147','1',NULL,'1','11','210','50','70'),
('149','1',NULL,'0','11','210','20','40'),
('151','1',NULL,'0','15','211','30','50'),
('153','1',NULL,'0','16','NULL','40','60'),
('155','2','08/29/2019','1','15','211','60','80'),
('157','2','08/04/2019','1','18','216','30','50'),
('159','2','08/06/2019','0','19','217','20','40'),
('161','2','08/03/2019','0','20','218','60','80'),
('163','2','08/15/2019','0','21','NULL','90','110')
create table #product
(id int,
vendor_id varchar(50)
)
insert into #product values
('1','201'),
('1','201'),
('1','201'),
('2','203'),
('2','203'),
('2','203'),
('7','NULL'),
('8','207'),
('8','207'),
('8','207'),
('11','210'),
('11','210'),
('11','210'),
('11','210'),
('15','211'),
('15','211'),
('15','211'),
('16','206'),
('18','216'),
('19','NULL'),
('20','218'),
('21','219')
create table #vendor
(vendor_id varchar(50),
vendorname varchar(50)
)
insert into #vendor values
('201','cola'),
('203','foam'),
('207','fill'),
('210','falon'),
('211','chiran'),
('216','Hummer'),
('218','sulps'),
('219','culp'),
('217','jko'),
('206','JINCO')
create table #asin
(disable int,
item_id int,
lastconfirmed datetime2,
lastconfirmedasin varchar(50)
)
insert into #asin values
('0','123','12/19/18 10:19 PM','iopyu'),
('1','123','12/19/16 10:19 PM','hjyug'),
('1','125','5/19/19 10:19 PM','uirty'),
('0','125','12/19/16 10:19 PM','1yuio'),
('0','127','2/19/19 10:19 PM','klbnm'),
('1','127','12/19/18 10:19 PM','lopgh'),
('0','127','12/19/16 10:19 PM','nmbh'),
('0','129','11/19/16 10:19 PM','jklh'),
('0','131','11/19/19 10:19 PM','werat'),
('1','133','6/19/19 10:19 PM','vbnwe'),
('0','133','1/19/19 10:19 PM','mnwer'),
('0','133','11/19/17 10:19 PM','sdert'),
('0','135','6/19/19 10:19 PM','vbsdx'),
('0','137','6/19/17 10:19 PM','bnxct')
create table #pageviews
(startdate date,
lastconfirmedasin varchar(50),
noofpageviews int,
datetype varchar(20))
insert into #pageviews values
('05/08/2017','hjyug','102','day'),
('09/05/2017','hjyug','201','day'),
('10/05/2019','hjyug','1002','day'),
('09/05/2018','iopyu','345','month'),
('10/06/2018','iopyu','545','month'),
('06/05/2019','1yuio','300','day'),
('06/06/2019','1yuio','200','day'),
('09/04/2019','uirty','150','day'),
('11/4/2019','uirty','200','day'),
('12/4/2019','nmbh','300','day'),
('04/15/2019','lopgh','400','day'),
('04/15/2019','klbnm','500','day'),
('04/16/2019','klbnm','1000','day'),
('04/15/2019','jklh','600','day'),
('04/15/2019','werat','700','day'),
('04/15/2019','sdert','800','day'),
('04/15/2019','mnwer','900','day'),
('04/15/2019','vbnwe','1000','day'),
('04/15/2019','vbsdx','1100','day'),
('04/25/2019','vbsdx','3000','day'),
('04/15/2019','bnxct','1200','month'),
('05/31/2019','bnxct','2200','month'),
('04/16/2019','bnxct','90','day'),
('04/17/2019','bnxct','100','day'),
('04/18/2019','bnxct','120','day')
create table #promo
(itemid int,
promo_cost float,
pre_promo_cost float,
promo_start varchar(50),
promo_end varchar(50))
insert into #promo values
('123','214.7','220','10/08/2019','22/08/2019'),
('123','225','230','01/02/2018','15/02/2018'),
('125','400','430','12/08/2019','19/08/2019'),
('125','380','390','15/02/2019','30/02/2019'),
('127','120','140','15/03/2019','30/03/2019'),
('129','80','100','15/04/2019','30/04/2019'),
('129','110','120','01/04/2019','10/04/2019'),
('131','80','100','01/02/2019','15/02/2019'),
('131','110','120','10/01/2019','15/01/2019'),
('133','230','420','10/01/2019','15/01/2019'),
('135','250','440','10/01/2019','15/01/2019'),
('137','270','460','10/01/2019','15/01/2019'),
('139','290','480','10/01/2019','15/01/2019'),
('141','310','500','10/01/2019','15/01/2019'),
('143','330','520','10/01/2019','15/01/2019'),
('145','350','540','10/08/2019','22/08/2019')
create table #output
(itemid int,
available int,
expected date,
isnotsellable int,
pid int,
vendor_id varchar(50),
vendorname varchar(50),
lastconfirmedasin varchar(50),
pageviewsperday int,
promoavg float,
cost float,
price float,
[power] int,
[priority] int)
insert into #output values
('151','1',NULL,'0','15','211','chiran','','0','0','30','50','0','1'),
('127','1',NULL,'0','1','201','cola','klbnm','750','6.3','60','80','60000','1'),
('125','2','08/05/2019','0','1','201','cola','','0','0','430','60','0','2'),
('143','1',NULL,'0','11','210','falon','','0','0','60','80','0','1'),
('145','1',NULL,'0','11','210','falon','','0','0','540','90','0','2'),
('149','1',NULL,'0','11','210','falon','','0','0','20','40','0','3'),
('137','1',NULL,'0','8','207','fill','bnxct','103','65.73','120','140','14420','1'),
('139','1',NULL,'0','8','207','fill','','0','0','30','50','0','2'),
('141','1',NULL,'0','8','207','fill','','0','0','40','60','0','3'),
('131','1',NULL,'0','2','203','foam','werat','700','30.16','70','90','63000','1'),
('135','1',NULL,'0','7','206','JINCO','vbsdx','2050','76','110','130','266500','1'),
('153','1',NULL,'0','16','206','JINCO','','0','0','40','60','0','2'),
('161','2','08/03/2019','0','20','218','sulps','','0','0','60','80','0','1')
Code tried:
; WITH firstsel AS (
SELECT itemid, available, expected, isnotsellable, pid, vendor_id
FROM #productdetail
WHERE available = 1
AND isnotsellable = 0
UNION ALL
SELECT itemid, available, expected, isnotsellable, pid, vendor_id
FROM #productdetail
WHERE available = 2
AND isnotsellable = 0
AND expected <= '20190805'
), addvendorid AS (
SELECT fs.itemid, fs.available, fs.expected, fs.pid, fs.isnotsellable,
coalesce(fs.vendor_id, p.vendor_id) AS vendor_id
FROM firstsel fs
JOIN #product p ON fs.pid = p.id
), asinnumbered AS (
SELECT item_id, lastconfirmedasin, row_number() OVER (PARTITION BY item_id ORDER BY lastconfirmedasin DESC) AS rowno
FROM #asin
)
SELECT av.itemid, av.available, av.expected, av.isnotsellable, av.pid,
av.vendor_id, v.vendorname, an.lastconfirmedasin,
isnull(pv.pageviews, 0) AS pageviews, isnull(p.promoavg, 0) AS promoavg
FROM addvendorid av
JOIN #vendor v ON av.vendor_id = v.vendor_id
LEFT JOIN (asinnumbered an
JOIN (SELECT lastconfirmedasin, SUM(pageviews) AS pageviews
FROM #pageviews
GROUP BY lastconfirmedasin) AS pv ON an.lastconfirmedasin = pv.lastconfirmedasin)
ON an.item_id = av.itemid
AND an.rowno = 1
LEFT JOIN (SELECT itemid, avg((pre_promo_price-promo_price)/promo_price*100) AS promoavg
FROM #promo
GROUP BY itemid) AS p ON p.itemid = av.itemid
ORDER BY av.itemid

Is there any way to speed up this query capturing customer information? Is there a better method for this data?

I have a long query but I'll try to break it down into it's parts.
First, there are the variables,
DECLARE #LocalCompanyCode VARCHAR(5)
SET #LocalCompanyCode = '09'
DECLARE #LocalDivisionCode VARCHAR(5)
SET #LocalDivisionCode = '001'
DECLARE #CustomerBaseFromDate DATETIME --CustomerBase
SET #CustomerBaseFromDate = '1/1/2019'
DECLARE #CustomerBaseToDate DATETIME
SET #CustomerBaseToDate = '5/30/2019'
DECLARE #RecurringBaseFromDate DATETIME --Recurring Base
SET #RecurringBaseFromDate = '1/1/2018'
DECLARE #RecurringBaseToDate DATETIME
SET #RecurringBaseToDate = '1/1/2019'
DECLARE #LifetimeBaseFromDate DATETIME --Lifetime Base
SET #LifetimeBaseFromDate = '1/1/2015'
DECLARE #LifetimeBaseToDate DATETIME
SET #LifetimeBaseToDate = '1/1/2018'
Company Code and Division Code select which Company we are running this query for. Customer Base will be the base of customers that we will be looking at. So in our example, it is the past 5 months.
So for all customers in the past 5 months, we will look in our Recurring Base. This is modular so we can control what time before we consider a customer "lost". We will compare against the recurring base and see how many customers are new customers who only ordered once over the customer and recurring base, and how many customers are recurring customers, those who ordered multiple times over those periods.
Then we will also have our Lifetime base. We will check customers who ordered once or more in our lifetime base, did not order in our recurring base, but did order again in our 5 months customer base. This calculates how many customers are "Reactivated" or were considered lost but bought with us again recently.
Then I declare the four tables
DECLARE #FullBase TABLE
(
Date_Created DATE,
Company_Code VARCHAR(2),
Division_Code VARCHAR(3),
Invoice_Number VARCHAR(50),
CUST_PO VARCHAR(50),
Total_Quantity NUMERIC,
TotalPrice MONEY,
City VARCHAR(50),
State VARCHAR(50),
Zip VARCHAR(50),
CountryCode VARCHAR(50),
Month NUMERIC,
CustomerEmail VARCHAR(MAX),
OrderCountBase NUMERIC,
TotalOrdersBase NUMERIC
)
DECLARE #LifetimeBase TABLE
(
Date_Created DATE,
Company_Code VARCHAR(2),
Division_Code VARCHAR(3),
Invoice_Number VARCHAR(50),
CUST_PO VARCHAR(50),
Total_Quantity NUMERIC,
TotalPrice MONEY,
City VARCHAR(50),
State VARCHAR(50),
Zip VARCHAR(50),
CountryCode VARCHAR(50),
Month NUMERIC,
CustomerEmail VARCHAR(MAX),
OrderCountLifetimeBase NUMERIC,
TotalOrdersLifetimeBase NUMERIC
)
DECLARE #RecurringBase TABLE
(
Date_Created DATE,
Company_Code VARCHAR(2),
Division_Code VARCHAR(3),
Invoice_Number VARCHAR(50),
CUST_PO VARCHAR(50),
Total_Quantity NUMERIC,
TotalPrice MONEY,
City VARCHAR(50),
State VARCHAR(50),
Zip VARCHAR(50),
CountryCode VARCHAR(50),
Month NUMERIC,
CustomerEmail VARCHAR(MAX),
OrderCountRecurringBase NUMERIC,
TotalOrdersRecurringBase NUMERIC
)
DECLARE #CustomerBase TABLE
(
Date_Created DATE,
Company_Code VARCHAR(2),
Division_Code VARCHAR(3),
Invoice_Number VARCHAR(50),
CUST_PO VARCHAR(50),
Total_Quantity NUMERIC,
TotalPrice MONEY,
City VARCHAR(50),
State VARCHAR(50),
Zip VARCHAR(50),
CountryCode VARCHAR(50),
Month NUMERIC,
CustomerEmail VARCHAR(MAX),
OrderCountCustomerBase NUMERIC,
TotalOrdersCustomerBase NUMERIC
)
Then I insert all of our customers into the "FullBase", from the beginning of the Lifetime Base, to the end of the Customer Base
INSERT INTO #FullBase
SELECT Orders.Date_Created
,Orders.Company_Code
,Orders.Division_Code
,Orders.Invoice_Number
,Orders.CUST_PO
,Orders.Total_Quantity
,Orders.Total
,Orders.City
,Orders.State
,Orders.Zip
,Orders.CountryCode
,Orders.Month
,Orders.CustomerEmail
,Row_Number() over (partition by CustomerEmail order by Date_Created asc) OrderCountBase
,Count(*) over (partition by CustomerEmail) TotalOrdersBase
FROM(
Select
CONVERT(Date, OrderCreated) Date_Created
,CONCAT ('0', LEFT(OrderName,1)) Company_Code
,CONCAT ('00', RIGHT(LEFT(OrderName,2),1)) Division_Code
,InvoiceNumber Invoice_Number
,OrderName CUST_PO
,1 Total_Quantity
,TotalPrice Total
,ShippingCity City
,CASE WHEN ShippingCountryCode <> 'US' THEN 'INT' ELSE ShippingProvinceCode END State
,ShippingZip Zip
,ShippingCountryCode CountryCode
,Month( OrderCreated) Month
,Email CustomerEmail
From [SHOPIFY].[shopify_moret].[dbo].orderwrappers O
Where CONVERT(Date, O.OrderCreated) >= Convert(datetime, '05/29/2019')
AND CONCAT ('0', LEFT(O.OrderName,1)) = #LocalCompanyCode--'09'
AND CONCAT ('00', RIGHT(LEFT(O.OrderName,2),1)) = #LocalDivisionCode --'001'
UNION
Select
Archive.Date_Created
,Archive.Company_Code
,Archive.Division_Code
,Archive.Invoice_Number
,('91'+Archive.CUST_PO) CUST_PO
,Archive.Total_Quantity
,Archive.Total
,Archive.City
,Archive.State
,Archive.Zip
,Archive.Country
,Archive.Month
,Archive.CustomerEmail
FROM SpraygroundArchivedOrders Archive
Where Archive.Date_Created < Convert(datetime, '05/29/2019')
) Orders
Where Orders.Date_Created BETWEEN #LifetimeBaseFromDate AND #CustomerBaseToDate
Here is an example of how the data looks like:
https://docs.google.com/spreadsheets/d/1wnjVHcPHHnugywa7Qz-aqctaD4cDI5DxehNna0TyaFU/edit?usp=sharing
Then I use this full base to populate the three other tables with orders from their range.
INSERT INTO #LifetimeBase
SELECT
F.Date_Created
,F.Company_Code
,F.Division_Code
,F.Invoice_Number
,F.CUST_PO
,F.Total_Quantity
,F.TotalPrice
,F.City
,F.State
,F.Zip
,F.CountryCode
,F.Month
,F.CustomerEmail
,Row_Number() over (partition by CustomerEmail order by Date_Created asc) OrderCountLifetimeBase
,Count(*) over (partition by CustomerEmail) TotalOrdersLifetimeBase
FROM #FullBase F
Where F.Date_Created BETWEEN #LifetimeBaseFromDate AND #LifetimeBaseToDate
INSERT INTO #RecurringBase
SELECT
F.Date_Created
,F.Company_Code
,F.Division_Code
,F.Invoice_Number
,F.CUST_PO
,F.Total_Quantity
,F.TotalPrice
,F.City
,F.State
,F.Zip
,F.CountryCode
,F.Month
,F.CustomerEmail
,Row_Number() over (partition by CustomerEmail order by Date_Created asc) OrderCountRecurringBase
,Count(*) over (partition by CustomerEmail) TotalOrdersRecurringBase
FROM #FullBase F
Where F.Date_Created BETWEEN #RecurringBaseFromDate AND #RecurringBaseToDate
INSERT INTO #CustomerBase
SELECT
F.Date_Created
,F.Company_Code
,F.Division_Code
,F.Invoice_Number
,F.CUST_PO
,F.Total_Quantity
,F.TotalPrice
,F.City
,F.State
,F.Zip
,F.CountryCode
,F.Month
,F.CustomerEmail
,Row_Number() over (partition by CustomerEmail order by Date_Created asc) OrderCountCustomerBase
,Count(*) over (partition by CustomerEmail) TotalOrdersCustomerBase
FROM #FullBase F
Where F.Date_Created BETWEEN #CustomerBaseFromDate AND #CustomerBaseToDate
If I just select * from an of the bases, including the full base, the query only takes a few seconds to run and returns 140,000 rows (for the #FullBase) of all customer orders.
However, the final part of this query, which is the part that runs, takes 10 minutes to run for my 6 months of customers
SELECT
CC.CustomerEmail
,CC.TotalOrdersCustomerBase
,RC.TotalOrdersRecurringBase
,LC.TotalOrdersLifetimeBase
From
(
SELECT DISTINCT
C.CustomerEmail
,C.TotalOrdersCustomerBase
FROM
#CustomerBase C
) CC
LEFT JOIN
(
SELECT DISTINCT
R.CustomerEmail
,R.TotalOrdersRecurringBase
FROM
#RecurringBase R
) RC ON CC.CustomerEmail = RC.CustomerEmail
LEFT JOIN
(
SELECT DISTINCT
L.CustomerEmail
,L.TotalOrdersLifetimeBase
FROM
#LifetimeBase L
) LC ON CC.CustomerEmail = LC.CustomerEmail
Does anyone have any tips for me at all? Is there any better way to do this?

find consecutive value in SQL

I want to find out the Consecutive Absences for the Each Student for Each Module,
here is my original result.
and I want my Output is to add a Consecutive Absences Column and find out the consecutive Absent for Each student by Module.
Try like this.
DECLARE #Tbl TABLE
(
[Id] [int] IDENTITY(1,1),
[Studentcode] VARCHAR(50),
[Semester] VARCHAR(50),
[ModuleCode] VARCHAR(50),
[Date] VARCHAR(50),
[WkNo] VARCHAR(50),
[Attended] VARCHAR(50),
[Absent] [int]
)
INSERT INTO #Tbl
SELECT [Studentcode]
,[Semester]
,[ModuleCode]
,[Date]
,[WkNo]
,[Attended]
,[Absent] FROM Attendance
ORDER BY studentcode,modulecode,date
SELECT T1.*, CASE WHEN (T1.Absent = 1 AND T2.Absent = 1)
AND (T1.ModuleCode = T2.ModuleCode)
THEN 1 ELSE 0 END AS CosecutiveAbsence
FROM #Tbl T1
LEFT OUTER JOIN #Tbl T2
ON T1.Id = T2.Id + 1

SQL join with previous row from the same group

I have a problem with sql query. I'm trying to create one time script which will put data to table. I have temporary table of new values ordered by date and i'm going to search previous value which have to have the same GroupId, TransactionId and FieldTypeId but ClaimModificationId have to be smaller.
Below i write a script which would be good if not error throwing on line:
where m2.ClaimModificationId < m1.ClaimModificationId
sql does not allow do referer to m1 table. Is there a method to write that condition differently?
create table #modifications (
[ClaimModificationId] INT IDENTITY(1,1),
[GroupId] INT,
[FieldTypeId] INT,
[FieldName] NVARCHAR(255),
[TransactionId] INT,
[NewValue] NVARCHAR(255),
[UserEmail] NVARCHAR(255),
[ModificationDate] DATETIME,
[Action] NVARCHAR(50))
select top 10
m1.[GroupId],
m1.[FieldTypeId],
m1.[FieldName],
m1.[TransactionId],
cm4.[NewValue] as OldValue,
m1.[NewValue],
m1.[UserEmail],
m1.[ModificationDate],
m1.[Action]
from #modifications m1
left join (
select max(m2.ClaimModificationId) as ClaimModificationId, m2.[GroupId], m2.[FieldTypeId], m2.TransactionId
from #modifications m2
where m2.ClaimModificationId < m1.ClaimModificationId
group by m2.GroupId, m2.FieldTypeId, m2.TransactionId) m3
on m3.groupId = m1.GroupId and m3.FieldTypeId = m1.FieldTypeId and m3.TransactionId = m1.TransactionId
LEFT JOIN #modifications cm4 ON m3.ClaimModificationId = cm4.ClaimModificationId
Try with OUTER APPLY:
SELECT TOP 10
m1.[GroupId] ,
m1.[FieldTypeId] ,
m1.[FieldName] ,
m1.[TransactionId] ,
cm4.[NewValue] AS OldValue ,
m1.[NewValue] ,
m1.[UserEmail] ,
m1.[ModificationDate] ,
m1.[Action]
FROM #modifications m1
OUTER APPLY ( SELECT MAX(m2.ClaimModificationId) AS ClaimModificationId ,
m2.[GroupId] ,
m2.[FieldTypeId] ,
m2.TransactionId
FROM #modifications m2
WHERE m2.ClaimModificationId < m1.ClaimModificationId
AND m2.groupId = m1.GroupId
AND m2.FieldTypeId = m1.FieldTypeId
AND m2.TransactionId = m1.TransactionId
GROUP BY m2.GroupId ,
m2.FieldTypeId ,
m2.TransactionId
) m3
LEFT JOIN #modifications cm4 ON m3.ClaimModificationId = cm4.ClaimModificationId
you can try something like this
create table #modifications
(
[ClaimModificationId] INT IDENTITY(1,1),
[GroupId] INT,
[FieldTypeId] INT,
[FieldName] NVARCHAR(255),
[TransactionId] INT,
[NewValue] NVARCHAR(255),
[UserEmail] NVARCHAR(255),
[ModificationDate] DATETIME,
[Action] NVARCHAR(50)
)
INSERT INTO #modifications values(1,1,'field',2,'new val1','email#email.com',GETDATE(),'inserted')
INSERT INTO #modifications values(1,2,'field',2,'val1','email#email.com',GETDATE(),'inserted')
INSERT INTO #modifications values(2,1,'field',3,'val2','email#email.com',GETDATE(),'inserted')
INSERT INTO #modifications values(1,1,'field',2,'val3','email#email.com',GETDATE(),'inserted')
INSERT INTO #modifications values(1,1,'field',2,'val4','email#email.com',GETDATE(),'inserted')
INSERT INTO #modifications values(1,1,'field',2,'val5','email#email.com',GETDATE(),'inserted')
INSERT INTO #modifications values(2,1,'field',3,'val5','email#email.com',GETDATE(),'inserted')
SELECT TOP 10
m1.[GroupId],
m1.[FieldTypeId],
m1.[FieldName],
m1.[TransactionId],
LAG([NewValue]) OVER(PARTITION by GroupId, FieldTypeId, TransactionId ORDER BY ClaimModificationId ASC) as OldValue,
m1.[NewValue],
m1.[UserEmail],
m1.[ModificationDate],
m1.[Action]
from #modifications m1