#1242 - Subquery returns more than 1 row using join and subquery [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 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;

Related

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().

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

Select distinct column 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 9 years ago.
Improve this question
So, I am making a query using mutiple tables and i want to get a distinct product name("designacao") however I've already tried adding DISTINCT and RIGHT JOINs but it didn't work.
Please check this SQLFiddle with the Query and the data
I want the same query as the provided but with DISTINCT "designacao"
Try this way:
select produtos.designacao, produtos.marca,produtos.categoria, lojas.superficie, lojas.localizacao, produtos_lojas.preco
from produtos_lojas
inner join lojas on lojas.id = produtos_lojas.id_loja
inner join produtos on produtos.id = produtos_lojas.id_produto
where produtos_lojas.preco=
(
select min(pl.preco)
from produtos_lojas pl
inner join lojas l on l.id = pl.id_loja
inner join produtos p on p.id = pl.id_produto
where produtos.designacao = p.designacao
)
SQL Fiddle DEMO
DISTINCT keyword will get eliminate any duplicate row. Since your rows are different due to the other columns you are selecting, you will only be able to pick up each designacao once if you include only the designacao column in your query.