Laravel - SQLSTATE[42000] - UNION - sql

I am making a UNION between these two queries:
$first =
DB::table('pedido')
->select(DB::raw('date(fecha_pago) as fecha'),DB::raw('sum(subtotal) as ingreso'),DB::raw('0 as egreso'))
->where('idestado','=','2')
->groupBy(DB::raw('date(fecha_pago)'));
$second =
DB::table('egreso')
->select(DB::raw('date(fecha) as fecha'),DB::raw('0 as ingreso'),DB::raw('sum(monto) as egreso'))
->groupBy(DB::raw('date(fecha)'));
$final_query=
DB::select(DB::raw('date(fecha),sum(ingreso) as ingreso,sum(egreso) as egreso'))
->union($first)->union($second)
->groupBy(DB::raw('date(fecha)'))
->get();
I get the error:
SQLSTATE[42000]: Syntax error or access violation: 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 'date(fecha),sum(ingreso) as ingreso,sum(egreso) as egreso' at line 1 (SQL: date(fecha),sum(ingreso) as ingreso,sum(egreso) as egreso)

DB::select() executes a raw query, unlike DB::table()->select() which is used to define fields for the query builder to select.
You're missing the DB::table() in your final query, without it, you won't be using the query builder and you'll actually be running a query of just date(fecha),sum(ingreso) as ingreso,sum(egreso) as egreso.
https://laravel.com/docs/5.6/queries#selects

Related

Sybase Database Error: Invalid data conversion

I need help with this Query, I'm making some calculations based on some results, but I'm receiving the following error:
Sybase Database Error: Invalid data conversion.
The query is as follows:
SELECT
DC.DIM_DATE.DATE_ID,
DC.DIM_E_RAN_UCELL.RBS_ID,
(SUM(DC.DC_E_RAN_UCELL_RAW.pmSumBestDchPsIntRabEstablish)+SUM(DC.DC_E_RAN_UCELL_RAW.pmSumFachPsIntRabEstablish)+SUM(DC.DC_E_RAN_UCELL_RAW.pmSumBestPsHsAdchRabEstablish)+SUM(DC.DC_E_RAN_UCELL_RAW.pmSumBestPsEulRabEstablish))/720 AS '3G_DATA_ERLANG',
(SUM(DC.DC_E_RAN_UCELL_RAW.pmSumBestCs12Establish)+SUM(DC.DC_E_RAN_UCELL_RAW.pmSumBestAmr12200RabEstablish)+SUM(DC.DC_E_RAN_UCELL_RAW.pmSumBestAmr7950RabEstablish)+SUM(DC.DC_E_RAN_UCELL_RAW.pmSumBestAmr5900RabEstablish)+SUM(DC.DC_E_RAN_UCELL_RAW.pmSumBestAmr4750RabEstablish)+SUM(DC.DC_E_RAN_UCELL_RAW.pmSumBestAmrWbRabEstablish)+SUM(DC.DC_E_RAN_UCELL_RAW.pmSumBestAmrNbMmRabEstablish))/720 AS '3G_SPEECH_ERLANG',
(100*(SUM(DC.DC_E_RAN_UCELL_RAW.pmTotNoRrcConnectReqCsSucc)/(1+SUM(DC.DC_E_RAN_UCELL_RAW.pmTotNoRrcConnectReqCs)-SUM(DC.DC_E_RAN_UCELL_RAW.pmNoLoadSharingRrcConnCs))*(SUM(DC.DC_E_RAN_UCELL_RAW.pmNoRabEstablishSuccessSpeech)+SUM(DC.DC_E_RAN_UCELL_RAW.pmNoRabEstablishSuccessCs64)+SUM(DC.DC_E_RAN_UCELL_RAW.pmRabEstablishEcSuccess))/(SUM(DC.DC_E_RAN_UCELL_RAW.pmRabEstablishEcAttempt)+SUM(DC.DC_E_RAN_UCELL_RAW.pmNoRabEstablishAttemptSpeech)+SUM(DC.DC_E_RAN_UCELL_RAW.pmNoRabEstablishAttemptCs64)-SUM(DC.DC_E_RAN_UCELL_RAW.pmNoDirRetryAtt)))) AS '3G_CSSR_CS'
FROM
DC.DIM_DATE,
DC.DIM_TIME,
DC.DIM_E_RAN_UCELL,
DC.DC_E_RAN_UCELL_RAW
WHERE
(DC.DC_E_RAN_UCELL_RAW.HOUR_ID=DC.DIM_TIME.HOUR_ID and DC.DC_E_RAN_UCELL_RAW.MIN_ID=DC.DIM_TIME.MIN_ID)
AND (DC.DC_E_RAN_UCELL_RAW.DATE_ID=DC.DIM_DATE.DATE_ID)
AND (DC.DC_E_RAN_UCELL_RAW.OSS_ID=DC.DIM_E_RAN_UCELL.OSS_ID)
AND (DC.DC_E_RAN_UCELL_RAW.RNC=DC.DIM_E_RAN_UCELL.RNC_ID)
AND (DC.DC_E_RAN_UCELL_RAW.UtranCell=DC.DIM_E_RAN_UCELL.UCELL_ID)
AND
(
DC.DIM_DATE.DATE_ID IN ('2017-08-14')
AND
DC.DIM_E_RAN_UCELL.RBS_ID IN ('DN1U0441')
)
GROUP BY
DC.DIM_DATE.DATE_ID,
DC.DIM_E_RAN_UCELL.RBS_ID
The problem is with the last row of the SELECT statement, without it the result is this:
DATE_ID RBS_ID 3G_DATA_ERLANG 3G_SPEECH_ERLANG
8/14/2017 DN1U0441 421.8541 33.5055
When is included I got this error:
Lookup Error - Sybase Database Error: Invalid data conversion
Any Help? Is Sybase ASE DataBase

cakephp 3 sql query builder error

Here is my query:
$today = date('m/d/y');
$todays=$this->Tasks->find()
->where(['user_id' => $this->Auth->user('id')])
->andWhere(["DATE_FORMAT(STR_TO_DATE(due_date, '%m/%d/%Y'),'%m/%d/%y')"=>$today]);
$this->set('todays',$todays);
Here is the error im getting:
Error: SQLSTATE[42000]: Syntax error or access violation: 1064 You
have an error in your SQL syntax; check the manual that corresponds to
your MariaDB server version for the right syntax to use near
''05/16/17')' at line 1.
dont know why or how to fix this?
here is the query
SELECT
Tasks.id AS `Tasks__id`,
Tasks.user_id AS `Tasks__user_id`,
Tasks.title AS `Tasks__title`,
Tasks.body AS `Tasks__body`,
Tasks.due_date AS `Tasks__due_date`,
Tasks.complete AS `Tasks__complete`,
Tasks.created AS `Tasks__created`,
Tasks.modified AS `Tasks__modified`
FROM
tasks Tasks
WHERE
(
user_id = :c0
AND
DATE_FORMAT(STR_TO_DATE(due_date, '%m/%d/%y'), '%m/%d/%y') :c1
)
use capital Y instead of small y while calling the date () function. refer to
http://php.net/manual/en/function.date.php
for details of formatting dates.
But,
use the date datatype always for storing dates in mariadb. Then use $today = date ("Y-m-d"); Then you can simply compare them without converting and formatting
Since you said in your question's comments that your column is a string containing dates in d/m/Y format, you just need to compare two strings in your query.
$today = date('m/d/Y');
$todays = $this->Tasks->find()
->where(['user_id' => $this->Auth->user('id')])
->where(['due_date' => $today]);
$this->set('todays', $todays);
I recommend you to create a migration to change due_date column type
to date.

Syntax Error In Query with selectRaw and time diff

please advice how to correct this query
$no_of_hours = DB::Table('shifts')
->where('time_sheet_id','=', $timesheet_id->id)
->selectRaw("SELECT time(sum(TIMEDIFF( 'shift_end_time', 'shift_start_time' )))")
->get();
return $no_of_hours;
im getting following error
SQLSTATE[42000]: Syntax error or access violation: 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 'SELECT time(sum(TIMEDIFF( 'shift_end_time', 'shift_start_time' ))) from `shifts`' at line 1 (SQL: select SELECT time(sum(TIMEDIFF( 'shift_end_time', 'shift_start_time' ))) from `shifts` where `time_sheet_id` = 35)
You have a sintax error probably because you don't have to write the SELECT keyword in the selectRaw function ( the keyword is added implicity by the query builder in this case ):
->selectRaw("time(sum(TIMEDIFF( 'shift_end_time', 'shift_start_time' )))")

getting exception with SQLKorma for syntax: Failure to execute query with SQL

Any idea what is this exception trying to convey ?
I am using sqlKorma with Postgres 9.3.
I am trying to insert multiple rows at once in a table and I am getting a syntax error.
Now neither do I have a "0" anywhere in my query nor am I writing raw SQL to be messing up the syntax.
the korma docs are also not very clear.
Failure to execute query with SQL:
DO 0 :: []
PSQLException:
Message: ERROR: syntax error at or near "0"
Position: 4
SQLState: 42601
Error Code: 0
org.postgresql.util.PSQLException: ERROR: syntax error at or near "0"
Position: 4
Thats my code
(insert vendor-subsidiary-allowance
(values (vec (for [p (seq percentages)]
{:id (java.util.UUID/randomUUID)
:type "%"
:id_allowance_category (utils.uuid/from-str (p 0))
:id_vendor_subsidiary (allowance-form "vendor_sub_id")
:value (parse-number (p 1))}))))
NOTE: percentages is a dictionary with uuid strings as keys and value as some numeric string
Perhaps you want to read this to find the way to print the generated SQL to console.
You can use sql-only or dry-run macro to do this.
Print the generated SQL with sql-only or dry-run and check that the SQL is okay.

Advantage Database 8.1 SQL IN clause

Using Advantage Database Server 8.1 I am having trouble executing a successful query. I am trying to do the following
SELECT * FROM Persons
WHERE LastName IN ('Hansen','Pettersen')
To check for multiple values in a column. But I get an error when I try to execute this query in Advantage.
Edit - Error
poQuery: Error 7200: AQE Error: State = 42000; NativeError = 2115; [iAnywhere Solutions][Advantage SQL Engine]Expected lexical element not found: ( There was a problem parsing the
WHERE clause in your SELECT statement. -- Location of error in the SQL statement is: 46
And here is the SQL i'm executing
select * from "Pat Visit" where
DIAG1 IN = ('43644', '43645', '43770', '43771', '43772', '43773', '43774',
'43842', '43843', '43845', '43846', '43847', '43848', '97804', '98961',
'98962', '99078')
Done
Does anyone have any Idea how I could do something similar in advantage that would be efficient as well?
Thanks
You have an extraneous = in the statement after the IN. It should be:
select * from "Pat Visit" where
DIAG1 IN ('43644', '43645', <snip> )