SQL Theory - Group by all attributes of relation - sql

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

Related

SQL Query to get multiple resultant on single column

I have a table that looks something like this:
id name status
2 a 1
2 a 2
2 a 3
2 a 2
2 a 1
3 b 2
3 b 1
3 b 2
3 b 1
and the resultant i want is:
id name total count count(status3) count(status2) count(status1)
2 a 5 1 2 2
3 b 4 0 2 2
please help me get this result somehow, i can just get id, name or one of them at a time, don't know how to put a clause to get this table at once.
Here's a simple solution using group by and case when.
select id
,count(*) as 'total count'
,count(case status when 3 then 1 end) as 'count(status1)'
,count(case status when 2 then 1 end) as 'count(status3)'
,count(case status when 1 then 1 end) as 'count(status2)'
from t
group by id
id
total count
count(status3)
count(status2)
count(status1)
2
5
1
2
2
3
4
0
2
2
Fiddle
Here's a way to solve it using pivot.
select *
from (select status,id, count(*) over (partition by id) as "total count" from t) tmp
pivot (count(status) for status in ([1],[2],[3])) pvt
d
total count
1
2
3
3
4
2
2
0
2
5
2
2
1
Fiddle

finding records which doesnt have a state

AID
BID
STATE
1
1
1
1
2
3
1
3
3
2
1
0
2
2
3
2
3
3
3
1
3
3
2
0
3
3
3
I am trying to find AID records which doesnt have any 0 state in this example AID = 1 (will be multiple records)
If you would like to find all the AID with no 0 state records you may use
SELECT
AID
FROM
mytable
GROUP BY
AID
HAVING
COUNT(
CASE WHEN STATE=0 THEN 1 END
)=0;
AID
1
or if you would like to find all AID records where the state is not 0 for any AID record you may use the following.
SELECT
*
FROM
mytable
WHERE AID NOT IN (
SELECT AID FROM mytable WHERE STATE=0
)
AID
BID
STATE
1
1
1
1
2
3
1
3
3
Let me know if this works for you.

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

How to Subtotal Value with MAX

1.I have data as follows (just a subset - there are 20K records)
sku,id
1 1
1 2
1 2
1 2
1 3
1 4
1 1
1 2
1 3
1 4
1 4
1 4
1 5
1 6
1 6
2 1
2 1
2 2
2 3
2 3
2 3
2 4
2 4
2 5
2 5
2 6
2 7
2 1
2 2
2 3
The above values translate to
1 = 4 records
1 = 6 records
2 = 7 records
2 = 3 records
The MAX would just give me 6 for one and 7 for 2
The actual total is 1 = 10 and 2 = 10
How do I sum up to get the correct values?
You can use order by and some way of limiting rows. In standard SQL this would be:
select t.*
from t
order by id desc
fetch first 2 rows only;
However, some databases might use limit or select top or some other method.
No handling of ties here. Thousands of other questions handle this topic.
select sku, id
from (
select *, row_number() over (order by id desc) rn
from T
) t
where rn <= 2
order by rn desc;

SQL code to sum duplicate rows and also retain the duplicate rows

How to write a sql query
A 1
B 1
C 1
A 1
A 1
B 1
B 1
C 1
B 1
to make it look like
A 3
B 4
C 2
A 3
A 3
B 4
B 4
C 2
B 4
SELECT field1, SUM(field2) OVER (PARTITION BY field1) as total
FROM table1
More info about window/analytic functions in Oracle : http://docs.oracle.com/cd/E11882_01/server.112/e41084/functions004.htm