How to get a block of number from snowflake sequence - sequence

I see only this way to get sequence number. but I want to get a block of them.
select seq1.nextval a from dual;

You can use a snowflake generator to generate rows and then use seq.nextval to get a block of sequences. I assume this is what you mean:
-- Create the sequence
create or replace sequence seq1;
-- Generate the block
select
s.nextval
from table (generator(rowcount => 10)) v,
table (getnextval(seq1)) s
order by 1;
Output of the above select:
+-------+
|NEXTVAL|
+-------+
|1 |
|2 |
|3 |
|4 |
|5 |
|6 |
|7 |
|8 |
|9 |
|10 |
+-------+
It's important to understand the semantics of sequences. Sequences do not necessarily get generated sequentially (without gaps) as they have in this example but they will always be unique.
From the docs linked above:
There is no guarantee that values from a sequence are contiguous (gap-free) or that the sequence values are assigned in a particular order. There is, in fact, no way to assign values from a sequence to rows in a specified order other than to use single-row statements (this still provides no guarantee about gaps).

Related

Select a large number of ids from a Hive table

I have a large table with format similar to
+-----+------+------+
|ID |Cat |date |
+-----+------+------+
|12 | A |201602|
|14 | B |201601|
|19 | A |201608|
|12 | F |201605|
|11 | G |201603|
+-----+------+------+
and I need to select entries based on a list with around 5000 thousand IDs. The straighforward way would be to use the list as a WHERE clause but that would have a really bad performance and probably it even would not work. How can I do this selection?
Using a partitioned table things run fast. Once you partitioned the table add your ids into the where.
You can also extract a subtable from the original one selecting all the rows which have their ids between the min and the max of you ids list.

SQL Architecture design

I have the following tables:
+---+---------+
|id | name | foreign_key1 = this table's id
+---+---------+
|1 | White |
|2 | Black |
+---+---------+
+----+------+-------------+
|id | name | foreign_key1|
+----+------+-------------+
|1 | Grey | 1 |
|2 | Grey | 2 |
+----+------+-------------+
Is there a way that I could persist the last table's information with only one row? So that table could represent that grey is both white and black in one row?
You could use an array-like column type (string) and make it a one row record, but I wouldn't suggest that, it's better to have them as separate rows. Your approach is fine, but I'll suggest (if I've understood your idea) a little different schema:
You can make two tables: Colors, and Related_Colors, like this:
Colors
+---+---------+
|id | name |
+---+---------+
|1 | White |
|2 | Black |
|3 | Gray |
+---+---------+
Related_Colors
+---+---------+---------+
|id |color1_id|color2_id|
+---+---------+---------|
|1 |3 |1 |
|2 |3 |2 |
+---+---------+---------+
You could; it's called denormalization (https://en.wikipedia.org/wiki/Denormalization). Essentially you would need to create (in the second table) a column for each one of the possible IDs in the first table. So the schema of the second table would be:
ID
Name
White
Black
This also explains why you probably should not do it; what happens when you want to add another ID in the first table (e.g. purple)? As things are you would just need to add another row in table 1, and reference it from the relevant rows. If you denormalize this way, you would need to change the schema to accommodate the new possible value. The new column would of course be empty for most rows.
Another possibility would be to maintain the values as a concatenated string; so the schema would be
ID
Name
List Of IDs From Table1
And in this case the last field would contain White, Black. The drawback of this approach is that you can no longer query efficiently by the values from table1. (You can't properly index that field)
Ultimately the question is - what are your needs. If you need to read rows quickly, and have them in a 'reporting friendly' format, denormalization may work for you. But in most DB design cases it would not be required.

Database Sql Chain Integers in row

At the moment I have for each users an integer in my database
which datatype would I have to use if I want to chain multiply integers for a single users in my database?
Now:
__________________________________
Users Numbers
Tom 2
__________________________________
What I want:
__________________________________
Users Numbers
Tom 2,12
__________________________________
As #jarlh stated, you shouldn't design your database to contain a set of data.
A relational database column must contain only a data of a single kind and not a set of data or different kinds of data through your rows.
To fix your error you can create another table named Numbers and associate it to your Users table with a 1:N (one to many) relation like shown here:
_Users___ _Numbers________________
|ID |name | |NumberID |UserID |value |
|1 |Tom | |1 |3 |243 |
|2 |Jess | |2 |1 |12 |
|3 |Luis | |3 |2 |87 |
In the Users table you have an ID and the name, then in your Numbers table you associate a number (for each new number you must insert a new row) to its owner with the foreign key UserID

Crystal Report SUM function with CASE

I have the following column values in my crystal report:
|Code |Unit |
|A |2 |
|B |3 |
|C |2 |
|D |3 |
|E |1 |
|F |1 |
|G |4 |
|H |(3) |
I want to summarize the Unit except the Units which has Code H,J,K, and L.
The Codes: H,J,K, and L contains units which has parenthesis.
Is there a way to do this?
If you want to exclude any row or value from summary, it can be done by writing your case inside Use a formula field under Evaluate in Running Total Field
Refer the following Image...
The rows or fields which doesn't satisfy the condition will be skipped from Evaluation of summary.
Try this and get back with results !!
If you want to omit only units with ‘(‘ in it just convert this filed to number
Use
Val ({Unit})
this will return 0 for non-numeric text and number for numeric create sum of these you will get what you want
If you want not to use any special then create formula field like this
if {fa_rep_vr_Main.CustomTitle} not in('A','B','C') then
0
else
val({Unit})
use sum of this
If you want sum of NewPriceAD then use it in mentained field

complex'ish SQL joins across multiple tables with multiple conditions across all tables

Given the following tables:
labels tags_labels
|id |name | |url |labelid |
|-----|-------| |/a/b |1 |
|1 |punk | |/a/c |2 |
|2 |ska | |/a/b |3 |
|3 |stuff | |/a/z |4 |
artists tags
|id |name | |url |artistid |albumid |
|----|--------| |------|-----------|---------|
|1 |Foobar | |/a/b |1 |2637 |
|2 |Barfoo | |/a/z |2 |23 |
|3 |Spongebob| |/a/c |1 |32 |
I would like to get a list of urls that match a couple of conditions (which can be entered by the user into the script that uses these statements).
For example, the user might want to list all urls that have the labels "(1 OR 2) AND 3", but only if they are by the artists "Spongebob OR Whatever".
Is it possible to do this within a single statement using inner/harry potter/cross/self JOINs?
Or would I have to spread the query across multiple statements and buffer the results inside my script?
Edit:
And if it is possible, what would the statement look like? :p
Yes, you can do this in one query. And maybe an efficient way would be to dynamically generate the SQL statement, based on the conditions the user entered.
This query would allow you to filter by label name or artist name.
Building the sql dynamically to concatenate the user parameters or
passing the desired parameters into a stored procedure would obviously change
the where clauses but that really depends on how dynamic your 'script' must be...
SELECT tl.url
FROM labels l INNER JOIN tags_labels tl ON l.id = tl.labelid
WHERE l.name IN ('ska','stuff')
UNION (
SELECT t.url
FROM artists a INNER JOIN tags t ON a.id = t.artistid
WHERE a.name LIKE '%foo%'
)
Good Luck!