Use this thread to discuss TSQL Challenge 47
Hmm, how did I get here? Is there a time machine or something? ;)
Yes, you got into a time machine :-)
Jacob Sebastian, SQL Server MVP
See this post that explains how to format TSQL code listing when posting in this forum.
How does
Rule 1 . The order of the events registered in the table is arbitrary.
Sit with the Overall Job Requirement? "Your job is to write a query that shows the first number, current number (most recent) and the number of changes made."
What is the first number and the current number for an arbitrary set of numbers?
MisterMagoo: How does Rule 1 . The order of the events registered in the table is arbitrary. Sit with the Overall Job Requirement? "Your job is to write a query that shows the first number, current number (most recent) and the number of changes made." What is the first number and the current number for an arbitrary set of numbers?
Funnily enough, I was just coming back to edit my post because I had that "Doh!" moment myself!
Thanks.
Is it possible for two chains to have same starting card number like below? In that case, the o/p will have two records for 10 => 10 - 18 and 10 - 43. Please confirm.
10 - 1212 - 18----10 - 4141 - 43
Rule 3 does not allow this.
A script to create some simple test data is below
DECLARE @numberOfRecords AS INTEGER
DECLARE @lastCard AS INTEGER
DECLARE @chainNumber AS INTEGER
SET @chainNumber = 1
SET @lastCard = 1
WHILE @chainNumber < 10 -- <== Number of card chains
BEGIN
SET @numberOfRecords = 0
--SET @lastCard = @lastCard+1
SET @lastCard = @chainNumber * 1000
WHILE @numberOfRecords < 10 -- <= Number of changes in each chain
IF @numberOfRecords % (@chainNumber+1) = 0
INSERT INTO TC47 (OldCardNo, NewCardNo)
SELECT @lastCard,@lastCard
ELSE
SELECT @lastCard,@lastCard+1
SET @lastCard = @lastCard + 1
END
SET @numberOfRecords = @numberOfRecords + 1
SET @chainNumber = @chainNumber + 1
The results I get are
FirstCardNo LastCardNo Changes1000 1005 102000 2006 103000 3007 104000 4008 105000 5008 106000 6008 107000 7008 108000 8008 109000 9009 10
But remember:1. they can be in the table in any order2. the chain of values can increase and decreaseThus shuffle things a bit to make it interesting. Adding the foillowing to the end of your script should produce the same result.
select NEWID() s,OldCardNo,NewCardNointo #tc47from tc47truncate table tc47insert into tc47select OldCardNo,NewCardNofrom #tc47order by sdrop table #tc47update tc47 set OldCardNo=OldCardNo-50 where right(CONVERT(varchar,OldCardNo),1)='2'update tc47 set NewCardNo=NewCardNo-50 where right(CONVERT(varchar,NewCardNo),1)='2'update tc47 set OldCardNo=OldCardNo-50 where right(CONVERT(varchar,OldCardNo),1)='4'update tc47 set NewCardNo=NewCardNo-50 where right(CONVERT(varchar,NewCardNo),1)='4'select * from tc47
for source :
SrNo OldCardNo NewCardNo----------- ----------- -----------1 2 22 2 13 10 114 11 115 11 126 101 1017 100 1018 1000 10009 5 6the output:FirstCardNo LastCardNo Changes----------- ---------- -------2 1 25 6 110 12 3100 101 21000 1000 1Here, I don't get why first card number is 100 where srno 6 in source there is 101 as first card number , why there is 100 as first card no. in expected result ? and what does this example means?For example: 10=>20, 20=>20, 20=>20, 20=>30 is OK. But 10=>20, 20=>20, 20=>30, 30=>20 is not allowed in the data.
The chain is 100 => 101 => 101 (so row 7 then row 6)
The first card in the chain is 100,
The last card is 101
The number of rows processed is 2
Regards
David
bharat:Here, I don't get why first card number is 100 where srno 6 in source there is 101 as first card number , why there is 100 as first card no. in expected result ? and what does this example means?For example: 10=>20, 20=>20, 20=>20, 20=>30 is OK. But 10=>20, 20=>20, 20=>30, 30=>20 is not allowed in the data.
dishdy: The problem is made slightly complicated by telling you that a number on a card can be repeated in a sequence of 1 or more occurences. And this can happen only once on a card!Hope this helps make things clearer.
The problem is made slightly complicated by telling you that a number on a card can be repeated in a sequence of 1 or more occurences. And this can happen only once on a card!Hope this helps make things clearer.
Just to check I understand, does this mean that 10->11, 11->11, 11->11, 11->11, 11->13 is possible but 10->11, 11->11, 11->11, 11->11, 11->13, 13->13 is not possible?
Not that it matters much right now as my brain has blown a fuse trying to figure out how to handle multiple "no change" rows... :o(
MisterMagoo: dishdy:The problem is made slightly complicated by telling you that a number on a card can be repeated in a sequence of 1 or more occurences. And this can happen only once on a card!Hope this helps make things clearer.Just to check I understand, does this mean that 10->11, 11->11, 11->11, 11->11, 11->13 is possible but 10->11, 11->11, 11->11, 11->11, 11->13, 13->13 is not possible? Not that it matters much right now as my brain has blown a fuse trying to figure out how to handle multiple "no change" rows... :o(
dishdy:The problem is made slightly complicated by telling you that a number on a card can be repeated in a sequence of 1 or more occurences. And this can happen only once on a card!Hope this helps make things clearer.