SQL QUERY: Want to query how many viewers per movie_type; (where comedy = comedy & dramatic comedy) [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 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 ', '')));

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;

what should i do to make this SQL query work? [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 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.

How to find total percentage of students who are absent or tardy in SQL? [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 have a grade_level and total_num_students columns but not sure how to create a column to get the percentage of students who are absent/tardy. (Each student id is unique and in both tables)
Here are the two tables:
And here is my current SQL query that I have so far:
SELECT
grade_level,
COUNT(student_attendance_log.attendance_status) AS 'total_num_students'
FROM student_info
INNER JOIN student_attendance_log
ON student_info.student_id = student_attendance_log.student_id
WHERE student_attendance_log.attendance_status = 'Tardy'
OR student_attendance_log.attendance_status = 'Absent'
GROUP BY grade_level
ORDER BY grade_level;
which produces:
I recommend avg() for this calculation:
SELECT grade_level, COUNT(*) as total_num_students,
AVG(CASE WHEN sal.attendance_status = 'Tardy' THEN 1.0 ELSE 0 END) as tardy_ratio,
AVG(CASE WHEN sal.attendance_status = 'Absent' THEN 1.0 ELSE 0 END) as absent_ratio
FROM student_info si INNER JOIN
student_attendance_log sal
ON si.student_id = sal.student_id
GROUP BY grade_level
ORDER BY grade_level;
Note some changes:
Table aliases makes the query easier to write and to read.
Only use single quotes for string and date constants. Don't use them for column names.
The filtering has moved from the WHERE clause to the AVG().

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';