I have a table with 3 columns :
ID
productId
customerId
1
5
1
2
4
1
3
5
1
4
4
1
I want to add a new column called ID_MOD and its value by default will be ID%X (X is a number).
Expected result for X=3 :
ID
productId
customerId
ID_MOD
1
5
1
1
2
4
1
2
3
5
1
0
4
4
1
1
I have X instances of my app and I want each instance to query specific ID_MOD values(0/1/2.../X-1).
Is it possible to use default values for columns? If it is, can the default value be calculated based on other columns ?
what you can do is create a stored function that receives x as an input parameter, and extends your table with a calculated column (at query time).
For example:
.create-or-alter function FunctionName(x:int)
{
TableName
| extend ID_MOD = ID % x
}
If you decide x always has the same value and shouldn't be parameter, you can name the function using the same name as the table, and it will 'hide' the original table.
If the logic of calculating the extended column is well-defined in advance, you can invoke it at ingestion time, using an update policy
Related
Is it possible to order a SQLite select result in way that highest value is in the center and lower value are distributed around center till start and end of result set?
data set:
5
3
2
1
1
1
1
desired result:
1
1
3
5
2
1
1
Use ROW_NUMBER() window function to assign a row number to each value and with MAX() window function get the difference of each value from the max value of the table so they can be distributed around the max value:
SELECT *
FROM tablename
ORDER BY CASE WHEN ROW_NUMBER() OVER (ORDER BY col) % 2 = 1 THEN -1 ELSE 1 END *
(MAX(col) OVER () - col)
Change col to the column's name.
See the demo.
I use SQLite to log every user's every access to my server. Every time a user uses a function, I append a record to the database.
The database looks like:
usr_id fun_id
3 1 // user_3 used function_1
2 13 // user_2 used function_13
3 11 // user_3 used function_11
2 1 // user_2 used function_1
7 2 // ...
usr_id stands for a user, fun_id stands for functions like login / send_text / logout...
I want to know each function's usage, used by who and how many times, to plot with gnuplot. In short, I need this for plotting:
fun_id usr_id used_count
1 2 1 // user_2 used function_1 once
1 3 1 // user_3 used function_1 once
2 7 1 // user_7 used function_2 once
13 2 3 // user_2 used function_13 three times
How to generate this with a SQL query?
Just use count(*) along with a grouping:
select fun_id, usr_id, count(*) as used_count
from tablename
group by fun_id, usr_id
order by fun_id, usr_id;
Still learning SQL :)
This time I'd like to a 'linked list' walk from a table I guess using CTE.
Despite all the example on the web I could not find one simple example I could start from then peek and poke from there.
Here is my table
create table yo (id integer, nx integer)
select * from yo
id nx
---------- ----------
1 5
2 4
3 7
4 9
5 3
6 0
7 0
8 6
9 8
I'd like to get a list of 'id','nx' from yo following the next link 'nx' given a start 'id'
So a start 'id' of 1 would produce
id nx
---------- ----------
1 5
3 7
5 3
7 0
Note that 0 is a end marker.
I can't find the magic SQL for doing this
Thanx in advance
Cheers,
Phi
The first row of the list is easy:
SELECT id, nx
FROM yo
WHERE id = 1
If the nx column of the previous entry is available as list.nx, the next entry can be returned with this query:
SELECT yo.id, yo.nx
FROM yo
JOIN list ON yo.id = list.nx
Then just plug these together:
WITH RECURSIVE list(id, nx) AS (
SELECT id, nx
FROM yo
WHERE id = 1
UNION ALL
SELECT yo.id, yo.nx
FROM yo
JOIN list ON yo.id = list.nx
)
SELECT * FROM list
(This stops automatically because there is no row where id is zero; otherwise, you could add a WHERE list.nx != 0.)
I have following two tables:
ID_PERSON NAME
-----------------
1 John
2 Joe
3 Peter
ID_PERSON ID_SPECIALIZATION
------------------------------
1 5
1 6
1 7
2 5
2 1
3 6
3 10
I need to filter data based on group of ids ID_SPECIALIZATION that will be provided. For example
I want to display only those persons who has specialization in 5 and 6 so it will return only first person. In ASP.NET Web form there will be two listboxes, left and right button, in first LB there will be all possible specializations and user will choose some of them to second LB as filtering options. I have no idea how to put this filtering condition in sql query. Thanks for help.
You could use the following:
SQL> SELECT p.id_person, p.NAME
2 FROM person p
3 JOIN person_spe s ON p.id_person = s.id_person
4 WHERE id_specialization IN (5, 6)
5 GROUP BY p.id_person, p.NAME
6 HAVING COUNT(*) = 2;
ID_PERSON NAME
---------- -----
1 John
One way to do it:
SELECT
ID_PERSON
, NAME
FROM
Person AS p
WHERE EXISTS
( SELECT *
FROM
PersonSpecialization AS ps
WHERE ps.ID_PERSON = p.ID_PERSON
AND ps.ID_SPECIALIZATION = 5
)
AND EXISTS
( SELECT *
FROM
PersonSpecialization AS ps
WHERE ps.ID_PERSON = p.ID_PERSON
AND ps.ID_SPECIALIZATION = 6
)
SELECT d1.id_person, d1.name FROM tbl_table1 d1
INNER JOIN tbl_table2 d1
ON d1.ID_PERSON=d2.ID_PERSON
WHERE ID_SPECILIZATION = ?
Theres the query but I'm not sure how asp.net works and passing in the value. It might be work looking up bind variables which allows you to use place holders in the sql which oracle then caches the query and just uses the values that you pass in at run tuime using EXECUTE IMMEDIATE.
how can i generate 6 numbers between 1 and 2 where 4 of the numbers will be 1 and the other 2 will be 2 in a random order i.e.
results
1
2
1
1
1
2
and also in a different ratio i.e. 3:2:1 for numbers between 1 and 3 for 12 numbers
i.e.
results
1
1
2
3
1
2
1
3
1
1
3
3
results don't have to be in this order but in the ratios as above in oracle SQL or PL/SQL
To get the ratios perfect you could do something like this - generate all the numbers, then sort in random order:
SELECT r
FROM (SELECT CASE
WHEN ROWNUM <=4 THEN 1
ELSE 2
END AS r
FROM DUAL
CONNECT BY LEVEL <= 6)
ORDER BY DBMS_RANDOM.value;
R
----------------------
2
1
1
2
1
1
I think this will work in straight SQL; it's horrifically inefficient, and a PL/SQL one might be less so. It's also completely static; differing ratios call for a different number of values selected.
select value
from (
select mod(value, 2) + 1 as value,
row_number() over (partition by
case mod(value, 2) = 1
then 1
else 0
end) as twos_row,
row_number() over (partition by
case mod(value, 2) = 0
then 1
else 0
end) as ones_row
from (select dbms_crypto.randominteger as value
from dba_objects
order by object_id
)
)
where twos_rows <= 2
or ones_rows <= 4
The inner-most select grabs a big stack of random numbers. The next query out determines whether that random value would be a 2 or a 1 by mod'ing the earlier random value. The last level of nesting just filters out all the rows after the correct number of that type of row has been returned.
This is untested and fragile. If you need a solution that's reliable and performance, I'd recommend PL/SQL, where you
loop
pick off random numbers
determine what partition in your set of values they'd fit into
keep them if that partition hasn't been satisfied
exit when all partitions have been satisfied.