Differences between SQL Fiddle and local - mysql

Over here: MySQL: Get start & end timestamp for each day I was given a working answer, but it only works for me in SQL Fiddle (http://sqlfiddle.com/#!2/62366/1). When I run the same SQL on my Debian Linux / MySQL 5.5.34 I get incorrect results (start & end times are the closest reading to midnight)! Anyone got any suggestions as to what might be causing the problem?

My mistake - real schema had another column for deviceID, and the data held readings for multiple devices. Once I added the following, all worked OK:
...
FROM myTable
HAVING deviceID = 1234
GROUP BY deviceID, StatusTime

Related

" ( " not valid in this position, expecting an identifier in MySQL

Today is, 1/30/2022, I have been following along with an #AlexTheAnalyst video. I am on a Mac and using MySQL version 8.0.27. (The video is using windows based SQL Server Workbench) I am stuck! I am trying to creating a temporary table function. MySQL is not liking the # in the table name #PercentPopVaccinated as used in the video. When I remove it and run the function/query without the # I get 0 rows returned. I have researched on stackoverflow etc. and I am not coming up with a solution that I understand. (Newbie here)
I have dropped the table that was created and I am starting over.
I am getting an error when creating the temp table that states MySQL is expecting an identifier after the first " ( ". Anyone else have a similar issue?
Create Table #PercentPopulationVaccinated
(
continent nvarchar(255),
location nvarchar(255),
date datetime,
population numeric,
new_vaccinations numeric,
RollingVacCount numeric
)
Insert into #PercentPopulationVaccinated
Select dea.continent, dea.location, dea.date, dea.population, vac.new_vaccinations
, SUM(cast(vac.new_vaccinations as UNSIGNED)) OVER (Partition by dea.location Order by dea.location, dea.date)
as RollingVacCount
-- (RollingVacCount/population)*100
From project_portfolio.covid_deaths dea
Join project_portfolio.covid_vaccinations vac
On dea.location = vac.location
and dea.date = vac.date
where dea.continent is not null
-- order by 2,3
Select *, (RollingVacCount/Population)*100
From #PercentPopulationVaccinated;
So I'd say the underlying problem is that you are watching a video tutorial that is using SQL Server, but you are using MySQL. There are many similarities, but it is not going to be an exact match. For instance, the # sign creates a temporary table in Sql Server, but the # is not valid in MySQL. If you want to use a different database service than the tutorial you are watching is for, you are going to have to translate some concepts for yourself.
Another commenter already posted this link, which indicates the syntax for creating temp tables in MySQL.
https://dev.mysql.com/doc/refman/8.0/en/create-table.html#create-table-temporary-tables

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 performance_schema how to get event time from events_statements_current table

I googled a lot for this, and don't see anyone talking about it, so it must be a simple issue, but still it has me stumped.
This performance_schema table - http://dev.mysql.com/doc/refman/5.6/en/events-statements-current-table.htm has timer_start and TIMER_END columns. Accordintg to the documentation " The TIMER_START and TIMER_END values indicate when event timing started and ended" .
One small problem. It's a bigint and not a date. How do I convert it to a date?
I saw one blogger suggest that it's the number of time units since the server was started. In my case statements are supposed to be measured to a nanosecond (10^9). So if I have a timer_start value of 3723676792253626000 that would mean 3723676792 s which would be unlikely since the server uptime is 3723716 s. a simple comparison of the number of digits in these two numbers would lead me to think that the unit of time is really picoseconds (10^12).
so the question is :
1. is timer_start really the number of units from the last restart?
2. if so, why is it in picoseconds when setup_timers indicates nanoseconds?
TIA
Here is a corrected version for MySQL 5.7:
SELECT
DATE_SUB(NOW(), INTERVAL (SELECT VARIABLE_VALUE FROM performance_schema.global_status WHERE VARIABLE_NAME='UPTIME') - TIMER_START*10e-13 second) AS `start_time`,
ROUND(timer_wait*10E-10, 3) AS `wait in (ms)`,
sql_text,
current_schema
FROM performance_schema.events_statements_history_long;
The fixes are:
get global_status from performance_schema (introduced in MySQL 5.7)
fix wait in (ms) (10E-8 vs. 10E-10) + formatting; example test is the following SQL query: SELECT SLEEP(0.5)
the returned columns
uses the "long" history table (events_statements_history_long)
ok, I was able to partially figure it out. Answer to the first question is yes. Here is a query that converts the timer_start value to a time stamp that a human can recognize:
select
date_sub(now(),INTERVAL (select VARIABLE_VALUE from information_schema.global_status where variable_name='UPTIME')-TIMER_START*10e-13 second) `start_time`
,timer_wait/10E-8 `wait in (ms)`
,timer_wait
,sql_text
,digest_text
from performance_schema.events_statements_history

Why would "WHERE `DATE` >={ts '2013-12-01 00:00:00'}" produce a different result to "WHERE `DATE` >='2013-12-01 00:00:00'"?

As title says - why would the first query (generated automatically by Crystal Reports) above produce a different result (unreliably - the first query gives different results depending on when I run it - the database is not being modified) to the second query above? Going by this reference guide: http://dev.mysql.com/doc/refman/5.6/en/date-and-time-literals.html, the output should be the same.
Can anyone offer any advice? Note I am using MySQL version 5.6.15, and this problem only started to occur when we upgraded