SQL Query (Simple) - group-by

I would like to know how could a return the number of songs registred each year based on this table:
Musics={MusicID, SingerID, Recorder, YearOfRecord}
MusicID as PK
Thanks for the attention

SELECT Count(MusicID)
FROM Musics
GROUP BY YearOfRecord
That query should work

Well, the correct query would be:
SELECT YearOfRecord, Count(MusicID) as NumberOfSongs
FROM Musics
GROUP BY YearOfRecord
ORDER BY YearOfRecord

SELECT
COUNT(*) AS NumberOfSongs,
YearOfRecord
FROM
Musics
GROUP BY
YearOfRecord
ORDER BY
YearOfRecord

select count(*), YearOfRecord
from Musics
group by YearOfRecord

Select YearOfRecord, COUNT(MusicID) AS NumberOFSongs
FROM Musics GROUP BY YearOfRecord

Related

In PostgresSQL how to write a query returning multiple rows and columns

My original query was like this:
SELECT *,
(SELECT COUNT(*), user_id FROM user_sessions WHERE company_id=companies.id GROUP BY user_id) as user_sessions
FROM companies
Which comes back with this error:
error: more than one row returned by a subquery used as an expression
I found a way past that error with this:
SELECT *,
ARRAY (SELECT COUNT(*), user_id FROM user_sessions WHERE company_id=companies.id GROUP BY user_id) as user_sessions
FROM companies
But then it has this error:
error: subquery must return only one column
If I remove either COUNT(*) or user_id from the returned columns it works, however I need both sets of data. How do I return more than one column in a sub-query like this?
I guess a join should do the trick:
select * from
companies
join
( select count(*), company_id, user_id
from user_sessions
group by (company_id, user_id)
) as user_sessions
on companies.id = company_id
For anyone who runs into this in the future, tested the best way to dot this is what #Matt mentioned in the comments above:
Replace COUNT(*), user_id with ARRAY[COUNT(*), user_id]
Works perfectly

Find rows which have different attribute value in ONE day for same product. (Postgresql)

can someone help me to write a query?
I have for example columns:
Date
product_key
category_code
In one day I expect to have same category_code for one product, but I want to check this with SQL.
Thank you.
If you want to find the day, the product_key and the category_code that doubles, You should use query like this:
SELECT
date,
product_key,
category_code,
count(1)
FROM your_table
GROUP BY date, product_key, category_code
HAVING count(1) > 1;
You can group your results by date and product, and use count and distinct to find if there is more than one category code for a product. You can then filter rows having more than 1 distinct category in the group.
SELECT
Date, product_key, count(distinct category_code) AS categories
FROM
my_table
GROUP BY
Date, product_key
HAVING
count(distinct category_code) > 1

Django-orm, select last date on distinct user

How to implement such query
SELECT DISTINCT ON (user_id) user_id, date FROM mayak_position WHERE user_id IN ('1111', '2222') ORDER BY user_id, date DESC;
with minimal raw sql, using django-orm
I tried many different queries, without success
Position.objects.annotate(s=Count('user_id', distinct=True)).order_by('s')
Position.objects.filter(user_id__in=child_ids).order_by('user_id', '-date').distinct('user_id')
source
result = Position.objects.filter(user_id__in=['1111', '2222']).values('user_id','date').distinct()

oracle not a group by expression

I have a table with 3 columns.
ORDER CATEGORY
NAV_PER_SHARE
Number_OUTSTANDING_SHARES
Now:
SELECT ORDER_CATEGORY, SUM(OUTSTANDING_SHARES) GROUP BY ORDER_CATEGORY , it runs fine
But:
SELECT ORDER_CATEGORY, NAV_PER_SHARE * SUM(OUTSTANDING_SHARES) GROUP BY ORDER_CATEGORY , it says : Not a group by expression .
What am I missing?
You caNnot group because NAV_PER_SHARE is given per result. Did you mean
SUM(NAV_PER_SHARE*OUTSTANDING_SHARES)
?
From what it looks like, the solution would be to use a subquery.
SELECT ORDER_CATEGORY, NAV_PER_SHARE * SUM_OF_OS
FROM (SELECT ORDER_CATEGORY, SUM(OUTSTANDING_SHARES) SUM_OF_OS
GROUP BY ORDER_CATEGORY);
Although, I'm not sure how your one query works without a FROM keyword.

SQL top + count() confusion

I've got the following table:
patients
id
name
diagnosis_id
What I need to do is get all the patients with N most popular diagnosis.
And I'm getting nothing using this query:
SELECT name FROM patients
WHERE diagnosis_id IN
(SELECT TOP(5) COUNT(diagnosis_id) FROM patients
GROUP BY diagnosis_id
ORDER BY diagnosis_id)
How to fix it?
SELECT name FROM patients
WHERE diagnosis_id IN
(
SELECT TOP(5) diagnosis_id FROM patients
GROUP BY diagnosis_id
ORDER BY COUNT(diagnosis_id) desc
)
A couple things wrong with this:
First, I'd recommend using a common table expression for the "top 5" lookup rather than a subquery - to me, it makes it a bit clearer, and though it doesn't matter here, it would likely perform better in a real work situation.
The main issue though is that you're ordering the top 5 lookup by the diagnosis id rather than the count. You'll need to do ORDER BY COUNT(diagnosis_id) instead.
select p.name from patients p
inner join (
select top 5 diagnosis_id, count(*) as diagnosis_count
from patients
group by diagnosis_id
order by diagnosis_count) t on t.diagnosis_id = p.diagnosis_id
try this:
SELECT name FROM patients
WHERE diagnosis_id IN
(SELECT TOP(5) diagnosis_id FROM patients
GROUP BY diagnosis_id
ORDER BY COUNT(diagnosis_id))