MySQL to SQL Server 2014 - mysql

I have the below query:
SELECT Date(time) AS date, COUNT(*) AS total FROM branches INNER JOIN
stats ON branches.branch_id = stats.branch_id WHERE stats.time BETWEEN
'$from' AND '$to' AND branches.bgroup='$group' GROUP BY date;
But when I run this query on SQL Server I get the error:
error SQLSTATE[42000]: [Microsoft][ODBC Driver 11 for SQL Server][SQL
Server]'Date' is not a recognized built-in function name.
How do I convert to SQL Server?

SQL Server has no date() function. Use cast() instead:
SELECT CAST(time as DATE) AS dte, COUNT(*) AS total
FROM branches INNER JOIN
stats
ON branches.branch_id = stats.branch_id
WHERE stats.time BETWEEN '$from' AND '$to' AND
branches.bgroup = '$group'
GROUP BY CAST(time as DATE)
ORDER BY CAST(time as DATE);

Related

"Every derived table must have its own alias" Mysql Error

Whats wrong with my query:
SELECT minDate, deviceType, COUNT(*)
FROM (SELECT visitorId, deviceType,
MIN(sessionDate) as minDate
FROM sessions
GROUP BY visitorId)
WHERE minDate > DATE_SUB(NOW(), INTERVAL 14 DAY)
GROUP BY minDate, deviceId
I've got this message:
Query Error: Error: ER_DERIVED_MUST_HAVE_ALIAS: Every derived table must have its own alias
The error message is clear enough. You must alias the derived table that is generated by your sub-select. So, give it an alias.
Another issue is that, in the subquery, non-aggregated column deviceType should be included in the GROUP BY clause. This change might, or might not produce the results that you do expect: if it doesn't, then you would need to provide sample data, expected results and an explanation of what you are trying to accomplish so one can help fixing the query.
SELECT minDate, deviceType, COUNT(*)
FROM (
SELECT visitorId, deviceType, MIN(sessionDate) as minDate
FROM sessions
GROUP BY visitorId, deviceType -- all non-aggregated columns in the GROUP BY clause
) t -- alias here
WHERE minDate > DATE_SUB(NOW(), INTERVAL 14 DAY)
GROUP BY minDate, deviceId

MySQL calculating age based on birthDate

I am trying to calculate the age based on the birthDate in MySQL using SQL statement.
My birthDate was varchar() and in this format: 29/11/1994 (DD/MM/YYYY).
My SQL statement:
SELECT DATEDIFF(YY, birthDate, GetDate()) AS Age
FROM bookedevent be INNER JOIN account a
ON be.bookedEventBY = a.accountName WHERE a.accountID = 1
However, when I test in MySQL workbench, I am getting this error message: Incorrect parameter count in the call DATEDIFF.
ANy guides?
Thanks in advance.
You are using SQL Server syntax in MySQL. That won't work. You can use TIMESTAMPDIFF():
SELECT TIMESTAMPDIFF(YEAR, birthDate, CURDATE()) AS Age
FROM bookedevent be INNER JOIN
account a
ON be.bookedEventBY = a.accountName
WHERE a.accountID = 1;
Note that the semantics for TIMESTAMPDIFF() are different than for DATEDIFF() in SQL Server. However, TIMESTAMPDIFF() is probably closer to what you really want.
Use this code
SELECT DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(birthDate, '%Y') - (DATE_FORMAT(NOW(), '00-%m-%d') < DATE_FORMAT(birthDate, '00-%m-%d')) AS age
FROM bookedevent be INNER JOIN
account a
ON be.bookedEventBY = a.accountName
WHERE a.accountID = 1;

MySQL: Count the distinct rows per day including 0

Thereis a good answer to the main question here: MySQL: Count the distinct rows per day
I need it with the days with values also included but the query
SELECT DATE(timestamp) Date, COUNT(DISTINCT ipNum) totalCOunt
FROM tableName
WHERE totalCOunt < 1
GROUP BY DATE(timestamp)
gives an error ( #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 'WHERE totalCOunt < 1
LIMIT 0, 25' at line 3 ). Where did I go wrong?
Sample data here: http://sqlfiddle.com/#!2/11aa6/146
It is throwing Unknown column 'totalCOunt' in 'where clause' error:
Please try the below query(replaced WHERE clause with HAVING clause):
SELECT DATE(timestamp) Date, COUNT(DISTINCT ipNum) totalCOunt
FROM tableName
GROUP BY DATE(timestamp)
HAVING totalCOunt<1
You have a few errors:
Date should be escaped, as it is a keyword in MySQL.
The syntax is wrong. You forgot AS.
So the corrected one is:
SELECT CAST(`timestamp` AS Date) AS `Date`, COUNT(DISTINCT(`ipNum`)) AS totalCOunt
FROM `tableName`
GROUP BY CAST(`timestamp` AS Date)
Fiddle: http://sqlfiddle.com/#!2/809838/8
You can't use an alias in the WHERE clause
SELECT DATE(timestamp) Date, COUNT(DISTINCT ipNum) totalCOunt
FROM tableName
WHERE totalCOunt < 1
GROUP BY DATE(timestamp)
Use HAVING instead:
SELECT DATE(timestamp) Date, COUNT(DISTINCT ipNum) totalCOunt
FROM tableName
GROUP BY DATE(timestamp)
HAVING totalCOunt =1

conversion mysql to postgresql

I have a working mysql query, but I can not get it work with postgres. This is the query (I already changed date format to to_char
SELECT country as grouper, date(users.created_at) as date,
to_char(users.created_at, '%Y-%m') as date_group,
count(id) as total_count
FROM "users"
WHERE (users.created_at >= '2011-12-01')
AND (users.created_at <= '2014-02-11')
GROUP BY grouper, date_group
ORDER BY date ASC
I am getting the error:
PG::Error: ERROR: column "users.created_at" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: SELECT country as grouper, date(users.created_at) as date, t...
Thank for your help.
SELECT country as grouper, date(MIN(users.created_at)) as date,
to_char(MIN(users.created_at), '%Y-%m') as date_group,
count(id) as total_count
FROM "users"
HAVING (users.created_at >= '2011-12-01')
AND (users.created_at <= '2014-02-11')
GROUP BY grouper, date_group
ORDER BY date ASC
MySQL is not very strict. In standard conform SQL all column values have to use an aggrate function (SUM, COUNT, MAX, MIN) on non-grouping fields - when using GROUP BY.
Honestly said, I am not entirely sure about data_group in the GROUP BY; can it be dropped?
Also note that I have switched WHERE with a HAVING.
You should use every selected column in GROUP BY section.
SELECT country as grouper, to_char(created_at, '%Y-%u') as date_group, count(id) as total_count
FROM "users"
WHERE created_at >= '2013-10-01'
AND created_at <= '2014-02-11'
GROUP BY grouper, date_group
ORDER BY date_group ASC

MySQL convert to datetime syntax error: unexpected IDENT_QUOTED

We have the following query that runs perfectly in MSSQL but fails to run in MySQL:
select CONVERT(datetime, dateVal) as DateOccurred, itemID, COUNT(*) as Hits from (
select itemID, CONVERT(datetime, DateClickUTC) as dateVal
from tb_items
where DateClickUTC >= '2008-06-03 22:00:28.893' and DateClickUTC <= '2013-06-03 22:00:28.893'
group by CONVERT(datetime, DateClickUTC), UserID, itemID) as a
group by a.dateVal, itemID
The error we get from MySQL says:
syntax error, unexpected IDENT_QUOTED
This error occurs on the dateVal variable on the first line: "Select CONVERT(datetime, dateVal)."
If we remove the first CONVERT the error then moves to the next CONVERT on the following line. So, obviously, there seems to be an error with our datetime conversion. Not sure what we're doing the wrong though, any ideas out there? Thanks all.
I prefer to use CAST, but as others have said, you need to specify the type after the field like this:
convert(DateClickUTC,datetime)
Here is a working example using CAST:
select a.dateVal as DateOccurred, itemID, COUNT(*) as Hits
from (
select itemID, cast(DateClickUTC as datetime) as dateVal
from tb_items
where DateClickUTC >= '2008-06-03 22:00:28.893' and DateClickUTC <= '2013-06-03 22:00:28.893'
group by cast(DateClickUTC as datetime), UserID, itemID
) as a
group by a.dateVal, itemID
SQL Fiddle Demo
BTW -- You actually don't need the subquery in this case -- this should work as well:
select cast(DateClickUTC as datetime) as DateOccurred,
itemID,
COUNT(*) as Hits
from tb_items
where DateClickUTC >= '2008-06-03 22:00:28.893' and DateClickUTC <= '2013-06-03 22:00:28.893'
group by cast(DateClickUTC as datetime), itemID