How to generate row number based on certain condition? - sql

I have column named type having values 1 and 2. i want to generate the expected results columns.
In this column value 2 should be converted to 0 and for consecutive 1's it should generate the Row number.
Please Refer this image...
Any advise how can we achieve this.
running out of logic.:(

Please try this :
select *,0 as RowNo into #tmp from YourTable
declare #id int
set #id=0
update #tmp
set #id = case typeId when 1 then #id+1 else 0 end,
RowNo = case typeId when 1 then #id else 0 end
select * from #tmp
drop table #tmp

This is the best way to use Cursors
CREATE TABLE [dbo].[Table_1](
[Type] [int] NULL)
Code
Declare #Type int = 0
DECLARE #Test TABLE(
[Type] INT ,
[ExpectedResult] INT
)
DECLARE vendor_cursor CURSOR FOR
SELECT [Type]
FROM [dbo].[Table_1]
OPEN vendor_cursor
FETCH NEXT FROM vendor_cursor
INTO #Type
Declare #ExpectedResult INT = 0
WHILE ##FETCH_STATUS = 0
BEGIN
IF #Type = 2
SET #ExpectedResult = 0
ELSE
SET #ExpectedResult+= 1
INSERT INTO #Test ([Type] ,[ExpectedResult] ) VALUES(#Type , #ExpectedResult)
FETCH NEXT FROM vendor_cursor
INTO #Type
END
CLOSE vendor_cursor;
DEALLOCATE vendor_cursor;
SELECT * FROM #Test

First, you are supposing that your rows have an ordering, but no ordering column is specified. SQL tables represent unordered sets. There is not ordering without such a column.
Let me assume you have one.
Then this is a gaps and islands problem. You want the islands of type = 1 so you can enumerate them. You can identify them by doing a cumulative sum of type = 2 -- this cumulative sum defines the grouping of adjacent type = 1 records. The rest is just row_number():
select t.*,
(case when type = 2 then 0
else row_number() over (partition by type, grp order by <ordering col>)
end) as expected_result
from (select t.*,
sum(case when type = 2 then 1 else 0 end) over (order by <ordering col>) as grp
from t
) t;
Here is a db<>fiddle.

Related

SQL Grouping with condition

I want to sum rows in table. The algorithm is rather simple in theory but hard (at least for me) when I need to build a query.
Generally, I want to sum "values" of a "sub-group". Sub-group is defined as a range of elements starting with first row where type=0 and finishing with last row where type=1. the sub-group should contain only one (first) row with type=0.
The sample below presents correct (left) and incorrect (right) behavior.
I tried several approaches including grouping and partitioning. Unfortunately w/o any success. Anybody had similar problem?
I used MS SQL Server (so T-SQL 'magic' is allowed)
EDIT:
The results I want:
"ab",6
"cdef",20
"ghi",10
"kl",8
You can identify the groups by doing a cumulative sum of zeros. Then use aggregation or window functions.
Note that SQL tables represent unordered sets, so you need a column to specify the ordering. The code below assumes that this column is id.
select min(id), max(id), sum(value)
from (select t.*,
sum(case when type = 0 then 1 else 0 end) over (order by id) as grp
from t
) t
group by grp
order by min(id);
You can use window function with cumulative approach :
select t.*, sum(value) over (partition by grp)
from (select t.*, sum(case when type = 0 then 1 else 0 end) over (order by id) as grp
from table t
) t
where grp > 0;
Solution with a cursor and output-table.
As Gordon wrote it is not defined how the set will be ordered, so ID is also used here.
declare #output as table (
ID_sum nvarchar(max)
,value_sum int
)
DECLARE #ID as nvarchar(1)
,#value as int
,#type as int
,#ID_sum as nvarchar(max)
,#value_sum as int
,#last_type as int
DECLARE group_cursor CURSOR FOR
SELECT [ID],[value],[type]
FROM [t]
ORDER BY ID
OPEN group_cursor
FETCH NEXT FROM group_cursor
INTO #ID, #value,#type
WHILE ##FETCH_STATUS = 0
BEGIN
if (#last_type is null and #type = 0)
begin
set #ID_sum = #ID
set #value_sum = #value
end
if (#last_type in(0,1) and #type = 1)
begin
set #ID_sum += #ID
set #value_sum += #value
end
if (#last_type = 1 and #type = 0)
begin
insert into #output values (#ID_sum, #value_sum)
set #ID_sum = #ID
set #value_sum = #value
end
if (#last_type = 0 and #type = 0)
begin
set #ID_sum = #ID
set #value_sum = #value
end
set #last_type = #type
FETCH NEXT FROM group_cursor
INTO #ID, #value,#type
END
CLOSE group_cursor;
DEALLOCATE group_cursor;
if (#last_type = 1)
begin
insert into #output values (#ID_sum, #value_sum)
end
select *
from #output

Incremental Group BY

How I can achieve incremental grouping in query ?
I need to group by all the non-zero values into different named groups.
Please help me write a query based on columns date and subscribers.
If you have SQL Server 2012 or newer, you can use few tricks with windows functions to get this kind of grouping without cursors, with something like this:
select
Date, Subscribers,
case when Subscribers = 0 then 'No group'
else 'Group' + convert(varchar, GRP) end as GRP
from (
select
Date, Subscribers,
sum (GRP) over (order by Date asc) as GRP
from (
select
*,
case when Subscribers > 0 and
isnull(lag(Subscribers) over (order by Date asc),0) = 0 then 1 else 0 end as GRP
from SubscribersCountByDay S
) X
) Y
Example in SQL Fiddle
In general I advocate AGAINST cursors but in this case it ill not hurt since it ill iterate, sum up and do the conditional all in one pass.
Also note I hinted it with FAST_FORWARD to not degrade performance.
I'm guessing you do want what #HABO commented.
See the working example below, it just sums up until find a ZERO, reset and starts again. Note the and #Sum > 0 handles the case where the first row is ZERO.
create table dbo.SubscribersCountByDay
(
[Date] date not null
,Subscribers int not null
)
GO
insert into dbo.SubscribersCountByDay
([Date], Subscribers)
values
('2015-10-01', 1)
,('2015-10-02', 2)
,('2015-10-03', 0)
,('2015-10-04', 4)
,('2015-10-05', 5)
,('2015-10-06', 0)
,('2015-10-07', 7)
GO
declare
#Date date
,#Subscribers int
,#Sum int = 0
,#GroupId int = 1
declare #Result as Table
(
GroupName varchar(10) not null
,[Sum] int not null
)
declare ScanIt cursor fast_forward
for
(
select [Date], Subscribers
from dbo.SubscribersCountByDay
union
select '2030-12-31', 0
) order by [Date]
open ScanIt
fetch next from ScanIt into #Date, #Subscribers
while ##FETCH_STATUS = 0
begin
if (#Subscribers = 0 and #Sum > 0)
begin
insert into #Result (GroupName, [Sum]) values ('Group ' + cast(#GroupId as varchar(6)), #Sum)
set #GroupId = #GroupId + 1
set #Sum = 0
end
else begin
set #Sum = #Sum + #Subscribers
end
fetch next from ScanIt into #Date, #Subscribers
end
close ScanIt
deallocate ScanIt
select * from #Result
GO
For the OP: Please next time write the table, just posting an image is lazy
In a version of SQL Server modern enough to support CTEs you can use the following cursorless query:
-- Sample data.
declare #SampleData as Table ( Id Int Identity, Subscribers Int );
insert into #SampleData ( Subscribers ) values
-- ( 0 ), -- Test edge case when we have a zero first row.
( 200 ), ( 100 ), ( 200 ),
( 0 ), ( 0 ), ( 0 ),
( 50 ), ( 50 ), ( 12 ),
( 0 ), ( 0 ),
( 43 ), ( 34 ), ( 34 );
select * from #SampleData;
-- Run the query.
with ZerosAndRows as (
-- Add IsZero to indicate zero/non-zero and a row number to each row.
select Id, Subscribers,
case when Subscribers = 0 then 0 else 1 end as IsZero,
Row_Number() over ( order by Id ) as RowNumber
from #SampleData ),
Groups as (
-- Add a group number to every row.
select Id, Subscribers, IsZero, RowNumber, 1 as GroupNumber
from ZerosAndRows
where RowNumber = 1
union all
select FAR.Id, FAR.Subscribers, FAR.IsZero, FAR.RowNumber,
-- Increment GroupNumber only when we move from a non-zero row to a zero row.
case when Groups.IsZero = 1 and FAR.IsZero = 0 then Groups.GroupNumber + 1 else Groups.GroupNumber end
from ZerosAndRows as FAR inner join Groups on Groups.RowNumber + 1 = FAR.RowNumber
)
-- Display the results.
select Id, Subscribers,
case when IsZero = 0 then 'no group' else 'Group' + Cast( GroupNumber as VarChar(10) ) end as Grouped
from Groups
order by Id;
To see the intermediate results just replace the final select with select * from FlagsAndRows or select * from Groups.

Update table with new value for each row

I need to update a column (type of datetime) in the top 1000 rows my table. However the catch is with each additional row I must increment the GETDATE() by 1 second... something like DATEADD(ss,1,GETDATE())
The only way I know how to do this is something like this:
UPDATE tablename
SET columnname = CASE id
WHEN 1 THEN DATEADD(ss,1,GETDATE())
WHEN 2 THEN DATEADD(ss,2,GETDATE())
...
END
Obviously this is not plausible. Any ideas?
How about using id rather than a constant?
UPDATE tablename
SET columnname = DATEADD(second, id, GETDATE() )
WHERE id <= 1000;
If you want the first 1000 rows (by id), but the id has gaps or other problems, then you can use a CTE:
with toupdate as (
select t.*, row_number() over (order by id) as seqnum
from tablename
)
update toupdate
set columnname = dateadd(second, seqnum, getdate())
where seqnum <= 1000;
I don't know what your ID is like, and I'm assuming you have at least SQL Server 2008 or else ROW_NUMBER() won't work.
Note: I did top 2 to show you that you that the top works. You can change it to top 1000 for your actual query.
DECLARE #table TABLE (ID int, columnName DATETIME);
INSERT INTO #table(ID)
VALUES(1),(2),(3);
UPDATE #table
SET columnName = DATEADD(SECOND,B.row_num,GETDATE())
FROM #table A
INNER JOIN
(
SELECT TOP 2 *, ROW_NUMBER() OVER (ORDER BY ID) row_num
FROM #table
ORDER BY ID
) B
ON A.ID = B.ID
SELECT *
FROM #table
Results:
ID columnName
----------- -----------------------
1 2015-03-31 13:11:59.760
2 2015-03-31 13:12:00.760
3 NULL
You don't make explicit the SQL Server version you're using, so I will assume SQL Server 2005 or above. I believe the WAITFOR DELAY command would be a good option to keep adding 1 second to each rows of the datetime column.
See this example:
-- Create temp table
CREATE TABLE #Client
(
RecordID int identity(1,1),
[Name] nvarchar(100) not null,
PurchaseDate datetime null
)
-- Fill in temp table with example values
INSERT INTO #Client
VALUES ( 'Jhon', NULL)
INSERT INTO #Client
VALUES ( 'Martha', NULL)
INSERT INTO #Client
VALUES ( 'Jimmy', NULL)
-- Create local variables
DECLARE #currentRecordId int,
#currentName nvarchar(100)
-- Create cursor
DECLARE ClientsCursor CURSOR FOR
SELECT RecordID,
[Name]
FROM #Client
OPEN ClientsCursor
FETCH FROM ClientsCursor INTO #currentRecordId, #currentName
-- Check ##FETCH_STATUS to see if there are any more rows to fetch.
WHILE ##FETCH_STATUS = 0
BEGIN
UPDATE #Client
SET PurchaseDate = DATEADD(ss,1,GETDATE())
WHERE RecordID = #currentRecordId
AND [Name] = #currentName
WAITFOR DELAY '00:00:01.000'
FETCH NEXT FROM ClientsCursor INTO #currentRecordId, #currentName
END
CLOSE ClientsCursor;
DEALLOCATE ClientsCursor;
SELECT *
FROM #Client
And here's the result:
1 Jhon 2015-03-31 15:20:04.477
2 Martha 2015-03-31 15:20:05.473
3 Jimmy 2015-03-31 15:20:06.470
Hope you find this answer helpful
This should be what you need (at least a guidline)
DELIMITER $$
CREATE PROCEDURE ADDTIME()
BEGIN
DECLARE i INT Default 1 ;
simple_loop: LOOP
UPDATE tablename SET columnname = DATE_ADD(NOW(), INTERVAL i SECOND) where rownumber = i
SET i=i+1;
IF i=1001 THEN
LEAVE simple_loop;
END IF;
END LOOP simple_loop;
END $$
To call that stored procedure use
CALL ADDTIME()

sql how to get consecutive appearance of value

suppose I have a column 'value', which can appear multiple times in a table with another column 'result' which can be either 1 or 0. I would like to search for consecutive 1s (ie result = 1) until the count reaches 4, then I can select value. given the result sets below:
-result set a)
value Result
----- ------
A 1
A 1
A 1
A 0
-result set b)
value Result
----- ------
A 1
A 1
A 1
A 1
result set b meets the condition and therefore value A is selected. How do I go about this ? Thanks.
This is the query: (usually this query is to detect double record in a table, but probably meet your demand).
select value, result, count(value) as [Result Sum]
from #temp
where result = 1
group by value, result
having count(value) >3
This is the Result
value result Result Sum
----- ----------- -----------
A 1 4
UPDATED:
This is the data example in my temporary table (#temp)
value result
----- -----------
A 1
A 1
A 1
A 0
A 1
D 1
D 1
D 1
D 1
B 1
B 1
C 1
C 1
C 1
C 1
From The example data C and D are the valid values
Declare #temp2 table
(
value nvarchar(5)
)
declare #value nvarchar(5), #result int, #total int, #flag bit, #tempValue nvarchar(5)
DECLARE myCursor CURSOR FOR
SELECT value, result
FROM #temp
set #flag = 1
set #tempValue = ''
OPEN myCursor;
FETCH NEXT FROM myCursor into #value, #result;
WHILE ##FETCH_STATUS = 0
BEGIN
--logic here
if (#tempValue <> #value and #result = 1) or #flag = 1
begin
set #tempValue = #value
set #total = 1
set #flag = 0
end
else --#tempvalue = #value
begin
if #result = 1
set #total = #total + 1
else --#result = 0
set #flag = 1
if #total >3 --valid value has reached 4 consecutive result =1
begin
set #flag = 1
insert into #temp2 values (#value)
end
end
FETCH NEXT FROM myCursor into #value, #result;
END;
CLOSE myCursor;
DEALLOCATE myCursor;
select * from #temp2
This is the Result of the loop (table #temp2)
value
-----
D
C
(2 row(s) affected)
You can do this in a select statement. You can find groups of items in a row by using row_number() assuming you have an id. SQL tables are inherently unordered, so you need an id or creation date or something to specify the ordering. Here is the SQL:
select value
from (select t,
(row_number() over (partition by value order by id) -
row_number() over (partition by value, results order by id)
) as grp
from table t
) t
group by value, result, grp
having count(*) > 3 and result = 1;

Conditional summing in SQL

I'm having trouble getting a query to work. What I'm trying to do is sum a counter by user id but with conditions.
Currently my query gives the following output.
User ID EndDate Date Index
123 5/1/12 1/1/12 -1
123 5/1/12 1/25/12 1
123 5/1/12 2/13/12 -1
456 4/1/12 1/18/12 -1
456 4/1/12 2/15/12 -1
456 4/1/12 2/18/12 1
What I want to do with this list is sum the Index by User Id but with a catch. The Index must be summed in date order, also the min value of the index is -1 and max is 1, so the values can be -1, 0, 1 only. So with user 123, the process would be -1 then you add 1 then you add -1 for a final sum of -1. But for user 456 you start with -1 then you have -1 again but the sum must remain -1 then you have 1, so the final sum is 0. Below is what I'm been trying to do but I can't figure it out. I would really appreciate some help.
DECLARE #Period char(6)
DECLARE #StatusCount int
SET #Period = '201201'
SET #StatusCount = 0
SELECT Q1.UserID, Q1.End_Date,
Sum(Case
When Index = -1 Then Case When #IndexCount >=0 Then #IndexCount - 1 Else #IndexCount + 0 End
When Index = 1 Then Case When #IndexCount <=0 Then #IndexCount + 1 Else #IndexCount + 0 End
END) as FinalIndex
FROM
(
(SELECT UserID, End_Date, Enter_Dt, 1 as Index
FROM UserTable
WHERE (Code in ('A', 'B') and PRD = #Period)
GROUP BY UserID, End_Date, Enter_Dt)
UNION
(SELECT UserID, End_Date, Enter_Dt, -1 as Index
FROM UserTable
WHERE (Code in ('C', 'D') and PRD = #Period)
GROUP BY UserID, End_Date, Enter_Dt)
) as Q1
GROUP BY Q1.UserID, Q1.End_Date
ORDER BY Q1.UserID ASC, Q1.End_Date ASC
I think my main problem is I can't figure out how to accumulate the Index properly. I can't get IndexCount to remember the the previous value and then start again from 0 with the next User ID
The result I get with this query is
User ID EndDate Index
123 5/01/12 -1
456 4/01/12 -1
Which is just summing the Index
I'll illustrate a running total by userID solution here, leaving out the other details of your query for clarity. Basically, add a IndexRT column populate it with a running total that resets for each new userID.
EDIT: constrain running total to -1,0,1
create table #temp(userID int, [Index] int, IndexRT int)
insert into #temp (userID,[Index]) values (123,-1) , (123,1) , (123,-1) , (456,-1) , (456,-1) , (456,1)
declare #rt int; set #rt=0;
with a as (
select select TOP 100 percent *
,r=ROW_NUMBER()over(partition by userID order by userID)
from #temp order by userID,ROW_NUMBER()over(partition by userID order by userID)
)
update a set #rt = [IndexRT] = case when case when r=1 then [Index] else #rt + [Index] end between -1 and 1
then case when r=1 then [Index] else #rt + [Index] end
else #rt
end
select * from #temp;
go
drop table #temp;
Result:
userID Index IndexRT
----------- ----------- -----------
123 -1 -1
123 1 0
123 -1 -1
456 -1 -1
456 -1 -1
456 1 0
Sounds like you need to define a cycling sequence:
http://www.postgresql.org/docs/8.1/static/sql-createsequence.html
This will create the effect you've described in your problem, cycling -1,0,1 as rows iterate. You can achieve this in a select or upon inserting the record in your table.
This uses cursors, and seemed to work in SQL 2005 (I would advise checking your results thoroughly in case I missed something obvious here).
As a disclaimer, most database experts will tell you that cursors are horrible, inefficient, and should never be used. And yes-- you should use straight SELECT statements when possible. However: (1) I don't know that there is a straightforward SELECT statement for this type of accumulation, and (2) this will be a much more readable solution for anyone who has to maintain your code. If your data set is not unreasonably large (and you don't expect it to grow too much), cursors should be OK.
Note that I have substituted "Table3" for your entire FROM statement; I imported your sample table to create this example. The key element to note here is that I am sorting it first by User ID, then by Date in the DECLARE csrOrdered CURSOR statement. So you should just need to substitute your own sorted SQL statement in the cursor definition.
CREATE TABLE #sumResult (
[User ID] int,
[Sum Result] int
)
DECLARE csrOrdered CURSOR FOR
SELECT [User Id], [Index] FROM Table3 -- <- your table name goes here!
ORDER BY [User ID], Date, EndDate
DECLARE #uid int
DECLARE #index int
DECLARE #lastUid int
DECLARE #curSum int
SET #uid=0
SET #index=0
SET #lastUid=0
SET #curSum=0
OPEN csrOrdered
FETCH NEXT FROM csrOrdered
INTO #uid, #index
WHILE ##FETCH_STATUS=0
BEGIN
--Are we working on a new User ID? Then reset the sum and insert the last group into the table.
IF #uid<>#lastUid BEGIN
--Don't do an insert if we just got into the loop
IF #lastUid <> 0 BEGIN
INSERT INTO #sumResult ([User ID], [Sum Result]) VALUES (#lastUid, #curSum)
END
SET #curSum=0
SET #lastUid=#uid
END
SET #curSum = #curSum + #index
IF #curSum < -1 SET #curSum=-1
IF #curSum > 1 SET #curSum=1
FETCH NEXT FROM csrOrdered
INTO #uid, #index
END
IF #lastUid <> 0 BEGIN
INSERT INTO #sumResult ([User ID], [Sum Result]) VALUES (#lastUid, #curSum)
END
CLOSE csrOrdered
DEALLOCATE csrOrdered
SELECT * FROM #sumResult
DROP TABLE #sumResult