Currently I'm working on a stored procedure to update the sort order of a table. The idea is to make sure that the correct rows are swapped.
INPUT: ToMoveID INT, Up BIT (1 up, 0 down)
SERVER: MSSQL 2012
example to use:
ID SortOrder UseForSwap
1 1 Yes
2 NULL No
3 2 Yes
4 NULL No
5 3 Yes
The idea is to put the row with ID 5 higher, the ID of 3 is unkown at this point. How Can i get the ID of the row with te first sortorder higher as the current?
Sample values: TomoveID = 5, Up = 1
After sorting the result shoud be:
ID SortOrder UseForSwap
1 1 Yes
2 NULL No
3 3 Yes
4 NULL No
5 2 Yes
Thanks in advance
You can search for a row with the biggest SortOrder that is less than SortOrder of ID 5 row in this way:
SELECT TOP 1
tbl.ID
FROM YourTable tbl
WHERE tbl.SortOrder < (SELECT SortOrder FROM YourTable tbl2 WHERE tbl2.ID = 5)
ORDER BY
tbl.SortOrder DESC
Related
I have a set of data that I want to classify into groups based on a prior record id existing on the newer rows. The initial record of the group has a prior sequence id = 0.
The data is as follows:
customer id
sequence id
prior_sequence id
1
1
0
1
2
1
1
3
2
2
4
0
2
5
4
2
6
0
2
7
6
Ideally, I would like to create the following grouping column and yield the following results:
customer id
sequence id
prior sequence id
grouping
1
1
0
1
1
2
1
1
1
3
2
1
2
4
0
2
2
5
4
2
2
6
0
3
2
7
6
3
I've attempted to utilize island gap logic utilizing the ROW_NUMBER() function. However, I have been unsuccessful in doing so. I suspect the need here is more along the lines of a recursive CTE, which I am attempting at the moment.
I agree that a recursive CTE will do the job. Something like:
WITH reccte AS
(
/*query that determines starting point for recursion
*
* In this case we want all records with no prior_sequence_id
*/
SELECT
customer_id,
sequence_id,
prior_sequence_id,
/*establish grouping*/
ROW_NUMBER() OVER (ORDER BY sequence_id) as grouping
FROM yourtable
WHERE prior_sequence_id = 0
UNION
/*join the recursive CTe back to the table and iterate*/
SELECT
yourtable.customer_id,
yourtable.sequence_id,
yourtable.prior_sequence_id,
reccte.grouping
FROM reccte
INNER JOIN yourtable ON reccte.sequence_id = yourtable.prior_sequence_id
)
SELECT * FROM reccte;
It looks like you could use a simple correlated query, at least given your sample data:
select *, (
select Sum(Iif(prior_sequence_id = 0, 1, 0))
from t t2
where t2.sequence_id <= t.sequence_id
) Grouping
from t;
See Example Fiddle
Here is the scenerio, I have a input data and a table table1
Input Data Table1
Customer Id Campaign ID CustomerId CampaignID
1 1 4 2
1 2 6 3
2 3 1 1
1 3 5 5
4 2 9 8
4 4
5 5
I want to query table1 such that it return only those values from the where clause which are not present in table1. So the result will be as below
Result
Customer Id Campaign ID
1 2
2 3
1 3
4 4
5 5
So the query should be something like
select CustomerId, CampaignID from Table1
where Customer Id in (Input data for customer id) and CampaignId in (Input data for campaign id)
. I know this query is not right, but can someone please help.
Is there a way to filter the values given in where clause based on if they are present in table1?
P.S. table1 primary key (CustomerId, CampaignID)
This will work as for your scenario. But it wont show last result record 5,5 since it does not fulfill your need.
select * from input where (cust_id, camp_id) not in (select cust_id, camp_id from table1)
I want to Update only top 1 OR only 1 row of a column where a column values are same.
(Just logical explanation don't go on syntax)
LIKE:
Update [Total] = (value from a another table with a common column)
but need to update only top 1 row OR any one row to the current (updating) table not all rows matching column value...
e.g
Table 1:
Skill Value
abc 3
def 4
xyz 3.5
Table 2:
Name Skill MyValue MyValue2(ColumnNeedsToBeUpdated)
Ram abc 3
shyam abc 4
Mohan abc 5
Raju xyz 4
Ratan xyz 6
Now I want to Update MyValue2 based on Table1 column Skill Value = MyValue2 but I want to update anyone OR top 1 row in Table2 NOT ALL Please help
Expected Output:
Name Skill MyValue MyValue2(ColumnNeedsToBeUpdated)
Ram abc 3 3
shyam abc 4
Mohan abc 5
Raju xyz 4 3.5
Ratan xyz 6
OR Alternate output can be:
Name Skill MyValue MyValue2(ColumnNeedsToBeUpdated)
Ram abc 3 Value from Table1 / no. of records with skill abc (3/3)
shyam abc 4
Mohan abc 5
Raju xyz 4 Value from Table1 / no. of records with skill xyz (3.5/2)
Ratan xyz 6
In Table 2, give a row number based on group by Skill column and order by MyValue column. And then updated the rows which having row number1 with Value from Table 1.
Query
;with cte as(
select [rn] = row_number() over(
partition by Skill
order by [MyValue]
), *
from [Table2]
)
update t1
set t1.[MyValue2] = t2.[Value]
from cte t1
join [Table1] t2
on t1.[Skill] = t2.[Skill]
where t1.[rn] = 1;
I have values in SQL Server derived column sorted as descending i.e.
id Count OR id Count OR id Count
1 4 1 5 1 11
2 4 2 2 2 1
3 4 3 1 3 1
4 4 4 1 4 1
5 4 5 1 5 1
Now I want to select top 3 maximum values. How can I select so that query returns consistent result every time.
For example if values of Count are same which id's should be returned as top 3 maximums, similarly if 3rd value is matched with other values and if 2nd value is matched with other values then which id's should be returned by the query. And the result should be consistent every time I execute the query.
The with ties argument of the top function will return all the of the rows which match the top values:
select top (3) with ties id, count from table1
order by count desc
Alternatively, if you wanted to return 3 values only, but make sure they are always the same 3 values, then you will need to use something else as a tie-breaker. In this case, it looks like your id column could be unique.
select top (3) id, count from table1
order by count desc, id
I have a temp table which has two columns: one is Name and another RecordNumber. They looks like below:
Name RecordNumber Rownum
EMEA-1111-SCHD-1 0 1
EMEA-12362-SCHD-1 0 2
EMEA-12362-SCHD-1 0 3
EMEA-12362-SCHD-1 0 4
EMEA-12362-SCHD-1 0 5
EMEA-2191-SCHD-1 0 6
EMEA-2191-SCHD-1 0 7
EMEA-2191-SCHD-1 0 8
I need to update column "RecordNumber" with increment number starting with 1. Let say for EMEA-1111-SCHD-1 only one record, so RecordNumber should be updated to 1. For EMEA-12362-SCHD-1 four records, so RecordNumber should be updated to 1,2,3,4 accordingly. Basically, I need to have a result as:
Name RecordNumber Rownum
EMEA-1111-SCHD-1 1 1
EMEA-12362-SCHD-1 1 2
EMEA-12362-SCHD-1 2 3
EMEA-12362-SCHD-1 3 4
EMEA-12362-SCHD-1 4 5
EMEA-2191-SCHD-1 1 6
EMEA-2191-SCHD-1 2 7
EMEA-2191-SCHD-1 3 8
Is it possible to do it without cursor?
Thank you, Ed.
I added identity col rownum to make this records unique. Is any idea how to update result to have record number by group?
You could do this query only in Sybase IQ with analytical functions.
http://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.infocenter.dc38151.1510/html/iqrefbb/BCGEEBHA.htm
I have't access to sybase IQ this time, so I can't check query, but I suppose the right query is something like
select name,
row_number() over (partition by name) as RecordNumber
from Table
AFAIK Sybase ASE has't this feature.
Update
I think you can create self join query like this
select t1.name,
t1.Rownum - t2.MinRowNum + 1
from Table as t1,
(select name, min (Rownum) as MinRowNum from Table group by name) as t2
where t1.name = t2.name
order by t1.name, t1.Rownum