oracle sql query question(grouping by 2 columns) - sql

I have a table called testgroup in my database, which is like following:
I J
---------------------- ----------------------
1 a
1 a
2 a
1 b
1 c
2 b
3 d
2 b
2 b
3 d
Now, I want the result as below:
I J COUNT(J) in I
---------------------- ---------------------- ----------------------
1 a 2
2 a 1
1 b 1
1 c 1
2 b 3
3 d 2
...where count(j) in I is the number of each J related to the I.
For example: with I = 1, there are 2 a in column J, so the third column would be equal 2.

select I, J, count(*) as JinI
FROM atable
GROUP BY I, J

In fact the question is about counting I and J pairs:
select I, J, count(*) from tblName group by I, J

Related

Qlik Sense Sum of One field based on unique value of other fields

Sample Data:
P Q R
1 A 3
1 A 3
1 A 2
1 B 5
1 C 7
2 A 3
2 A 3
Expected Output:
P Q R
1 A 5
1 B 5
1 C 7
2 A 3
i Have tried this Sum (Distinct R) but it is not working. i need to group by P and Q column and add Unique Value of R for that. Please support
In chart, you have to add P and Q fields as dimensions. Then your expression should work just fine.
In script your code should look like this:
Load P, Q, Sum( Distinct R ) as sum_of_R
FROM sample_data
Group By P, Q;

Sequence in SELECT statement

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

SQL Order By Custom Sequence

I have a data in this order
Id Value
-- ----
1 a
1 b
1 c
2 a
2 c
3 b
4 c
4 b
4 a
I want to sort data in this order
Id Value
-- ----
1 a
2 a
3 b
4 c
1 b
2 c
4 b
1 c
4 a
You seem to want to intersperse the numbers. For this purpose, you can use row_number():
order by row_number() over (partition by id order by value),
id

Convert Range Records

I have the following scenario and would need some help with the SQL
ID Flag ValidFrom Valid To
1 A 2017001 2017005 ( Valid Till end of 2017004)
1 B 2007005 2017008
1 C 2017008 2017012
2 D 2017001 2017006
2 E 2007006 2017008
2 F 2017008 2017012
I need result somthing like this
1 2017001 A
1 2007002 A
1 2017003 A
1 2017004 A
1 2007005 B
1 2017006 B
1 2017007 B
1 2007008 C
1 2017009 C
1 2017010 C
1 2017011 C
2 2017001 D
2 2007002 D
2 2017003 D
2 2017004 D
2 2007005 D
2 2017006 E
2 2017007 E
2 2007008 F
2 2017009 F
2 2017010 F
2 2017011 F
Actually i need the only Min 3 months so i actually need something like this
1 2017001 A
1 2007002 A
1 2017003 A
2 2017001 D
2 2007002 D
2 2017003 D
Thanks in advance
Could you please check following SELECT statement
;with cte as (
select 0 as n union all
select 1 as n union all
select 2 as n
)
select
ID,
Flag,
ValidFrom,
case when r.ValidTo - r.ValidFrom > 2 then r.ValidFrom+2 else r.ValidTo end as ValidTo,
ValidFrom + n as result
from ranges r, cte
where ValidFrom + n <= ValidTo

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