SQL group by within a group - sql

How can we use group by within separate categories? That is treat each category as it's own group and then group by something else within the category.
For instance in this Image, I would like to group by quarter only within a particular ISSN.
Expected result would be

It is possible to group by more than one column:
select issn, quarter, count(*)
from t
group by issn, quarter

Simply add columns -as prioritized- to group by, example:
SELECT * FROM MyTable GROUP BY ISSN, Quarter

Related

How to group an already grouped data-set in oracle sql

I want to first group data as per "Roll" and then further group it as per "Name" with different "Marks".
I have grouped data using Group by and having but I am not sure how to further group it.
Group by two columns :
SELECT Roll,Name FROM table GROUP BY Roll,Name;
If you want to see marks also you have aggegrate(sum) it like :
SELECT Roll,Name,sum(Marks)as 'Total Marks' FROM table GROUP BY Roll,Name;
If you want to see marks also you have aggegrate(average) it like :
SELECT Roll,Name,avg(Marks)as 'Average Marks' FROM table GROUP BY Roll,Name;
Refer this image
If I understand correctly, you want roll/name pairs that have multiple distinct values. You can do this using window functions:
select distinct roll, name, marks
from (select t.*, count(distinct marks) over (partition by roll, name) as cnt
from t
) t
where cnt > 1;

SQL PIVOT group by 2 columns

I have a attendance table as below i want group them by time and section,status is null mean that the employee is absent :
Any idea how to generate output like below?
my current code :
SELECT TIME,COUNT(SECTION) AS SECTION,COUNT(STATUS) AS COUNT
FROM attendance_record
GROUP BY TIME,SECTION
ORDER BY TIME
If I understand your question, just use conditional aggregation:
SELECT TIME, SECTION, COUNT(*) as TOTAL,
COUNT(STATUS) AS IN, ( COUNT(*) - COUNT(STATUS) ) as ABSENT
FROM attendance_record
GROUP BY TIME, SECTION
ORDER BY TIME

Selecting and modifying a field in SQL

Let's say I have a table with name and birthday fields. I can use this SQL to return a table of items sharing the same birthday (e.g. 4 people have the birthday 4/8/1995):
SELECT DISTINCT "Birthday", COUNT("Birthday") as "FieldCount"
FROM "test_main" Group BY "Birthday" Order By "FieldCount" DESC
But how can I modify the value that I select to ignore the year, e.g. get a count of birthdays by month, for example, Jan: 42 names, Feb: 28 names, etc
Thanks
SELECT month(Birthday), COUNT(Birthday)
FROM test_main
Group BY month(Birthday)
Order By COUNT(Birthday) DESC
One way is to use built in functions to extract the year and month:
SELECT month(birthday), count(*) as FieldCount
FROM test_main
Group BY month(birthday)
Order By FieldCount DESC;
Notes:
There is no need for the distinct with a group by.
You do not need to surround everything with double quotes.
COUNT(*) should be find for what you want.

Summary Query to Sum Operations not working

SELECT price,total_price,SUM(mount) As TotalAmount
FROM sales
group by [date]
is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
You cannot select fields in the SELECT part which is not a part of an Aggregated function or specified in the the GROUP BY clause when using GROUP BY.
You can select "mount" because it's aggregated (SUM), but not price or total_price because they're not aggregated nor part of your group by date
You will need to either change your group by functionality, or the select part.
You have to group by the remaining tables that are not a function, therefore:
SELECT price,total_price,SUM(mount) As TotalAmount
FROM sales
Group by price,total_price
Order by date

Combine two SQL queries, one involving GROUP BY?

I would like to combine these two SQL queries into one.
SELECT COUNT() as total_grants, SUM("CURRENT_AWARD") as total_spent FROM t;
SELECT YEAR, COUNT(), SUM('CURRENT_AWARD') FROM t GROUP BY YEAR AS by_year;
The first query shows the total number of grants, and the total spent. The second is the same, but by year.
Is this possible? I've already combined two queries into one in the first query, but I can't figure out how to use an AS clause properly in the second query.
Thanks for any help.
How about using CROSS JOIN
SELECT YEAR,
COUNT(*),
SUM('CURRENT_AWARD') ,
t2.total_grants,
t2.total_spent
FROM t
CROSS JOIN
(
SELECT COUNT(*) as total_grants,
SUM("CURRENT_AWARD") as total_spent
FROM t
) t2
GROUP BY YEAR;
Maybe something like this?
SELECT BY_YEAR, COUNT(), SUM('CURRENT_AWARD') FROM t GROUP BY rollup(by_year);
(I think rollup can be rdbms/version dependent...)
Try this.
SELECT YEAR, COUNT(*) as total_grants, SUM(CURRENT_AWARD) as total_spent
FROM t
GROUP BY YEAR;
COUNT(*) will count all rows in table t, including ones with NULL. If you want to ignore rows with NULL, count a specific column. For example, COUNT(CURRENT_AWARD).
You don't need the quotes around CURRENT_AWARD since this identifier contains only letters and underscores.