SQL where to query in Rails - mysql

When I type:
User.where(:created_at => "2015-08-06".."2015-08-07").where("updated_at > created_at + 1.day").count
Error Returned:
SELECT COUNT(*) FROM `users` WHERE `users`.`created_at` BETWEEN '2015-08-06' AND '2015-08-07' AND (updated_at > created_at + 1.day)ActiveRecord::StatementInvalid: Mysql2::Error: 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 'day)' at line 1: SELECT COUNT(*) FROM `users` WHERE `users`.`created_at` BETWEEN '2015-08-06' AND '2015-08-07' AND (updated_at > created_at + 1.day)
from /home/guinness/shared/bundle/ruby/2.1.0/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:245:in query'
What's wrong with this? As you know I want to know how many users retained after 1 day of their sign up.

1.day is ruby syntax, not sql. try this:
User.where(:created_at => "2015-08-06".."2015-08-07").where("updated_at > DATE_ADD(created_at, INTERVAL 1 DAY)").count
see http://www.w3schools.com/sql/func_date_add.asp

Try it like this
User.where(:created_at => "2015-08-06".."2015-08-07").where("updated_at > ?", created_at + 1.day).count

Related

Trying to perform divison using MySQL subqueries but getting syntax error

I am trying to get the value from MySQL subqueries but I am getting 'Error Code: 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 ')' at line 1'
The code is:
UPDATE national_avg_tariff SET Value= (CASE WHEN ((SELECT * FROM((SELECT Value
/ ((Select Sum_CPP_MlnRs from ppp_fy2020_2030_annex4.FY_CPP_exclKE_MlnRs where FiscalYear=2020) + (Select UoSC_MoF_exclKE_MlnRs from ppp_fy2020_2030_annex4.FY_UoSC_MoF_exclKE_MlnRs where FiscalYear=2020))/ (SELECT Sum(Sum_of_AvgMW) FROM ppp_fy2020_2030_annex4.AVG_OF_FY_MAX_DEMAND where FiscalYear=2020 GROUP BY FiscalYear)FROM tariff_true_cost.national_avg_tariff WHERE Sector='Residential' AND Category='Up to 50 Units' AND Parameter= 'Fixed Charge(Rs/kW/M)' AND FiscalYear=2020)temptbl8))>0)
THEN
(SELECT * FROM((SELECT Value
/ ((Select Sum_CPP_MlnRs from ppp_fy2020_2030_annex4.FY_CPP_exclKE_MlnRs where FiscalYear=2020) + (Select UoSC_MoF_exclKE_MlnRs from ppp_fy2020_2030_annex4.FY_UoSC_MoF_exclKE_MlnRs where FiscalYear=2020))/ (SELECT Sum(Sum_of_AvgMW) FROM ppp_fy2020_2030_annex4.AVG_OF_FY_MAX_DEMAND where FiscalYear=2020 GROUP BY FiscalYear)FROM tariff_true_cost.national_avg_tariff WHERE Sector='Residential' AND Category='Up to 50 Units' AND Parameter= 'Fixed Charge(Rs/kW/M)' AND FiscalYear=2020)temptbl8))
ELSE NULL END) where FiscalYear=2020 AND Parameter='Fixed Charge(%)';
The code doesn't show any red underline or pre-execution error something, the error only appears once I execute it.
Any help in rectifying syntax error of this piece of code would be appreciable. Thanks

MYSQL syntax error, with date time

I am currently converting my api from SQL to MySQL, however one of my queries is failing at this point below.
The query:
SELECT u.DATETIME,
SUM(Value * m.Multiplier) * 1 AS Usage,
TIMESTAMPDIFF(SECOND, '1970-01-01 00:00:00', UNIX_TIMESTAMP(u.DATETIME)) as Datestamp_epoch
FROM MeterMaps m
JOIN UsageElectricityDaily u ON m.ChanID = u.ChanID
JOIN SourceChannels c ON c.ChanID = m.ChanID
WHERE m.LocationID = 1
AND c.QuantityID = 'Usage'
AND u.DateStamp >= '01/12/2016 00:00:00'
AND u.DateStamp < '31/12/2016 00:00:00'
AND m.Utility = 'Electric'
GROUP BY u.Datestamp
The error that is thrown is
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 'Usage, TIMESTAMPDIFF(SECOND, '1970-01-01 00:00:00',
UNIX_TIMESTAMP(u.DATETIME)) ' at line 1
In MySQL the word usage is a keyword, therefore it must be enclosed by backticks or change it.
SELECT u.DATETIME,
SUM(Value * m.Multiplier) * 1 AS `Usage`
...

List error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version

Can you help me on this error?
Mon, 15 Dec 2014 18:22:16 +0000
Error encountered during the process
The reported error was List error:
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 ') AS FLOAT)) AS datetime) < GETDATE()) AND company = '27074BAY') AND gages.ga' at line 6.
Here is my code,
$sql_statement = "SELECT gages.*
FROM gages
WHERE gages.company = '$selectcomp'
AND GAGES.isactive != '0'
AND gages.gage_sn IN
(SELECT schedgi.gage_sn FROM schedgi WHERE (CAST(FLOOR(CAST(schedgi.sched_due_date) AS FLOAT)) AS datetime) < GETDATE() AND company = '$selectcomp')
AND gages.gage_sn NOT IN
( SELECT gages.gage_sn
FROM gages
WHERE gages.company = '$selectcomp'
AND gage_sn IN
(SELECT schedgi.gage_sn FROM schedgi WHERE (CAST(FLOOR(CAST(schedgi.sched_due_date) AS FLOAT)) AS datetime) < GETDATE() AND company = '$selectcomp'))"
Choose different names for your aliases. FLOAT is a reserved word in MySQL: http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html.
You can wrap the reserved words in backticks, but in my opinion it isn't worth the hassle.
SELECT something AS `FLOAT`...

mysql timediff where condition

I have to use a condition in the time diff of mysql. I have to modify the following query:-
select
bugs_team_user_view.bug_id,
bugs_team_user_view.creation_ts
from
bugs_team_user_view, bugs_activity
where
bugs_team_user_view.team like "Red%"
and
timediff(
(select bugs_activity.bug_when from bugs_activity where bugs_activity.added = "RESOLVED"),
bugs_team_user_view.creation_ts) > "00:30:00"
and
bugs_team_user_view.bug_id=bugs_activity.bug_id
and bugs_team_user_view.creation_ts < "2013-08-17 00:00:00"
and bugs_team_user_view.creation_ts >"2013-08-01 00:00:00";
Getting error:-
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to usenear 'bugs_team_user_view.creation_ts < "2013-08-17 00:00:00" and bugs_team_user_view.' at line 1
Please help
Change it to
timediff(
(select bugs_activity.bug_when from bugs_activity where bugs_activity.added = "RESOLVED"),
bugs_team_user_view.creation_ts) > TIME('00:30:00')
it should work.

SELECT error querying

What could be wrong with this line:
'$query = "SELECT * FROM messages WHERE (rlat => '".$latmin."' AND rlat <= '".$latmax."') AND (rlon >= '".$lonmin."' AND rlon <= '".$lonmax."')";'
Error: Error in query: SELECT * FROM messages WHERE (r_lat =>
'55.4655951769' AND r_lat <= '55.496987423') AND (r_lon >=
'25.5338700398' AND r_lon <= '25.5989507602'). 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 '=> '55.4655951769'
AND r_lat <= '55.496987423') AND (r_lon >= '25.5338700398' AN' at line
1...
Thanks!
=> isn't a known operator. If you want the greater than or equal comparison operator, then you are after >=.
Incidentally, you might find that your query can be written more concisely with the BETWEEN ... AND ... operator:
SELECT *
FROM messages
WHERE rlat BETWEEN $latmin AND $latmax
AND rlon BETWEEN $lonmin AND $lonmax
You should also investigate passing variables into your SQL by way of parameters to prepared statements.