Month Differences in qlikview - expression

There is 2 dates column one is from date and second is to date .. and i want to get month difference from these two dates
like if
from date to date month difference
01-02-2019 02-02-2020 13
here 02 (feb) month 2019 till 02 (feb) moth 2020 so this means total 13 months covered..
i tried this but this shows wrong results
month(from date) - month(to date)
and i also try this
month([from date] - [to date])

I've been using the code below for this case.
It basically converts both dates to months and returns the difference.
First the Year component of the date is "converted" to months (year([to date]) * 12 part) and second adds the month number of the date (month([to date])
Num (
( (year([to date]) * 12) + month([to date]) )
- ( ((year([from date]) * 12) + month([from date])) ) + 1
)
UPDATE:
below is a screenshot of the result table with 2 expressions - including the +1 and excluding it. Depends how you want to calculate the full months +1 will "include" the last month as well

Related

Oracle NUMTODSINTERVAL function and interval function

Can someone please explain what the below is doing?
and effective_DATE < (TRUNC(CURRENT_DATE) - NUMTODSINTERVAL(EXTRACT(DAY FROM TRUNC( CURRENT_DATE)), 'DAY' ) + INTERVAL '1' DAY)
I don't have access to the database so can not run it myself to test.
What I understand is that the NUMTODSINTERVAL converts a number to an INTERVAL DAY TO SECOND literal.
So say if I ran this query today it would check to see if the effective date is less than the 5th of Feb 2019 - 5 days converted to seconds plus 1 day?
So, 5th Feb - 000000005 + 1 = 6th Feb?
Is this correct or am I looking at this wrong?
Also why would developers use this method?
The second argument to NUMTODSINTERVAL specifies the unit. "INTERVAL DAY TO SECOND" is a confusing name - it just means an abstract interval of time, in this case 5 days.
EXTRACT(DAY FROM TRUNC( CURRENT_DATE)) finds the current day of the month (5 today)
NUMTODSINTERVAL(5, 'DAY') creates an INTERVAL of +5 days
So today it's saying, take Feb 5, subtract 5 days (which = Jan 31), then add one day (so Feb 1).
It seems to be trying to find the first day of the month, which would be much simpler as:
and effective_DATE < TRUNC(CURRENT_DATE, 'MM')

Oracle sql how to get the date of a week

I have the following query that gets the week of a date:
SELECT pdm.serie, rta.matricula_ant, TO_CHAR (fecha, 'ww') semana,
SUM (rta.kms_acumulados) kms,
COUNT
(DISTINCT (CASE
WHEN v.secuencia BETWEEN rta.sec_origen AND rta.sec_destino
THEN v.cod_inc
ELSE '0'
END
)
)
- 1 numincidencias
FROM (SELECT ms.tren, ms.fecha_origen_tren, ms.secuencia, ri.cod_inc
FROM r_incidencias ri, mer_sitra ms
WHERE ri.cod_serv = ms.tren
AND ri.fecha_origen_tren = ms.fecha_origen_tren
AND ri.cod_tipoin IN (SELECT cod_tipo_iincidencia
FROM v_tipos_incidencias
WHERE grupo = '45')
AND ri.punto_desde = ms.cod_estacion) v,
r_trenes_asignar rta,
r_maquinas rm,
planificador.pl_dh_material pdm
WHERE rta.fecha BETWEEN TO_DATE ('21/09/2018', 'dd/mm/yyyy') AND TO_DATE ('21/09/2018',
'dd/mm/yyyy'
)
AND rta.serie >= 4000
AND rta.matricula_ant IS NOT NULL
AND rm.matricula_maq = rta.matricula_ant
AND rm.cod_serie = pdm.id_material
AND rta.grafico BETWEEN pdm.desde AND pdm.hasta
AND v.tren(+) = rta.tren
AND v.fecha_origen_tren(+) = rta.fecha
GROUP BY pdm.serie, rta.matricula_ant, TO_CHAR (fecha, 'ww')
ORDER BY pdm.serie, rta.matricula_ant, TO_CHAR (fecha, 'ww')
For example week 1
I want to display
week 1 : 1 january - 7 january
How can I get this?
Oracle offers the TRUNC(datestamp, format) function to manipulate dates this way. You may use a variety of format strings to get the first day of a quarter, year, or even the top of the hour.
Given a particular datestamp value, Oracle returns midnight on the first day of the present week with this expression:
TRUNC(datestamp,'DY')
You can add days to a datestamp. Therefore this expression gives you midnight on the last day of the week
TRUNC(datestamp,'DY') + 6
A WHERE-clause selector for all rows in the present week might be this.
WHERE datestamp >= TRUNC(SYSDATE,'DY')
AND datestamp < TRUNC(SYSDATE,'DY') + 7
Notice that the end of the range is just before (<) midnight on the first day of the next week. You need that because you may have datestamps after midnight on the last day of the week. (Beware using BETWEEN for datestamp ranges.)
And,
SELECT TO_CHAR(TRUNC(SYSDATE,'DY'),'YYYY-MM-DD'),
TO_CHAR(TRUNC(SYSDATE,'DY')+6,'YYYY-MM-DD')
FROM DUAL;
displays the first and last dates of the present week in ISO-like format.
Date arithmetic is cool. It's worth your trouble to study the date-arithmetic functions in your DBMS at least once a year.

Date Logic - Tableau

Could someone help me achieve the date logic as below:
I work for a fiscal year starting from Oct to Sep, once I finish the fiscal year and step into a new one I want the last month of last fiscal year's sales for reference until the end of new fiscal year. For example, now in Sep'17 the fiscal year ended but I want Sep'17 sales number to be shown in graph as reference until next Sep'18 later that I want Sep'18 number to be shown until Sep'19 and so on so forth.
The logic I have arrived is not a permanent solution as it requires editing once I step into Year 2018, it is as below:
IF YEAR([Invoice Date]) = YEAR(TODAY()) AND
DATEPART('month', [Invoice Date]) = 9 THEN [Sales] END
once I step into Year 2018, I need to make change to the above logic like:
IF YEAR([Invoice Date]) = YEAR(TODAY())**-1** AND
DATEPART('month', [Invoice Date]) = 9 THEN [Sales] END
Is there a way to achieve permanent solution without editing the logic?
Try this:
Create 2 calcualted fields for start date and end date, Where start date is september last year and end date is september current year.
Start Date:
DATEADD('month',-1,MAKEDATE(YEAR(TODAY())-1,MONTH(TODAY()),01 ) )
End Date:
DATEADD('month',-1,MAKEDATE(YEAR(TODAY()),MONTH(TODAY()),01 ) )
Now one more calcualted field which will create the filter and add the formula to filter to get the require data.
[Order Date]>=[Start Date] AND
[Order Date] <=[End Date]
Add to filter and then select True
Note: Here today function means start of the fiscal year that you need to manage.

select unique trunc(sysdate-370 + level, 'IW') AS datetime from dual connect by level <= 360 order by datetime

Can someone explain me what does the below oracle query do and what is it's output?
select unique trunc(sysdate-370 + level, 'IW') AS datetime from dual
connect by level <= 360 order by datetime;
select sysdate-370 + level AS datetime
from dual
connect by level <= 360;
Will generate 360 rows starting with the current date/time minus 370 days plus one day per row. So rows between 369 and 10 days before the current date/time.
TRUNC( datetime, 'IW' ) will truncate the date to the start of the ISO week (midnight on Monday of that week - irrespective of the NLS settings for date language and/or territory that affect some other options for truncating dates). So you will end up with duplicate rows for each generated row that is in the same week.
The UNIQUE keyword will get rid of those duplicate rows.
The order by datetime will order the results in ascending date order - however, the rows are generated in ascending order so this clause is unnecessary.
So the output will be 52 or 53 rows (depending on what the current day of the week is) starting with Monday midnight of each week containing the date 369 days before the current day up until the week containing 10 days before the current date.
The output (when run on 13th September 2017) is 52 rows (I skipped a few):
05-SEP-2016
12-SEP-2016
19-SEP-2016
26-SEP-2016
03-OCT-2016
...
31-JUL-2017
07-AUG-2017
14-AUG-2017
21-AUG-2017
28-AUG-2017
According to documentation trunc(dateval, 'IW') truncates to:
Same day of the week as the first day of the calendar week as defined by the ISO 8601 standard, which is Monday
connect by level <= N is a trick for producing a set of N rows with level values from 1 to N.

Change the year of a date to the current year in PL/SQL

I'm currently trying to do a comparison in my select. If the current date is before August 1st of the current year then display august 1st of the last year, otherwise display august 1st of this year. Essentially I'm trying to do:
CASE
WHEN (SYSDATE < 08/01/2015) THEN
08/01/2014
ELSE
08/01/2015
But I am at a loss as to how to get august for the month. So far I have:
TRUNC(SYSDATE, 'MON')
To get /01/ but how would I get it to constantly return august as the month? Would it be better to hardcode in the date and month and dynamically get the year instead? like 01/08/
Try something like this:
1 select sysdate,
2 trunc(sysdate,'YEAR'),
3 add_months(trunc(sysdate,'YEAR'),7),
4 add_months(trunc(sysdate,'YEAR'),7-12)
5* from dual
SQL> /
SYSDATE TRUNC(SYSDA ADD_MONTHS( ADD_MONTHS(
----------- ----------- ----------- -----------
31-jul-2015 01-jan-2015 01-aug-2015 01-aug-2014
SQL>
the columns are:
1) pulling the current sysdate.
2) converting to the first day of the year.
3) adding 7 months to get Aug 1 of current year.
4) -12 months to get Aug 1 of last year.
(that shows you the usage, you can figure out how to plug those suckers into your CASE statement ;) )

Resources