I have a problem in counting by MySQL in a GROUP BY
This is the query that does not return the desired result.
SELECT COUNT(bagno)
FROM disposizione_assegnazione_pezze
JOIN pezze
ON pezza = id
WHERE id_prodotto_tessuto = 12096
AND id_collezione = 11
AND id_stagione = 22
AND id_tema = 1
GROUP BY bagno
The result of the count is 3
This is the pezza table and its primary key is id
This is the table disposizione_assegnazione_pezze that has the pezza column which refers to the previous table
Why does not return 1 as a result my query?
Question of the problem
I want to count how many different bagno are there
I dont think you need GROUP BY, instead use DISTINCT
SELECT COUNT(DISTINCT bagno)
SQL DEMO
Check your query without agregatted function COUNT/GROUP BY
As you can see bagno = 55 appear three times, that is why when you group by bagno and count get 3.
Related
I can't finish writing query to filter row by multiply ids. Here is query:
select distinct `storage_file`.*, `storage_tag`.`id` as `tid` from `storage_file`
inner join `storage_file_tag` on `storage_file`.`id` = `storage_file_tag`.`storage_file_id`
inner join `storage_tag` on `storage_tag`.`id` = `storage_file_tag`.`storage_tag_id`
where `storage_file`.`user_id` = 17 and `storage_file`.`deleted_at` is null and
`storage_tag`.`id` IN(13,17);
So the result is without group by statement is:
So.. I need result only with two records which contain tid 13 and 17
And when i replace "IN(13,17)" with storage_tag.id = 13 AND storage_tag.id = 17 - i get no records at all
How can i write subquery which will work like a + b but not a OR b ?
I'm not sure what you do exactly but it seams, that the distinct is not working as you expect, because you select "*" from storage_file, as there are different values in the columns of storage_file, the result is distincted but over all selected columnns and so more the two are selected.
You can replace
... AND id IN (11,22)
with
... AND ( id = 11 OR id = 12)
You need the parentheses because WHERE operator precedence rules are very simple.
Of course,
... AND id = 11 AND id = 12
never returns anything because the id cannot have two different values at the same time.
I am trying to get sum of all rows with multiply two column values with where condition but getting some error of mysql. After try some example i achieve my result but i don't know that is right way or not:
Table: store_stocks
i just want to count the stock qty with amount multiply with qty according to with VAT, with non VAT and total stock.
I just created that query:
SELECT sum(qty*sale_price) as total_stock,
(select sum(qty*sale_price) from store_stocks where vat_status = 0 and store_id = 8) as non_vat_stock,
(select sum(qty*sale_price) from store_stocks where vat_status = 1 and store_id = 8) as with_vat_stock
FROM `store_stocks` where store_id = 8 group by store_id
and its showing result:
can any one tell me is there any another way to achieve this, because i think that's query is little bit complicated, each time i am using where in sub query and i also have to implement this query in laravel eloquent.
You do not need subqueries, you can use a condition within the sum() to make it summarise the specific records only:
SELECT sum(qty*sale_price) as total_stock,
sum(if(vat_status = 0, qty*sale_price, 0)) as non_vat_stock,
sum(if(vat_status = 1, qty*sale_price, 0)) as with_vat_stock
FROM `store_stocks` where store_id = 8 group by store_id
You can use a case expression instead of the if() function as well.
select u.UM_TOKEN_NO ,u.UM_FULLNAME,u.SECTOR, u.department_name,t.TS_PROJECT_CODE,sum(t.TS_TOTAL_HRS)
from prm_user_master u , prm_time_sheet t
WHERE (u.UM_TOKEN_NO = t.ts_token_no
AND t.ts_week_no BETWEEN 35 and 40)
GROUP BY t.TS_PROJECT_CODE;
I want to get data where I want sum of total hours for each TS_PROJECT_CODE.
select
u.UM_TOKEN_NO,
u.UM_FULLNAME,
u.SECTOR,
u.department_name,
t.TS_PROJECT_CODE,
sum(t.TS_TOTAL_HRS)
from prm_user_master u
INNER JOIN prm_time_sheet t ON u.UM_TOKEN_NO = t.ts_token_no
where t.ts_week_no BETWEEN 35 and 40
GROUP BY u.UM_TOKEN_NO,u.UM_FULLNAME,
u.SECTOR,u.department_name,t.TS_PROJECT_CODE;
Given all column names in group by clause as in select query except the SUM(t.TS_TOTAL_HRS) column and it worked fine...
I have table rozpis_riesitelov which contains columns :
id_rozpisu_riesit, id_zam, id_projektu, id_ulohy.
I made query :
select id_zam, id_ulohy, count(*) as counted
from rozpis_riesitelov
group by id_zam
having id_ulohy in (1,2,8)
which shows me id of employee (id_zam) and how many times He was in project (id_ulohy is irrevelant but I had to select it beacuse of having clause). It shows me everyone in db but I am looking for employee with ID of 4 who is in 6 projects (Yes, I could do order by but I want to see max). When I do max of this query like this:
select max(counted)
from (select id_zam, id_ulohy, count(id_zam) as counted
from rozpis_riesitelov
group by id_zam
having id_ulohy in (1,2,8)) as riesitel
which shows me number 149 instead of 6.
So basically I only need to find employee that occurs in the most of the projects.
What's wrong with sorting by the COUNT() value, and limiting to one result?
SELECT `id_zam`,
`id_ulohy`,
COUNT(*) AS `counted`
FROM `rozpis_riesitelov `
WHERE `id_ulohy` IN ( 1, 2, 8 )
GROUP BY `id_zam`
ORDER BY `counted` DESC
LIMIT 1
Not sure exactly what you are trying to accomplish but you only use HAVING to filter on your aggregate like this:
HAVING COUNT(*) > 1
you should be able to move the condition to a WHERE clause and get correct max returned:
select max(counted)
from (select id_zam, count(id_zam) as counted
from rozpis_riesitelov
where id_ulohy in (1,2,8)
group by id_zam) as riesitel
From this post, enter link description here
I would like to improve the query
SELECT `BetType`,
count(`BetType`) AS COUNT,
sum(`BetAmount`) AS BetAmountTotal,
sum(`Payout`) AS PayoutTotal
FROM `betdb`
LEFT JOIN `matchdb` ON `betdb`.`MatchID` = `matchdb`.`MatchID`
WHERE `betdb`.`MatchID`=135
GROUP BY `BetType`
thanks to Sadikhasan, who helped on this query
I would like to add another row showing the totals of the columns
BetType Count BetAmount Total Payout Total
Handi 2 60000 950000
Homerun Count 4 10000 0
Total 6 70000 950000
this seems to be needing another SELECT statement but how would I put another row explicitly showing the "Total" string and getting the sum of the previously used columns with Aggregate Functions?
You can use WITH ROLLUP modifier to GROUP BY, which will give you another row with totals, but the column you group on (BetType) will show NULL for that row. But nothing stops you from using COALESCE() to replace that NULL with 'Total' string.
SELECT COALESCE(`BetType`,'Total') AS BetType,
COUNT(*) AS `Count`,
sum(BetAmount) AS BetAmountTotal,
sum(Payout) AS PayoutTotal
FROM betdb
WHERE betdb.MatchID=135
GROUP BY BetType WITH ROLLUP