Solved

How to find a record change and raise a flag in T-SQL

Posted on 2016-09-08
12
48 Views
Last Modified: 2016-09-09
Hi Guys,

I have got a master table which contains the unique records, just like below

JobNo	   BookingStatusID
4363267	   2

Open in new window


I have got a child table as below
JobNo      BookingStatusDescription      WebSupplierStatusID      WebSupplierStatusDescription      RevisionNo      SupplierID
4363267      Amended                                   2                                           Assigned                                             2                 4969
4363267      Confirmed                                   3                                           Rejected                                             1                  5202
4363267      Amended                                   4                                          Accepted                                             4                  4969
4363267      Amended                                   5                                          Error                                                      3                   4969

WIth in my T-SQL I want to figure out that if the supplier ID has been changed then raise some sort of Flag of 0 or 1. I don't want to use inline UDF as the data contains hundred of thousands of rows.

How do i achieve this. Please note i am using SQL Server 2014

regards
0
Comment
Question by:shah36
12 Comments
 
LVL 6

Expert Comment

by:pritaeas
ID: 41789261
What do you mean by raising a flag?

You can use a TRIGGER do detect that the record is updated, and specifically check if the supplier column has changed.
0
 
LVL 25

Expert Comment

by:Lee Savidge
ID: 41789262
Use a SQL trigger that fires on update to that table and you can code it to do something you want when the supplier field value changes.
0
 

Author Comment

by:shah36
ID: 41789264
I mean in my query if it detects that supplier has changed then i add an extra column if say AmmendedSupplier to True or False.

Well i am not allowed to write any triggers as this is live database. I want to achieve by querying and using TSQL?
0
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
LVL 50

Expert Comment

by:Ryan Chong
ID: 41789268
you can try this:
WITH CTE AS (
	SELECT
	rownum = ROW_NUMBER() OVER (ORDER BY p.jobNo),
	p.*
	FROM yourTable p
)
SELECT
CTE.JobNo
FROM CTE
LEFT JOIN CTE nex ON nex.rownum = CTE.rownum + 1
where nex.SupplierID <> CTE.SupplierID
Group By CTE.JobNo
Order By CTE.JobNo

Open in new window

script above detect the change of SupplierID in your records in a particular JobNo and show the JobNo accordingly.
0
 

Author Comment

by:shah36
ID: 41789270
Hi Ryan,

Thanks a lot for your help. However it is showing those job numbers where the supplierId has not changed. I need those job numbers where SupplierId has been changed

regards
0
 
LVL 50

Expert Comment

by:Ryan Chong
ID: 41789273
>>However it is showing those job numbers where the supplierId has not changed. I need those job numbers where SupplierId has been changed
it was tested before post so it should work just fine.

anyway, you can try this as well:
WITH CTE AS (
	SELECT
	rownum = ROW_NUMBER() OVER (ORDER BY p.jobNo),
	p.*
	FROM yourTable p
)
select a.*, case when b.JobNo is null then 0 else 1 end AmmendedSupplier 
from
yourTable a
left join (
	SELECT
	CTE.JobNo
	FROM CTE
	LEFT JOIN CTE nex ON nex.rownum = CTE.rownum + 1
	where nex.SupplierID <> CTE.SupplierID
	Group By CTE.JobNo
) b
on a.JobNo = b.JobNo

Open in new window

0
 

Author Comment

by:shah36
ID: 41789283
Hi Ryan,

I really appreciate your help. I have got almost 5 millions records and have tested with few records and am afraid it is not showing the correct data. Please find the images of the two test jobnos below Where it is showing as Ammended supplier but in data supplier id is same
JobNos showing that supplier has been changed but its not the case
0
 

Author Comment

by:shah36
ID: 41789286
Sorry just forgot to attach the query confirming directly from the table
Suppliers confirming from the table
0
 
LVL 50

Expert Comment

by:Ryan Chong
ID: 41789661
let me try to diagnose the issue... will get back to you tomorrow when I go back to office.
0
 
LVL 40

Expert Comment

by:Sharath
ID: 41789947
check this.
declare @table table (JobNo int, 
                      BookingStatusDescription varchar(100), 
					  WebSupplierStatusID int, 
					  WebSupplierStatusDescription varchar(100), 
					  RevisionNo int, 
					  SupplierID int)
insert @table values (4363267, 'Amended', 2, 'Assigned', 2, 4969),
                     (4363267, 'Confirmed', 3, 'Rejected', 1, 5202),
					 (4363267, 'Amended', 4, 'Accepted', 4, 4969),
					 (4363267, 'Amended', 5, 'Error', 3, 4969)

;with cte as (
select *,lag(SupplierID) over (order by WebSupplierStatusID) prev_SupplierID 
  from @table)
select *,case when SupplierID <> coalesce(prev_SupplierID, SupplierID) then 0 else 1 end Flag
  from cte
/*
JobNo	BookingStatusDescription	WebSupplierStatusID	WebSupplierStatusDescription	RevisionNo	SupplierID	prev_SupplierID	Flag
4363267	Amended	2	Assigned	2	4969	NULL	1
4363267	Confirmed	3	Rejected	1	5202	4969	0
4363267	Amended	4	Accepted	4	4969	5202	0
4363267	Amended	5	Error	3	4969	4969	1
*/

Open in new window

0
 
LVL 50

Accepted Solution

by:
Ryan Chong earned 500 total points
ID: 41790677
@shah36

try this instead:
WITH CTE AS (
	SELECT
	rownum = ROW_NUMBER() OVER (partition by p.jobNo ORDER BY p.jobNo),
	p.*
	FROM BookingRevision p
)
select a.*, case when b.JobNo is null then 0 else 1 end AmmendedSupplier 
from
BookingRevision a
left join (
	select 
	cte.jobNo from CTE
	LEFT JOIN CTE nex ON nex.rownum = CTE.rownum + 1 and cte.jobNo = nex.jobNo
	where nex.SupplierID is not null
	and nex.SupplierID <> cte.SupplierID
	group by cte.jobNo
) b
on a.JobNo = b.JobNo

Open in new window

0
 

Author Comment

by:shah36
ID: 41791287
@Ryan Chong. Thanks a lot it worked as expected. Thanks again you are a champ.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question