Table has an index , it starts from 1 and the rest will be sequentially numbered, and now there are missing data that cause discoontinuity, how can I write sql to find the serial number missing, such as the following, by serial number sorting line number is greater than the ratio of the current line number 1 data.
You can use the deltas function, for example:
t=table(1..10 join [12, 14,16] as id)
select id from t where deltas(id)>1
The id in the above t table jumps from 10 to 12, 14, 16, and the select statement can query 12, 14, and 16.
Related
I need to build a sales pipeline with one query in SQL (Big Query).
The table has columns:
-timestamp (event time)
-id (user id)
-event
Each event is a number from 1 to 8. And I need to calculate how many unique users there were at each step.
Each step is counted only if the previous steps have been completed.
It is not necessary to go through them straight one after the other, but the main thing is that before each step, n-1 step was taken earlier.
If you sort the table by 'timestamp', you often get such sequences for one 'id' at one day:
4, 4, 1, 1, 3, 6, 5, 5, 6, 5, 6, 7, 8, 1, 2, 5, 3, 4.
In this example, the longest sequence is 1, 2, 3, 4.
The sequence is counted in one day!
I failed to solve the problem through the max/min/lag/lead window functions. I even did a 'case' with a sequential comparison with lag+n values.
I wasted 2 days for this task(
I need any kind of DB that will be able to do my task which is to know if there is any way to grab only data that the user hasn't pulled from the databases yet for example:
I have a user that pulls 5 country names from the database each time, and when he finishes viewing them I want him to get 5 more country names that he didn't pull till now.
can you help me find a way to do it?
*sorry for my English
The key to this is ordered results:
select id, name from country where id > #highest_id_so_far order by id limit 5;
Start with a negative #highest_id_so_far. You get the first entries, say, IDs 1, 4, 5, 6, 7.
The highest ID returned was 7, so query with #highest_id_so_far = 7 then and you get the next five rows (e.g. 8, 10, 12, 23, 24). And so on.
I have 2 following tables:
Table: product_hierarchy
SELECT * FROM product_hierarchy;
HEAD: parent_id, child_id
ROW 1: 1000, 1001
ROW 2: 1000, 1002
ROW 3: 1000, 1003
ROW 4: 2000, 2001
ROW 5: 2000, 2002
... etc.
Table: attribute_names
SELECT * FROM attribute_names;
HEAD: aname
ROW 1: product_name
ROW 2: price
ROW 3: height
ROW 4: width
ROW 5: length
... etc.
I need to run a function called
FUNCTION get_attribute(id NUMBER(10), attribute_name VARCHAR2(256)) RETURNS VARCHAR2
on every child_id in table product_hierarchy,
using every attibute name in table attribute_names
and then combine everything into table product_hierarchy.
The final table should look like this for example:
Table: product_hierarchy
SELECT * FROM table_hierarchy;
HEAD: parent_id, child_id, product_name, price, height, width, length
ROW 1: 1000, 1001, box1, 20, 10, 10, 10
ROW 2: 1000, 1002, box2, 15, 9, 9, 9
ROW 3: 1000, 1003, box3, 30, 15, 15, 15
ROW 4: 2000, 2001, panel1, 5, 10, 10, 2
ROW 5: 2000, 2002, panel2, 10, 20, 20, 3
... etc.
I can't seem to come up with an appropriate SQL query that does the thing I need. Anyone up to the task?
To get all data in a long list is doable:
SELECT
ph.parent_id,
ph.child_id,
th.aname AS attribute_label,
get_attribute(child_id, th.aname) AS attribute
FROM
product_hierarchy ph
CROSS JOIN table_hierarchy th
To get those attributes as separate columns is a tough job. Even when you use the pivot features of Oracle, you cannot do this for a variable/unknown number of values. You have to specify the labels that are made into columns.
So I hope the query above will do.
If not, I think you would have to query all the labels first for table_hierarchy, then, you can build a dynamic SQL statement, based on the labels you found, and translate every one of them into a column. Then, you can return that data from a function. Not a very nice solution, but I think there is no other way if you must return the data in exact that structure.
I started to create a sequence in Oracle. While going through the oracle documentation I got this prototype
Create Sequence SeqTest5
Start With 0
Increment by 1
Min value 0
Maxvalue 8
NoCycle --I got to know we can give this as 'Cycle' which will again
-- Repeat the loop. But my doubt is why cannot we specify number of
-- Loops. Such as Cycle 20
NoCache --I got to know we can give certain buffer lenght via cache
Can you Please explain me why cannot we declare it as I have tried it and got this error
1 create sequence seqtest4
2 cache 30,
3* cycle 20,
SQL> /
cache 30,
*
ERROR at line 2:
ORA-00933: SQL command not properly ended
For Example:-
TNUM
0
1
4
2
3
5
6
7
8
This 0-8 should write 10 times and stop.
You can't specify the number of cycles; just whether or not you want to cycle. The CREATE SEQUENCE syntax is here.
There are a few problems with your CREATE SEQUENCE above:
The commas - they don't belong; just get rid of them.
Specifying CYCLE 20 - you can specify CYCLE or NOCYCLE only. The default is NOCYCLE.
If you specify CYCLE you must also specify a MAXVALUE.
Addendum: Question updated with actual requirement, which is to count 1-8 ten times. Here's how to do it without sequences; it's based on an often-used Oracle trick for generating number sequences:
SELECT LEVEL FROM DUAL CONNECT BY LEVEL <= 8
The statement above will output the numbers 1 through 8, in order. To repeat it ten times you need to do another "1 through 10" counter, then cross join it to the "1 through 8", then make sure it orders correctly. This complicates thing a bit, which can be seen in the final answer:
SELECT SeqCounter FROM (
SELECT SeqCounter, CycleCounter FROM (
SELECT LEVEL AS SeqCounter FROM DUAL CONNECT BY LEVEL <= 8)
CROSS JOIN (
SELECT LEVEL AS CycleCounter FROM DUAL CONNECT BY LEVEL <= 10)
) ORDER BY CycleCounter, SeqCounter
The statement above will give the output requested in the question.
Wouldn't this be best handled with a modulo function?
CREATE SEQUENCE seqtest5 START WITH 0 INCREMENT BY 1 MINVALUE 0 MAXVALUE 80 NOCYCLE;
SELECT mod(seqtest5.nextval, 9) from dual;
0, 1, 2, 3, 4, 5, 6, 7, 8, 0, 1, 2, 3, 4, 5, 6, ...
ORA-08004: sequence SEQTEST5.NEXTVAL exceeds MAXVALUE and cannot be instantiated
This is a homework task for Microsoft Access 2010.
How to create a query that will return a list of numbers from 1 to 18? Though on one condition, that the list of numbers will not contain any number that exists in table A.
So for example.
Table A has 1, 5, 10, 8, and 16.
Therefore, the query will only return this list of numbers (2, 3, 4, 6, 7, 9, 11, 12, 13, 14, 15, 17, 18)
Would anyone be kind enough to shed some pointers?
Thanks.
SQL is a set-based language. So you are always working with intersections, unions, complements, etc.
Consider that Table A could be completely empty. In this case you need to return all the numbers from 1 to 18. You are going to need to get these numbers from somewhere. (Hint, how about creating another table?)
Once you can write a query returning all the numbers from 1 to 18, you can start thinking about getting all the numbers from 1 to 18 EXCEPT those in table A.