Error SQL 979 with the group by - sql

I have a problem with my Java EE/hibernate application.
This query works in my unit test, but not in my application.
<named-query name="list.vacant.accessory">
<query>SELECT proty, COUNT(acc.id)
FROM ProductType proty, Accessory acc
LEFT JOIN acc.productHistoryList phl
WHERE phl.status LIKE 'En stock ATOS'
AND proty.id = acc.productType.id
AND phl.statusDate = (SELECT MAX(statusDate)
FROM ProductHistory ph
WHERE ph.product=phl.product)
GROUP BY proty
</query>
</named-query>
I have this error : SQL Error: 979, SQLState: 42000
ORA-00979: not an expression GROUP BY
Have you any idea ?
Thanks.

I don't know hibernate mapping. But purely from Query stand point:
SELECT proty, COUNT(acc.id)
FROM ProductType proty, Accessory acc
LEFT JOIN acc.productHistoryList phl
Here proty is referring to table / Entity and you are grouping by table name? Shouldn't there be a column name? proty.column?

Related

Why am i getting Unknown column 'list_class.pk_class_id' in 'on clause' error?

I want to get student details from different tables with given academic year and class.
Query that i used is:
SELECT
list_acad_years.acad_year,
a.fk_stu_id,
tbl_stu_details.stu_fname,
tbl_stu_details.stu_sname,
a.fk_section_id,
b.fk_class_id,
list_class.class_name,
list_sections.section_code
FROM
tbl_stu_details,
list_class,
list_sections,
list_acad_years
INNER JOIN
tbl_stu_class AS a
ON
(
list_acad_years.pk_acad_year_id = a.fk_year_id
)
INNER JOIN
tbl_stu_class AS b
ON
(list_class.pk_class_id = b.fk_class_id)
WHERE
(
list_acad_years.acad_year = '2019'
) AND(list_class.class_name = '10')
it shows the following error:
#1054 - Unknown column 'list_class.pk_class_id' in 'on clause'
columns of my table are:
tbl_stu_class:
pk_stu_cls_id`, `fk_stu_id`, `fk_year_id`, `fk_class_id`, `fk_section_id`, `current_yr`
list_class:
`pk_class_id`, `class_name`, `class_code`, `fk_user_id`
list_sections:
pk_section_id`, `section_code`, `section_description`, `fk_user_id`
list_acad_years:
`pk_acad_year_id`, `acad_year`, `acad_year_code`, `fk_user_id`
tbl_stu_details:
`pk_stu_id`, `stu_id`, `username`, `stu_fname`, `stu_mname`, `stu_sname`
list_sections:
`pk_section_id`, `section_code`, `section_description`, `fk_user_id`
Why did it say unknown column when the column is present?
It would be great help if you can help me make this query better...
Thanks in advance.
Your problem is that you are using old-style joins. Period. To make matters worse, you are combining them with new style joins.
The names of the tables are not understood across commas. That limits the scope of the definitions.
You appear to know how to write JOINs correctly. So, just fix the FROM clause and your code should work.
Your Inner join will alway connect to the last table in from statement if you are using multiple FROM table.
I have just change the position of INNER JOIN
try this let me know if it works.
SELECT
list_acad_years.acad_year,
a.fk_stu_id,
tbl_stu_details.stu_fname,
tbl_stu_details.stu_sname,
a.fk_section_id,
b.fk_class_id,
list_class.class_name,
list_sections.section_code
FROM
tbl_stu_details,
list_class INNER JOIN
tbl_stu_class AS b
ON
(list_class.pk_class_id = b.fk_class_id),
list_sections,
list_acad_years
INNER JOIN
tbl_stu_class AS a
ON
(
list_acad_years.pk_acad_year_id = a.fk_year_id
)
WHERE
(
list_acad_years.acad_year = '2019'
) AND(list_class.class_name = '10')

Error 1214 when using GROUP BY in my view

I've got a problem with an view on my database. All other views are working correctly, but when I'm trying to add a GROUP BY statement behind my query,
then I'll get the next Error:
1214 - The used table type doesn't support FULLTEXT indexes
I've set all my tables to ISAM, and i noticed the whole problem is in the GROUP BY statement.
My query looks like this:
CREATE VIEW `id_winkels`
AS
SELECT
`w`.`w_id` AS `w_id`,
`w`.`k_id` AS `k_id`,
`w`.`w_naam` AS `w_naam`,
`w`.`w_logo` AS `w_logo`,
`w`.`w_homepage` AS `w_homepage`,
`w`.`w_straat` AS `w_straat`,
`w`.`w_huisnummer` AS `w_huisnummer`,
`w`.`w_postcode` AS `w_postcode`,
`w`.`w_woonplaats` AS `w_woonplaats`,
`w`.`w_land` AS `w_land`,
`w`.`w_actief` AS `w_actief`,
`w`.`w_datum` AS `w_datum`,
COUNT(`p`.`p_id`) AS `totaal`
FROM `Winkels` `w`
LEFT JOIN `Producten` `p`
ON `w`.`w_id` = `p`.`w_id`
GROUP BY `w`.`w_naam`
Is there another option to count the amount of products by my shop without useing a group by?
When I'm running the query without the group by statement, it will return my total amount of product in one row.
As you only select fields from Winkels, you should get the product count in a subquery rather than joining the tables:
CREATE VIEW id_winkels AS
SELECT
w.w_id AS w_id,
w.k_id AS k_id,
w.w_naam AS w_naam,
w.w_logo AS w_logo,
w.w_homepage AS w_homepage,
w.w_straat AS w_straat,
w.w_huisnummer AS w_huisnummer,
w.w_postcode AS w_postcode,
w.w_woonplaats AS w_woonplaats,
w.w_land AS w_land,
w.w_actief AS w_actief,
w.w_datum AS w_datum,
(
select count(*)
from Producten p
where p.w_id = w.w_id
) AS totaal
FROM Winkels w;

group_concat and SQLSTATE[HY000]: General error: 1111 Invalid use of group function

On the sales order admin page in Magento I sometimes (though not always) get an error on the page, and see in var/report:
SQLSTATE[HY000]: General error: 1111 Invalid use of group function
SELECT COUNT(DISTINCT main_table.entity_id) FROM `m_sales_flat_order_grid` AS `main_table`
INNER JOIN `m_sales_flat_order_item`
ON `m_sales_flat_order_item`.order_id=`main_table`.entity_id
INNER JOIN `m_catalog_product_entity_varchar`
ON (m_catalog_product_entity_varchar.entity_id = `m_sales_flat_order_item`.`product_id`)
AND `m_catalog_product_entity_varchar`.attribute_id=163
WHERE (`m_sales_flat_order_item`.parent_item_id IS NULL)
AND ((group_concat(`m_catalog_product_entity_varchar`.value SEPARATOR ', ') like '%tenzen%'))
Can anyone shed light on why this happens and how I could fix it?
The problem with the query is the group_concat() in the where clause. However, you don't need group_concat() at all for this logic.
However, you do need to aggregate to bring all the rows for a given entity together. This requires a subquery, so I would suggest rewriting it as:
SELECT COUNT(*)
FROM (SELECT main_table.entity_id
FROM `m_sales_flat_order_grid` AS `main_table` INNER JOIN
`m_sales_flat_order_item`
ON `m_sales_flat_order_item`.order_id = `main_table`.entity_id INNER JOIN
`m_catalog_product_entity_varchar`
ON m_catalog_product_entity_varchar.entity_id = `m_sales_flat_order_item`.`product_id` AND
`m_catalog_product_entity_varchar`.attribute_id = 163
WHERE `m_sales_flat_order_item`.parent_item_id IS NULL
GROUP BY main_table.entity_id
HAVING SUM(`m_catalog_product_entity_varchar`.value like '%tenzen%') > 0
) t

POSTGRES Group by / Inner Join

I am having problems writing a query to summarize the number trips from a OD matrix table. I am still new to using databases other than MS Access, so please forgive my inexperience. SMGZ is the table with the number of zones and matrix_od is the matrix table with all the OD pairs(4109*4109) from zone to zone with the total number of trip types. I am not sure this is the best way to write this query. SO what i need to do is summarize 4 types of trips(LHDT,MHDT,HHDT,HDT_Tota) by the OD. Help is much appreciated.
SELECT
smgz.smg_zone AS "O_ID",
smgz.x AS "O_X",
smgz.y AS "O_Y",
smgz1.smg_zone AS "D_ID",
smgz1.x AS "D_X",
smgz1.y AS "D_Y",
SUM(matrix_od."LHDT") AS "LHDT_Tot",
SUM(matrix_od."MHDT") AS "MHDT_Tot",
SUM(matrix_od."HHDT") AS "HHDT_Tot",
SUM(matrix_od."TOT_HDT") AS "HDT_Tot"
FROM
public.smgz,
public.matrix_od,
public.smgz smgz1
GROUP BY
"O_ID","O_X","O_Y","D_ID","D_X","D_Y"
INNER JOIN
smgz on matrix_od.O_ID = smgz.ID
INNER JOIN
smgz1 on matrix_od.D_ID = smgz1.ID;
ERROR: syntax error at or near "INNER"
LINE 18: INNER JOIN
^
********** Error **********
ERROR: syntax error at or near "INNER"
SQL state: 42601
Character: 414
As stated in comments the two main errors are that the group by clause is in the wrong place, it should be after the joins, and that you are mixing implicit joins (multiple tables in the from clause) with explicit joins (using the join keyword). The fix is to change this part:
FROM
public.smgz,
public.matrix_od,
public.smgz smgz1
GROUP BY
"O_ID","O_X","O_Y","D_ID","D_X","D_Y"
INNER JOIN
smgz on matrix_od.O_ID = smgz.ID
INNER JOIN
smgz1 on matrix_od.D_ID = smgz1.ID;
to this:
FROM
public.matrix_od
INNER JOIN
public.smgz on matrix_od.O_ID = smgz.ID
INNER JOIN
public.smgz smgz1 on matrix_od.D_ID = smgz1.ID
GROUP BY
"O_ID","O_X","O_Y","D_ID","D_X","D_Y";
That did the trick! Thank you #a_horse_with_no_name & #Lamak.
SELECT
smgz.smg_zone AS "O_ID",
smgz.x AS "O_X",
smgz.y AS "O_Y",
smgz1.smg_zone AS "O_ID",
smgz1.x AS "D_X",
smgz1.y AS "D_Y",
SUM(matrix_od."LHDT") AS "LHDT_Tot",
SUM(matrix_od."MHDT") AS "MHDT_Tot",
SUM(matrix_od."HHDT") AS "HHDT_Tot",
SUM(matrix_od."TOT_HDT") AS "HDT_Tot"
FROM
public.matrix_od
INNER JOIN
smgz on matrix_od."O_ID" = smgz."id"
INNER JOIN
public.smgz smgz1 on matrix_od."D_ID" = smgz1."id"
GROUP BY
smgz.smg_zone,smgz.x,smgz.y,smgz1.smg_zone,smgz1.x,smgz1.y;

SQL query seems to have an Error

I have a DBF - foxpro query and seems like I have an error, I am using codeIgniter and its feedback is just Fatal error: Call to a member function execute() on a non-object in D:\xampp\htdocs\accounting\system\database\drivers\pdo\pdo_driver.php on line 193 and I have encountered this error many times already and it means I have an error in my SQL but I cant figure out where. here are my tables
GUESTS
Guest ID | Guest_Name | Guest_Seat_No
1 | John | 24
SEATS
Seat_No | Room_Location
24 | 2nd Floor Room 11
HERE IS MY SQL QUERY
SELECT A.Guest_ID, A.Guest_Name, A.Guest_Seat_No, B.Room_Location
FROM GUESTS A JOIN SEATS B
ON A.Guest_Seat_No = B.Seat_No
WHERE A.Guest_ID = '1'
It seems there's something wrong in my query, its very difficult to determine the error because it just returns a fatal error generated by codeIgniter not the actual sql syntax error can someone please help me?
you should define what kind of JOIN type you are using, like INNER , LEFT, OUTER, FULL,
SELECT A.Guest_ID, A.Guest_Name, A.Guest_Seat_No, B.Room_Location
FROM GUESTS AS A
JOIN SEATS AS B ON A.Guest_Seat_No = B.Seat_No
WHERE A.Guest_ID = '1'
Whew, I cant believe I got stuck on this just because of the word INNER in INNER JOIN I usually use just JOIN because knowing INNER JOIN is the default right? maybe DBF foxpro really wants the keyword INNER in JOIN statement :) strict fellow. Anyway Thanks to all of you for the help.
SELECT A.Guest_ID, A.Guest_Name, A.Guest_Seat_No, B.Room_Location
FROM GUESTS A INNER JOIN SEATS B
ON A.Guest_Seat_No = B.Seat_No
WHERE A.Guest_ID = '1'