How to increment in a select query - sql

I've got a query I'm working on and I want to increment one of the fields and restart the counter when a key value is different.
I know this code doesn't work. Programmatically this is what I want...
declare #counter int, #id
set #counter = 0
set #id = 0
select distinct
id,
counter = when id = #id
then #counter += 1
else #id = id
#counter = 1
...with the end result looking something like this:
ID Counter
3 1
3 2
3 3
3 4
6 1
6 2
6 3
7 1
And yes, I am stuck with SQL2k. Otherwise that row_number() would work.

Assuming a table:
CREATE TABLE [SomeTable] (
[id] INTEGER,
[order] INTEGER,
PRIMARY KEY ([id], [order])
);
One way to get this in Microsoft SQL Server 2000 is to use a subquery to count the rows with the same id and a lower ordering.
SELECT *, (SELECT COUNT(*) FROM [SomeTable] counter
WHERE t.id = counter.id AND t.order < counter.order) AS row_num
FROM [SomeTable] t
Tip: It's 2010. Soon your SQL Server will be old enough to drive.
If you use SQL Server 2005 or later, you get wonderful new functions like ROW_NUMBER() OVER (PARTITION...).

Yes you want ROW_NUMBER().
I would try:
SELECT id, ROW_NUMBER() OVER (PARTITION BY ID ORDER BY ID) AS Counter

One way to do this is to throw the data into a temp table with an identity column that is used as a row number. Then make the counter column a count of the other rows with the same Id and a lower row number + 1.
CREATE TABLE #MyData(
Id INT
);
INSERT INTO #MyData VALUES(3);
INSERT INTO #MyData VALUES(3);
INSERT INTO #MyData VALUES(3);
INSERT INTO #MyData VALUES(3);
INSERT INTO #MyData VALUES(6);
INSERT INTO #MyData VALUES(6);
INSERT INTO #MyData VALUES(6);
INSERT INTO #MyData VALUES(7);
CREATE TABLE #MyTempTable(
RowNum INT IDENTITY(1,1),
Id INT,
Counter INT
);
INSERT INTO #MyTempTable
SELECT Id, 0
FROM #MyData
ORDER BY Id;
SELECT Id, (SELECT COUNT(*) + 1 FROM #MyTempTable WHERE Id = t1.Id AND RowNum < t1.RowNum) AS 'Counter'
FROM #MyTempTable t1;
You should get the following output based on your example:
Id Counter
3 1
3 2
3 3
3 4
6 1
6 2
6 3
7 1

Having row_number() means you have to deal with far, far fewer correlated subqueries. #Bill Karwin's solution works (+1); here's another version that does the same thing but that might be a bit easier to follow. (I used datetimes to determine ordering.)
-- Test table
CREATE TABLE Test
( Id int not null
,Loaded datetime not null
)
-- Load dummy data with made-up distinct datetimes
INSERT Test values (3, 'Jan 1, 2010')
INSERT Test values (3, 'Jan 2, 2010')
INSERT Test values (3, 'Jan 5, 2010')
INSERT Test values (3, 'Jan 7, 2010')
INSERT Test values (6, 'Feb 1, 2010')
INSERT Test values (6, 'Feb 11, 2010')
INSERT Test values (7, 'Mar 31, 2010')
-- The query
SELECT t1.Id, count(*) Counter
from Test t1
inner join Test t2
on t2.Id = t1.Id
and t2.Loaded <= t1.Loaded
group by t1.Id, t1.Loaded
-- Clean up when done
DROP TABLE Test
It is important to note that, without good indexes (and perhaps even with them), these kinds of queries can perform very poorly, particularly on large tables. Check and optimize carefully!

For MySql, I was able to make it with this query.
SELECT (SELECT COUNT(id) +1 FROM sku s WHERE t.item_id = s.item AND s.id < t.sku_id) AS rowNumber, t.*
FROM
(select item.Name as itemName ,item.id as item_id , sku.Name as skuName ,sku.id as sku_id from item
INNER JOIN sku ON item.id = sku.item
WHERE item.active = 'Y'
) t
1 Roasted Pistachios (Salted, In Shell) 84 1 Pound Bags 84
3 Roasted Pistachios (Salted, In Shell) 84 25 Pound Cases 1174
5 Roasted Pistachios (Salted, In Shell) 84 12 x 2.6 Ounce Bags 5807
2 Roasted Pistachios (Salted, In Shell) 84 5 Pound Bags 814
4 Roasted Pistachios (Salted, In Shell) 84 Samples 4724
6 Roasted Pistachios (Salted, In Shell) 84 12 x 3.2 Ounce Bags 18145
4 Star Fruit 981 5 Pound Bags 17462
1 Star Fruit 981 1 Pound Bags 2125
3 Star Fruit 981 11 Pound Bags 2226
2 Star Fruit 981 44 Pound Cases 2156

Related

T-SQL Select to compute a result row on preceeding group/condition

How to achieve this result using a T-SQL select query.
Given this sample table :
create table sample (a int, b int)
insert into sample values (999, 10)
insert into sample values (16, 11)
insert into sample values (10, 12)
insert into sample values (25, 13)
insert into sample values (999, 20)
insert into sample values (14, 12)
insert into sample values (90, 45)
insert into sample values (18, 34)
I'm trying to achieve this output:
a b result
----------- ----------- -----------
999 10 10
16 11 10
10 12 10
25 13 10
999 20 20
14 12 20
90 45 20
18 34 20
The rule is fairly simple: if column 'a' has the special value of 999 the result for that row and following rows (unless the value of 'a' is again 999) will be the value of column 'b'. Assume the first record will have 999 on column 'a'.
Any hint how to implement, if possible, the select query without using a stored procedure or function?
Thank you.
António
You can do what you want if you add a column to specify the ordering:
create table sample (
id int identity(1, 1),
a int,
b int
);
Then you can do what you want by finding the "999" version that is most recent and copying that value. Here is a method using window functions:
select a, b, max(case when a = 999 then b end) over (partition by id_999) as result
from (select s.*,
max(case when a = 999 then id end) over (order by id) as id_999
from sample s
) s;
You need to have an id column
select cn.id, cn.a
, (select top (1) b from sample where sample.id <= cn.id and a = 999 order by id desc)
from sample as cn
order by id

Difference in consecutive rows in SQL

I have a table which has any integer number. There is no specific criteria for a number to start but next row will be +2000 in number then above row and so on. So I want to find out through query where the difference of 2 rows are not 2000. Could you please help me on this? Comparison would be as follows:
Row 1 = 1000
2 = 3000
3 4000
4= 6000
5= 7000
Then only 3 and 5 should be output as the difference of Row 3 and Row 5 is not 2000. Row 3 should be compared with 2 and 5 should be compared with 4.
My data looks like :
Label Formorder date
test 480000 3/31/2015
test2 481000 3/31/2014
test3 482000 3/31/2015
test4 483000 3/31/2014
If you have SQL Server 2012 or above, you can use the LAG function.
LAG will give you a value in the previous row, compare this value to see if it is 2000 lower than the current row:
WITH diffs as (
SELECT rowValue,
rowValue - LAG(rowValue) OVER (ORDER BY rowValue) diff
FROM dataTable)
SELECT rowValue
FROM diffs
WHERE diff <> 2000
http://sqlfiddle.com/#!6/59d28/2
Possible solution:
declare #t table(id int, v int)
insert into #t values
(1, 1000),
(2, 3000),
(4, 7000),
(6, 9000),
(11, 11000),
(17, 17000)
select * from #t t1
outer apply(select * from (
select top 1 id as previd, v as prevv
from #t t2
where t2.id < t1.id
order by id desc)t
where t1.v - t.prevv <> 2000) oa
where oa.prevv is not null
Output:
id v previd prevv
4 7000 2 3000
17 17000 11 11000
If ID (row) does not skip
select t2.*
from table t1
jion table t2
on t2.ID = t1.ID + 1
and t2.value <> t1.value + 2000
A JOIN-based approach should work on SQL Server 2008.
In the query below, row numbers are added to the source data. Then, an inner join connects the current row to the previous row if and only if the previous row's value is not exactly 2000 less than the current row.
WITH Data AS (
SELECT *, RowNumber = ROW_NUMBER() OVER (ORDER BY rowValue)
FROM dataTable
)
SELECT n.rowValue
FROM data n
JOIN data p ON p.RowNumber = n.RowNumber - 1
AND p.rowValue != n.rowValue - 2000
http://sqlfiddle.com/#!3/59d28/10

SQL if statement with two tables

Given the following tables:
table objects
id Name rating
1 Megan 9
2 Irina 10
3 Vanessa 7
4 Samantha 9
5 Roxanne 1
6 Sonia 8
swap table
id swap_proposalid counterpartyid
1 4 2
2 3 2
Everyone wants the ten. I would like to make a list for Irina of possible swaps where id 4 and 3 don't appear because the propositions are already there.
output1
id Name rating
1 Megan 9
5 Roxanne 1
6 Sonia 8
Thanks
This should do the trick:
SELECT o.id, o.Name, o.rating
FROM objects o
LEFT JOIN swap s on o.id = s.swap_proposalid
WHERE s.id IS NULL
AND o.Name != 'Irina'
This works
SELECT mt2.ID, mt2.Name, mt2.Rating
FROM [MyTable] mt2 -- Other Candidates
, [MyTable] mt1 -- Candidate / Subject (Irina)
WHERE mt2.ID NOT IN
(
SELECT st.swap_proposalid
FROM SwapTable st
WHERE
st.counterpartyid = mt1.ID
)
AND mt1.ID <> mt2.ID -- Don't match Irina with Irina
AND mt1.Name = 'Irina' -- Find other swaps for Irina
-- Test Data
CREATE TABLE MyTable
(
ID INT,
Name VARCHAR(100),
Rating INT
)
GO
CREATE TABLE SwapTable
(
ID INT,
swap_proposalid INT,
counterpartyid INT
)
GO
INSERT INTO MyTable VALUES(1 ,'Megan', 9)
INSERT INTO MyTable VALUES(2 ,'Irina', 10)
INSERT INTO MyTable VALUES(3 ,'Vanessa', 7)
INSERT INTO MyTable VALUES(4 ,'Samantha', 9)
INSERT INTO MyTable VALUES(5 ,'Roxanne', 1)
INSERT INTO MyTable VALUES(6 ,'Sonia', 8)
INSERT INTO SwapTable(ID, swap_proposalid, counterpartyid)
VALUES (1, 4, 2)
INSERT INTO SwapTable(ID, swap_proposalid, counterpartyid)
VALUES (1, 3, 2)
Guessing that the logic involves identifying the objects EXCEPT the highest rated object EXCEPT propositions with the highest rated object e.g. (using sample DDL and data kindly posted by #nonnb):
WITH ObjectHighestRated
AS
(
SELECT ID
FROM MyTable
WHERE Rating = (
SELECT MAX(T.Rating)
FROM MyTable T
)
),
PropositionsForHighestRated
AS
(
SELECT swap_proposalid AS ID
FROM SwapTable
WHERE counterpartyid IN (SELECT ID FROM ObjectHighestRated)
),
CandidateSwappersForHighestRated
AS
(
SELECT ID
FROM MyTable
EXCEPT
SELECT ID
FROM ObjectHighestRated
EXCEPT
SELECT ID
FROM PropositionsForHighestRated
)
SELECT *
FROM MyTable
WHERE ID IN (SELECT ID FROM CandidateSwappersForHighestRated);

find records for user that are not contiguous

I am having trouble figuring out how to even start this query.
I have a table that has the following columns and data:
User BeginMile EndMile
1 1 5
1 5 6
1 6 20
1 20 25
1 25 29
2 1 9
2 15 20
3 1 2
3 6 10
3 10 12
I need to first find where there are gaps for each user from the EndMile of the previous record, to the BeginMile of the next record. I then need to return the record before and after where the gap occurs for each user.
In the previous data example, I would like the following returned:
User PrevBeginMile PrevEndMile AfterBeginMile AfterEndMile Gap
2 1 9 15 20 6
3 1 2 6 10 4
How can this be done?
Considering you're on SQL 2005, this should work:
DECLARE #Runners TABLE (Id INT, BeginMile INT, EndMile INT)
INSERT INTO #Runners VALUES (1,1,5)
INSERT INTO #Runners VALUES (1,5,6)
INSERT INTO #Runners VALUES (1,6,20)
INSERT INTO #Runners VALUES (1,20,25)
INSERT INTO #Runners VALUES (1,25,29)
INSERT INTO #Runners VALUES (2,1,9)
INSERT INTO #Runners VALUES (2,15,20)
INSERT INTO #Runners VALUES (3,1,2)
INSERT INTO #Runners VALUES (3,6,10)
INSERT INTO #Runners VALUES (3,10,12)
WITH OrderedUsers AS (
SELECT *
, ROW_NUMBER() OVER (PARTITION BY Id ORDER BY BeginMile) RowNum
FROM #Runners
)
SELECT a.Id [User]
, a.BeginMile PrevBeginMile
, a.EndMile PrevEndMile
, b.BeginMile AfterBeginMile
, b.EndMile AfterEndMile
, b.BeginMile - a.EndMile Gap
FROM OrderedUsers a
JOIN OrderedUsers b
ON a.Id = b.Id
AND a.EndMile <> b.BeginMile
AND a.RowNum = b.RowNum - 1
Other than using RowNumber() [as in other answers], you could use...
SELECT
[current].User,
[current].BeginMile AS [PrevBeginMile],
[current].EndMile AS [PrevEndMile],
[next].BeginMile AS [AfterBeginMile],
[next].EndMile AS [AfterEndMile],
[next].BeginMile - [current].EndMile AS [Gap]
FROM
myTable AS [current]
CROSS APPLY
(SELECT TOP 1 * FROM myTable WHERE user = [current].User AND BeginMile > [current].BeginMile ORDER BY BeginMile ASC) AS [next]
WHERE
[current].EndMile <> [next].BeginMile
Or possibly...
FROM
myTable AS [current]
INNER JOIN
myTable AS [next]
ON [next].BeginMile != [current].EndMile
AND [next].BeginMile = (
SELECT
MIN(BeginMile)
FROM
myTable
WHERE
user = [current].User
AND BeginMile > [current].BeginMile
)
How about
WITH acte(user,beginmile,endmile) AS
(
SELECT user,start,end
ROW_NUMBER() OVER(PARTITION BY user ORDER BY START ASC) rownum
FROM mytable
)
SELECT base.user,base.beginmile,base.endmile,base.BeginMile - lead.EndMile Gap
FROM acte base
LEFT JOIN acte lead on base.id=lead.id AND base.rownum=lead.rownum-1
WHERE base.BeginMile - lead.EndMile > 0

disaggregate summarised table in SQL Server 2008

I've received data from an external source, which is in a summarised format. I need a way to disaggregate this to fit into a system I am using.
To illustrate, suppose the data I received looks like this:
receivedTable:
Age Gender Count
40 M 3
41 M 2
I want this is a disaggregated format like this:
systemTable:
ID Age Gender
1 40 M
2 40 M
3 40 M
4 41 M
5 41 M
Thanks
Karl
Depending of the range of your count you could use a lookup table that holds exactly x records for each integer x. Like this:
create table counter(num int)
insert into counter select 1
insert into counter select 2
insert into counter select 2
insert into counter select 3
insert into counter select 3
insert into counter select 3
insert into counter select 4
insert into counter select 4
insert into counter select 4
insert into counter select 4
then join with this table:
create table source(age int, gender char(1), num int)
insert into source select 40, 'm', 3
insert into source select 30, 'f', 2
insert into source select 20, 'm', 1
--insert into destination(age, gender)
select age, gender
from source
inner join counter on counter.num = source.num
From the "Works on my machine (TM)" stable a recursive query, with all the usual caveats about maximum recursion depth.
with Expanded(exAge, exGender, exRowIndex) as
(
select
Age as exAge,
Gender as exGender,
1 as exRowIndex
from
tblTest1
union all
select
exAge,
exGender,
exRowIndex+1
from
tblTest1 t1
inner join
Expanded e on (e.exAge = t1.Age and e.exGender = t1.Gender and e.exRowIndex < t1.Count)
)
select
exAge,
exGender,
exRowIndex
from
Expanded
order by
exAge,
exGender,
exRowIndex
option (MAXRECURSION 0) -- BE CAREFUL!!
You don't get the row identifier - but inserting the result of the query into a table with an identity column would deal with that.