Rank using many Partition values in Linq c# - linq

My Table data are
Id FileName Created Date Month Year Date
1 ff1 2022-07-24 12:19:59.740 7 2022 24
2 ff1 2022-07-24 12:19:59.740 7 2022 24
3 ff1 2021-07-24 12:19:59.740 7 2021 24
4 ff1 2022-05-24 12:19:59.740 5 2022 24
5 ff1 2021-03-24 12:19:59.740 3 2021 24
6 ff1 2021-03-24 11:19:59.740 3 2021 24
7 ff1 2021-03-24 08:19:59.740 3 2021 24
My SQL query is as follows
select
filename,
createddate,
month,
dense_rank() over(partition by year, month order by createddate desc) rank
from filedetails;
My output is
Name Date Month Date Rank
ff1 2021-03-25 08:19:59.740 3 25 1
ff1 2021-03-24 12:19:59.740 3 24 2
ff1 2021-03-24 11:19:59.740 3 24 3
ff1 2021-03-24 08:19:59.740 3 24 4
ff1 2021-07-28 12:19:59.740 7 28 1
ff1 2021-07-24 12:19:59.740 7 24 2
ff1 2022-05-28 01:29:59.740 5 28 1
ff1 2022-05-24 12:19:59.740 5 24 2
ff1 2022-07-24 12:19:59.740 7 24 1
ff1 2022-07-24 12:19:59.740 7 24 1
how to achieve this using c# linq rank.
Thank you in advance.

Related

Oracle SQL Group By Hour for 24 hours

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 tried 2 style of nested loop in golang, but it has different output

i have this quiz, you should make an output like this, and i search youtube tutorials for "for golang" and it explain that it has 2 style of for in golang,
1
21
11
12
13
14
22
11
12
13
14
23
11
12
13
14
24
11
12
13
14
2
21
11
12
13
14
22
11
12
13
14
23
11
12
13
14
24
11
12
13
14
3
21
11
12
13
14
22
11
12
13
14
23
11
12
13
14
24
11
12
13
14
4
21
11
12
13
14
22
11
12
13
14
23
11
12
13
14
24
11
12
13
14
5
21
11
12
13
14
22
11
12
13
14
23
11
12
13
14
24
11
12
13
14
it should be vertically outputted, not horizontally, so i build 3 variable, i = 1, j = 21, and k = 11, and i use for to automatically increase the value, the 1st style worked, but the 2nd style somehow its different
yt vid : https://www.youtube.com/watch?v=jZ-llP_yKNo on 5:28 min he explain that for has 2 style
1st style :
for i:=1; i <= 5; i++{
fmt.Println(i)
for j:=21; j <= 24; j++ {
println(j)
for k:=11; k<=14; k++ {
fmt.Println(k)
}
}
}
2nd style :
i:=1
j:=21
k:=11
for i <= 5{
fmt.Println(i)
i++
for j <= 24 {
println(j)
j++
for k<=14 {
fmt.Println(k)
k++
}
}
}
It's not about the syntax but about your logic.
In the 1st style with for i := ..., whenever next loop run, you reset the value to the init state, means it always sets j to 21 and k to 11. So there will a many sub loop runs.
In contrast, 2nd style, you init value j and k right before going to loop. So in the second loop of i, j and k are still the same value with 25 and 15 in that order.
There are multiple options to print the output in the golang.
fmt.Println appends a new line in the end.
fmt.Printf prints content as it is.
For more details read the documentation.
for i := 1; i <= 5; i++ {
fmt.Printf("%v ", i)
for j := 21; j <= 24; j++ {
fmt.Printf("%v ", j)
for k := 11; k <= 14; k++ {
fmt.Printf("%v ", k)
}
}
}
Output
1 21 11 12 13 14 22 11 12 13 14 23 11 12 13 14 24 11 12 13 14 2 21 11 12 13 14 22 11 12 13 14 23 11 12 13 14 24 11 12 13 14 3 21 11 12 13 14 22 11 12 13 14 23 11 12 13 14 24 11 12 13 14 4 21 11 12 13 14 22 11 12 13 14 23 11 12 13 14 24 11 12 13 14 5 21 11 12 13 14 22 11 12 13 14 23 11 12 13 14 24 11 12 13 14
To add a new line use the \n escape sequence.
Check the running code link

Power BI - Get last month from last year

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 )

why is my for loop() not outputting as expected

d
The above 'd' is a data frame which outputs the following table containing
14 obs and 2 vars.
NUMBER DATE
1 20 2017-01-01
2 30 2017-01-02
3 40 2017-01-03
4 40 2017-01-04
5 50 2017-01-05
6 50 2017-01-06
7 60 2017-01-07
8 20 2017-01-08
9 30 2017-01-09
10 40 2017-01-10
11 40 2017-01-11
12 50 2017-01-12
13 50 2017-01-13
14 60 2017-01-14
After running the following code:
a<-c(0,7)
for(i in a){
w <- subset(d, DATE >= as.Date("2017-01-01")+a & DATE <= as.Date("2017-01-
07")+a )
print(w)
}
I get the following output. I was expecting the output to be two tables with the first dates 1st-7th and the second 8th-14. If i change the 'a' variable in the code to just '0' or just '7' it will run the dates as i'd expect (but obviously only 1 table each time i run the for loop).
NUMBER DATE
1 20 2017-01-01
3 40 2017-01-03
5 50 2017-01-05
7 60 2017-01-07
8 20 2017-01-08
10 40 2017-01-10
12 50 2017-01-12
14 60 2017-01-14
NUMBER DATE
1 20 2017-01-01
3 40 2017-01-03
5 50 2017-01-05
7 60 2017-01-07
8 20 2017-01-08
10 40 2017-01-10
12 50 2017-01-12
14 60 2017-01-14
So how do i get the output to show table 1: 1st-7th dates and table 2: 8th-14th dates?
I realise my error now. I was using 'a' instead of 'i' within the for loop.

Person age program in pascal

I have this task and i can't figure out how to do it.
I need to find persons age in days, there are given birth and death dates, there's data file:
8
Albertas Einšteinas 1879 03 14 1955 04 18
Balys Sruoga 1896 02 02 1947 10 16
Antanas Vienuolis 1882 04 07 1957 08 17
Ernestas Rezerfordas 1871 08 30 1937 10 17
Nilsas Boras 1885 10 07 1962 11 18
Nežiniukas Pirmasis 8 05 24 8 05 25
Nežiniukas Antrasis 888 05 25 888 05 25
Nežiniukas Trečiasis 1 01 01 125 01 01
and there's how result file should look like:
1879 3 14 1955 4 18 27775
1896 2 2 1947 10 16 18871
1882 4 7 1957 8 17 27507
1871 8 30 1937 10 17 24138
1885 10 7 1962 11 18 28147
8 5 24 8 5 25 1
888 5 25 888 5 25 0
1 1 1 125 1 1 45260
Few things to notice: all februarys have 28 days.
My function for calculating age:
function AmziusFunc(Mas : TZmogus) : longint;
var amzius, max : longint;
begin
max := 125 * 365;
amzius := (Mas.mirY - Mas.gimY) * 365 + (Mas.mirM - Mas.gimM) * 31 +
(Mas.mirD - Mas.gimD);
if ( amzius >= max ) then amzius := 0;
AmziusFunc := amzius;
end;
What should i change there? Thanks.
function AmziusFunc(Mas : TZmogus) : longint;
var amzius, max : longint;
begin
max := 125 * 365;
amzius := (Mas.mirY - Mas.gimY) * 365 + (Mas.mirM - Mas.gimM) * 31 +
(Mas.mirD - Mas.gimD);
if ( amzius >= max ) then amzius := 0;
AmziusFunc := amzius;
end;

Resources