Azure SQL DW: How can I create an external table with calculated columns to split DateTime - azure-sqldw

I am receiving an error trying to create an External Table with a calculated column based on DOB column for the purpose of splitting the date into three columns (Year, Month, Day).
Example Columns of dbo.Persons:
| FirstName | LastName | DOB | DOBYear | DOBMonth | DOBDay |
SQL Code:
CREATE EXTERNAL TABLE [dbo].Person
(
FirstName VARCHAR(250),
LastName VARCHAR(250),
DOB datetimeoffset,
DOBYear DATE AS DATEPART("yyyy", DOB)
)
WITH
(
LOCATION='.....'
, DATA_SOURCE = ....
, FILE_FORMAT = ....
, REJECT_TYPE = VALUE
, REJECT_VALUE = 0
)
;
However I can never get it to excute as I get this error below:
Msg 103010, Level 16, State 1, Line 22
Parse error at line: 28, column: 16: Incorrect syntax near 'DATEPART'.
How can I resolve this? I have the theory that perhaps this is not supported via external table? If there is a way to implement are there any suggestions?

Azure SQL Data Warehouse external tables do not support computed columns. You could however create a view over the top to serve the same purpose, eg
CREATE VIEW dbo.vw_person
AS
SELECT
FirstName,
LastName,
DOB,
DATEPART( yyyy, DOB ) AS DOBYear
FROM dbo.Person
NB DATEPART parameters do not take quotes, so I have corrected your example.

Related

Is there any way to get customer duplicate data from customers table then update all others except 1 row from duplicate data?

I am trying to get duplicate data of my customers table then after finding update isactive column of all duplicates found to 0 except 1 row of the duplicate data.
here is my script using oracle 19c:
merge into customers c
using (
WITH cte AS (
SELECT DISTINCT ROWID, fn_createfullname(firstname, middlename, lastname) as fullName, mobile, branchid, isactive,
ROW_NUMBER() OVER (PARTITION BY fn_createfullname(firstname, middlename, lastname), mobile, branchid ORDER BY ROWID) AS rn
FROM customers
)
select * from cte
WHERE rn > 1
) tbl
on (tbl.mobile = c.mobile and fn_createfullname(c.firstname, c.middlename, c.lastname) = tbl.fullname)
when matched then update
SET c.isactive = 0
WHERE rn > 1;
i am expecting to get all duplicate data then update single row from duplicate data.
plz any help.
after running my query is displaying this error:
Error report - ORA-30926: unable to get a stable set of rows in the
source tables

Power BI Text to Long Date Errors For Data Model

I have Sales table and this table contains OrderDate column this column foreign key. And i have Dates table this table has date column i want to connect for this two table on orderDate and date column. But orderDate columns type is text and i did'nt change the type how can i do this ?
I solved hard way. I am gonna explain;
There is example data : 6 Haziran 2014 Cuma I splitted this data like dd/mm/YYYY
Day=LEFT(Sales[OrderDate],SEARCH(" ",Sales[OrderDate])-1)
Year = RIGHT(LEFT(Sales[OrderDate],SEARCH(" ",Sales[OrderDate],12)),5)
For Month i used conditional column. Like: each if Text.Contains([OrderDate], "Ocak") then 1 else if .....
And finally OrderDateForConnection = Sales[Day] & "/" & Sales[Month] & "/" & Sales[Year] and its worked :)

Rank by created date and company name

I have table Company. I need to display the TOP 6 recently created.
Exemple of data:
Expected results
What I tried :
Rank =
VAR d = Companies[CreatedDate].[Date]
RETURN
CALCULATE (
RANK.EQ ( d, Companies[CreatedDate], DESC)
)
the calculated column returned wrong values:
I need to order by creatd date, and when it's the same date, I need to order by Company Namr
How to correct it!,
For ranking based on created date and company name try the following steps, and if it helps accept it is as answer.
Create a calculated column for ranking the company name.
Company Sort = RANKX(ALL('Table'), 'Table'[Company Name], , ASC)
Create another calculated column for ranking using following DAX
Ranking on Date and Company Name =
VAR X = MAX('Table'[Company Sort])
var res =
RANKX(
ALL('Table),
'Table'[Created Date] * X + 'Table'[Company Sort]
)
RETURN res
Sort by using the column `Ranking on Date and Company Name'.
If you want to create a visual, then just add the column `Ranking on Date and Company Name' in the filter pane and select the Top N filter accordingly.

How to calculate working days on ORACLE APEX?

I have 2 tables; employee and time. For time table, I want to take id information automatically from employee table and subtracting every employees' start date of work from employee table from system date. If result = 1 year plus, result will be automatically added to the time table.
employee.hire_date date /
time.year number
I want it to do this (time.year = sysdate - employee.hire_date) process automatically.
For example, the employee started on 21.01.2019. Today(21.01.2020) write '1' on time table year column automatically.
Thanks.
Create times as a view on the employees table.
Oracle Setup:
CREATE TABLE employees ( id, start_date ) AS
SELECT 1, DATE '2019-01-21' FROM DUAL UNION ALL
SELECT 2, DATE '2019-06-21' FROM DUAL UNION ALL
SELECT 3, DATE '2015-01-01' FROM DUAL;
Create View:
CREATE VIEW times ( id, years ) AS
SELECT id,
FLOOR( MONTHS_BETWEEN( SYSDATE, start_date ) / 12 )
FROM employees;
Output:
SELECT *
FROM times;
gives:
ID | YEARS
-: | ----:
1 | 1
2 | 0
3 | 5
db<>fiddle here

powerbi directquery distinct count group by issue

I have a table format with
record date(including seconds), user ID, Database
I need to show the maximum distinct number of users per hour grouped by every 5 minutes ( not sure if this explains - please see example below)
I am only using DirectQuery storage and not intended to change that to import as well.
I have tried various methods but could not manage without changing the storage mode. Any help is appreciated.
My table sample is,
21/01/2019 12:35:00, jane, UK
21/01/2019 12:35:00, joe, UK
21/01/2019 12:35:00, joe, NL
21/01/2019 12:40:00, bob, NL
21/01/2019 12:40:00, jane, NL
21/01/2019 12:40:00, joe, NL
21/01/2019 12:40:00, jakob, NL
Expected result
21/01/2019 12, UK, 2
21/01/2019 12, NL, 4
Start by adding an hour column in the data table.
Hour = HOUR([DateTime])
Then create a temporary summarized table (this table can be hidden from the report view).
SummTemp =
SUMMARIZE(
Data;
[Datetime];
[Nat];
[Hour]; // this is the calculated column with only hour.
"Date"; DATE(YEAR([DateTime]); MONTH([DateTime]); DAY([DateTime]));
"Count"; COUNTA('Data'[Nat])
)
Then create a second summarized table:
SummaryTable =
SELECTCOLUMNS(
SUMMARIZE(
SummTemp;
[Date];
[Hour];
[Nat];
"MaxCount"; MAX('SummTemp'[Count]);
"NewName"; [Date]&" "&[Hour]
// OR "NewName"; ( [Date]&" "&TIME([Hour]; 0; 0))*
);
"DateHour"; [NewName];
"Nationality"; [Nat];
"MaxCNX"; [MaxCount]
)
The end result will look like this:
*If you use this line, then you will get a column in text format but with [dd/mm/yyyy hh:00:00]. You can convert this column to a datetieme format by selecting it and change it from text to datetime via Modeling->Data type. NMot sure if you can drop both minutes and seconds from it but you can drop seconds so that your DateHour column will have a format like: [dd/mm/yyyy hh:00]