Using an Oracle sequence when the data isn't a complete sequence - sql

Given a DB table containing a unique sequence as primary key, and an additional value VAL;
I need to retrieve the values that match 'like %b', but I need to retrieve them one at a time in a sequence. If the values were consecutive, I would just use an Oracle sequence to work through PKEY using nextval, currval etc.
The problem here is that if the sequence is 4, and I go for nextval, it gives me an incorrect value (a %a value instead of a %b value).
The one solution I can think of is to use the sequence with 'more than' rather than 'equals', so for example (excuse the pseudocode, I can work out correct syntax later);
select pkey, val from table where val like '%b' and pkey>seq.currval
And then shift the sequence past the value of pkey (again using pseudocode for clarity);
alter sequence seq set currval=pkey
For a little context, this table will be accessed by many virtual users as part of a performance test, they each need to be able to run a query that will get them a unique value (so vuser 1 gets 58b, vuser2 gets 24b, etc). The data cannot be ordered or filtered beforehand.

Do you just want to count the values that end in b?
select pkey, val,
(case when val like '%b'
then row_number() over (partition by (case when val like '%b' then 1 else 0 end) order by pkey
)
end) as b_seq
from table;
You can use this as a subquery if you want to find the row corresponding to a particular row of b_seq.
If you don't care about the other values, this is simpler to write with a where clause:
select pkey, val,
row_number() over (order by pkey) as b_seq
from table
where val like '%b';

Building on Gordon's answer I would do:
with
x as (
select seq.nextval as s
),
y as (
select pkey, val,
row_number() over (order by pkey) as b_seq
from table
where val like '%b'
)
select y.* from y join x on y.b_seq = x.s
This way each request will increment the sequence and will return a different SINGLE value from the "b subset".

Related

Loop through table and update a specific column

I have the following table:
Id
Category
1
some thing
2
value
This table contains a lot of rows and what I'm trying to do is to update all the Category values to change every first letter to caps. For example, some thing should be Some Thing.
At the moment this is what I have:
UPDATE MyTable
SET Category = (SELECT UPPER(LEFT(Category,1))+LOWER(SUBSTRING(Category,2,LEN(Category))) FROM MyTable WHERE Id = 1)
WHERE Id = 1;
But there are two problems, the first one is trying to change the Category Value to upper, because only works ok for 1 len words (hello=> Hello, hello world => Hello world) and the second one is that I'll need to run this query X times following the Where Id = X logic. So my question is how can I update X rows? I was thinking in a cursor but I don't have too much experience with it.
Here is a fiddle to play with.
You can split the words apart, apply the capitalization, then munge the words back together. No, you shouldn't be worrying about subqueries and Id because you should always approach updating a set of rows as a set-based operation and not one row at a time.
;WITH cte AS
(
SELECT Id, NewCat = STRING_AGG(CONCAT(
UPPER(LEFT(value,1)),
SUBSTRING(value,2,57)), ' ')
WITHIN GROUP (ORDER BY CHARINDEX(value, Category))
FROM
(
SELECT t.Id, t.Category, s.value
FROM dbo.MyTable AS t
CROSS APPLY STRING_SPLIT(Category, ' ') AS s
) AS x GROUP BY Id
)
UPDATE t
SET t.Category = cte.NewCat
FROM dbo.MyTable AS t
INNER JOIN cte ON t.Id = cte.Id;
This assumes your category doesn't have non-consecutive duplicates within it; for example, bora frickin bora would get messed up (meanwhile bora bora fickin would be fine). It also assumes a case insensitive collation (which could be catered to if necessary).
In Azure SQL Database you can use the new enable_ordinal argument to STRING_SPLIT() but, for now, you'll have to rely on hacks like CHARINDEX().
Updated db<>fiddle (thank you for the head start!)

If else using case for returning value

I am trying to do an if else statement using CASE. If no records, then make it 0 then +1. Or else, just take the last record then +1. At first try i used ISNULL(statement,0). But it doesn't have else statement.
Then I saw many other examples on StackOverflow for a case statement, but I don't seem to understand how to implement it
INSERT INTO TICKET_SALES (
TRXDATE,
KIOSKID,
BOOKINGREFERENCENUM,
)
VALUES (
'2019-01-18 16:59:29',
'KIOSK1',
((SELECT TOP 1 BOOKINGREFERENCENUM FROM TICKET_SALES ORDER BY BOOKINGREFERENCENUM DESC)+1),
);
If you need to use expressions to supply values, you can change VALUES for SELECT, although you will only be able to supply 1 row with SELECT (unless you use UNION ALL).
INSERT INTO TICKET_SALES (
TRXDATE,
KIOSKID,
BOOKINGREFERENCENUM
)
SELECT
'2019-01-18 16:59:29',
'KIOSK1',
1 + ISNULL(
(SELECT TOP 1 BOOKINGREFERENCENUM FROM TICKET_SALES ORDER BY BOOKINGREFERENCENUM DESC),
0)
Please be ware of using this type of "tricks" to generate IDs, as others pointed out, it's a very bad idea (check StepUp's answer to know how to correct).
Just create table with Idetitity(1, 1):
CREATE table TicketSales
(
TrxDate DATETIME
, KioskId INT
, BookingReferenceNum int IDENTITY(1,1)
)
As MSDN says:
Identity columns can be used for generating key values. The identity
property on a column guarantees the following:
Each new value is generated based on the current seed & increment.
Each new value for a particular transaction is different from other
concurrent transactions on the table.
The identity property on a column does not guarantee the following:
Uniqueness of the value - Uniqueness must be enforced by using a
PRIMARY KEY or UNIQUE constraint or UNIQUE index.
... to further read
The easiest way IMO would be to declare variable to hold desired value, assign value to it in easy-to-read way, and then use it in INSERT statement.
Moreover, no need to takke TOP 1 with ordering, just use MAX function :)
DECLARE #id INT;
SELECT #id = CASE COUNT(*) WHEN 0 THEN 0 ELSE MAX(BOOKINGREFERENCENUM) + 1 END
FROM MyTable;
INSERT INTO TICKET_SALES (
TRXDATE,
KIOSKID,
BOOKINGREFERENCENUM,
)
VALUES (
'2019-01-18 16:59:29',
'KIOSK1',
#id
);
NOTE: you are reinventing the wheel, SQL has something like IDENTITY, it's good for this kind of task.

SQL Query : should return Single Record if Search Condition met, otherwise return Multiple Records

I have table with Billions of Records, Table structure is like :
ID NUMBER PRIMARY KEY,
MY_SEARCH_COLUMN NUMBER,
MY_SEARCH_COLUMN will have Numeric value upto 15 Digit in length.
What I want is, if any specific record is matched, I will have to get that matched value only,
i.e. : If I enter WHERE MY_SEARCH_COLUMN = 123454321 and table has value 123454321 then this only should be returned.
But if exact value is not matched, I will have to get next 10 values from the table.
i.e. : if I enter WHERE MY_SEARCH_COLUMN = 123454321 and column does not have the value 123454321 then it should return 10 values from the table which is greater than 123454321
Both the case should be covered in single SQL Query, and I have have to keep in mind the Performance of the Query. I have already created Index on the MY_SEARCH_COLUMN columns, so other suggestions are welcome to improve the Performance.
This could be tricky to do without using a proc or maybe some dynamic SQL, but we can try using ROW_NUMBER here:
WITH cte AS (
SELECT ID, MY_SEARCH_COLUMN,
ROW_NUMBER() OVER (ORDER BY MY_SEARCH_COLUMN) rn
FROM yourTable
WHERE MY_SEARCH_COLUMN >= 123454321
)
SELECT *
FROM cte
WHERE rn <= CASE WHEN EXISTS (SELECT 1 FROM yourTable WHERE MY_SEARCH_COLUMN = 123454321)
THEN 1
ELSE 10 END;
The basic idea of the above query is that we assign a row number to all records matching the target or greater. Then, we query using either a row number of 1, in case of an exact match, or all row numbers up to 10 in case of no match.
SELECT *
FROM your_table AS src
WHERE src.MY_SEARCH_COLUMN = CASE WHEN EXISTS (SELECT 1 FROM your_table AS src2 WITH(NOLOCK) WHERE src2.MY_SEARCH_COLUMN = 123456321)
THEN 123456321
ELSE src.MY_SEARCH_COLUMN
END

SQL Server using a sequence in conjunction with union selects

CREATE SEQUENCE id AS INTEGER START WITH 1 INCREMENT BY 1;
WITH source (Id, SomeColumn) AS
(
SELECT NEXT VALUE FOR id, 'some value 1'
UNION
SELECT NEXT VALUE FOR id, 'some value 2'
)
SELECT * FROM source;
DROP SEQUENCE id;
It throws the following error:
NEXT VALUE FOR function is not allowed in check constraints, default
objects, computed columns, views, user-defined functions, user-defined
aggregates, user-defined table types, sub-queries, common table
expressions, derived tables or return statements.
Why does the NEXT VALUE FOR function not work with unions?
What is a good alternative?
I am using a similar CTE to seed a table with default values using a merge statement. I am trying to avoid manually typing in a sequence of numbers.
I need something like UNION SELECT id++
Based on Alex's comment and link to a similar problem here is the solution:
WITH source (SomeColumn) AS
(
SELECT 'some value 1'
UNION SELECT 'some value 2'
)
SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL)), SomeColumn FROM source;
For future reference: ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) is the way to get row numbers without ordering a set

SQL Server random using seed

I want to add a column to my table with a random number using seed.
If I use RAND:
select *, RAND(5) as random_id from myTable
I get an equal value(0.943597390424144 for example) for all the rows, in the random_id column. I want this value to be different for every row - and that for every time I will pass it 0.5 value(for example), it would be the same values again(as seed should work...).
How can I do this?
(
For example, in PostrgreSql I can write
SELECT setseed(0.5);
SELECT t.* , random() as random_id
FROM myTable t
And I will get different values in each row.
)
Edit:
After I saw the comments here, I have managed to work this out somehow - but it's not efficient at all.
If someone has an idea how to improve it - it will be great. If not - I will have to find another way.
I used the basic idea of the example in here.
Creating a temporary table with blank seed value:
select * into t_myTable from (
select t.*, -1.00000000000000000 as seed
from myTable t
) as temp
Adding a random number for each seed value, one row at a time(this is the bad part...):
USE CPatterns;
GO
DECLARE #seed float;
DECLARE #id int;
DECLARE VIEW_CURSOR CURSOR FOR
select id
from t_myTable t;
OPEN VIEW_CURSOR;
FETCH NEXT FROM VIEW_CURSOR
into #id;
set #seed = RAND(5);
WHILE ##FETCH_STATUS = 0
BEGIN
set #seed = RAND();
update t_myTable set seed = #seed where id = #id
FETCH NEXT FROM VIEW_CURSOR
into #id;
END;
CLOSE VIEW_CURSOR;
DEALLOCATE VIEW_CURSOR;
GO
Creating the view using the seed value and ordering by it
create view my_view AS
select row_number() OVER (ORDER BY seed, id) AS source_id ,t.*
from t_myTable t
I think the simplest way to get a repeatable random id in a table is to use row_number() or a fixed id on each row. Let me assume that you have a column called id with a different value on each row.
The idea is just to use this as a seed:
select rand(id*1), as random_id
from mytable;
Note that the seed for the id is an integer and not a floating point number. If you wanted a floating point seed, you could do something with checksum():
select rand(checksum(id*0.5)) as random_id
. . .
If you are doing this for sampling (where you will say random_id < 0.1 for a 10% sample for instance, then I often use modulo arithmetic on row_number():
with t as (
select t.* row_number() over (order by id) as seqnum
from mytable t
)
select *
from t
where ((seqnum * 17 + 71) % 101) < 0.1
This returns about 10% of the numbers (okay, really 10/101). And you can adjust the sample by fiddling with the constants.
Someone sugested a similar query using newid() but I'm giving you the solution that works for me.
There's a workaround that involves newid() instead of rand, but it gives you the same result. You can execute it individually or as a column in a column. It will result in a random value per row rather than the same value for every row in the select statement.
If you need a random number from 0 - N, just change 100 for the desired number.
SELECT TOP 10 [Flag forca]
,1+ABS(CHECKSUM(NEWID())) % 100 AS RANDOM_NEWID
,RAND() AS RANDOM_RAND
FROM PAGSEGURO_WORK.dbo.jobSTM248_tmp_leitores_iso
So, in case it would someone someday, here's what I eventually did.
I'm generating the random seeded values in the server side(Java in my case), and then create a table with two columns: the id and the generated random_id.
Now I create the view as an inner join between the table and the original data.
The generated SQL looks something like that:
CREATE TABLE SEED_DATA(source_id INT PRIMARY KEY, random_id float NOT NULL);
select Rand(5);
insert into SEED_DATA values(1,Rand());
insert into SEED_DATA values(2, Rand());
insert into SEED_DATA values(3, Rand());
.
.
.
insert into SEED_DATA values(1000000, Rand());
and
CREATE VIEW DATA_VIEW
as
SELECT row_number() OVER (ORDER BY random_id, id) AS source_id,column1,column2,...
FROM
( select * from SEED_DATA tmp
inner join my_table i on tmp.source_id = i.id) TEMP
In addition, I create the random numbers in batches, 10,000 or so in each batch(may be higher), so it will not weigh heavily on the server side, and for each batch I insert it to the table in a separate execution.
All of that because I couldn't find a good way to do what I want purely in SQL. Updating row after row is really not efficient.
My own conclusion from this story is that SQL Server is sometimes really annoying...
You could convert a random number from the seed:
rand(row_number over (order by ___, ___,___))
Then cast that as a varchar
, Then use the last 3 characters as another seed.
That would give you a nice random value:
rand(right(cast(rand(row_number() over(x,y,x)) as varchar(15)), 3)