I have this table:
Year
Month
Agency
Value
2019
9
1
233
2019
9
4
132
2019
8
3
342
2020
3
2
321
2020
3
4
34
2020
5
2
56
2020
5
4
221
2020
5
1
117
2018
12
2
112
2018
12
2
411
2020
4
3
241
2020
4
2
155
I'd like to set a new measure/column where last month from last year is 1, and 0 in another cases:
Year
Month
Agency
Value
Filter
2019
9
1
233
0
2019
9
4
132
0
2019
8
3
342
0
2020
3
2
321
0
2020
3
4
34
0
2020
5
2
56
1
2020
5
4
221
1
2020
5
1
117
1
2018
12
2
112
0
2018
12
2
411
0
2020
4
3
241
0
2020
4
2
155
0
I've been able to "copy" a new table with values from Month=5 and Year=2020 ("the lastest from the lastest"):
TableData - Last Charge =
var table = FILTER(
TableData,
AND(
MAX('TableData '[Year])='TableData '[Year],
MAX('TableData '[Month])='TableData '[Month]
)
)
return SUMMARIZE(table , TableData [Year], TableData [Month], TableData [Agency], TableData [Value])
However, my intention is don't create new tables and use measures/columns tu use it like FILTER when I create a graphic.
Thanks a lot, and sorry for my poor english.
I solved it with this measure:
Measure =
VAR a =
MAX ( 'Table'[Year] )
VAR b =
MAX ( 'Table'[Months] )
VAR c =
MAXX ( ALL ( 'Table' ), [Year] )
VAR d =
MAXX ( FILTER ( ALL ( 'Table' ), [Year] = c ), [Months] )
RETURN
IF ( a * 100 + b = c * 100 + d, 1, 0 )
Related
We've run an Interrupted Time Series analysis on some aggregate count data using a Poisson regression. Code shown below - where Subject Total is the count, Quarter is time, int2 is the dummy variable for the intervention [0 pre, 1 post] and time_since_intervention2 the dummy variable for time since intervention [0 pre, 1:N post].
fit1a <- glm(`Subject Total` ~ Quarter + int2 + time_since_intervention2 , df, family = "poisson")
Quarter Subject Total int2 time_since_intervention2 subjectfit subcounter
1 1 34 0 0 34.20968 34.20968
2 2 32 0 0 33.39850 33.39850
3 3 36 0 0 32.60656 32.60656
4 4 34 0 0 31.83339 31.83339
5 5 23 0 0 31.07856 31.07856
6 6 34 0 0 30.34163 30.34163
7 7 33 0 0 29.62217 29.62217
8 8 24 0 0 28.91977 28.91977
9 9 31 0 0 28.23402 28.23402
10 10 32 0 0 27.56454 27.56454
11 11 21 0 0 26.91093 26.91093
12 12 26 0 0 26.27282 26.27282
13 13 22 0 0 25.64984 25.64984
14 14 28 0 0 25.04163 25.04163
15 15 28 0 0 24.44784 24.44784
16 16 22 0 0 23.86814 23.86814
17 17 14 1 1 17.88365 23.30218
18 18 16 1 2 17.01622 22.74964
19 19 20 1 3 16.19087 22.21020
20 20 19 1 4 15.40556 21.68355
21 21 13 1 5 14.65833 21.16939
22 22 15 1 6 13.94735 20.66743
23 23 16 1 7 13.27085 20.17736
24 24 8 1 8 12.62717 19.69892
Due to the need to exponentiate the outcome the summary is currently being derived using the margins package.
> summary(margins(fit1a))
factor AME SE z p lower upper
int2 -5.7843 5.1734 -1.1181 0.2635 -15.9241 4.3555
Quarter -0.5809 0.2469 -2.3526 0.0186 -1.0649 -0.0970
time_since_intervention2 -0.6227 0.9955 -0.6255 0.5316 -2.5738 1.3285
If reading the outcome correctly it would suggest that the level change between the final quarter in the pre-intervention period and first in the post-intervention period is -5.7843.
I've tried inputting coefficient values into my model [initial intercept = 35.0405575], but they don't appear to correlate at all with the subjectfit data, which I believed it would. Should the level change reported by the margins package replicate the difference in the full data.....?
I've been recently asked to do a simple exercice to calculate the difference between 2 dates.
first date is the birth date, second date is the actual date (or any date entered as the end date) let's call it "end date".
Format: Year, Month, Day (we don't care about hours for now)
Here is the deal :
The birth date : 2021, 02, 11
The end date : 2022, 02, 10
The difference between these 2 dates is : 11 months and 30 days. That's seems the logic answer but let's try to get a little bit deeper :
11/02 -> 11/03 = 1 month : 17 + 11 = 28 }
11/03 -> 11/04 = 1 month : 20 + 11 = 31 |
11/04 -> 11/05 = 1 month : 19 + 11 = 30 |
11/05 -> 11/06 = 1 month : 20 + 11 = 31 | From February
11/06 -> 11/07 = 1 month : 19 + 11 = 30 | To January
11/07 -> 11/08 = 1 month : 20 + 11 = 31 | 11 months
11/08 -> 11/09 = 1 month : 20 + 11 = 31 | => 334 days
11/09 -> 11/10 = 1 month : 19 + 11 = 30 |
11/10 -> 11/11 = 1 month : 20 + 11 = 31 |
11/11 -> 11/12 = 1 month : 19 + 11 = 30 |
11/12 -> 11/01 = 1 month : 20 + 11 = 31 }
11/01 -> 10/02 ------------> 20 + 10 = 30
This is the reasoning behind the 11 months and 30 days, but there is another approach (correct me if I'm wrong) which is :
the starting stays the same, which is 11/02 so the rest is :
02/2021: 17/28
03/2021: 31/31 }
04/2021: 30/30 |
05/2021: 31/31 |
06/2021: 30/30 |
07/2021: 31/31 |
08/2021: 31/31 | 11 months
09/2021: 30/30 | => 337 days
10/2021: 31/31 |
11/2021: 30/30 |
12/2021: 31/31 |
01/2022: 31/31 }
02/2022: 10/28
There is a difference of 3 days between the 2 dates when using the 2nd approach, and therefore the difference will be
11 month and 27 days.
Which approach do you think is the right one ?
I have this query, which works:
SELECT TO_CHAR(last_date_called,'HH24'), count(*)
FROM log_table
GROUP BY TO_CHAR(last_date_called,'HH24');
But, in some cases there are not 24 hours worth of data. What I want to do, is always generate 24 rows, and if there is nothing for that hour, return 0. So, results may look like this:
00 10
01 25
02 33
03 0
04 55
05 0
06 23
And so on........
You'll need a row generator to create all hours in a day, and then outer join it to your "real" table. Something like this (see comments within code):
SQL> with
2 hours as
3 -- row generator, to create all hours in a day
4 (select lpad(level - 1, 2, '0') hour
5 from dual
6 connect by level <= 24
7 ),
8 log_table (last_date_called) as
9 -- sample data, just to return "something"
10 (select to_date('08.07.2021 13:32', 'dd.mm.yyyy hh24:mi') from dual union all
11 select to_date('16.02.2021 08:20', 'dd.mm.yyyy hh24:mi') from dual
12 )
13 -- final query
14 select h.hour,
15 count(l.last_date_called) cnt
16 from hours h left join log_table l on h.hour = to_char(l.last_date_called, 'hh24')
17 group by h.hour
18 order by h.hour;
HO CNT
-- ----------
00 0
01 0
02 0
03 0
04 0
05 0
06 0
07 0
08 1
09 0
10 0
11 0
12 0
13 1
14 0
15 0
16 0
17 0
18 0
19 0
20 0
21 0
22 0
23 0
24 rows selected.
SQL>
I have the below table and I am trying to write an Oracle select query that would result in the second table.
I understand I have to use Pivot but I can't figure at all how to do.
MSGID
KEY
COLVALUE
15
height
18
15
length
19
15
width
20
15
notImportant
xxx
16
height
21
16
length
22
16
width
23
16
notImportant
xxx
17
height
24
17
length
25
17
width
26
17
notImportant
xx
Desired result:
MsgID
height
length
width
15
18
19
20
16
21
22
23
17
24
25
26
Tried the below code but without success....
select MSGID, HEIGHT, LENGTH, WIDTH
from (select MSGID, KEY, COLVALUE
from table )
PIVOT
(
max(COLVALUE)
FOR KEY IN ('HEIGHT','LENGTH','WIDTH')
)
Do you have any tips?
Here's one option:
SQL> select msgid,
2 max(case when key = 'height' then colvalue end) height,
3 max(case when key = 'length' then colvalue end) length,
4 max(case when key = 'width' then colvalue end) width
5 from test
6 group by msgid
7 order by msgid;
MSGID HEIGHT LENGTH WIDTH
---------- ---------- ---------- ----------
15 18 19 20
16 21 22 23
17 24 25 26
SQL>
Or, with pivot:
SQL> select *
2 from test
3 pivot
4 (max(COLVALUE)
5 FOR KEY IN ('height','length','width')
6 )
7 order by 1;
MSGID 'he 'le 'wi
---------- --- --- ---
15 18 19 20
16 21 22 23
17 24 25 26
SQL>
I have a problem trying to group some records and trying to count the number of records returned by a query is adding an example:
07 COMERCIO 92
15 SERVICIOS OTROS 41
01 AGRICULTURA, GANADERIA Y SILVICULTURA 141
04 INDUSTRIA MANUFACTURERA 28
10 BANCA Y FINANZAS 5
12 ADMINISTRACION PUBLICA 16
03 MINERIA 3
16 HOGAR 2
08 HOTELES Y RESTAURANTES 37
11 EMPRESARIAL 21
14 SOCIAL Y SALUD 4
06 CONSTRUCCIÓN 3
09 TRANSPORTE 30
13 EDUCACION 10
This is query:
SELECT
AGRUP.VC_CODDET AS CHR_SECECO,
AGRUP.VC_NOMDET AS VC_SECECO,
0 AS INT_NROPRESTAMO,
COUNT(*) INTO_BENEFICIARIOS,
0 AS DEC_SALCON
FROM TB_JSI_PRESTAMO_DETALLE PREDET
INNER JOIN TB_JSI_PRESTAMO PRE ON
PRE.INT_IDPRESTAMO=PREDET.INT_IDPRESTAMO
INNER JOIN TB_JSI_BENEFICIARIO_IFI BENIFI ON
BENIFI.INT_IDBENEIFI=PRE.INT_IDBENEIFI
INNER JOIN TB_JSI_OPERACION OPE ON PRE.INT_IDOPE = OPE.INT_IDOPE AND
TRUNC(TO_DATE(OPE.DT_FECVEN))>TRUNC(TO_DATE('30/09/2018', 'DD/MM/YY'))
INNER JOIN TB_JSI_CIIU CIIU ON PRE.INT_IDACT = CIIU.INT_IDACT AND
PRE.INT_IDSEC=CIIU.INT_IDCLAS
INNER JOIN TB_JSI_TABLA_DET SECECO ON SECECO.INT_IDDET=CIIU.INT_IDCLAS
INNER JOIN TB_JSI_AGRUPA_SECTOR AGRSEC ON SECECO.INT_IDDET =
AGRSEC.INT_IDSEC
INNER JOIN TB_JSI_TABLA_DET AGRUP ON AGRSEC.INT_IDAGRU = AGRUP.INT_IDDET
INNER JOIN TB_JSI_TABLA_DET MON ON OPE.INT_IDMON = MON.INT_IDDET
INNER JOIN TB_JSI_IFI IFI ON OPE.INT_IDIFI = IFI.INT_IDIFI
WHERE TRUNC(PREDET.DTE_FECPRO) = (
SELECT
TRUNC(DTE_FECPRO)
FROM (SELECT
DTE_FECPRO
FROM TB_JSI_PRESTAMO_DETALLE
WHERE DTE_FECPRO<=TO_DATE('30/09/2018','DD/MM/YY')
AND DEC_SALDOL>0
ORDER BY DTE_FECPRO DESC)
WHERE ROWNUM = 1
)
AND (NULL IS NULL OR OPE.INT_IDTIPPRO =2)
AND (NULL IS NULL OR OPE.INT_IDMON = 364)
AND (NULL IS NULL OR OPE.INT_IDIFI=72)
GROUP BY AGRUP.VC_CODDET, AGRUP.VC_NOMDET
and should return this result
07 COMERCIO 92
15 SERVICIOS OTROS 41
01 AGRICULTURA, GANADERIA Y SILVICULTURA 141
04 INDUSTRIA MANUFACTURERA 28
10 BANCA Y FINANZAS 4
12 ADMINISTRACION PUBLICA 16
03 MINERIA 3
16 HOGAR 2
08 HOTELES Y RESTAURANTES 37
11 EMPRESARIAL 21
14 SOCIAL Y SALUD 4
06 CONSTRUCCIÓN 3
09 TRANSPORTE 30
13 EDUCACION 10
since I have these records and when counting I should return 4 and not 5
269516 10 BANCA Y FINANZAS 1
269558 10 BANCA Y FINANZAS 1
269592 10 BANCA Y FINANZAS 2
269611 10 BANCA Y FINANZAS 1
#Roller, I have analyzed the result and it is now clear that one of the tables used in the query has two records with the matching condition and that is why it is returning 5 and not 4.
You just need to identify the table and if you are not able to, then use the following trick.
First of all, identify the table, basis of which you are saying that there must be 4 rows and then use that table's PK in the count with distinct as follows:
COUNT(DISTINCT MY_PK)