Strange Mysql error 1111, supposedly worked before - mysql

I am trying to troubleshoot a particular 'report' that gets generated on a PHP application running on a VOIP platform (billing related). The person that asked me to do this stated "it worked before" ;)
MySQL Error thrown:
1111: Invalid use of group function
Crazy thing is, it 'worked before' but stopped after awhile, they cannot place what event took place that might have created this bug. One thought is maybe MYSQL was upgraded? I googled the error, and can't seem to find a clear answer.
At first glance do you guys see anything wrong here in the SQL?
SELECT C.ResourceGroupID AS CustomerID, CS.CustomerName, SUM(C.IN_RndDuration/60) AS Minutes, SUM(CASE WHEN (C.OUT_Duration>0 AND C.IN_RndDuration>0) THEN 1 ELSE 0 END) AS Successfull, count(0) AS Attempts
FROM ws_call_current AS C
LEFT JOIN customer_rg AS V_RG ON
V_RG.RGId=C.OtherResourceGroupID AND V_RG.RateTableId IS NOT NULL AND V_RG.Direction='O'
LEFT JOIN customer AS V ON V.CustomerId=V_RG.CustomerId
LEFT JOIN customer_rg AS CS_RG ON CS_RG.RGId=C.ResourceGroupID AND CS_RG.RateTableId IS NOT NULL AND CS_RG.Direction='I'
LEFT JOIN customer AS CS ON CS.CustomerId=CS_RG.CustomerId
WHERE DATE_FORMAT(DATE_ADD(C.SeizeDate, INTERVAL TIME_TO_SEC(C.SeizeTime) SECOND), '%Y-%m-%d %H:%i:00') BETWEEN '2010-10-19 00:00:00' AND '2010-10-19 23:59:59'
AND C.SeizeDate BETWEEN '2010-10-19' AND '2010-10-19'
GROUP BY CS.CustomerName
ORDER BY SUM(C.IN_RndDuration/60) DESC
This is written for MYSQL 3/4 I believe, not yet sure. Just wanted to get some feedback.
From google results I find some people had success with fixing this by modifying the query to be a Having instead of a where?
Not sure. Anything look odd below?

Try including C.ResourceGroupID in your group by statement. You need to include all non-aggregate items in your select in your group by.
You shouldn't need a having in the code above.
I'm not sure how it could have worked before...

What I found after review is that the cause of this was simply a MySQL discrepancy. Initially the MySQL code was written for 5.0.51a, and the running MySQL version (it was somehow 'attempted' to be upgraded, was 5.0.21).
This version 'downgrade' was the cause of the failed SQL. I honestly don't know what the cause was other than we installed a new version of MySQL to just do a test (on a remote box) and it solved the issue (pointing from primary box to backup MySQL server.
Solution for this error: Version compatibility

Related

How to get the GROUPING and OVER(PARTITION) functions to work in MySQL?

So we're learning MySQL in a class currently, and there's a bunch of us that basically can't run these functions. The professor has used examples in class that run on his system (he's currently running version 8.0.19) yet do not run on ours. We get the error message "Error Code: 1305. FUNCTION ap.GROUPING does not exist" when we try to run this:
SELECT IF(GROUPING(invoice_date) = 1, 'Grand totals', invoice_date) AS invoice_date,
IF(GROUPING(payment_date) = 1, 'Invoice date totals', payment_date) AS payment_date,
SUM(invoice_total) AS invoice_total,
SUM(invoice_total - credit_total - payment_total) AS balance_due
FROM invoices
WHERE invoice_date BETWEEN '2018-07-24' AND '2018-07-31' GROUP BY invoice_date, payment_date WITH ROLLUP;
His only real solution to try was to make sure we are running the 64bit version (which I am), so I've been trying to read up on my own and still having some trouble. My only real hypothesis is that maybe it's the version of MySQL that I am running. I've tried to update MySQL to a new version following this:
https://dev.mysql.com/doc/refman/8.0/en/windows-upgrading.html#windows-upgrading-installer
But once I get to step 3 of "Upgrading MySQL with MySQL Installer", I get this message
this message.
Figured I'd try here to see if anyone else has any ideas on how I can fix this, or how I can actually upgrade to a newer version. Appreciate any help. Thanks.
Here's the connection I use to get on MySQL. Always use the right one.

Why Does This MySQL Query Not Work On The Live Server

I have a query that works when I troubleshoot this within my code on my local pc, it works perfectly but the moment I put this up on the live server the query fails for some reason, I think, but I'm not sure, that this boils down to the date fields somehow but honestly I have no idea why it's failing on the server. I have tried the sql functions Date, date_to_string and my dates are in the format '2019-03-08' please assist, my query is as per below.
select posted_ads.id, posted_ads.title,
posted_ads.description, posted_ads.price,
posted_ads.datePosted,
posted_ads.adCategory,
posted_ads.adSuspended, posted_ads.suspensionEmailSent,
posted_ads.adSuspensionReason, posted_ads_images.id, posted_ads_images.imageName,
posted_ads_images.type, posted_ads_images.adID
from posted_ads inner join
posted_ads_images
on posted_ads.id = posted_ads_images.adID
where title like '%mechanic%' and
posted_ads.adExpired = 0 and
posted_ads.datePosted between '2018-12-09' and '2019-03-09'
group by posted_ads.id
limit 6 offset 6

MySQL Left Outer Join and {?}

I have been tasked with converting some Crystal SQL queries into QlikView and am having trouble deciphering the SQL code as it has been a decade since I last played with this. I'm modelling the data in MySQL workbench prior to importing it into QlikView.
I have posted the code bellow for the sake of completeness although I realize a lot of it is surplus.
The issue I am having is I don't know and cant seem to work out how do duplicate these queries in MySQL workbench as I don't understand what these code segments are doing:
={?APS: ITEM1.ST_Prodcode} and ={?FKE: ITEM1_1.ST_Prodcode} etc.
The above mentioned code appears to me to be calling the next query. Am I right in thinking this?
Pricing
SELECT
`stock_management1`.`st_prodcode`,
`stock_management1`.`st_sdesc`,
`stock_management1`.`st_mstockist`,
`stock_management1`.`APS_rol`,
`stock_management1`.`APS_eoq`,
`stock_management1`.`APS_ms`
FROM
`pricing`.`stock_management`
`stock_management1`
WHERE
(`stock_management1`.`st_mstockist`='BRA'
OR
`stock_management1`.`st_mstockist`='FCS'
OR
`stock_management1`.`st_mstockist`='FKE')
AND
`stock_management1`.`APS_ms`>0
AND
(`stock_management1`.`st_prodcode`>='A'
AND
`stock_management1`.`st_prodcode`<='WZZZZZ999')
EXTERNAL JOIN
stock_management1.st_prodcode={?APS: ITEM1.ST_Prodcode}
AND
stock_management1.st_prodcode={?FCS: stocktake1.S_ProdCode}
AND
stock_management1.st_prodcode={?CENTRAL: Command.mv_PRODCODE}
APS
SELECT `ITEM1`.`ST_SOH`, `ITEM1`.`ST_Prodcode`
FROM `aps`.`ITEM` `ITEM1`
WHERE `ITEM1`.`ST_Prodcode`={?pricing: stock_management1.st_prodcode}
EXTERNAL JOIN ITEM1.ST_Prodcode={?FKE: ITEM1_1.ST_Prodcode}
FKE
SELECT `ITEM1_1`.`ST_SOH`, `ITEM1_1`.`ST_Prodcode`
FROM `iewkelvin`.`ITEM` `ITEM1_1`
WHERE `ITEM1_1`.`ST_Prodcode`={?APS: ITEM1.ST_Prodcode}
EXTERNAL JOIN ITEM1_1.ST_Prodcode={?FCS: ITEM1_2.ST_Prodcode}
Thanks for comments imran & ralfbecher.
I found that the {? implies WHERE followed by the Database: table.field
Simple as that really.

MySQL statement failing on local machine but not deployment machine?

As part of work, I have been assigned to a massive Java EE project that has hundreds of SQL statements strewn throughout it. I set up the project on my computer and have been able to deploy the website just fine and use 99% of it without issue. I came across one odd case though, and that's when I try to create an entry on the website (its a government-based form sort of project). MySQL throws an exception when the program tries to execute the query:
SELECT d.id, ..., dol.weight, d.status
FROM DeliveryOrderEJB d, InbondEJB_DeliveryOrderEJB_link lnk
LEFT JOIN DOLineEJB dol
ON dol.id=(
SELECT MIN(dol2.id)
FROM DOLineEJB dol2
WHERE d.id=dol2.DeliveryOrderEJB_lines
)
WHERE d.id=lnk.DeliveryOrderEJB_id
AND lnk.InbondEJB_itNo='...'
ORDER BY d.id
The exception it gives me is Unknown column 'd.id' in 'where clause'
The part that I am confused about is why it seems to work fine on the production machine, and this appears to be the only one failing on my machine. The column clearly exists in the DeliveryOrderEJB table.
Thank you!
The failure is referring to the d.id in the nested subquery:
ON dol.id=(
SELECT MIN(dol2.id)
FROM DOLineEJB dol2
WHERE d.id=dol2.DeliveryOrderEJB_lines
)
You can fix the query:
SELECT d.id, ..., dol.weight, d.status
FROM DeliveryOrderEJB d join
InbondEJB_DeliveryOrderEJB_link lnk
on d.id=lnk.DeliveryOrderEJB_id left join
DOLineEJB dol
ON dol.id=(
SELECT MIN(dol2.id)
FROM DOLineEJB dol2
WHERE d.id=dol2.DeliveryOrderEJB_lines
)
WHERE lnk.InbondEJB_itNo='...'
ORDER BY d.id
I believe the problem is because you have different versions of MySQL on the machines. MySQL changed the semantics of the , around version 5.0.

Joomla 1.6 menu problem in mysql (execution time < 10 min)

I found that code in the bottom executes more than 10 minutes on my server. The server itself is quite good and I cannot find any suitable explanation for that.
I am using Joomla 1.6.3 the data is migrated from Joomla 1.5.23 using jUpgrade and MySQL client version is 5.1.45.
SELECT a.*,COUNT(DISTINCT m1.id) AS count_published,COUNT(DISTINCT m2.id) AS count_unpublished,COUNT(DISTINCT m3.id) AS count_trashed
FROM `j16_menu_types` AS a
LEFT JOIN `j16_menu` AS m1 ON m1.menutype = a.menutype AND m1.published = 1
LEFT JOIN `j16_menu` AS m2 ON m2.menutype = a.menutype AND m2.published = 0
LEFT JOIN `j16_menu` AS m3 ON m3.menutype = a.menutype AND m3.published = -2
GROUP BY a.id
ORDER BY a.id asc;
I would be very pleased if someone could help me since I am in big trouble :)
P.s. I have downloaded db and checked it on my computer - still the same, execution time is terrible. Is there any way to solve this problem? Or maybe to remove this sql part without significant changes in administration of joomla?
Well it was done quite faster on my PC but still the result is far from one which would satisfy my.
EDIT
Well, I've found a reported bug of this problem. http://joomlacode.org/gf/project/joomla/tracker/?action=TrackerItemEdit&tracker_item_id=24868
And the solution is:
CREATE INDEX idx_menu_published ON j16_menu (published);
However, I am not sure how this will affect administration. I there is anyone who could briefly tell how this part works and should I edit Joomla core code or just use the above code on mysql once. I am wondering if I should index table everytime when I edit menu.
A database / table Index is just a kind of yellow pages if you don't mind the comparison. It updates automatically. If this Index fixes your problem there is nothing else you need to do or do again. You can't break anything either - so just give it a try.