Inner Join rowset from flat file and SQL - azure-data-lake

I have TSV file like below.
Domain_ID Domain_URL Company_Name_1 Company_Type
179238792 sample.com sample IT
and I have an USQL table like below.
Domain_ID Domain_URL Company_Name_1
179238792 sample.com sample
I am trying to join both the row-set and trying to get Company_Type for each company.
Script:
#result = EXTRACT
Domain_ID int,
Domain_URL string,
Company_Name_1 string,
Company_Type string
FROM #"sample.txt"
USING Extractors.Tsv(skipFirstNRows:1);
#result1=SELECT * FROM table
#result2= #result2= SELECT #result1.Company_Name_1 , #result1.Company_Type FROM #result1
INNER JOIN #result ON #result.Domain_ID==#result1.Domain_ID
OUTPUT #result2
TO "/Mobius_POC/Output/Company_Type.tsv"
USING Outputters.Tsv();
While executing the above script I am getting the following error.
Error:
Description
Invalid syntax found in the script.
Resolution
Correct the script syntax, using expected token(s) as a guide.
Details
at token '#result2', line 9
near the ###:
**************
_Name_1 string,
Company_Type string
FROM #"sample.txt"
USING Extractors.Tsv(skipFirstNRows:1);
#result1=SELECT * FROM MobiusPoc.dbo.TLD_AE
### #result2= SELECT #result1.Company_Name_1 , #result1.Company_Type FROM #result1
INNER JOIN #result ON #result.Domain_ID==#result1.Domain_ID
OUTPUT #resul
Error
E_CSC_USER_SYNTAXERROR
Message
syntax error. Expected one of: '.' ALL ANTISEMIJOIN ANY AS BROADCASTLEFT BROADCASTRIGHT CROSS DISTINCT EXCEPT FROM FULL FULLCROSS GROUP HASH HAVING INDEXLOOKUP INNER INTERSECT JOIN LEFT LOOP MERGE ON OPTION ORDER OUTER OUTER UNION PAIR PARTITION PRESORT PRODUCE READONLY REQUIRED RIGHT SAMPLE SELECT SEMIJOIN SERIAL TO UNIFORM UNION UNIVERSE USING VALUES WHERE WITH ';' '(' ')' ','

There is a semicolon (;) missing where the ### are in the error message (before #result2)
#result=SELECT * FROM table;
#result2= SELECT #result1.Company_Name_1 , #result1...

Related

Cannot use column of outside reference in my inside query

I am using SQL query like below:
SELECT distinct uf.SystemName as System ,uf.SystemId,
(SELECT SUM(CASE WHEN c.SystemId = uf.SystemTypeId THEN 1 ELSE 0 END)
FROM OPENJSON((SELECT TOP 1 CAST(JsonStringColumn AS NVARCHAR(MAX)) FROM RefTable where TagId = (Select top 1 TagId from CSSTAGS where projectid='9abbeecf-15a4-412f-ba0c-358b8f09ac9e')))
WITH (Data NVARCHAR(MAX) AS JSON) a
CROSS APPLY OPENJSON(a.Data)
WITH (System NVARCHAR(MAX) AS JSON) b
CROSS APPLY OPENJSON(b.System)
WITH (SystemId NVARCHAR(MAX) '$.SystemId') c
) As SystemIdExists
FROM Table1 uf
I am not able to use uf.SystemId inside my second select query which throwing below error: Multiple columns are specified in an aggregated expression containing an outer reference.
How can I achieve this?
Adding Where condition to second select query resolved my issue

Error while compiling statement: FAILED: ParseException line 4:0 cannot recognize input near '(' 'select' 'applicationprofileid' in select clause

create table sandbox_p_measurable_security.temp_calc_myaccess_privilegeddata1
stored as parquet as (
select
applicationprofileid,
'OneBridge' as 'IsOnebridge'
from bigdata_normalized.myaccess_tblapplicationprofile apro
where apro.applicationprofileid in (
select apd.applicationprofileid
from bigdata_normalized.myaccess_tblapplicationprofiledependencies apd
inner join bigdata_normalized.myaccess_tblsecuritygroup sg
on sg.securitygroupid = apd.securitygroupid
where sg.businessrefcde = 'CDSOneBridge'
)
);
I was able to ge the same error as you got and it got resolved after removing the round bracket ( you have used after stored as parquet as
Test Results:
Failure:
Create table customers1
as (select * from customers)
Error as:
Error while compiling statement: FAILED: ParseException line 2:3 cannot recognize input near '(' 'select' '*' in select clause
Success:
Create table customers1
as select * from customers
Success.
Try running the query as below (Removed the round brackets)
create table sandbox_p_measurable_security.temp_calc_myaccess_privilegeddata1
as
select
applicationprofileid,
'OneBridge' as 'IsOnebridge'
from bigdata_normalized.myaccess_tblapplicationprofile apro
where apro.applicationprofileid in (
select apd.applicationprofileid
from bigdata_normalized.myaccess_tblapplicationprofiledependencies apd
inner join bigdata_normalized.myaccess_tblsecuritygroup sg
on sg.securitygroupid = apd.securitygroupid
where sg.businessrefcde = 'CDSOneBridge')
stored as parquet;

How to use DISTINCT with a text data type?

I need to use the DISTINCT statement for the query below but I also need to add another column from a third table which has a text data type.
This query returns the results I'm looking for.
SELECT DISTINCT
a."APT_ID", a."BLDG", a."UNIT", a."UTILITIES",
b."ADDRESS", b."CITY"
FROM
APT_INFO a
INNER JOIN
BLDG_INFO b ON a."APT_ID" = b."APT_ID"
How can I add this column to the report above? I get results when I run this query standalone but when I add to the query above I receive an error message.
INNER JOIN (
SELECT CAST ("SUMMARY" AS VARCHAR(MAX)) AS 'Lay Summary' FROM TEST
SELECT DISTINCT
a."APT_ID", a."BLDG", a."UNIT", a."UTILITIES", b."ADDRESS", b."CITY"
FROM
APT_INFO a
INNER JOIN
BLDG_INFO b ON a."APT_ID" = b."APT_ID"
INNER JOIN
(SELECT
CAST ("SUMMARY" AS VARCHAR(MAX)) AS 'Lay Summary'
FROM TEST
Error:
Msg 156, Level 15, State 1, Line 48
Incorrect syntax near the keyword 'ON'

Using SELECT inside COALESCE

How do I correct the following SQL code, specifically the COALESCE part?
insert into Stmt G (ID,blah,foo)
select
coalesce(SELECT ID FROM Stmt G WHERE G.CLAIMNO=C.CLNUMBER, select StmtSeq.nextval from dual),
c.blah,
d.foo
from claim c
left join d on ...;
I'm taking the ID from the Stmt table itself if the ClaimNo matches, otherwise creating a new one. Is this not allowed in SQL? How else can I write this statement?
I'm getting a "Missing Expression" error on the coalesce part right now.
You should place parenthesis around the selects:
coalesce( (SELECT ID FROM Stmt G WHERE G.CLAIMNO=C.CLNUMBER)
, (select StmtSeq.nextval from dual)
)

Use stored procedure in Select query

I have a stored procedure that I want to use in a SELECT like below but I get an error.
The stored procedure's output is a table with one column of type int
select * from users where resCode in (EXEC getUserResselers 1)
-- stored procedure
create procedure getUserResselers
#userCode int
as
;WITH Directories AS (SELECT code FROM Resseler WHERE code =(select resselerCode from Users where code=#userCode)
UNION ALL SELECT d.code FROM Resseler d INNER JOIN Directories p ON d.parent = p.code)
SELECT * FROM Directories d
Direct selection from a stored procedure is not supported. However, this type of operation is a good candidate for a table-valued user-defined function, which might look something like:
Note that you have misspelled "resellers". I've left the misspelling so that you could test with your existing schema.
CREATE FUNCTION getUserResselers
(
#UserCode INT
)
RETURNS TABLE AS
RETURN(
WITH Directories AS (
SELECT code FROM Reseller WHERE code = (select resselerCode from Users where code=#userCode)
UNION ALL SELECT d.code FROM Resseler d INNER JOIN Directories p ON d.parent = p.code
)
SELECT * FROM Directories
)
You could then include a join to the function in your query, or you could continue to use IN, which should be something like:
select * from users where resCode in (SELECT Code FROM getUserResselers(1));