Sequence in SELECT statement - sql

I need to create SELECT statement with sequence in Oracle. When col_flag is 1 then sequence increase with mod(col_seq, max_seq) and when col_flag is 0 then sequence don't increment.
Example:
col_group col_flag col_seq
--------- -------- --------
A 1 1
A 1 2
A 1 3
A 0 3
A 0 3
B 1 4
B 1 1
B 1 2
B 1 3
B 0 3
B 1 4
B 1 1
C 1 2
C 0 2
...

It guess that a window sum and arithmetics can do what you want - but you need a column that defines the ordering of the rows, I assumed id.
select col_flag,
mod(sum(col_flag) over(order by id), 4) + 1 col_seq
from mybltae

Related

Hive - Group by with respect to following values

I have a table with rows:
id
a
b
0
1
1
1
1
2
2
2
1
3
1
1
I need to get sum of field "b" values grouped by "a" with respect to changes in "a".
For my example i want to get:
a
b
1
3
2
1
1
1

SQL Theory - Group by all attributes of relation

Theoretical question. Say we have relation R(A,B,C).
For fun, let's say that this is the Relation Table
A B C
1 2 3
1 2 2
1 2 3
1 1 1
And we execute the following query:
SELECT *
FROM R
GROUP BY A,B,C;
What would the result be?
Assuming it is SQL Server 2008 (or similar compliance).
Input:
A B C
1 2 3
1 2 2
1 2 3
1 1 1
Query:
SELECT *
FROM R
GROUP BY A,B,C;
Output
A B C
1 1 1
1 2 2
1 2 3
SQL Fiddle: http://sqlfiddle.com/#!3/d2bcd/1/0

Count occurrences of field values as they are displayed in order

thanks in advance for the help and sorry for how the "table" looks. Here's my question...
Let's say I have a subquery with this table (imagine the bold as column headers) as its output -
id 1 1 2 3 3 3 3 4 5 6 6 6
action o c o c c o c o o c c c
I would like my new query to output -
id 1 1 2 3 3 3 3 4 5 6 6 6
action o c o c c o c o o c c c
ct 1 2 1 1 2 3 4 1 1 1 2 3
#c 0 1 0 1 2 2 3 0 0 1 2 3
#o 1 1 1 0 0 1 1 1 1 0 0 0
where ct stands for count. Basically, I want to count (for each id) the occurrences of consecutive id and action as they happen. Let me know if this makes sense, and if not, how I can clarify my question.
Note: I realize the lag/lead functions may be helpful in this situation, along with the row_number() function. Looking for as many creative solutions as possible!
You are looking for the row_number() analytic function:
select id, action, row_number() over (partition by id order by id) as ct
from table t;
For #c and #o, you want cumulative sum:
select id, action, row_number() over (partition by id order by id) as ct,
sum(case when action = 'c' then 1 else 0 end) over
(partition by id order by <some column here>) as "#c",
sum(case when action = 'c' then 1 else 0 end) over
(partition by id order by <some column here>) as "#o"
from table t;
The one caveat is that you need a way to specify the order of the rows -- an id or date time stamp or something. SQL result sets and tables are inherently unordered, so there is no idea that one row comes before or after another.
SQL> select id, action,
2 row_number() over(partition by id order by rowid) ct,
3 sum(decode(action,'c',1,0)) over(partition by id order by rowid) c#,
4 sum(decode(action,'o',1,0)) over(partition by id order by rowid) o#
5 from t1
6 /
ID A CT C# O#
---------- - ---------- ---------- ----------
1 o 1 0 1
1 c 2 1 1
2 o 1 0 1
3 c 1 1 0
3 c 2 2 0
3 o 3 2 1
3 c 4 3 1
4 o 1 0 1
5 o 1 0 1
6 c 1 1 0
6 c 2 2 0
6 c 3 3 0
P.S. Sorry Gordon, didn't see your post.

Using Row_Number to deal with non unique data

I have 4 columns a,b,c,d.
Some of my rows have the same values for all columns, is there any option to use row_number to insert same row number for those rows and continue counting if at least one of the values is different from values in the previous row
Example:
a b c d
1 1 1 1
1 1 1 1
1 1 1 2
1 1 1 2
1 1 1 3
1 1 1 3
1 1 2 4
I need it to look like: r=row_number
r a b c d
1 1 1 1 1
1 1 1 1 1
2 1 1 1 2
2 1 1 1 2
3 1 1 1 3
3 1 1 1 3
4 1 1 2 4
P.S. How to write here something like a table?
declare #t table(a int, b int, c int, d int)
insert #t values(1,1,1,1),(1,1,1,1),(1,1,1,2),
(1,1,1,2),(1,1,1,3),(1,1,1,3),(1,1,1,4)
select dense_rank() over(order by a,b,c,d) r, a,b,c,d from #t
Result:
r a b c d
1 1 1 1 1
1 1 1 1 1
2 1 1 1 2
2 1 1 1 2
3 1 1 1 3
3 1 1 1 3
4 1 1 1 4

MySQL SUM Query

I've got two tables.
I'm trying to calculating the SUM quantity of tbl1
tbl1.xid is the primary, while tbl2.xid is the foreign
tbl1
xid pub quantity
1 1 10
2 1 2
3 0 1
4 1 5
tbl2
id ttype fno xid qnty
1 A 0 1 0
2 A 1 1 3
3 B 1 1 4
4 A 1 2 1
5 A 1 3 2
6 A 1 4 3
7 A 1 4 1
8 A 0 1 0
We are calculating the sum of tbl1's quantity
1) Whos tbl1.pub is 1
Thus tbl1.xid 3 is removed form the list, for it's pub is 0
Results
tbl1
xid pub quantity
1 1 10
2 1 2
4 1 5
2) AND Who's tbl1 has at least one tbl2.xid who's tbl2.ttype is 'A' and who's tbl2.fno is '0'
Thus tbl1.xid 2 & 4 are removed form the list, because none of them have at least one tbl2.xid who's fno is '0' and who's tbl2.ttype is 'A'
Results
parent_tbl1
xid pub quantity
1 1 10
The final results should be 10
SELECT SUM(quantity) AS Total
FROM tbl1
WHERE pub=1
AND EXISTS
(SELECT *
FROM tbl2
WHERE tbl2.ttype = 'A'
AND tbl2.fno = 0
AND tbl1.xid = tbl2.xid
)