Getting error "Operand should contain 1 column(s)" on this query - mysql

So I have the code below:
SELECT student_name FROM student S WHERE S.student_id IN
(SELECT t1.student_id, SUM(IF(t2.test_date IS NULL, 0, 1)) AS increase_score
FROM
test t1
LEFT JOIN test t2
ON t1.student_id = t2.student_id
AND t1.test_date < t2.test_date
AND t1.Score <= t2.Score
GROUP BY t1.student_id
HAVING
increase_score = 0
AND count(*) > 1)
I am getting an error "Operand should contain 1 column(s)". This only arises after adding the outer SELECT statement. I have confirmed the inner query is working as intended otherwise. I've looked at some other examples involving the same error, but I have not been able to determine what to do to fix it in this case.

The error is quite clear : you can't have more than one field in your sub query, you're macthing a field against a single column, obviously.
So just move the SUM in the HAVING clause :
SELECT student_name
FROM student S
WHERE S.student_id IN
(SELECT t1.student_id
FROM test t1
LEFT JOIN test t2
ON t1.Name = t2.Name
AND t1.Date < t2.Date
AND t1.Score <= t2.Score
GROUP BY t1.Name
HAVING
SUM(IF(t2.test_date IS NULL, 0, 1)) = 0
AND count(*) > 1)
http://dev.mysql.com/doc/refman/5.7/en/any-in-some-subqueries.html

A query used as an argument to the in operator must return a single column. One way to rework this query is to remove the sum in the select list, and keep it only in the having clause:
SELECT student_name
FROM student
WHERE student_id IN (SELECT t1.student_id
FROM test t1
LEFT JOIN test t2 ON t1.Name = t2.Name AND
t1.Date < t2.Date AND
t1.Score <= t2.Score
GROUP BY t1.Name
HAVING SUM(IF(t2.test_date IS NULL, 0, 1)) = 0 AND
COUNT(*) > 1
)

Related

Getting invalid use of group function in update query

Can Someone please help me where is the issue
am getting 'invalid use of group function' in below query
UPDATE t1
JOIN t2 ON t1.id=t2.id
SET t1.total_amount = SUM(IF((t2.`due` <= 0), t2.`amount`, 0))
WHERE t2.flag=1 AND t2.id=003;
You want to aggregate in a subquery, then join and set in the outer query:
update t1
inner join (
select id, sum(case when due < 0 then t2.amount else 0 end) total_amount
from t2
where id = 3 and flag = 1
group by id
) t2 on t1.id = t2.id
set t1.total_amount = t2.total_amount

SQL make COUNT() return total rows in query

I have a query in MySQL as follows (table names are changed):
SELECT
t1.name,
t2.type,
if(data1 = "6", sum(coalesce(data2, 0) * coalesce(data3, 1)) as more_data
FROM table_main tm
JOIN table1 t1 ON tm.t1_id = t1.id
JOIN table2 t2 ON tm.t2_id = t2.id
WHERE day = '2018-01-01'
GROUP BY t1.name, t2.type
ORDER BY more_data DESC
This query returns around 400 results, but for some reason every time I try to count() anything it keeps returning random data and not 1 record giving the total amount of records given by this query, now I've tried to get rid of most of the query and only leaving the 'if'-statement as a select also gives the required results, but then still it won't let me use that value in the count() method, is there any way I can count the records from this query without getting random data>
Your conditional aggregation is turned inside out... Use a case expression inside sum() instead.
SELECT
t1.name,
t2.type,
sum(case when data1 = "6" then coalesce(data2, 0) * coalesce(data3, 1) else 0 end) as more_data
FROM table_main tm
JOIN table1 t1 ON tm.t1_id = t1.id
JOIN table2 t2 ON tm.t2_id = t2.id
WHERE day = '2018-01-01'
GROUP BY t1.name, t2.type
ORDER BY more_data DESC

Distinct SUM with Mysql Count

I have a query:
SELECT
t1.name as Name
count ( distinct t2.key ) as Total
SUM ( IF( t2.time = '12:00' , 1 , 0) ) as QttMidDay
FROM t1
LEFT JOIN t2 on t1.key = t2.key
GROUP BY t1.key
The question is, how i do the "Conditional Count" on the 2ยบ parameter SUM for QttMidDay ?
I am guessing that you can solve your problem by aggregating before the join. My best guess is:
SELECT t1.name as Name, t2.Total, t2.QttMidDay
FROM t1 LEFT JOIN
(SELECT COUNT(DISTINCT t2.key) as Total,
SUM(t2.time = '12:00') as QttMidDay
FROM t2
GROUP BY t2.key
) t2
ON t1.key = t2.key;
I am not sure if the COUNT(DISTINCT) is necessary in the subquery.

How to count and get result of two rows in Mysql Select Query?

This is my table structure.
sometime table1 data my repeat (Ex : Actually Id 1 should have only 4 rows but sometime it is 8 due to duplication) so avoid duplication I use GROUP BY command in select query
Table 1
|id| website|time|
-----------------
|01|facebook|20.0|
|01|google |40.0|
|01|youtube |10.0|
|01|ebay |30.0|
|02|facebook|50.0|
|02|ebay |50.0|
Table 2
|id|marks|
-----------
|01| 80|
|02| 90|
|03| 70|
|04| 100|
I want to select (marks),(time on facebook) and (count of time on google & youtube) of specific user
Following select query gives (marks),(time on facebook) of user id '01'
How to receive count of time of both google and youtube of id'1' in same query ?
SELECT table2.id,table2.marks, table1.time
FROM table1
RIGHT JOIN table2 ON table1.id= table2.id
WHERE table1.website LIKE ('%facebook%')
AND table1.id= '01'
GROUP BY table1.id, table1.website
You want to find the time on facebook and then the sum of youtube and google for a particular user you can use the mysql conditional sum to achieve it
select
sum(case when t1.website = 'facebook' then t1.time else 0 end) as `fb_time`,
(
sum(case when t1.website='google' then t1.time else 0 end)+
sum(case when t1.website='youtube' then t1.time else 0 end)
)
as `google_youtube`,
t2.marks
from table1 t1
join table2 t2 on t1.id = t2.id
where t1.id = '01'
If you need to calculate the same for all the users then you can do it as
select
t1.id,
sum(case when t1.website = 'facebook' then t1.time else 0 end) as `fb_time`,
(
sum(case when t1.website='google' then t1.time else 0 end)+
sum(case when t1.website='youtube' then t1.time else 0 end)
)
as `google_youtube`,
t2.marks
from table1 t1
join table2 t2 on t1.id = t2.id
group by t1.id
If I understand your query correctly, I think you will need to use a subquery.
The following subquery returns two counts; time_on_facebook & time_on_google_and_youtube
for all users
SELECT t1.id, t2.marks,
COUNT(t1.time) as time_on_facebook,
(SELECT COUNT(t1_sq.time)
FROM `table1` as t1_sq
WHERE (t1_sq.website = "youtube" OR t1_sq.website = "google")
AND t1_sq.id = t1.id
GROUP BY t1.id) as time_on_google_and_youtube
FROM `table1` as t1
LEFT JOIN table2 t2 ON t2.id = t1.id
WHERE t1.website = "facebook"
GROUP BY t1.id
To restrict it to user id = 01, add in a WHERE clause
SELECT t1.id, t2.marks,
COUNT(t1.time) as time_on_facebook,
(SELECT COUNT(t1_sq.time)
FROM `table1` as t1_sq
WHERE (t1_sq.website = "youtube" OR t1_sq.website = "google")
AND t1_sq.id = t1.id
GROUP BY t1.id) as time_on_google_and_youtube
FROM `table1` as t1
LEFT JOIN table2 t2 ON t2.id = t1.id
WHERE t1.website = "facebook" AND t1.id = 1
GROUP BY t1.id
Are you sure you want COUNT(time) or do you want SUM(time)?
Lastly, consider adding a primary key to both tables and maybe rename the "id" column to "user_id" for clarity.
Its not clear what you want the output to look like. I made a query,
but did not try it. Try it and let me know if it works.
select t1.id, t1.website, sum(t1.time) as total_time, max(t2.marks) as marks
from table1 as t1
left join table2 as t2
on t1.id = t2.id
where t1.website = 'facebook'
and t1.id = '01'
group by t1.id, t1.website
UNION
select t1.id, t1.website, sum(t1.time) as total_time, max(t2.marks) as marks
from table1 as t1
left join table2 as t2
on t1.id = t2.id
where t1.website IN ('youtube', 'google')
and t1.id= '01'
group by t1.id, t1.website

How can I read more than one column in this

In 1_products t2 I have other columns called make,model
When I add them
(SELECT
t2.code,t2.make,t2.model
FROM .....
I get
Error 1241 Operand should contain 1 column(s)
SELECT
t1.fk_products_id,
(SELECT
t2.code
FROM
1_products t2
WHERE
t2.id = t1.fk_products_id
order by code
limit 1)
FROM
1_stock t1
WHERE
t1.branch = 1 and t1.dispatch <> 0;
You cannot return more than one column in an inline query. If you want to return more than one column for each t1.fk_products_id then you will have to rewrite the query similar to this:
SELECT t1.fk_products_id,
t2.code,
t2.make,
t2.model
FROM 1_stock t1
LEFT JOIN 1_products t2
on t1.fk_products_id = t2.id
WHERE t1.branch = 1
and t1.dispatch <> 0;