mariadb Lead() function not accepting today's date as default parameter - mariadb

I am trying to use Lead/Lag function in mysql. In lead and lag function there is option to use default value for last non existent row. see documentation below
LEAD(expr [, N[, default]]) [null_treatment] over_clause
In above default value I want to use curdate() function but it is giving error
for example of my query is below
SELECT
AdmissionNo,
PatientShiftDate,
timestampdiff(DAY, PatientShiftDate,
lead(PatientShiftDate, 1 , NOW() )
over ( order by IPDBedAllocationNo) ) as NoofStay
FROM
PatientBedShift
it is giving error
#42000You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ' curdate() ) over ( order by IPDBedAllocationNo) ) as NoofStay FROM...' at line 5
I want to calculate number days patient stayed in each room. It is returning null instead it should use curdate(), now() or current_timestamp() function so I can get correct number of days patient stayed in single query.

Related

Is there a way to get more than 1 value using?

I have a better-sqlite3 statement that orders and ranks my database, and I have a IN statement so I can select more than 1 row. That is where I run into an issue, I need to fetch multiple rows based on a dynamic array of IDs.
My SQLITE Statement looks like this:
Table.prepare('SELECT *, RANK () OVER (ORDER BY amount DESC) rank FROM table WHERE user IN(?)');
And I try to get from this statement with things like this:
getAll.get(['1','2','3']);
getAll.get('6,9,4');
getAll.get('7','5','8');
I get an error:
RangeError: Too many parameter values were provided
How exactly can I select multiple values without knowing the length of my array (so ?,? won't cut it), and allow as much values as possible? I used ?* and I get a Syntax error.
I am using better-sqlite3 for Node.JS
Instead of IN use the operator LIKE:
SELECT *, RANK () OVER (ORDER BY amount DESC) rank
FROM table
WHERE ',' || ? || ',' LIKE '%,' || user || ',%'
and pass the list as 1 string of comma separated values (without spaces):
all('6,9,4');

Selected non-aggregate values must be part of the associated group. SELECT Command Failed

I am using the below query and it gives error that "Selected non-aggregate values must be part of the associated group. SELECT Command Failed."
SELECT TOP 100 X_ISP_AFF_ADDR_SEQ, CAST(COUNT(*) AS BIGINT) AS COUNT_ROW FROM S_CONTACT WHERE X_ISP_AFF_ADDR_SEQ NOT LIKE '%[a-zA-Z]%';
I changed the above query as written below but the error still persists.
SELECT TOP 100 X_ISP_AFF_ADDR_SEQ, CAST(COUNT(X_ISP_AFF_ADDR_SEQ) AS BIGINT) AS COUNT_ROW FROM S_CONTACT WHERE X_ISP_AFF_ADDR_SEQ NOT LIKE '%[a-zA-Z]%';
You presumably are missing a GROUP BY clause here:
SELECT TOP 100
X_ISP_AFF_ADDR_SEQ,
CAST(COUNT(*) AS BIGINT) AS COUNT_ROW
FROM S_CONTACT
WHERE
X_ISP_AFF_ADDR_SEQ NOT LIKE '%[a-zA-Z]%'
GROUP BY
X_ISP_AFF_ADDR_SEQ;
The exact error you were seeing with your original query has to do with that selecting X_ISP_AFF_ADDR_SEQ instructs Teradata to return a value for each record in the table, whereas COUNT() returns a value over the entire table. It is not possible (in general) to mix aggregates and non aggregates in a select clause.

In Cognos 10.2.1 - how can I use substring portions of an input parameter to create an expression to use for date comparison?

In Cognos 10.2.1 (FP7), I have a situation with two tables (see below), where a given transaction may be in the same "PERIOD", but what I want to do is to only give me results for transactions that have a date after the 15th of the month (i.e - the transaction may be in PERIOD 201510, and I only want transactons on/after 10/15/2015). The PERIOD is an input parameter that the user selects - I want to build the date from a portion of the PERIOD. (Before anyone complains about syntax, etc. - I've tried to simplify this as much as possible).
Given Table A:
ID varchar
PERIOD varchar
Given Table B:
ID varchar
TYPE varchar
TRANDATE timestamp2
Query1
[A.PERIOD]=?PARM1? <- this is the user selected input parameter
Query2
[B.TYPE] in ('A','B')
Then create a join from the results of Query1 and Query2 on the A.ID=B.ID to give Results1
I've tried the following (I'm keeping this as simple as I can - so I'm ONLY working with the YEAR portion of the period):
[Results1].[TRANDATE] >= concat(substring([Results1].[PERIOD],1,4),'-10-15T00:00:00.000000000')
[Results1].[TRANDATE] >= cast(concat(substring([Results1].[PERIOD],1,4),'-10-15T00:00:00.000000000'), timestamp2)
In both cases - Cognos won't validate the expression, or, if it does validate, I get a runtime error when running the report. In both cases, I get messages basically "literal does not match format string", even with the cast.
So - how can I get pieces/portions of the parameter, and slice/dice them to use as a date comparison as I mentioned above?
Given: Column [PERIOD] of type integer in the form YYYYMM, [TRANDATE] of type date/time
[TRANDATE] >= cast(cast(cast([PERIOD]/100,integer),varchar(4)) || '-' || cast(mod([PERIOD],100),varchar(2)) || '-15',date)
Given: Column [PERIOD] of type string in the form 'YYYYMM', [TRANDATE] of type date/time
[TRANDATE] >= cast(substring([PERIOD],1,4) || '-' || substring([PERIOD],5,2) || '-15',date)
The trick is that you don't have to build a full date/time string. You only have to build a string of format 'YYYY-MM-DD' in order to cast to a date type.

Stored procedure - A constant expression was encountered in the ORDER BY list, position 2

I have written a stored procedure below in MSSQL. It's throwing error in the ORDER BY clause.
a) It works fine if I put only one condition in ORDER BY clause.
b) I am not sure why it is executing both conditions of the CASE statement in the ORDER BY clause while executing the procedure. Ideally if the first condition is met, it should skip the second.
The error message is:
Error: Msg 408, Level 16, State 1, Line 3
A constant expression was encountered in the ORDER BY list, position 2.
This is the query:
SELECT *
FROM tb_officemaster
WHERE 1 = 1
AND officecity LIKE '%gurgaon%'
AND minprice > 100
AND maxprice < 1000
ORDER BY
CASE WHEN 'priceasc'='priceasc' THEN "minprice" END ASC,
CASE WHEN 'pricedesc'='priceasc' THEN minprice END DESC
Look at your CASE expression in your ORDER BY clause as below. Here the condition 'priceasc'='priceasc' is a constant expression and is always true no matter what and actually doesn't really makes any sense. It will not evaluate to anything. ORDER BY somehow has to evaluate the given expression to a row's value and order accordingly. In your case, THEN "minprice" is as well a constant.
ORDER BY CASE WHEN 'priceasc'='priceasc' THEN "minprice" END ASC
Per your posted query, the order by clause doesn't make sense and you can just omit it. Your query can simply be
select *
from tb_officemaster
where 1=1
and officecity like'%gurgaon%'
and minprice >100
and maxprice < 1000
Per your comment, change your ORDER BY to be like below
ORDER BY CASE WHEN pricedesc = priceasc THEN minprice END DESC

Doing datetime math in a function call in an Oracle query

Ok, I got the first part of my question answered, so here's the second part. :-) In a PLSQL query, I have criteria that looks like this:
where Doc3.clinicalDate >= ml.convert_date_to_id(:DateBegin)
and Doc3.clinicalDate < ml.convert_date_to_id(:DateEnd)
Now, I don't want to use :DateEnd itself -- I want to add 1 day so that when it compares the datetime to midnight, I get midnight of the next day. Unfortunately, when I do
where Doc3.clinicalDate >= ml.convert_date_to_id(:DateBegin)
and Doc3.clinicalDate < ml.convert_date_to_id(:DateEnd + 1)
I get "ORA-06553: PLS-306: wrong number or types of arguments in call to 'CONVERT_DATE_TO_ID'". ":DateEnd + interval '1' day" gives me "ORA-30081: invalid data type for datetime/interval arithmetic" (where :DateEnd is bound to 31-MAY-2012). If I do "convert_date_to_id(add_months(:DateEnd, 1))", it works fine. Any thoughts? Thanks.
ETA: I should clarify that this is an SSRS 2008 R2 project, and DateBegin and DateEnd are defined in the report parameters as DateTime parameters. My current workaround involves setting the :DateEnd query parameter equal to the #DateEnd report parameter + 1, but I'm worried that someday I'll forget to document this properly and confuse the heck out of whomever's trying to maintain the report (and it might be me). I don't want to pass string parameters, as suggested before.
I'm thinking that the parameters being DateTime is root of the problem. Microsoft DateTime datatypes are way more granular than Oracle's in that it supports fractional seconds and Oracle DATE format does not (Oracle TIMESTAMP does however).
Since ADD_MONTHS just spits back whatever it's passed in DATE datatype (i.e. passed TIMESTAMP becomes DATE). So maybe you can convert the parameter and add the day that way:
where Doc3.clinicalDate >= ml.convert_date_to_id(:DateBegin)
and Doc3.clinicalDate < ml.convert_date_to_id(CAST(:DateEnd as DATE)+1)
Alternatively, forget about conversions and date arithmetic on the parameters and subtract a day from the second clinicalDate:
where Doc3.clinicalDate >= ml.convert_date_to_id(:DateBegin)
and Doc3.clinicalDate - 1 < ml.convert_date_to_id(:DateEnd)
Assuming that ml.convert_date_to_id takes a DATE as an input parameter rather than a VARCHAR2 that represents a date, and assuming that the :DateEnd bind variable is a VARCHAR2, you would need something like
ml.convert_date_to_id( to_date( :DateEnd, 'DD-MON-YYYY' ) + 1 )
or
ml.convert_date_to_id( to_date( :DateEnd, 'DD-MON-YYYY' ) + interval '1' day )
Use to_date to convert the value. For example:
select &date + 1 from dual
Informing to_date('29052012','ddmmyyyy') works fine
Informing '29-may-2012' gives ORA-01722: invalid number

Resources