MySQL server version for the right syntax group by datetime - datetime

I have been trying to solve the issue to group by datetime for sometimes but I still can't resolve it.
Please give some advice on what wrong with my syntax?
Sytnax error message i get:-
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_FORMAT(mdatetime,'%Y-%m-%d') AS 'Date',count(*) AS 'Total Registrants' fro' at line 1
select $regdate AS 'Date',count(*) AS 'Total Registrants' from $regTable where $regdate <= '$date' group by $regdate order by $regdate DESC LIMIT 10

Related

PeopleSoft Power Query convert inside of a Case statement

Okay I'm using PeopleSoft's Query Manager trying to write an expression. The original expression converted a date format into DD MON YY, here's the original expression that works:
CONVERT(VARCHAR(10), A.TERMINATION_DT, 103) AS [DD MON YY]
I need to add termination dates for a few people, manually (they're not termed yet, but will be, so I have to input it like this).
So I thought I could do a case statement:
(CASE WHEN B.EMPLID IN ('XXXXXX') THEN '2022-08-19' ELSE (CONVERT(VARCHAR(10), A.TERMINATION_DT, 103) AS [DD MON YY]) END)
When I input the above I get the following error:
A SQL error occurred. Please consult your system log for details.
Error in running query because of SQL Error, Code=8603,
Message=[Microsoft][ODBC Driver 13 for SQL Server][SQL
Server]Incorrect syntax near the keyword 'AS'. [Microsoft][ODBC Driver
13 for SQL Server][SQL Server]Incorrect syntax near 'PS_JOB'.
[Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Incorrect syntax
nea란ᆌŪ (50,380)
Where am I going wrong?
The problem with your edit of the query is that you put AS [DD MON YY] statement, which sets the alias for the column, into the CASE statement.
It should be after the entire CASE statement.
Provided that the rest of the syntax is correct, it should be:
(CASE
WHEN B.EMPLID IN ('XXXXXX') THEN '2022-08-19'
ELSE CONVERT(VARCHAR(10), A.TERMINATION_DT, 103)
END) AS [DD MON YY]
Okay I figured it out. Not terribly difficult in the end.
(CASE WHEN B.EMPLID IN ('XXXXXX') THEN '2022-08-19' ELSE (CONVERT(VARCHAR(10), A.TERMINATION_DT, 103) ) END)

SQR - how to use FROM [dynamic table name} within BEGIN-SELECT?

I have to create an SQR that generates a list of EEIDs, if there were any changes to the Pension data in the past day. The SQR compiles and works perfectly when I hardcode in the table names.
However, when I tried using variables for the table names, I get a compile error
I've pasted the portion of SQR that I'm trying to fix
When I start using $tableName and $auditTableName as table variables, that's when I get the error and I'm not sure what is going wrong
Can anyone help?
Please and Thank You
!***************************
begin-procedure Process-Main
!***************************
let $tableName = 'PS_PENSION_PLAN'
let $auditTableName = 'PS_AUDIT_PENSION_PLN'
let $dummy-dyn-variable = ''
begin-SELECT DISTINCT
L.EMPLID
L.EMPL_RCD
do someProcName(&L.EMPLID, &L.EMPL_RCD)
FROM [$dummy-dyn-variable]
(
SELECT DISTINCT
PP.EMPLID,
PP.EMPL_RCD,
PP.EFFDT,
'1901-01-01 12:00:00' AS AUDIT_STAMP
FROM [$dummy-dyn-variable] [$tableName] PP
UNION
SELECT DISTINCT
A.EMPLID,
A.EMPL_RCD,
A.EFFDT,
A.AUDIT_STAMP
FROM [$dummy-dyn-variable] [$auditTableName] A
)L
WHERE DATEDIFF(DAY,CAST(L.AUDIT_STAMP AS DATE),SYSDATE) = 1
ORDER BY 1,2
end-SELECT
end-procedure
Edit:
does the UNION have anything to do with this?
I keep receiving is this error:
(SQR 5528) ODBC SQL dbdesc: SQLNumResultCols error 102 in cursor 1:
[Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near 'FROM'.
(SQR 5528) ODBC SQL dbdesc: SQLNumResultCols error 8180 in cursor 1:
[Microsoft][ODBC SQL Server Driver][SQL Server]Statement(s) could not be prepared.
Edit2:
Ok, initial problem solved with [$dummy-dyn-variable], which led to the next problem with the DO command. I've updated the code above with DO someProcName(param_a, param_b)
I am now getting an error saying:
(SQR 2002) DO arguments do not match procedure's
Weird part, if I remove the dynamic table variables and hardcode the table names in the FROM section, then it compiles properly without errors. This makes me believe that the error is not related to my someProcName (maybe?)
am I missing something here?

MariaDB SphinxSE not accepting weights parameter

If I try this query:
select * FROM sphinx.products where `query` = "test";
it works. But if I try to give it weights it returns an error:
select * FROM sphinx.products where `query` = "test;sort=extended:#weight DESC;weights=3,1,1,1";
Fails with error:
Error in query (1430): There was a problem processing the query on the foreign data source.
Data source error: searchd error: invalid deprecated unordered_weight count 4 (expe
(Error reported by MariaDB gets truncated there, but I believe it says "expecting 0")
And:
select * FROM sphinx.products where `query` = "test;sort=extended:#weight DESC";
Fails with error:
Error in query (1430): There was a problem processing the query on the foreign data source.
Data source error: searchd error: index 'sku_products': sort-by attribute '#weight'
(Again, error returned by SphinxSearch gets truncated by MariaDB)
All the documentation I find about SphinxSE tells me to query the index this way, yet it does not work, but nobody in the Internet seem to have met this error since nobody is asking about this anywhere...
Am I doing something wrong?
Well, the option weights= didn't work, but it accepted fieldweights=sku,90,partnumber,30,barcode,20,name,10.
(I.e., fieldweights=<field1_name>,<field1_weight>,...)
Results came ordered by weight even without specifying sort=extended:#weight DESC, so I dodged both errors and got what I needed.
Hope this helps anyone in the same situation.

How might I get detailed database error messages from dplyr::tbl?

I'm using R to plot some data I pull out of a database (the Stack Exchange data dump, to be specific):
dplyr::tbl(serverfault,
dbplyr::sql("
select year(p.CreationDate) year,
avg(p.AnswerCount*1.0) answers_per_question,
sum(iif(ClosedDate is null, 0.0, 100.0))/count(*) close_rate
from Posts p
where PostTypeId = 1
group by year(p.CreationDate)
order by year(p.CreationDate)
"))
The query works fine on SEDE, but I get this error in the R console:
Error: <SQL> 'SELECT *
FROM (
select year(p.CreationDate) year,
avg(p.AnswerCount*1.0) answers_per_question,
sum(iif(ClosedDate is null, 0.0, 100.0))/count(*) close_rate
from Posts p
where PostTypeId = 1
group by year(p.CreationDate)
order by year(p.CreationDate)
) "zzz11"
WHERE (0 = 1)'
nanodbc/nanodbc.cpp:1587: 42000: [FreeTDS][SQL Server]Statement(s) could not be prepared.
I reckoned "Statement(s) could not be prepared." meant that SQL Server didn't like the query for some reason. Unfortunately, it didn't give any hint about what went wrong. After fiddling with the query for a bit, I noticed it was wrapped in a subselect, according to the error message. Copying and executing the full query as constructed by one of the libraries in the chain, SQL Server gave me this more informative error message:
The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP, OFFSET or FOR XML is also specified.
Now the solution is obvious: remove (or comment out) the order by clause. But where is the detailed error message in the R console? I'm using Rstudio, should that matter. If I could get the full exception right next to the code I'm working on, it would help me fix bug a lot quicker. (And just to be clear, I get cryptic errors from dplyr::tbl often and typically use binary search debugging to fix them.)

ORA-00907: missing right parenthesis, on Oracle 10 and not on Oracle 11

Why the following query fails on Oracle 10 an not on Oracle 11.
SELECT trunc(DBMS_RANDOM.value(low => 10, high =>50)) from dual;
Oracle 10:
ORA-00907: missing right parenthesis
This answer is a bit speculative, but one possible explanation for the missing right parentheses error is that this error is not really about missing parentheses. Instead, if the API for DBMS_RANDOM.value is different in your version of Oracle 10 vs. Oracle 11 then you could be seeing this error. Try this query instead:
SELECT TRUNC(DBMS_RANDOM.value(10, 50))
FROM dual
If this works, then you will know that the API has changed between Oracle 10 and 11.
Here is a reference which uses the API as I have in my query.
This was a new feature in 11gR1:
Beginning in this release, it is now possible to invoke the function
in a SQL statement. For example, named notation syntax is:
SELECT f(pn=>3, p2=>2, p1=>1) FROM dual
Or, mixed notation is:
SELECT f(1, pn=>3) FROM dual
In previous releases, attempting named or mixed notation resulted in
an error.
So prior to 11g you could only call a PL/SQL function from SQL using positional notation, e.g.
SELECT trunc(DBMS_RANDOM.value(10, 50)) from dual;
... as #TimBiegeleisen has already shown works. The 'missing right parenthesis' error doesn't necessarily mean your parentheses are unbalanced; just that the parser saw something it didn't expect where it thought a parenthesis might go - in this case, at the =>.

Resources