Google Sheets, query, select by month - date

I'm trying to select data by month but the behavior of the query/select is not what I expect. Here's a simplified sheet:
The month column just uses month() on column A and produces what I'd expect 1, 2, 3, 4
But the query in column D behaves as if the months are numbered 0, 1, 2, 3
What am I doing wrong?

months in QUERY actually starts from 0 so your formula should be:
=QUERY(A:B, "select B where month(A)+1 = 1", )
or:
=QUERY(A:B, "select B where month(A) = 0", )
for getting January
documentation:

Related

Calculating hours based on dates for weekend/weekday/public holiday

I've been attempting to use networkdays.intl to calculate hours for a weekend/weekday and public holiday.
The calculation is correct except when the time starts in 1 day and ends in another day for a shift (IE: 9pm until 2am)
Formula used for weekday
=NETWORKDAYS.INTL(B2,D2,"0000011",$T$2:$T$7)*G2
Formula used for weekend
=NETWORKDAYS.INTL(B2,D2,"1111100",$T$2:$T$7)*G2
Formula used for public holiday (basic)
=SUM(I2-Q2-R2)
Example sheet with highlighted cells causing the miscalculation.
Calculating hours on dates
Appreciate any suggestions.
Q12:
=INDEX(TEXT(SUM(QUERY({TEXT(SEQUENCE(D12-B12+1, 1, B12), {"ddd", "#"}),
{1-C12; SEQUENCE((D12-B12+1)-2, 1, 1, ); E12}},
"select Col3
where not Col1 matches 'Sat|Sun'
and not Col2 matches '"&TEXTJOIN("|", 1, IFERROR(1/(1/T2:T)))&"'")*1), "[h]:mm:ss"))
R12:
=INDEX(TEXT(SUM(IFNA(QUERY({TEXT(SEQUENCE(D12-B12+1, 1, B12), {"ddd", "#"}),
{1-C12; SEQUENCE((D12-B12+1)-2, 1, 1, ); E12}},
"select Col3
where Col1 matches 'Sat|Sun'
and not Col2 matches '"&TEXTJOIN("|", 1, IFERROR(1/(1/T2:T)))&"'"))*1), "[h]:mm:ss"))
S12:
=INDEX(TEXT(SUM(IFNA(QUERY({TEXT(SEQUENCE(D12-B12+1, 1, B12), {"ddd", "#"}),
{1-C12; SEQUENCE((D12-B12+1)-2, 1, 1, ); E12}},
"select Col3
where Col2 matches '"&TEXTJOIN("|", 1, IFERROR(1/(1/T2:T)))&"'"))*1), "[h]:mm:ss"))

Second minimum date in power bi

I have some users with different game_id.
for each user, I want to find the second minimum date. (column: min2_date)
If a user doesn't have a second date (look at user_id: 2, in this example), his min2_date should be -1.
If the second minimum date is the same as the first minimum date(look at user_id: 4), we should write that date in the min2_date column.
I don't know How I should calculate the second minimum date in Power BI.
please help me if you know.
Because you dont want a real second min date (but date in second row by order), we must try some trickyway.
One of way that we can do that is:
MinDate2 =
var _countrows = CALCULATE(countrows(VALUES(games[dates])), ALL(games[dates]) )
return
if(_countrows = 1, -1,
FORMAT(DISTINCT(TOPN(1,TOPN(2,CALCULATETABLE(SELECTCOLUMNS(games, "dates",games[dates]), ALL(games[dates])),[dates], asc), [dates], desc)), "yyyy-mm-dd")
)
where MinDate and MinDate2 are measures.
AS a calculatedColumn:
MinDate2_col =
var _countrows = CALCULATE(countrows(VALUES(games[dates])), ALL(games[dates]) )
return
if(_countrows = 1, "-1",
FORMAT(DISTINCT(TOPN(1,TOPN(2,CALCULATETABLE(SELECTCOLUMNS(games, "dates",games[dates]), ALL(games[dates])),[dates], asc), [dates], desc)), "yyyy-mm-dd")
)

Find all instances of a date in a date range - SQL Server

I need to find the price for an item for each financial year end date in a date range. In this case the financial year is e.g. 31 March
The table I have for example:
ItemID
Value
DateFrom
DateTo
1
10
'2019/01/01'
'2021/02/28'
1
11
'2021/03/01'
'2021/05/01'
SQL Fiddle
The SQL would thus result in the above table to be:
ItemID
Value
DateFrom
DateTo
1
10
'2019/01/01'
'2019/03/30'
1
10
'2020/03/31'
'2021/02/28'
1
11
'2020/03/01'
'2021/03/30'
1
11
'2020/03/31'
'2021/05/01'
You can solve it, but a prerequisite is the creation of a table called financial_years and filling it with data. This would be the structure of the table:
financial_years(id, DateFrom, DateTo)
Now that you have this table, you can do something like this:
select ItemID, Value, financial_years.DateFrom, financial_years.DateTo
from items
join financial_years
on (items.DateFrom between financial_years.DateFrom and financial_years.DateTo) or
(items.DateTo between financial_years.DateFrom and financial_years.DateTo)
order by financial_years.DateFrom;
The accepted answer is not correct, as it does not split out different parts of the year which have different values.
You also do not need a Year table, although it can be beneficial. You can generate it on the fly using a VALUES table.
Note also a better way to check the intervals overlap, using AND not OR
WITH Years AS (
SELECT
YearStart = DATEFROMPARTS(v.yr, 3, 31),
YearEnd = DATEFROMPARTS(v.yr + 1, 3, 31)
FROM (VALUES
(2015),(2016),(2017),(2018),(2019),(2020),(2021),(2022),(2023),(2024),(2025),(2026),(2027),(2028),(2029),(2030),(2031),(2032),(2033),(2034),(2035),(2036),(2037),(2038),(2039)
) v(yr)
)
SELECT
i.ItemID,
i.Value,
DateFrom = CASE WHEN i.DateFrom > y.YearStart THEN i.DateFrom ELSE y.YearStart END,
DateTo = CASE WHEN i.DateTo > y.YearEnd THEN y.YearEnd ELSE i.DateTo END
FROM items i
JOIN Years y ON i.DateFrom <= y.YearEnd
AND i.DateTo >= y.YearStart;

PostgreSQL, getting min and max date from text column

I have such situation in table:
1 01.02.2011
2 05.01.2011
3 06.03.2012
4 07.08.2011
5 04.03.2013
6 06.08.2011
7
8 02.02.2013
9 04.06.2010
10 10.10.2012
11 04.04.2012
where first column is id (INT) and second column is TEXT in which may be written date in format 'dd.mm.yyyy'.
I would like to get:
1) lowest entered date in whole table and highest entered date in whole table.
2) lowest entered date in year 2012 and highest entered date in year 2012.
Lowest and highest date in year may be a same (like for year 2010) or field may be empty (like in row 7).
I am tying to use TO_TIMESTAMP but unsuccessfully.
Example:
SELECT (TO_TIMESTAMP(mydatetxt, 'DD.MM.YYYY'))
FROM " & myTable & "
ORDER BY (TO_TIMESTAMP(mydatetxt, 'DD.MM.YYYY')) ASC LIMIT 1
Also with BETWEEN I don't get wanted result.
How to write those two queries?
SOLUTION:
Thanks for all suggestions.
Igor's solution is most suitable and simple enough for me.
Dim sqlText As String = "SELECT min(to_date(nullif(mydate,''), 'DD.MM.YYYY')) FROM " & mytable
cmd1 = New NpgsqlCommand(sqlText, conn)
min = CDate(cmd1.ExecuteScalar())
If Not IsDate(min) Then
min = CType(CDate("01.01." & myyear) & " 00:00:00", Date)
End If
fromdate = CType(CDate(min), Date)
sqlText = "SELECT max(to_date(mydate, 'DD.MM.YYYY')) FROM " & mytable
cmd1 = New NpgsqlCommand(sqlText, conn)
max = CDate(cmd1.ExecuteScalar())
If Not IsDate(max) Then
max = CType(CDate("31.12." & myyear) & " 23:59:59.9999", Date)
End If
todate = CType(CDate(max), Date)
Try something like:
SELECT max(to_date(nullif(mydatetxt,''), 'DD.MM.YYYY')),
min(to_date(nullif(mydatetxt,''), 'DD.MM.YYYY'))
FROM table_name;
SELECT max(to_date(nullif(mydatetxt,''), 'DD.MM.YYYY')),
min(to_date(nullif(mydatetxt,''), 'DD.MM.YYYY'))
FROM table_name
WHERE date_part('year',to_date(nullif(mydatetxt,''), 'DD.MM.YYYY')) = 2012;

JPA query, find values for chosen members of groups of records

Suppose I have a table with some natural grouping and ordering, for example records by date, where the records for any given date are ordered by some other differentiator field
1 July, 1, 56.6
1 July, 2, 45.8
1 July, 3, 78.9
2 July, 1, 34.2
2 July, 2, 26.7
I want to select the records with the highest differentiator for each day, for example, to get at
1 July, 3, 78.9
2 July, 2, 26.7
in this simple case. I can't think how to structure a query to retrieve those records. So far I'm pulling back the whole set and selecting in Java - not really what i want to do.
Perhaps something like,
Select o from MyClass o where o.value = (Select Max(g.value) from MyClass g where g.date = o.date)