Dynamic date pivot using CTE - postgresql

I have a CTE which i get after lot of calculations, So the final CTE contains three columns
namely, "Name", "Values" & "Dates".
I have searched google and in all PIVOT the dates are hardcoded and i don't wan this dates to be hardcoded as the dates will be dynamically changed.
Any suggestions how i can get this result with a CTE and with dynamic dates pivot?
Version PostgreSQL 11
Sample input
Value. Name Date
"1" "Coenzym Q10" "2020-06-29"
"1" "Coenzym Q10" "2020-06-30"
"4" "Coenzym Q10" "2020-07-01"
"1" "Coenzym Q10" "2020-07-02"
"5" "Coenzym Q10" "2020-07-03"
"1" "Coenzym Q10" "2020-07-04"
"2" "D12" "2020-07-01"
"4" "D12" "2020-07-04"
Desired Output
"Name" "2020-07-04". "2020-07-03" "2020-07-02" "2020-07-01" "2020-06-30". "2020-06-29"
"Coenzym Q10". "4" "5" "1" "1" "1" "1"
"D12" "4" "2"

If you don't insist of having the dates as the column name (header), you could do something like this:
select "Name",
max("Value") filter (where date = current_date) as "Today",
max("Value") filter (where date = current_date - 1) as "Today -1",
max("Value") filter (where date = current_date - 2) as "Today -2",
max("Value") filter (where date = current_date - 3) as "Today -3",
max("Value") filter (where date = current_date - 4) as "Today -4",
max("Value") filter (where date = current_date - 5) as "Today -5",
max("Value") filter (where date = current_date - 6) as "Today -6",
max("Value") filter (where date = current_date - 7) as "Today -7"
from the_table
group by "Name"
order by "Name;

Related

Is there a formula for upcoming date using formula in Google sheet based on condition?

I have a google sheet where I need to get the next upcoming date based on the start date set in column A
Any pointers are greatly appreciated? I am unable exhibit the efforts as I am completely new to this sort of recurrence using Google sheets
https://docs.google.com/spreadsheets/d/1g_UNg4MjDy3gFufpjZtMRkbBz80K3scQdZOaiAnZi7I/edit#gid=0
This behavior (the next date from today including today) could be implemented manually by this formula:
={
"Next date from today";
ARRAYFORMULA(
IFS(
A2:A >= TODAY(),
A2:A,
B2:B = "Daily",
TODAY() + MOD(TODAY() - A2:A, C2:C),
B2:B = "Weekly",
TODAY() + MOD(TODAY() - A2:A, 7 * C2:C),
B2:B = "Monthly",
EDATE(A2:A, ROUNDUP((12 * (YEAR(TODAY()) - YEAR(A2:A)) + (MONTH(TODAY()) - MONTH(A2:A)) - IF(DAY(TODAY()) < DAY(A2:A), 1, 0)) / C2:C, 0) * C2:C),
True,
""
)
)
}
For additional options (like "every 2nd monday of the month" and others) additional options should be implemented in that IFS part.
If you are interested in a trivial case where the next date from the start date (column F:F on the screenshot) is needed, then the formula would be much simpler:
={
"Next date";
ARRAYFORMULA(
IFS(
B2:B = "Daily",
A2:A + C2:C,
B2:B = "Weekly",
A2:A + 7 * C2:C,
B2:B = "Monthly",
EDATE(A2:A, C2:C),
True,
""
)
)
}
Again, for additional options you'll need to add corresponding part to the IFS.
You could use IFS to check Frequency, and:
If Daily, add Count value to start date.
If Weekly, add Count value multiplied by 7.
If Monthly, since not all months have the same duration, retrieve the YEAR, MONTH and DAY indexes, add Count to the MONTH index, and set a new DATE, EDIT: or as suggested by kishkin, use EDATE.
It could be something like this:
=ARRAYFORMULA(IFNA(IFS(
B2:B = "Daily", A2:A + C2:C,
B2:B = "Weekly", A2:A + 7 * C2:C,
B2:B = "Monthly", EDATE(A2:A,C2:C)
)))

How to check whether day satisfies the recurrence condition using google sheet formula

I have a Google sheet where it has startdate, frequency, count. I need to check whether the todays date is satisfying the recurrence and display TRUE if today() is satisfied and false if not satisfied.
This will help me determine each time, the sheet is opened I can find out from TODAY() date is satisifed or not?
https://docs.google.com/spreadsheets/d/142od5eUib5nHmdi4mnENuPbh-Yn485NzUSt90p84QE0/edit?usp=sharing
Like #Scott Craner said, just compare the result of the formula from the previous question with TODAY():
={
"Today is the day?";
ARRAYFORMULA(
IF(
A2:A = "",
,
IFS(
A2:A >= TODAY(),
A2:A,
B2:B = "Daily",
TODAY() + MOD(TODAY() - A2:A, C2:C),
B2:B = "Weekly",
TODAY() + MOD(TODAY() - A2:A, 7 * C2:C),
B2:B = "Monthly",
EDATE(A2:A, ROUNDUP((12 * (YEAR(TODAY()) - YEAR(A2:A)) + (MONTH(TODAY()) - MONTH(A2:A)) - IF(DAY(TODAY()) < DAY(A2:A), 1, 0)) / C2:C, 0) * C2:C),
True,
""
) = TODAY()
)
)
}

MDX: How to get the weeks between two dates in MDX Query

I want to get the weeks as columns between two dates in an MDX query.
For example if I input these dates: 2015-01-01 and 2015-02-01 I want to get this:
Week ending January 1,Week ending January 8,Week ending January 15,Week ending January 22
I have created this MDX query with "Filter" method but it doesn't work:
WITH SET [WeeksBetweenDates] AS
Filter([Date].[Year - Week - Date Hierarchy].[Week],
[Date].[Year - Week - Date Hierarchy].CurrentMember.Member_Value >= [Date].[Year - Week - Date Hierarchy].[Week].&[2015-01-01T00:00:00]
AND [Date].[Year - Week - Date Hierarchy].CurrentMember.Member_Value <= [Date].[Year - Week - Date Hierarchy].[Week].&[2015-02-01T00:00:00])
SELECT [WeeksBetweenDates] ON COLUMNS
FROM [Team System]
I get "Unknown" column if I run this query.
You should define dates as subcube slice ({[Date1] : [Date2]}), this will greatly increase query performance + make code more transparent (tested against our TFS):
select
{
[Date].[Week].members
} on 0
from ( select {
[Date].[Date].&[2015-01-01T00:00:00]
:
[Date].[Date].&[2015-02-01T00:00:00]
} on 0
from [Team System])
To make it work through using hierarchy, you should correct
[Date].[Year - Week - Date Hierarchy].[Week].&[2015-01-01T00:00:00] to [Date].[Year - Week - Date Hierarchy].[Date].&[2015-01-01T00:00:00]

Comparing Dates in If Statement

I am trying to compare dates in an expression. If the Closed_Date matches today's date (I am using Today()), then it would output 1 in the box, otherwise output 0. So far I have this but it doesn't seem to work:
=IIF(Fields!Closed_Date = Mid(Today(),1,9), "1", "0")
The reason I am using Mid is to just get the month, day, and year. I don't want the time included. Is there a way you can compare dates using this or another method?
Today() actually returns today's date at midnight, so to compare your date to today you'll need to strip the time from Closed_Date instead. I'd recommend the DateValue function, since it returns date information with time set to midnight which makes for an easy comparison:
=IIF(DateValue(Fields!Closed_Date.Value) = Today(), "1", "0")
Try something like this:
=IIF(
Format(CDate(Fields!Closed_Date), "MM/dd/yyyy") = Today()
, "1", "0"
)
OR
=IIF(
FormatDateTime(Fields!Closed_Date, DateFormat.ShortDate) = Today()
, "1", "0"
)
Avoid using string functions like Mid with the dates. There are lot of date related functions available in SSRS.

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;