I am getting wrong result while calculating date difference between two dates using saiku.
/*** Query ***/
With Member Measures.DD as
DateDiff('d',CDate('Mar 20, 2014'),CDate('Apr 19, 2014'),3,3)
select {Measures.DD,Measures.DF} on Columns
from [Cube_AR]
/*** Output ***/
1
/*** Expected Output ***/
30
Try fuller dates and also I usually use double quotes around my "D".
With Member Measures.DD as
DateDiff("D",CDate('March 20, 2014'),CDate('April 19, 2014'),3,3)
select {Measures.DD,Measures.DF} on Columns
from [Cube_AR]
3 is a strange day to have as the first day of the week?
Related
Please can someone help me with the SQL code that list all the data for the 3 Employee ID's:
60-578-2269, 50-218-3739 and 80-772-4580 ? I was only able to pull the data for one Employee ID below:
SELECT *
FROM [dbo].Employee_Certification
WHERE Employee_ID = '60-578-2269'
Also, the sql statement that list every employee that was hired after 1st April 2021, in date format 04/01/2021. I ran this:
SELECT *
FROM [dbo].Employee_Certification
WHERE Date_Hired> = 01/04/2021
but the error message below came up:
Msg 245, Level 16, State 1, Line 23 Conversion failed when converting
the nvarchar value '4/1/21' to data type int.
For the first query use WHERE IN (...) with a tuple of employee IDs which you want to find:
SELECT *
FROM [dbo].Employee_Certification
WHERE Employee_ID IN ('60-578-2269', '50-218-3739', '80-772-4580');
For the second query, use a proper date literal which SQL Server will recognize:
SELECT *
FROM [dbo].Employee_Certification
WHERE Date_Hired >= '20210401';
I want to get the 1st, 2nd, 3rd,...largest value of Time in SQL Server 2018's table.
In my code when I get the 3rd largest, if my month condition is 5, it returns correct value. But if my month condition is 4, it returns the max time value not my expected time value ( 3rd ). I don't know why.
My query:
SELECT MAX([Time1].[Time]) FROM [dbo].[index4_MonthChart] as [Time1]
WHERE
Month([Time]) = '4'
AND [Time1].[Time] < ( SELECT MAX([Time2].[Time]) FROM [dbo].[index4_MonthChart] as [Time2]
WHERE
[Time2].[Time] < ( SELECT MAX([Time3].[Time]) FROM [dbo].[index4_MonthChart] as [Time3]))
All my value in table:
When month(time) condition is 5, it returns:
But when month(time) is 4, it always returns the max time value and it is wrong ( The correct time value have to be 2023-04-28 ) :
Very simple. You need to add a where clause in your sub-selects to restrict those to Month= 4 as well. Since there are at least two times greater than 30.04 (IE all the times from May), your current query returns the maximum time for April. This doesn't happen for May, because your data has no values later than May.
I have been trying to retrieve the rows registered between two dates (since every time a person goes through a door a row is inserted). This way we get the "amount of visitors" on a monthly basis.
When querying the following:
select MOV_DATAHORA from LOG_CREDENCIAL
WHERE MOV_DATAHORA>'2017-01-01'`
a total of 5851 rows are shown. This is an example of a row:
2017-01-05 21:33:30.000
However when trying the following:
select MOV_DATAHORA from LOG_CREDENCIAL
WHERE MOV_DATAHORA>'2017-01-01'
AND MOV_DATAHORA<'2017-01-02'
0 rows are shown. I even tried the count(MOV_DATAHORA) statement but it still isn't working. I also tried with the BETWEEN statement unsuccessfully. Any ideas why?
Well... the simplest explanation based on your query predicate:
WHERE MOV_DATAHORA > '2017-01-01'
AND MOV_DATAHORA < '2017-01-02'
is that on January 1st (right after the New years) there were no visitors.
You are comparing a DateTime and a Date data types. Your table has a DateTime as you can see the 21:33:30.000 after the date, to convert and compare like types do the following.
SELECT MOV_DATAHORA FROM LOG_CREDENCIAL WHERE DATE(MOV_DATAHORA)>'2017-01-01'
AND DATE(MOV_DATAHORA)<'2017-01-02'
or using between
SELECT MOV_DATAHORA FROM LOG_CREDENCIAL WHERE DATE(MOV_DATAHORA) BETWEEN '2017-01-01'
AND '2017-01-02'
I am trying to select next 30 dates for an input date parameter, i.e. if I enter 2/3/2016 I should be able to select next 30 days. Is there a way to do it in oracle ?
The expected output is :
3/3/2016
4/3/2016
5/3/2016....
Try the following, replacing sysdate with your start date:
select trunc(sysdate) + level
from dual
connect by level <= 30
This is based on hierarchical queries and on the fact that adding a number to a date means adding days.
LEVEL is a pseudo-column, disposable where you use CONNECT BY, which indicates, as it seems, the level in recursion; so, the first occurrence has level 1, then 2, and so on.
Here are some more details on syntax and on the way this implements recursion.
Given that we have recursion on DUAL, with no conditions ( no PRIOR clause used), we use LEVEL both to sum 1, 2, 3 ( + LEVEL) days and to limit the number of recursione levels we need ( <=30)
I have a table po010 in which columns are polref, POLCRCDTE,PUPDTE.
I have to fetch polref from table po010 where , difference of months between POLCRCDTE and PUPDTE is ateast 14 months.
My tries are .
SELECT polref,POLCRCDTE,PUPDTE FROM po010,
GROUP BY polref
HAVING months_between(pck_utility.f_dtcnv(POLCRCDTE),pck_utility.f_dtcnv(PUPDTE))>14
Note: POLCRCDTE and PUPDTE are number formats ,
and pck_utility.f_dtcnv returns date in DATE format taking number format as input.
Second try is
DECLARE
sysdte DATE;
req_dt DATE;
BEGIN
SELECT system_dt INTO sysdte FROM cs340;
SELECT add_months(sysdte, -14) INTO req_dt FROM po010;
dbms_output.put_line(req_dt);
select * from po010 where pupdte <= pck_utility.f_dtcnv((add_months (sysdte ,-14)));
END;
Note : here taking system_dt from cs340 because in other scenario , i have to compare the dates between those two.
Please help me out.
Both the codes are giving error.
first one error is ORA 00900
second one is giving ORA 06550
.
Basically i need this resultset in a cursor that i need to use in a procedure.
HAVING is used to filter results based on an aggregate, months_between() is not an aggregate function.
Perhaps you just want to use WHERE:
SELECT DISTINCT polref,POLCRCDTE,PUPDTE
FROM po010
WHERE months_between(pck_utility.f_dtcnv(POLCRCDTE)
,pck_utility.f_dtcnv(PUPDTE)
)>14
You typically need to GROUP BY every non-aggregate field in your SELECT list, I'm not sure what you were after with your GROUP BY clause, so I removed it and used DISTINCT in the SELECT list.
Thank you for your support,b it is solved now.
I can use like this.
SELECT DISTINCT p.polref,
p.polcrcdte,
c.system_dt
FROM ddl00.po010 p,
ddl00.cs340 c
WHERE Months_between(pck_utility.F_dtcnv(polcrcdte), system_dt) >= 14