Generate Row Count, increasing in 10,000 per row - sql

I'm needing to return a generated ID where for each row it increases by 10,000.
For example, the ExpectedResult column in the below, and if there are more rows, it would increase by 10,000 each time.
Create Table #temp
(
ID uniqueidentifier,
ExpectedResult int
)
insert into #temp
(
ID,
ExpectedResult
)
select
NEWID(),
10000
union
select
NEWID(),
20000
union
select
NEWID(),
30000
union
select
NEWID(),
40000
union
select
NEWID(),
50000
select * from #temp
order by ExpectedResult
drop table #temp
I've found the example below, but I'm not sure how to increase the count by 10,000 each time
ROW_NUMBER() OVER (ORDER BY (SELECT 100))

If you are using SQL Server 2012 or later (including SQL Server 2017), you could create a numbering sequence using CREATE SEQUENCE.
To create SEQUENCE with increment of 10000, add the clause INCREMENT BY.
For example:
CREATE SEQUENCE Test.CountBy1
START WITH 10000
INCREMENT BY 10000
For more information, please consult this SQL Server documentation on CREATE SEQUENCE:
https://learn.microsoft.com/sql/t-sql/statements/create-sequence-transact-sql?view=sql-server-2017

you can use cte to generate N number for guids. Below is a sample cte to generate 100 rows for your guid.
Drop table #temp
Create Table #temp
(
ID uniqueidentifier,
ExpectedResult int
);
with cte as(
select newid() as new_id, 10000 as ctr
union all
select new_id, ctr + 10000 from cte where ctr/10000 < 100
)
insert into #temp
select * from cte option (MaxRecursion 0 );
select * from #temp;

You need to divide ROW_NUMBER() by 10000 and then multiply by 10000
You can write something like this
select *, rowNum10K = 10000 * (1 + (row_number() over (order by object_id)) / 10000)
from #temp

As suggested by Nick in the original question comments, have done this:
ROW_NUMBER() OVER (ORDER BY (SELECT 100))*10000

You can define the column as an identity to do this for you:
Create Table temp (
ID uniqueidentifier,
ExpectedResult int identity (10000, 10000)
);
insert into temp (ID)
select v.id
from (values (NEWID()), (NEWID()), (NEWID()), (NEWID()), (NEWID())) v(id);
Here is a db<>fiddle.

Related

Can I use lag function in an update function to update a variable table

I have this #variable table with an initial row. I'd like to update the following rows based on that first row.
DECLARE #varTable1 Table
(
[id] [int],
[field1] [decimal](18,4)
)
INSERT INTO #varTable1
VALUES
(1,20),
(1,NULL),
(1,NULL),
(1,NULL)
SELECT * FROM #varTable1
Let's just say I want to multiply field1 by 2. So the expected values following inital row would be
1 20
2 40
3 60
4 80
One way is to get the min value and multiple it by a row number - assuming your id column should actually be incrementing rather than all 1's e.g.
WITH cte AS (
SELECT *
, MIN(field1) OVER () * ROW_NUMBER() OVER (ORDER BY id ASC) newField1
FROM #varTable1
)
UPDATE cte SET field1 = newField1;
I guess id is important so you have to multiple field of same id
DECLARE #varTable1 Table
(
[id] [int],
[field1] [decimal](18,4)
)
INSERT INTO #varTable1
VALUES
(1,20),
(1,NULL),
(1,NULL),
(1,NULL),
(2,NULL)
;with CTE as
(
SELECT *,row_number()over(order by id)rn FROM #varTable1
),
CTE1 as
(
select id, min([field1])Minfield from #varTable1
group by [id]
)
select vt.id,vt.field1,c.Minfield*rn from CTE VT
inner join CTE1 c on vt.id=c.id

Adding Random Id for each unique value in table

I have the table like
ID RANDOM_ID
1 123
10 456
25 789
1 1112
55 1314
10 1516
I want the result to be like :
ID RANDOM_ID
1 123
10 456
25 789
1 123
55 1314
10 456
The same ID should have same random_ids. I'm using the update statement to generate the Random_IDs after creating the table.
CREATE TABLE [RANDOMID_TABLE]([ID] [int] NULL, [RANDOM_ID] [int] NULL)
GO
INSERT INTO [RANDOMID_TABLE] ([ID])
select distinct ABC_ID from RANDOMID_ABC
GO
******** This is the update statement for the RANDOM_ID column in
[RANDOMID_TABLE] table ************
UPDATE [RANDOMID_TABLE]
SET RANDOM_ID = abs(checksum(NewId()) % 1000000)
Is there something else that I need to add to the update statement?
Please advise.
Why would you use update for this? Just generate the values when you insert them:
insert into [RANDOMID_TABLE] (ID, RANDOM_ID)
select ABC_ID, abs(checksum(NewId()) % 1000000)
from RANDOMID_ABC
group by ABC_ID;
EDIT:
If your problem is collisions, then fix how you do the assignment. Just assign a number . . . randomly:
insert into [RANDOMID_TABLE] (ID, RANDOM_ID)
select ABC_ID, row_number() over (order by newid())
from RANDOMID_ABC
group by ABC_ID;
This is guaranteed to not return duplicates.
At a total guess, are you simpling wanting to UPDATE the table so that all the values of a specific ID to have the same value for Random_ID? Like this?
CREATE TABLE YourTable (ID int, Random_ID int);
INSERT INTO YourTable
VALUES(1 ,123),
(10,456),
(25,789),
(1 ,1112),
(55,1314),
(10,1516);
GO
WITH CTE AS(
SELECT ID,
Random_ID,
MIN(Random_ID) OVER (PARTITION BY ID) AS Min_Random_ID
FROM YourTable)
UPDATE CTE
SET Random_ID = Min_Random_ID;
GO
SELECT *
FROM YourTable;
GO
DROP TABLE YourTable;
Here is the script you need with use of temporary table (you need it to persist your random results for each unique ID):
DECLARE #Tbl TABLE (ID INT, RANDOM_ID INT)
INSERT #Tbl (Id) VALUES(1), (10), (25), (1), (55), (10)
SELECT Id, abs(checksum(NewId()) % 1000000) AS Random_Id INTO #distinctData FROM #Tbl GROUP BY Id
SELECT D.* FROM #Tbl T JOIN #distinctData D ON D.ID = T.ID
DROP TABLE #distinctData
Obviously, you don't need the first two rows where I create and initialize data table
Result:
Id Random_Id
1 354317
1 62026
10 532304
10 604768
25 874209
55 718643
You want one random value per ID. So one should think that the following would work:
with ids as
(
select distinct id
from randomid_table
)
, ids_with_rnd as
(
select id, abs(checksum(NewId()) % 1000000) as rnd
from ids
)
update randomid_table
set random_id =
(
select rnd
from ids_with_rnd
where ids_with_rnd.id = randomid_table.id
);
It doesn't however. SQL Server is somewhat buggy here and still creates different numbers for the same ID.
So, your best bet may be: do your update that does create different values (your original update statement). Then correct the data as follows:
update randomid_table
set random_id =
(
select min(random_id)
from randomid_table rt2
where rt2.id = randomid_table.id
);
Demo: https://dbfiddle.uk/?rdbms=sqlserver_2017&fiddle=504236db66fba0f12dc7e407a51451f8

Delete old entries from table in MSSQL

a table in my MSSQL db is getting over 500MB. I would like to delete the x last entries so the table size is only 100MB. This should be a task which runs once a week. How could I do this?
Example:
Table before deleting the old entries:
Table after deleting the old entries:
You can use DATALENGTH to get the size of the data in a particular column. With a window function, you can sum up a running total of DATALENGTH values. Then you can delete all records in a table that push you past a desired max table size. Here's an example:
-- Sample table with a VARBINARY(MAX) column
CREATE TABLE tmp (id INT IDENTITY(1,1) PRIMARY KEY, col VARBINARY(MAX))
-- Insert sample data - 20 bytes per row.
;WITH cte AS
(
SELECT 1 AS rn, HASHBYTES('sha1', 'somerandomtext') t
UNION all
SELECT rn + 1, HASHBYTES('sha1', 'somerandomtext')
FROM cte
WHERE rn< 5000
)
INSERT INTO tmp (col)
SELECT t FROM cte
OPTION (maxrecursion 0)
-- #total_bytes is the desired size of the table after the delete statement
DECLARE #total_bytes int = 200
-- Use the SUM window function to get a running total of the DATALENGTH
-- of the VARBINARY field, and delete when the running total exceeds
-- the desired table size.
-- You can order the window function however you want to delete rows
-- in the correct sequence.
DELETE t
FROM tmp t
INNER JOIN
(
SELECT id, SUM(DATALENGTH(col)) OVER (ORDER BY id) total_size
FROM tmp
)sq ON t.id = sq.id AND sq.total_size > #total_bytes
Now check what's left in tmp: 10 rows, and the total size of the "col" column matches the 200 byte size specified in the #total_bytes variable.
UPDATE: Here's an example using your sample data:
CREATE TABLE tmp (id INT PRIMARY KEY, contact VARCHAR(100), country VARCHAR(25))
GO
INSERT INTO tmp VALUES
(1, 'Maria Anders', 'Germany'),
(2, 'Francisco Chang', 'Mexico'),
(3, 'Roland Mendel', 'Austria'),
(4, 'Helen Bennett', 'UK'),
(5, 'Yoshi Tannamuri', 'Canada'),
(6, 'Giovanni Rovelli', 'Italy')
GO
-- #total_bytes is the desired size of the table after the delete statement
DECLARE #total_bytes INT = 40
-- Use the SUM window function to get a running total of the DATALENGTH
-- of the VARBINARY field, and delete when the running total exceeds
-- the desired table size.
-- You can order the window function however you want to delete rows
-- in the correct sequence.
DELETE t
FROM tmp t
INNER JOIN
(
SELECT id, SUM(DATALENGTH(contact)) OVER (ORDER BY id)
+ SUM(DATALENGTH(country)) OVER (ORDER BY id) total_size
FROM tmp
)sq ON t.id = sq.id AND sq.total_size > #total_bytes
SELECT * FROM tmp -- 2 rows left!
DELETE FROM TABLE_NAME WHERE date_column < '2018-01-01';
This will delete all data that entered before January 2018
if you want to delete last 7days data
delete from table_name WHERE date_column >= DATEADD(day,-7, GETDATE())

SQL Server window function for running percentage

I know there are several examples of recursion with CTE and so on, but how can this be accomplished just by using window functions in SQL Server 2012:
CREATE TABLE #temp
(
ID INT PRIMARY KEY IDENTITY(1,1) NOT NULL,
Percentage INT NOT NULL
)
DECLARE #Calculated MONEY = 1000
INSERT INTO #temp ( Percentage ) VALUES ( 100 )
INSERT INTO #temp ( Percentage ) VALUES ( 90)
INSERT INTO #temp ( Percentage ) VALUES ( 60)
INSERT INTO #temp ( Percentage ) VALUES ( 50)
INSERT INTO #temp ( Percentage ) VALUES ( 100)
And the result would be a running percentage like so (we are starting with $1000)
id percentage calculated
-- -------- ---------
1 100 1000
2 50 500
3 90 450
4 80 360
5 100 360
So the value for the next row is the percentage multiplied by the calculated value above that row. Can LAG be used on a computed alias?
Thanks,
You need a running product of the percentages instead of always comparing 2 consecutive rows, which is why LEAD and LAG won't work here.
You can use a windowed sum to keep a running product of the percentages against your variable to get your desired calculation:
SELECT
ID,
Expected,
EXP(SUM(LOG(CONVERT(FLOAT, Percentage) / 100)) OVER (ORDER BY ID)) * #Calculated AS Actual
FROM #Temp
Adding this to your sample code (with a column I added for your expected output):
CREATE TABLE #temp
(
ID INT PRIMARY KEY IDENTITY(1,1) NOT NULL,
Percentage INT NOT NULL,
Expected MONEY NOT NULL
)
DECLARE #Calculated MONEY = 1000
INSERT INTO #temp ( Percentage, Expected ) VALUES ( 100 , 1000)
INSERT INTO #temp ( Percentage, Expected ) VALUES ( 50, 500)
INSERT INTO #temp ( Percentage, Expected ) VALUES ( 90, 450)
INSERT INTO #temp ( Percentage, Expected ) VALUES ( 80, 360)
INSERT INTO #temp ( Percentage, Expected ) VALUES ( 100, 360)
SELECT
ID,
Expected,
EXP(SUM(LOG(CONVERT(FLOAT, Percentage) / 100)) OVER (ORDER BY ID)) * #Calculated AS Actual
FROM #Temp
This will yield your expected output:
ID Expected Actual
----------- --------------------- ----------------------
1 1000.00 1000
2 500.00 500
3 450.00 450
4 360.00 360
5 360.00 360
you can use recursive cte to get the desired result
with cte
as
(
select id, percentage, 1000 as calculated
from #temp
where id =1
union all
select t.id, t.percentage, t.percentage*cte.calculated/100 as calculated
from #temp t
join cte
on t.id = cte.id+1
)
select * from cte
I'm afraid, widow functions won't help here (at least they won't make it simple). The easiest way to achieve your goal is update statement with double assignment:
alter table #temp add VAL decimal
declare #val decimal = 1000
update t set
#val = VAL = #val * Percentage / 100
from (select top 100 percent * from #temp order by id) as t
select * from #temp

SQL query to know skipped number

Hi im new to SQL query i only know simple query.
My question is it possible to SELECT skipped check number EX2001,EX2002,EX2004
select result will show EX2003.
thanks in advance, sorry for my english.
you can answer algorithm only, ill try to implement it to SQL.
example:
SELECT * FROM SETTLEMENT WHERE checkno not in (between ex2001 and ex2900)
is it possible like this? im using MS SQL 2008.
create table sequence(st varchar(50))
insert into sequence values('EX2001');
insert into sequence values('EX2002');
insert into sequence values('EX2004');
insert into sequence values('EX2005');
insert into sequence values('EX2008');
Assuming your original table name is sequence with only one field, you can modify as per your needs
Try below
DECLARE #all TABLE
(
st varchar(20)
)
declare #start int
declare #end int
declare #str varchar(20)
set #start=2000 //define starting point
set #end=2010 //define end point
while(#start<#end)
BEGIN
SET #start=#start+1
set #str='EX'+cast(#start as varchar(20))
INSERT INTO #all VALUES (''+#str+'')
END
SELECT * from #all
except
select * from sequence
output
st
EX2003
EX2006
EX2007
EX2009
EX2010
if you need hard coded values in query then it can be done like (as you did not posted any code so here is a simple query). you can use IN() or NOT IN() in sql
select * from table where check_number not in ('EX2001','EX2002','EX2004' );
Since SQL engine is not mentioned, this answer is only for Oracle 11G
Option 1: With hierarchical queries
See Fiddle here
CREATE TABLE TEST1 ( A VARCHAR2 ( 9 ) );
INSERT INTO
TEST1
VALUES
( 'EX2001' );
INSERT INTO
TEST1
VALUES
( 'EX2002' );
INSERT INTO
TEST1
VALUES
( 'EX2004' );
COMMIT;
WITH TEST2
AS (SELECT
TO_NUMBER(SUBSTR ( A,
3 ))
AS A
FROM
TEST1)
SELECT
MIN_A
- 1
+ LEVEL
FROM
(SELECT
MIN ( A ) MIN_A,
MAX ( A ) MAX_A
FROM
TEST2)
CONNECT BY
LEVEL <= MAX_A
- MIN_A
+ 1
MINUS
SELECT A FROM TEST2;
Option 2: With Oracle analytics function
See Fiddle here
WITH T
AS (SELECT
TO_NUMBER(SUBSTR ( A,
3 ))
AS SNO,
SYSDATE AS SDATE
FROM
TEST1)
SELECT
SDATE,
SNO
+ 1
FIRST_MISSING,
DECODE ( NEXT_SNO
- 1,
SNO
+ 1, TO_NUMBER ( NULL ),
NEXT_SNO
- 1 )
LAST_MISSING
FROM
(SELECT
SDATE,
SNO,
LAG ( SNO )
OVER ( PARTITION BY SDATE
ORDER BY SNO )
LAST_SNO,
LEAD ( SNO )
OVER ( PARTITION BY SDATE
ORDER BY SNO )
NEXT_SNO
FROM
T)
WHERE
NVL ( NEXT_SNO,
SNO
+ 1 ) <> SNO
+ 1;