I have the following query (UPDATED SEE BELOW):
SELECT
i0_.id AS id_0,
i0_.address AS address_1,
i1_.name AS name_2,
c2_.name AS name_3,
c3_.code AS code_4,
l4_.iso AS iso_5,
c5_.id AS id_6,
c6_.name AS name_7,
i7_.identifier AS identifier_8
FROM institutions i0_
LEFT JOIN institution_languages i1_ ON (i1_.institution_id = i0_.id)
LEFT JOIN countries c3_ ON (c3_.id = i0_.country_id)
LEFT JOIN country_languages c2_ ON (c2_.country_id = c3_.id)
LEFT JOIN country_spoken_languages c8_ ON (c8_.country_id = c3_.id)
LEFT JOIN cities c5_ ON (c5_.id = i0_.city_id)
LEFT JOIN city_languages c6_ ON (c6_.city_id = c5_.id)
LEFT JOIN languages l4_ ON (l4_.id = i1_.language_id)
LEFT JOIN institution_types i7_ ON (i0_.institution_type_id = i7_.id)
WHERE c8_.is_primary = 1
AND c8_.language_id = l4_.id
AND c2_.language_id = 546
AND i7_.identifier = "work_place"
GROUP BY id_6 #here is the issue...
UPDATED Query
SELECT
i0_.id AS id_0,
i0_.address AS address_1,
i1_.name AS name_2,
c2_.name AS name_3,
c3_.code AS code_4,
c4_.name AS name_5,
i5_.identifier AS identifier_6
FROM institutions i0_
LEFT JOIN institution_languages i1_ ON (i1_.institution_id = i0_.id)
LEFT JOIN countries c3_ ON (c3_.id = i0_.country_id)
LEFT JOIN country_languages c2_ ON (c2_.country_id = c3_.id AND c2_.language_id = ?)
LEFT JOIN country_spoken_languages c6_ ON (c6_.country_id = c3_.id AND c6_.language_id = ? AND c6_.is_primary = ?)
LEFT JOIN city_languages c4_ ON (c4_.city_id = i0_.city_id)
LEFT JOIN institution_types i5_ ON (i0_.institution_type_id = i5_.id)
WHERE i5_.identifier = ?
GROUP BY i0_.city_id
This query is having a problem with GROUP_BY which I am not sure how to solve:
1055 - Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'database.i0_.id' which is not
functionally dependent on columns in GROUP BY clause; this is
incompatible with sql_mode=only_full_group_by
I know this can be easily solved by setting off the only_full_group_by but what can i do to my query to make it work properly and not having to modify the MySQL setup on my server?
if you want not change the sql_mode=only_full_group_by
you can simply add an aggegation function to the column not involved in group by ( eg min() or max()
(in the previuos versione the result for this column was impredictable. in this way you assign a rule for get the value for these columns )
SELECT
i0_.id AS id_0,
min(i0_.address AS) address_1,
min(i1_.name) AS name_2,
min(c2_.name )AS name_3,
min(c3_.code) AS code_4,
min(c4_.name) AS name_5,
min(i5_.identifier) AS identifier_6
FROM institutions i0_
LEFT JOIN institution_languages i1_ ON (i1_.institution_id = i0_.id)
LEFT JOIN countries c3_ ON (c3_.id = i0_.country_id)
LEFT JOIN country_languages c2_ ON (c2_.country_id = c3_.id AND c2_.language_id = ?)
LEFT JOIN country_spoken_languages c6_ ON (c6_.country_id = c3_.id AND c6_.language_id = ? AND c6_.is_primary = ?)
LEFT JOIN city_languages c4_ ON (c4_.city_id = i0_.city_id)
LEFT JOIN institution_types i5_ ON (i0_.institution_type_id = i5_.id)
WHERE i5_.identifier = ?
GROUP BY i0_.city_id
I think you should use at least on aggregate function in select field for the query to work, see below i tried with a sample database 'classicmodels' from tables 'customers' and 'orders':
select c.customerNumber, sum(o.orderNumber)
from customers c
join orders o
on(c.customerNumber = o.customerNumber)
group by c.customerNumber;
the result is returned but if i remove the function 'sum()' the out is as:
ERROR 1055 (42000): Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'classicmodels.o.orderNumber' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
Related
I have created Query Which Gives error of only_full_group_by. I Want To change Query Not SET sql_mode=only_full_group_by
#1055 - Expression #4 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'hrdk.s.item_stock_id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by#
This is the query that is giving me trouble:
SELECT `s`.`department_id`, `s`.`category_id`, `s`.`item_id`, `s`.`item_stock_id`, `s`.`tunch`, `cat`.`category_name`, `im`.`item_name`, `im`.`stock_method`, `cat`.`category_group_id`, SUM(s.grwt) AS grwt, SUM(s.ntwt) AS ntwt, sum(s.less) AS less, SUM(s.fine) AS fine
FROM `item_stock` `s`
LEFT JOIN `item_master` `im` ON `im`.`item_id` = `s`.`item_id`
LEFT JOIN `account` `pm` ON `pm`.`account_id` = `s`.`department_id`
LEFT JOIN `category` `cat` ON `cat`.`category_id` = `s`.`category_id`
WHERE (im.stock_method = 1 AND (`s`.`grwt` =0 OR `s`.`grwt` !=0)
OR (`im`.`stock_method` = 2 AND `s`.`grwt` != 0))
AND s.department_id IN(26,27,28,29,30,31,32,59)
AND `s`.`grwt` !=0
AND `s`.`department_id` = '26'
GROUP BY `s`.`category_id`, `s`.`item_id`, if(`im`.`stock_method` = 1, `s`.`tunch`, "")
ORDER BY `s`.`item_stock_id` DESC
Let me know if you need more information.
You've to add all non aggregated columns in group by
SELECT s.department_id, s.category_id, s.item_id, s.item_stock_id, s.tunch, cat.category_name, im.item_name, im.stock_method, cat.category_group_id, SUM(s.grwt) AS grwt, SUM(s.ntwt) AS ntwt, sum(s.less) AS less, SUM(s.fine) AS fine
FROM item_stock s LEFT JOIN item_master im ON im.item_id = s.item_id
LEFT JOIN account pm ON pm.account_id = s.department_id
LEFT JOIN category cat ON cat.category_id = s.category_id
WHERE (im.stock_method = 1 AND (s.grwt =0 OR s.grwt !=0) OR (im.stock_method = 2 AND s.grwt != 0))AND s.department_id IN(26,27,28,29,30,31,32,59) AND s.grwt !=0 AND s.department_id = '26'
GROUP BY s.department_id, s.category_id, s.item_id, s.item_stock_id, s.tunch, cat.category_name, im.item_name, im.stock_method, cat.category_group_id
ORDER BY s.item_stock_id DESC
I have a Date/Time parameter to my report:
But when I run my query, I get no results:
SELECT HD_QUEUE.NAME as qname, HD_TICKET.ID, HD_TICKET.CREATED, HD_TICKET.TIME_CLOSED, CUSTOMER.FULL_NAME as custfullname,
HD_STATUS.NAME as statname, HD_TICKET.TITLE, left(ASSIGNEE.FULL_NAME, 40) as assignee,
HD_PRIORITY.NAME as pname, HD_CATEGORY.NAME as catname
FROM HD_TICKET
INNER JOIN HD_QUEUE
ON HD_TICKET.HD_QUEUE_ID = HD_QUEUE.ID
INNER JOIN USER CUSTOMER
ON HD_TICKET.SUBMITTER_ID=CUSTOMER.ID
INNER JOIN USER ASSIGNEE
ON HD_TICKET.OWNER_ID=ASSIGNEE.ID
INNER JOIN HD_STATUS
ON (HD_TICKET.HD_STATUS_ID=HD_STATUS.ID)
AND (HD_TICKET.HD_QUEUE_ID=HD_STATUS.HD_QUEUE_ID)
INNER JOIN HD_PRIORITY
ON HD_TICKET.HD_PRIORITY_ID = HD_PRIORITY.ID
and HD_TICKET.HD_QUEUE_ID = HD_PRIORITY.HD_QUEUE_ID
INNER JOIN HD_CATEGORY
ON HD_TICKET.HD_CATEGORY_ID = HD_CATEGORY.ID
and HD_TICKET.HD_QUEUE_ID = HD_CATEGORY.HD_QUEUE_ID
left join ASSET on ASSET.ID = HD_TICKET.ASSET_ID
left join ASSET_DATA_6 on ASSET.ASSET_DATA_ID = ASSET_DATA_6.ID
WHERE (HD_STATUS.NAME = 'Closed'
AND HD_TICKET.TIME_CLOSED < #date_param);
What am I doing wrong?
MySQL does not allow named parameters. Use '?' instead of '#date_param' in the query.
WHERE (HD_STATUS.NAME = 'Closed'
AND HD_TICKET.TIME_CLOSED < ?;
Then check the Dataset Properties and make sure the '?' is associated with the value of your parameter:
SELECT `mpeda_fish`.`id`, `mpeda_fish`.`fish` as analysis, sum(mpeda_fishdetails.quantity) as qty
FROM (`mpeda_fishdetails`)
INNER JOIN `mpeda_scientificfish` ON `mpeda_scientificfish`.`id` = `mpeda_fishdetails`.`scientificfish`
INNER JOIN `mpeda_fish` ON `mpeda_fish`.`id` = `mpeda_scientificfish`.`fish`
INNER JOIN `mpeda_fishcatch` ON `mpeda_fishcatch`.`id` = `mpeda_fishdetails`.`fishcatch`
INNER JOIN `mpeda_harbour` ON `mpeda_harbour`.`id` = `mpeda_fishcatch`.`harbour`
WHERE `mpeda_fishcatch`.`status` = 1
ORDER BY `mpeda_fishdetails`.`id` ASC
this query gets 2 columns null value and one column gets data inside why?
You use the SUM() function. In order to get meaningful results you should have a group by clause.
SELECT `mpeda_fish`.`id`, `mpeda_fish`.`fish` as analysis, sum(mpeda_fishdetails.quantity) as qty
FROM (`mpeda_fishdetails`)
INNER JOIN `mpeda_scientificfish` ON `mpeda_scientificfish`.`id` = `mpeda_fishdetails`.`scientificfish`
INNER JOIN `mpeda_fish` ON `mpeda_fish`.`id` = `mpeda_scientificfish`.`fish`
INNER JOIN `mpeda_fishcatch` ON `mpeda_fishcatch`.`id` = `mpeda_fishdetails`.`fishcatch`
INNER JOIN `mpeda_harbour` ON `mpeda_harbour`.`id` = `mpeda_fishcatch`.`harbour`
WHERE `mpeda_fishcatch`.`status` = 1
GROUP BY `mpeda_fish`.`id`, `mpeda_fish`.`fish`
ORDER BY `mpeda_fishdetails`.`id` ASC
This is my query
SELECT CONCAT(`SM_Title`,' ',`SM_Full_Name`) AS NAME,
`RG_Date`,
`RG_Reg_No`,
`RG_Stu_ID`,
`SM_Tell_Mobile`,
`SM_Tel_Residance`,
`RG_Reg_Type`,
Default_Batch,
`RG_Status`,
`RG_Final_Fee`,
`RG_Total_Paid`,
(`RG_Final_Fee`-`RG_Total_Paid`) AS TOTALDUE,
SUM(`SI_Ins_Amount` - `SI_Paid_Amount`) AS AS_AT_APRIAL_END
INNER JOIN
(SELECT `SI_Ins_Amount`,
`SI_Reg_No`
FROM
`student_installments`
GROUP BY MONTHNAME(`SI_Due_Date`)) Z ON
Z.`SI_Reg_No` = `registrations`.`RG_Reg_No`
FROM `registrations`
LEFT JOIN `student_master` ON `student_master`.`SM_ID` = `registrations`.`RG_Stu_ID`
LEFT JOIN `student_installments` ON `student_installments`.`SI_Reg_No` = `registrations`.`RG_Reg_No`
WHERE (`RG_Reg_Type` LIKE '%HND%' OR `RG_Reg_Type` LIKE '%LMU%' )
AND `SI_Due_Date` <= '2014-04-30' GROUP BY `SI_Reg_No`
It gave me an error near
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Z LIMIT 0, 25' at line 1
SELECT
CONCAT(SM_Title,' ',SM_Full_Name) AS NAME,
RG_Date,
RG_Reg_No,
RG_Stu_ID,
SM_Tell_Mobile,
SM_Tel_Residance,
RG_Reg_Type,
Default_Batch,
RG_Status,
RG_Final_Fee,
RG_Total_Paid,
(RG_Final_Fee-RG_Total_Paid) AS TOTALDUE,
SUM(SI_Ins_Amount - SI_Paid_Amount) AS AS_AT_APRIAL_END
FROM registrations
INNER JOIN
(SELECT SI_Ins_Amount,SI_Reg_No
FROM student_installments
GROUP BY MONTHNAME(SI_Due_Date)) Z ON Z.SI_Reg_No = registrations.RG_Reg_No
LEFT JOIN student_master ON student_master.SM_ID = registrations.RG_Stu_ID
LEFT JOIN student_installments ON student_installments.SI_Reg_No = registrations.RG_Reg_No
WHERE (RG_Reg_Type LIKE '%HND%' OR RG_Reg_Type LIKE '%LMU%' )
AND SI_Due_Date <= '2014-04-30'
GROUP BY SI_Reg_No
I notice you have fogotten the left table or subselect that you want to join to the (SELECT SI_INs .....) and previous this I could see there is no from clause before join.
I hope this helps you
Regards
You are using from clause in wrong position it should be just after selection of your columns, you can use below query:
SELECT
CONCAT(SM_Title,' ',SM_Full_Name) AS NAME ,RG_Date,RG_Reg_No,RG_Stu_ID,SM_Tell_Mobile,SM_Tel_Residance,RG_Reg_Type,Default_Batch,RG_Status,RG_Final_Fee,RG_Total_Paid,(RG_Final_Fee-RG_Total_Paid) AS TOTALDUE, SUM(SI_Ins_Amount - SI_Paid_Amount) AS AS_AT_APRIAL_END
FROM registrations AS reg
JOIN
(SELECT
SI_Ins_Amount,SI_Reg_No
FROM student_installments
GROUP BY MONTHNAME(SI_Due_Date)) AS Z
ON Z.SI_Reg_No = reg.RG_Reg_No
LEFT JOIN student_master AS sm
ON sm.SM_ID = reg.RG_Stu_ID
LEFT JOIN student_installments AS si
ON si.SI_Reg_No = reg.RG_Reg_No
WHERE (RG_Reg_Type LIKE '%HND%' OR RG_Reg_Type LIKE '%LMU%' ) AND SI_Due_Date <= '2014-04-30'
GROUP BY SI_Reg_No;
In the part below, from keyword should go before the inner join:
FROM registrations
INNER JOIN
(SELECT SI_Ins_Amount,
SI_Reg_No
FROM student_installments
GROUP BY MONTHNAME(SI_Due_Date)
) Z
ON Z.SI_Reg_No = registrations.RG_Reg_No
Somehow am not successful with creating the query that I want.
DB is to do with locations, there are the following tables which are relevant
t_location - list of locations incl. field t_location_zipcode, and t_location_id_location
t_zipcodecity - join table just t_zipcodecity_zipcode t_zipcodecity_id_city
t_city - city list with t_city_id_city
t_citystate - join table, t_citystate_id_city, t_citystate_id_state
t_state - list of states with t_state_id_state
Initially I tried to get a list of states with locations using this query:
SELECT DISTINCT `t_state_id_state`
, `t_state_name_full`
FROM (`t_state`)
LEFT JOIN `t_citystate` ON `t_state_id_state` = `t_citystate_id_state`
LEFT JOIN `t_city` ON `t_citystate_id_state` = `t_city_id_city`
LEFT JOIN `t_zipcodecity` ON `t_city_id_city` = `t_zipcodecity_id_city`
LEFT JOIN `t_location` ON `t_zipcodecity_zipcode` = `t_location_zipcode`
ORDER BY `t_state_name_full` asc
which works fine.
Now what I also need / want which I am failing dismally at is to get the number of locations in each state. I don't know if it can be done in this one query or if i need another, either way I need help!
you can use a count and a group by. Something like this:
SELECT DISTINCT `t_state_id_state`
, `t_state_name_full`
, COUNT(*)
FROM (`t_state`)
LEFT JOIN `t_citystate` ON `t_state_id_state` = `t_citystate_id_state`
LEFT JOIN `t_city` ON `t_citystate_id_state` = `t_city_id_city`
LEFT JOIN `t_zipcodecity` ON `t_city_id_city` = `t_zipcodecity_id_city`
LEFT JOIN `t_location` ON `t_zipcodecity_zipcode` = `t_location_zipcode`
GROUP BY `t_state_id_state` , `t_state_name_full`
ORDER BY `t_state_name_full` asc
SELECT t_state_id_state
, t_state_name_full
, COUNT(DISTINCT t_location_id_location) AS locations_number
FROM
t_state
LEFT JOIN
t_citystate ON t_state_id_state = t_citystate_id_state
LEFT JOIN
t_city ON t_citystate_id_state = t_city_id_city
LEFT JOIN
t_zipcodecity ON t_city_id_city = t_zipcodecity_id_city
LEFT JOIN
t_location ON t_zipcodecity_zipcode = t_location_zipcode
GROUP BY t_state_id_state
ORDER BY t_state_name_full ASC
Ok thats looking good, my bad with the left joins, i was initially trying to get ALL the states which is why i used them. But i change that to use inner joins
SELECT t_state_id_state,
t_state_name_full,
COUNT(DISTINCT t_location_id_location) AS locations_number
FROM t_state
INNER JOIN t_citystate ON t_citystate_id_state = t_state_id_state
INNER JOIN t_city ON t_city_id_city = t_citystate_id_city
INNER JOIN t_zipcodecity ON t_zipcodecity_id_city = t_city_id_city
INNER JOIN t_location ON t_location_zipcode = t_zipcodecity_zipcode
GROUP BY t_state_id_state
ORDER BY t_state_name_full ASC
then i actually end up with a result that looks good !