what should i do to make this SQL query work? [closed] - mysql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 months ago.
Improve this question
SELECT s.Name, (
COUNT(*) FROM (SELECT 1 FROM pruefen p WHERE p.MatrNr = s.MatrNr AND p.Note < 5) /
COUNT(*) FROM (SELECT 1 FROM pruefen p WHERE p.MatrNr = s.MatrNr)
) as Anteil FROM Studenten s
ORDER BY Anteil DESC, s.Name ASC
LIMIT 10;

I'm quite sure this query will work; meaning that it won't return an error but I'm not certain if it will return correct result as what you intended:
SELECT s.Name,
SUM(p.Note=5)/COUNT(*) as Anteil
FROM Studenten s
JOIN pruefen p ON p.MatrNr = s.MatrNr
GROUP BY s.Name
ORDER BY Anteil DESC, s.Name ASC;
If this doesn't return the result you're looking for, update your question with data samples and expected output.

Related

#1242 - Subquery returns more than 1 row using join and subquery [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 19 hours ago.
Improve this question
I'm using a subquery and join together and get this error:
#1242 - Subquery returns more than 1 row
select COUNT( DISTINCT refnumber) AS TxnNumber,
invoiceline.customer_listid,
customer_fullname,
invoiceline.txndate,
customertype_fullname,
phone,
ROUND(SUM(line_amount),2) AS subtotal,
(
SELECT totalamount
from receivepaymentline
WHERE DATE(txndate) = '2023/02/18' AND customerreflistid = customer_listid
) AS appliedamount,
ROUND(customer.balance,2) AS balance
from invoiceline
join customer on invoiceline.customer_listid = customer.listid
where DATE(invoiceline.txndate) = '2023/02/18' AND Balance > 0
group by customer_listid;

I also want to see the count of zero [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I also want to show the count of zero in this sql.
SELECT diaries.id, emotion_id, image_url, color, summary, public, is_completed, diaries.created_at, diaries.is_deleted, likes.is_deleted, users.nickname, likes.diary_id, COUNT(likes.user_id) AS cnt FROM diaries
INNER JOIN emotions ON diaries.emotion_id = emotions.id
INNER JOIN users ON diaries.user_id = users.id
LEFT OUTER JOIN likes ON diaries.id = likes.diary_id
WHERE diaries.is_deleted = 0 AND public = 1 AND is_completed = 1 AND likes.is_deleted = 0
GROUP BY diary_id
ORDER BY cnt DESC
I also want to see the count of zero.....
The where clause cannot include any fields from the table that you use LEFT JOIN on. This causes the LEFT JOIN to convert into an INNER JOIN.
You need to move this clause into the LEFT JOIN statement:
AND likes.is_deleted = 0
This will then return a COUNT(likes = 0)

SQL QUERY: Want to query how many viewers per movie_type; (where comedy = comedy & dramatic comedy) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
made up table
Basically I want to group by the movie_type, but I want comedy & dramatic comedy to just be counted as one category = Comedy.
You can use case expression then do aggregation in the outer query
select
movie_type,
count(*) as total_viewer
from
(
select
case
when movie_type = 'dramatic comedy' or movie_type = 'comedy' then 'comedy'
else movie_type
end as movie_type,
viewer_id
from yourTable
) subq
group by
movie_type
You can replace dramatic with null and fetch the results. Below is the query,
select ltrim(rtrim(replace(movie_type, 'dramatic ', ''))) as movie_type , count(1) from
table
group by ltrim(rtrim(replace(movie_type, 'dramatic ', '')));

SQL Count joining table with WHERE clause [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I need some help regarding counting the data from.
Here is my query so far:
SELECT COUNT(*) FROM (SELECT COUNT(biodata.Nomor_Induk) as jumData
FROM biodata
INNER JOIN statuspegawai ON biodata.Nomor_Induk = statuspegawai.Nomor_Induk
WHERE statuspegawai.bagian='IT'
GROUP BY biodata.Nomor_Induk)
I have got two tables: biodata and statuspegawai.
Both of tables have the Nomor_Induk column.
Use the code here :
-missing table alias !
select count(*) FROM (
SELECT COUNT(biodata.Nomor_Induk) as jumData
from biodata INNER JOIN
statuspegawai
ON biodata.Nomor_Induk = statuspegawai.Nomor_Induk
where statuspegawai.bagian='IT'
Group By biodata.Nomor_Induk
) as Alias1;
Your query needs a subquery table alias:
select count(*)
FROM (SELECT COUNT(biodata.Nomor_Induk) as jumData
from biodata INNER JOIN
statuspegawai
ON biodata.Nomor_Induk = statuspegawai.Nomor_Induk
where statuspegawai.bagian = 'IT'
Group By biodata.Nomor_Induk
) t;
You can also express this without a subquery:
select COUNT(distinct biodata.Nomor_Induk) as jumData
from biodata INNER JOIN
statuspegawai
ON biodata.Nomor_Induk = statuspegawai.Nomor_Induk
where statuspegawai.bagian = 'IT';

SELECT in DATEDIFF not working [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
SELECT DATEDIFF(day,(SELECT creation_date
FROM time_info
WHERE is_on = 'yes'
LIMIT 0,1),
(SELECT creation_date
FROM time_info
WHERE is_on = 'no'
LIMIT 0,1)
FROM time_info;
I want to calculate date difference, but things aren't working this way. Is there another way to do this?
Try this
set #date1:=(SELECT creation_date
FROM time_info
WHERE is_on = 'yes'
LIMIT 0,1);
set #date2:= (SELECT creation_date
FROM time_info
WHERE is_on = 'no'
LIMIT 0,1);
select datediff(#date1,#date2);
You need to provide more information to understand what you are trying to do
Your braces '(',')' are not balanced in the SELECT statement.
try this
SELECT DATEDIFF(t1.creation_date,t2.creation_date )
FROM time_info t1
INNER JOIN time_info t2 ON t1.your_id_column = t2.your_id_column
WHERE t1.is_on = 'yes' AND t2.is_on = 'no';