How to add two date columns to query in teradata sql? - sql-date-functions

I want to add two date columns, run_date and report_date to my existing query, what is the best way to do this?
select
*,
current_date as run_date,
Add_Months(Current_Date - Extract(DAY From Current_Date), 0 ) as report_date
from my_final_table;
will the run_date and report_date appear in my last two columns from the data set? Thank you!

Related

Power BI DAX subquery for multiple Tables - Complex DAX

I have 3 tables which I have mentioned in the below SQL Query, which I need to convert into a DAX formula which I'm not able to do as a newbie to Power BI, which similarly I need to do the same for almost 5 - 6 columns.
If this has figured out, I can do the remaining 5 DAX Calculated columns, which I need support on writing Complex DAX for below SQL
SELECT
Substr(TableA.Text_Noted,0,500)
FROM SYS_DATA_NODE_TAB TableA, SYS_DATA_NOTED TableC
WHERE TableA.ID_Name = TableC.ID_Name
AND TableA.task_note_text LIKE '%Field update Sender:%'
AND TableA.task_note_entered_date =
(
SELECT Max(TableB.task_note_entered_date)
FROM SYS_DATA_NODE_TAB TableB, SYS_DATA_NOTED TableC
WHERE TableB.task_note_text LIKE '%Field update Sender:%'
AND TableB.ID_Name = TableC.ID_Name )
AND rownum = 1 as Text_Noted;
Please support on this or help me guidance in writing this Complex DAX.
Thanks

To Get YEARMONTH from date in Athena

I can get year and month separately in Athena but i don't know how to get YEARMONTH from date.
select year(date1) from table1
select Month(date1) from table1
Please suggest how to get YEARMONTH. Thank you in advance.
You can use date_format to represent your date in needed format:
select date_format(now(), '%Y%m')

SQL statement with where clause subquery to DAX expression

I have a query written in SQL that I want to convert to DAX.
SELECT DISTINCT TeamId
FROM Teams
WHERE UId NOT IN
(
SELECT UId
FROM Items
)
AND IsArchived = 0
The power bi data model relationship with Teams and Items is One-to-Many (Teams to Items).
How can I convert the above SQL to DAX.
I found the solution to the task.
SELECT DISTINCT TeamId
FROM Teams
WHERE TeamId NOT IN
(
SELECT TeamId
FROM Items
)
AND IsArchived = 0
I used EXCEPT function in DAX.
EXCEPT(VALUES('Teams'[TeamId]), VALUES(DriveItems[TeamId]))
This returns distinct TeamID as a table

Pivot with dynamic DATE columns

I have a query that I created from a table.
example:
select
pkey,
trunc (createdformat) business_date,
regexp_substr (statistics, 'business_ \ w *') business_statistics
from business_data
where statistics like '% business_%'
group by regexp_substr(statistics, 'business_\w*'), trunc(createdformat)
This works great thanks to your help.
Now I want to show that in a crosstab / pivot.
That means in the first column are the "business_statistics", the column headings are the "dynamic days from business_date".
I've tried the following, but it doesn't quite work yet
SELECT *
FROM (
select
pkey,
trunc(createdformat) business_date,
regexp_substr(statistics, 'business_\w*') business_statistics
from business_data
where statistics like '%business_%'
)
PIVOT(
count(pkey)
FOR business_date
IN ('17.06.2020','18.06.2020')
)
ORDER BY business_statistics
If I specify the date, like here 17.06.2020 and 18.06.2020 it works. 3 columns (Business_Statistic, 17.06.2020, 18.06.2020). But from column 2 it should be dynamic. That means he should show me the days (date) that are also included in the query / table. So that is the result of X columns (Business_Statistics, Date1, Date2, Date3, Date4, ....). Dynamic based on the table data.
For example, this does not work:
...
IN (SELECT DISTINCT trunc(createdformat) FROM BUSINESS_DATA WHERE statistics like '%business_%' order by trunc(createdformat))
...
The pivot clause doesn't work with dynamic values.
But there are some workarounds discuss here: How to Convert Rows to Columns and Back Again with SQL (Aka PIVOT and UNPIVOT)
You may find one workaround that suits your requirements.
Unfortunately, I am not very familiar with PL / SQL. But could I still process the start date and the end date of the user for the query?
For example, the user enters the APEX environment as StartDate: June 17, 2020 and as EndDate: June 20, 2020.
Then the daily difference is calculated in the PL / SQL query, then a variable is filled with the value of the entered period using Loop.
Example: (Just an idea, I'm not that fit in PL / SQL yet)
DECLARE
startdate := :P9999_StartDate 'Example 17.06.2020
enddate := P9999_EndDate 'Example 20.06.2020
BEGIN
LOOP 'From the startdate to the enddate day
businessdate := businessdate .... 'Example: 17.06.2020,18.06.2020,19.06.2020, ...
END LOOP
SELECT *
FROM (
select
pkey,
trunc(createdformat) business_date,
regexp_substr(statistics, 'business_\w*') business_statistics
from business_data
where statistics like '%business_%'
)
PIVOT(
count(pkey)
FOR business_date
IN (businessdate)
)
ORDER BY business_statistics
END;
That would be my idea, but I fail to implement it. Is that possible? I hope you understand what I mean

Counting occurrences in a column

I have a dataset with >7K observations. I have a column RFLP that can have a numerous amount of different responses. I want to get a table that tells me how many occurrences of each unique response are in the column. Since there are so many responses, I am not able to specify them in the statement.
Thanks!
Denise
I would use proc sql. This is an aggregation query:
proc sql;
select rflp, count(*)
from <dataset>
group by rflp;