Databricks Error in SQL statement: ParseException: mismatched input 'Service_Date - azure
I am running this script in Azure Databricks using spark SQL , getting this error:
Error in SQL statement: ParseException:
mismatched input 'Service_Date' expecting {'(', 'DESC', 'DESCRIBE', 'FROM', 'MAP', 'REDUCE', 'SELECT', 'TABLE', 'VALUES', 'WITH'}(line 16, pos 0)
I am new to Databricks so wondering any tips on troubleshooting this.
CREATE OR REPLACE VIEW operations_staging.v_claims AS (
/
WITH Snapshot_Date AS
(
SELECT T1.claim_number,
T1.source_system,
MAX(T1.snapshot_date) snapshot_date
FROM bhc_claim.medical_claim T1
GROUP BY T1.claim_number,
T1.source_system
),
/
Service_Date AS
(
SELECT T1.claim_number,
T1.source_system,
MIN(T1.service_from_date) claim_service_date
FROM bhc_claim.medical_claim_detail T1
GROUP BY T1.claim_number,
T1.source_system
),
Pend_Step1 AS
(
SELECT T1.claim_num Claim_Number,
T1.tax_id,
T1.provider provider_name,
TO_DATE(T1.incurred,"MM/dd/yyyy") Service_Date,
TO_DATE(T1.received,"MM/dd/yyyy") Received_Date,
TO_DATE(T1.report_date,"MM/dd/yyyy") Report_Date,
T1.pending_amount Pend_Amount,
T1.pend_code Pend_Code,
T1.pend_code_description Pend_Code_Desc,
T1.hold_reason_code Hold_Code,
T1.hold_code_description Hold_Code_Desc
FROM loomis_2021.pu T1 -- 277,011
GROUP BY T1.claim_num,
T1.tax_id,
T1.provider,
T1.incurred,
T1.received,
T1.report_date
T1.pending_amount,
T1.pend_code,
T1.pend_code_description,
T1.hold_reason_code,
T1.hold_code_description
),
Pend_Step2 AS
(
SELECT Claim_Number,
concat_ws(",", collect_set(DISTINCT T1.Hold_Code)) Hold_Code,
concat_ws(",", collect_set(DISTINCT T1.Pend_Code)) Pend_Code
FROM Pend_Step1 T1
GROUP BY Claim_Number
),
Pend_Step3 AS
(
SELECT T1.Claim_Number,
T1.tax_id,
T1.provider_name,
T1.Service_Date,
T1.Received_Date,
T1.Report_Date,
T2.Hold_Code,
T2.Pend_Code,
T1.Pend_Amount
FROM Pend_Step1 T1
LEFT JOIN Pend_Step2 T2
ON T1.Claim_Number = T2.Claim_Number
GROUP BY T1.Claim_Number,
T1.tax_id,
T1.provider_name,
T1.Service_Date,
T1.Received_Date,
T1.Report_Date,
T2.Hold_Code,
T2.Pend_Code,
T1.Pend_Amount
),
Pend_Step4 AS
(
SELECT T1.Claim_Number,
T1.tax_id,
T1.provider_name,
T1.Service_Date,
T1.Received_Date,
T1.Hold_Code,
T1.Pend_Code,
SUM(T1.Pend_Amount) Pend_Amount
FROM Pend_Step3 T1 -- 277,011
GROUP BY T1.Claim_Number,
T1.tax_id,
T1.provider_name,
T1.Service_Date,
T1.Received_Date,
T1.Hold_Code,
T1.Pend_Code
),
Paid_Previous_Step1 AS
(
SELECT MAX(claim_received_date) Max_Received_Date
FROM bhc_claim.medical_claim
WHERE DAYOFWEEK(claim_received_date) = 1
AND claim_received_date < NOW()
),
Paid_Previous_Step2 AS
(
SELECT T1.`claim_#` Claim_Number,
T1.tax_id Tax_ID,
CASE WHEN T1.provider_group_name IS NOT NULL THEN T1.provider_group_name
ELSE CONCAT(T1.provider_first_name,T1.provider_last_name)
END provider_name,
TO_DATE(T1.last_refresh_date,"yyyyMMdd") Refresh_Date,
TO_DATE(T1.received_date,"yyyyMMdd") Received_Date,
TO_DATE(T1.processed_date,"yyyyMMdd") Processed_Date,
MIN(TO_DATE(T1.from_dos,"yyyyMMdd")) Service_Date,
'Issued' Status,
SUM(T1.covered) Paid_Amount,
SUM(T1.billed) Billed_Amount
FROM Loomis_2021.paid_previous T1
CROSS JOIN Paid_Previous_Step1 T2
WHERE TO_DATE(T1.last_refresh_date,"yyyyMMdd") >= T2.Max_Received_Date
GROUP BY T1.`claim_#`,
T1.tax_id,
CASE WHEN T1.provider_group_name IS NOT NULL THEN T1.provider_group_name
ELSE CONCAT(T1.provider_first_name,T1.provider_last_name)
END,
T1.last_refresh_date,
T1.received_date,
T1.processed_date
),
Paid_Previous_Step3 AS
(
SELECT T1.Claim_Number,
MAX(T1.Refresh_Date) Refresh_Date
FROM Paid_Previous_Step2 T1
GROUP BY T1.Claim_Number
),
Paid_Previous_Step4 AS
(
SELECT T1.Claim_Number,
T1.Tax_ID,
T1.provider_name,
T1.Refresh_Date,
T1.Received_Date,
T1.Processed_Date,
T1.Service_Date,
T1.Status,
T1.Paid_Amount,
T1.Billed_Amount
FROM Paid_Previous_Step2 T1 -- 60,746
INNER JOIN Paid_Previous_Step3 T2 -- 60,746
ON T1.Claim_Number = T2.Claim_Number --3,816,359
AND T1.Refresh_Date = T2.Refresh_Date
),
Claim_Detail_Step1 AS
(
SELECT T1.claim_number,
T1.source_system,
MAX(T1.snapshot_date) snapshot_date
FROM bhc_claim.medical_claim_detail T1 -- 277,011
GROUP BY T1.claim_number,
T1.source_system
),
Revenue_Code_Step1 AS
(
SELECT T1.claim_number,
T1.source_system,
T1.snapshot_date,
concat_ws(",", collect_set(DISTINCT T2.revenue_code)) Revenue_Code
FROM Claim_Detail_Step1 T1
INNER JOIN bhc_claim.medical_claim_detail T2
ON T1.claim_number = T2.claim_number
AND T1.source_system = T2.source_system
AND T1.snapshot_date = T2.snapshot_date -- 277,011
GROUP BY T1.claim_number,
T1.source_system,
T1.snapshot_date
),
Provider_Detail_Step1 AS
(
SELECT T1.claim_number,
T1.source_system,
T2.provider_npi,
T2.provider_tin,
T2.provider_type,
CASE WHEN provider_group_name IS NOT NULL THEN provider_group_name
ELSE CONCAT(T2.provider_first_name,T2.provider_last_name)
END provider_name,
T2.sequence_number
FROM Claim_Detail_Step1 T1
INNER JOIN bhc_claim.medical_claim_detail_provider T2
ON T1.claim_number = T2.claim_number
AND T1.source_system = T2.source_system
AND T1.snapshot_date = T2.snapshot_date
WHERE T2.provider_type = 'BILLING'
GROUP BY T1.claim_number,
T1.source_system,
T2.provider_npi,
T2.provider_tin,
T2.provider_type,
CASE WHEN provider_group_name IS NOT NULL THEN provider_group_name
ELSE CONCAT(T2.provider_first_name,T2.provider_last_name)
END,
T2.sequence_number
),
Market_Detail_Step1 AS
(
SELECT REPLACE(T1.hios_plan_id_standard_component_variant, '-', '') Hios_ID,
MAX(T1.plan_year) plan_year
FROM bdp.plans T1
GROUP BY 1
),
Market_Detail_Step2 AS
(
SELECT T1.Hios_ID,
T1.plan_year,
TRIM(T2.market) market
FROM Market_Detail_Step1 T1
INNER JOIN bdp.plans T2
ON T1.Hios_ID = REPLACE(T2.hios_plan_id_standard_component_variant, '-', '')
AND T1.plan_year = T2.plan_year
),
Market_Detail_Step3 AS
(
SELECT T1.member_id,
TO_DATE(T1.source_start_date) source_start_date,
TO_DATE(T1.source_end_date) source_end_date,
T2.market
FROM dev.enrollment__base_enrollment T1 -- 3,568,717 | 3,568,717
LEFT JOIN Market_Detail_Step2 T2
ON T2.Hios_ID = T1.plan_id
WHERE T1.source_start_date <> T1.source_end_date
AND T1.effectuation_date_utc IS NOT NULL
GROUP BY T1.member_id,
TO_DATE(T1.source_start_date),
TO_DATE(T1.source_end_date),
T2.market
),
Remark_Code_Step1 AS
(
SELECT T1.claim_number,
T1.source_system,
concat_ws(",", collect_set(DISTINCT T1.remark_code)) Remark_Code
FROM bhc_claim.medical_claim_detail_remark T1 -- 3,731,653 | 3,731,653
INNER JOIN bhc_claim.medical_claim_detail T2
ON T1.claim_number = T2.claim_number
AND T1.source_system = T2.source_system
AND T1.snapshot_date = T2.snapshot_date
GROUP BY T1.claim_number,
T1.source_system
),
Integration_Step1 AS
(
SELECT CASE WHEN T1.claim_number IS NOT NULL THEN T1.claim_number
WHEN T7.Claim_Number IS NOT NULL THEN T7.Claim_Number
WHEN T10.Claim_Number IS NOT NULL THEN T10.Claim_Number
END Claim_Number,
TO_DATE(CASE WHEN T1.claim_received_date IS NOT NULL THEN T1.claim_received_date
WHEN T7.Received_Date IS NOT NULL THEN T7.Received_Date
WHEN T10.Received_Date IS NOT NULL THEN T10.Received_Date
END) Received_Date,
TO_DATE(CASE WHEN T1.claim_processed_date IS NOT NULL THEN T1.claim_processed_date
WHEN T10.Processed_Date IS NOT NULL THEN T10.Processed_Date
END) Processed_Date,
TO_DATE(CASE WHEN T3.claim_service_date IS NOT NULL THEN T3.claim_service_date
WHEN T7.Service_Date IS NOT NULL THEN T7.Service_Date
WHEN T10.Service_Date IS NOT NULL THEN T10.Service_Date
END) Service_Date,
TO_DATE(T1.check_date) Check_Date,
CASE WHEN T7.Claim_Number IS NOT NULL THEN 'Loomis'
WHEN T10.Claim_Number IS NOT NULL THEN 'Loomis'
ELSE T1.source_system
END Source_System,
CASE WHEN T1.claim_status_description = 'P' AND T1.total_excluded_amount = T1.total_original_claim_amount THEN 'Denied'
WHEN T10.status = 'Issued' THEN 'Issued'
ELSE T1.claim_status_description
END Status,
T1.payment_status_description Payment_Status,
CASE WHEN T7.Claim_Number IS NOT NULL
THEN 'Pending' END Pend_Status,
CASE WHEN T10.Claim_Number IS NOT NULL
THEN 'Paid Previous' END Paid_Previous_Status,
T1.claim_submission_type_description Submission_Type,
T1.line_of_business Segment,
T7.Hold_Code,
T7.Pend_Code,
T5.Remark_Code,
T9.Revenue_Code,
CASE WHEN T7.Pend_Code IN('17','18','19','44','60','63','86','89','97')
OR T7.Hold_Code LIKE('%13%')
OR T7.Hold_Code LIKE('%70%')
THEN 'Bright'
ELSE 'Loomis'
END Bucket_Owner,
T1.member_id Member_ID,
T1.subscriber_id Subscriber_ID,
T1.subscriber_group_number Subscriber_Group,
T4.provider_npi,
CASE WHEN T4.provider_tin IS NOT NULL THEN T4.provider_tin
WHEN T7.tax_id IS NOT NULL THEN T7.tax_id
WHEN T10.Tax_ID IS NOT NULL THEN T10.Tax_ID
END provider_tin,
CASE WHEN T4.provider_name IS NOT NULL THEN T4.provider_name
WHEN T7.provider_name IS NOT NULL THEN T7.provider_name
WHEN T10.provider_name IS NOT NULL THEN T10.provider_name
END provider_name,
CASE WHEN T10.Billed_Amount IS NOT NULL THEN T10.Billed_Amount
ELSE (T1.total_billed_amount/100)
END Billed_Amount,
(T1.total_processed_amount/100) Processed_Amount,
CASE WHEN T10.Paid_Amount IS NOT NULL THEN T10.Paid_Amount
ELSE (T1.total_paid_amount/100)
END Paid_Amount,
(T1.total_interest_paid_amount/100) Interest_Paid_Amount,
T7.Pend_Amount
FROM bhc_claim.medical_claim T1 -- 3,472,165
INNER JOIN Snapshot_Date T2 -- 3,472,165
ON T1.claim_number = T2.claim_number
AND T1.source_system = T2.source_system
AND T1.snapshot_date = T2.snapshot_date
LEFT JOIN Service_Date T3 -- 3,472,165
ON T1.claim_number = T3.claim_number
AND T1.source_system = T3.source_system
LEFT JOIN Provider_Detail_Step1 T4 -- 3,498,170 MATCH
ON T1.claim_number = T4.claim_number
AND T1.source_system = T4.source_system
LEFT JOIN Remark_Code_Step1 T5
ON T1.claim_number = T5.claim_number
AND T1.source_system = T5.source_system
FULL OUTER JOIN Pend_Step4 T7 -- 3,472,419 | Match Disctinct 3,472,419
ON LEFT(T1.claim_number,10) = T7.Claim_Number
LEFT JOIN Revenue_Code_Step1 T9
ON T1.claim_number = T9.claim_number
AND T1.source_system = T9.source_system
FULL OUTER JOIN Paid_Previous_Step4 T10 --3,816,359 | Match Disctinct 3,472,419
ON LEFT(T1.claim_number,10) = T10.Claim_Number
),
FINAL AS
(
SELECT T1.Claim_Number,
T1.Received_Date,
T1.Processed_Date,
T1.Service_Date,
T1.Report_Date
T1.Check_Date,
T1.Source_System,
T1.Status,
T1.Payment_Status,
T1.Pend_Status,
T1.Paid_Previous_Status,
T1.Submission_Type,
T1.Segment,
MIN(T2.Market) Market,
T1.Hold_Code,
T1.Pend_Code,
T1.Remark_Code,
T1.Revenue_Code,
T1.Bucket_Owner,
T1.Member_ID,
T1.Subscriber_ID,
T1.Subscriber_Group,
T1.provider_npi,
T1.provider_tin,
T1.provider_name,
T1.Billed_Amount,
T1.Processed_Amount,
T1.Paid_Amount,
T1.Interest_Paid_Amount,
T1.Pend_Amount
FROM Integration_Step1 T1
LEFT JOIN Market_Detail_Step3 T2
ON T1.member_id = T2.member_id
AND T1.Service_Date >= T2.source_start_date
AND T1.Service_Date <= T2.source_end_date
GROUP BY T1.Claim_Number,
T1.Received_Date,
T1.Processed_Date,
T1.Service_Date,
T1.Report_Date,
T1.Check_Date,
T1.Source_System,
T1.Status,
T1.Payment_Status,
T1.Pend_Status,
T1.Paid_Previous_Status,
T1.Submission_Type,
T1.Segment,
T1.Hold_Code,
T1.Pend_Code,
T1.Remark_Code,
T1.Revenue_Code,
T1.Bucket_Owner,
T1.Member_ID,
T1.Subscriber_ID,
T1.Subscriber_Group,
T1.provider_npi,
T1.provider_tin,
T1.provider_name,
T1.Billed_Amount,
T1.Processed_Amount,
T1.Paid_Amount,
T1.Interest_Paid_Amount,
T1.Pend_Amount
)
SELECT T1.Claim_Number,
T1.Received_Date,
T1.Processed_Date,
T1.Service_Date,
T1.Report_Date,
T1.Check_Date,
T1.Source_System,
T1.Status,
T1.Payment_Status,
T1.Pend_Status,
T1.Paid_Previous_Status,
T1.Submission_Type,
T1.Segment,
T1.Market,
T1.Hold_Code,
T1.Pend_Code,
T1.Remark_Code,
T1.Revenue_Code,
T1.Bucket_Owner,
T1.Member_ID,
T1.Subscriber_ID,
T1.Subscriber_Group,
T1.provider_npi,
T1.provider_tin,
T1.provider_name,
T1.Billed_Amount,
T1.Processed_Amount,
T1.Paid_Amount,
T1.Interest_Paid_Amount,
T1.Pend_Amount
FROM FINAL T1-- 3,789,713
)
;
I think your intend to comment and use "/" and "/" is not correct and hence the error . Can you please remove them and test the query ?
Related
No output when one side of UNION is empty
I have an Azure Stream Analytics job that combines the results of multiple queries and outputs them to the same sink. To do this, I define my queries within a WITH statement, then combine them using UNION and then write them to my sink. However, unfortunately I only get an output to my sink whenever all of my queries actually have an output, and this is where it goes wrong. I have some queries that continuously (every 5 minutes) give an output, but I also have some queries that rare give an output (maybe a few times per day). This causes the output to not get any results, until the queries all have something to return. Does anyone know why this is and how I can fix it? Shouldn't UNION also give results when set A has results, but set B doesn't? I'm running this locally in VS Code, with a live connection to Event Hub by the way. Here is a simplified example of 2 queries (one with frequent output, one with infrequent output) that goes wrong: WITH HarmonizedMeasurements AS ( SELECT CAST(EHHARM.TimeStamp AS datetime) AS "TimeStamp", CAST(EHHARM.ValueNumber AS float) AS "ValueNumber", EHHARM.ValueBit AS "ValueBit", EHHARM.MeasurementName, EHHARM.PartName, EHHARM.ElementId, EHHARM.ElementName, EHHARM.ObjectName, EHHARM.TranslationTableId FROM EventHubHarmonizedMeasurements AS EHHARM TIMESTAMP BY "TimeStamp" PARTITION BY TranslationTableId ), ToerenAandrijvingCategoriesMeasurements AS ( SELECT AANDRCAT.TimeStamp AS "TimeStamp", AANDRCAT.ValueNumber AS "ValueNumber", AANDRCAT.ValueBit AS "ValueBit", AANDRCAT.MeasurementName AS "MeasurementName", AANDRCAT.PartName AS "PartName", AANDRCAT.ElementId AS "ElementId", AANDRCAT.ElementName AS "ElementName", AANDRCAT.ObjectName AS "ObjectName", AANDRCAT.TranslationTableId AS "TranslationTableId", CASE WHEN (-5000 < AANDRCAT.ValueNumber AND AANDRCAT.ValueNumber <= -1000) THEN 'Dalen' WHEN (-1000 < AANDRCAT.ValueNumber AND AANDRCAT.ValueNumber <= -200) THEN 'Dalen Retarderen' WHEN (-200 < AANDRCAT.ValueNumber AND AANDRCAT.ValueNumber <= 0) THEN 'Stilstand' WHEN (0 < AANDRCAT.ValueNumber AND AANDRCAT.ValueNumber <= 250) THEN 'Nivelleren' WHEN (250 < AANDRCAT.ValueNumber AND AANDRCAT.ValueNumber <= 400) THEN 'Heffen Retarderen' WHEN (400 < AANDRCAT.ValueNumber AND AANDRCAT.ValueNumber <= 5000) THEN 'Heffen' ELSE 'NoCategory' END AS "Category" FROM HarmonizedMeasurements AS AANDRCAT WHERE AANDRCAT.ObjectName LIKE 'Schutsluis%' AND AANDRCAT.MeasurementName = 'Motortoerental terugkoppeling' AND AANDRCAT.ValueNumber <> 0 ), AandrijvingCatStartMeasurements AS ( SELECT AANDRCAT.TimeStamp AS "StartTime", AANDRCAT.Category AS "Category", AANDRCAT.ElementId AS "ElementId", AANDRCAT.TranslationTableId AS "TranslationTableId" FROM ToerenAandrijvingCategoriesMeasurements AS AANDRCAT WHERE LAG(Category, 1) OVER (PARTITION BY ElementId LIMIT DURATION(day, 1)) <> Category ), AandrijvingCatEndMeasurements AS ( SELECT AANDRST.StartTime AS "EndTime", LAG(AANDRST.StartTime, 1) OVER (PARTITION BY ElementId LIMIT DURATION(day, 1)) AS "StartTime", LAG(AANDRST.Category, 1) OVER (PARTITION BY ElementId LIMIT DURATION(day, 1)) AS "Category", AANDRST.ElementId AS "ElementId", AANDRST.TranslationTableId AS "TranslationTableId" FROM AandrijvingCatStartMeasurements AS AANDRST ), VermogenAandrijvingMeasurements AS ( SELECT AANDRVER.TimeStamp AS "TimeStamp", AANDRVER.ValueNumber AS "ValueNumber", AANDRVER.ValueBit AS "ValueBit", CONCAT(AANDRVER.MeasurementName, ' ', AANDREN.Category) AS "MeasurementName", AANDRVER.PartName AS "PartName", AANDRVER.ElementId AS "ElementId", AANDRVER.ElementName AS "ElementName", AANDRVER.ObjectName AS "ObjectName", AANDRVER.TranslationTableId AS "TranslationTableId" FROM HarmonizedMeasurements AS AANDRVER LEFT JOIN AandrijvingCatEndMeasurements AS AANDREN ON DATEDIFF(minute, AANDRVER, AANDREN) BETWEEN 0 AND 30 AND AANDRVER.TimeStamp >= AANDREN.StartTime AND AANDRVER.Timestamp < AANDREN.EndTime AND AANDRVER.ElementId = AANDREN.ElementId AND AANDRVER.TranslationTableId = AANDREN.TranslationTableId INNER JOIN SQLMeasurementType AS MEAS ON MEAS.Name = CONCAT(AANDRVER.MeasurementName, ' ', AANDREN.Category) WHERE AANDRVER.ObjectName LIKE 'Schutsluis%' AND AANDRVER.MeasurementName = 'Vermogen' ), LockDoorTop AS ( SELECT Lock.TimeStamp AS "TimeStamp", Lock.ValueNumber AS "ValueNumber", Lock.ValueBit AS "ValueBit", Lock.MeasurementName, Lock.PartName, Lock.ElementId, Lock.ElementName, Lock.ObjectName, Lock.TranslationTableId FROM HarmonizedMeasurements AS Lock WHERE Lock.MeasurementName = 'Sluisdeur open' AND Lock.ElementName = 'Deur sluiskolk 1' AND Lock.PartName = 'Bovenhoofd' AND Lock.ValueBit = '1' ), WaterLTop AS ( SELECT WaterTop.TimeStamp AS "TimeStamp", WaterTop.ValueNumber AS "ValueNumber", WaterTop.ValueBit AS "ValueBit", WaterTop.MeasurementName, WaterTop.PartName, WaterTop.ElementId, WaterTop.ElementName, WaterTop.ObjectName, WaterTop.TranslationTableId FROM HarmonizedMeasurements AS WaterTop WHERE WaterTop.MeasurementName = 'Waterniveaumeting' AND WaterTop.ElementName = 'Sluiskolk 1' AND WaterTop.PartName = 'Opvaartzijde' ), WaterLLock AS ( SELECT WaterLock.TimeStamp AS "TimeStamp", WaterLock.ValueNumber AS "ValueNumber", WaterLock.ValueBit AS "ValueBit", WaterLock.MeasurementName, WaterLock.PartName, WaterLock.ElementId, WaterLock.ElementName, WaterLock.ObjectName, WaterLock.TranslationTableId FROM HarmonizedMeasurements AS WaterLock WHERE WaterLock.MeasurementName = 'Waterniveaumeting' AND WaterLock.ElementName = 'Sluiskolk 1' AND WaterLock.PartName = 'Sluiskamer' ), WaterLevelTopMeasurements AS ( SELECT LockDoor.TimeStamp AS "TimeStamp", CAST(ROUND((WaterLevelLock.ValueNumber - WaterLevelTop.ValueNumber), 2) AS float) AS "ValueNumber", null AS "ValueBit", MEAS.Name AS "MeasurementName", LockDoor.PartName AS "PartName", LockDoor.ElementId AS "ElementId", LockDoor.ElementName AS "ElementName", LockDoor.ObjectName AS "ObjectName", LockDoor.TranslationTableId AS "TranslationTableId" FROM LockDoorTop AS LockDoor JOIN WaterLTop AS WaterLevelTop ON DATEDIFF(minute, LockDoor, WaterLevelTop) BETWEEN 0 AND 1 AND LockDoor.ObjectName = WaterLevelTop.ObjectName JOIN WaterLLock AS WaterLevelLock ON DATEDIFF(minute, LockDoor, WaterLevelLock) BETWEEN 0 AND 1 AND LockDoor.ObjectName = WaterLevelLock.ObjectName INNER JOIN SQLMeasurementType AS MEAS ON MEAS.Name = 'Waterniveauverschil' ), -- Combine queries DatalakeCombinedMeasurements AS ( SELECT * FROM VermogenAandrijvingMeasurements UNION SELECT * FROM WaterLevelTopMeasurements ) -- Output data SELECT * INTO DatalakeHarmonizedMeasurements FROM DatalakeCombinedMeasurements PARTITION BY TranslationTableId
Could not add table 'SELECT('
Error given when adding query that runs in SQL developer but not in MS Query. Seems to not like my nested query. Code I am using: SELECT ORDER_DATE ,SALES_ORDER_NO ,CUSTOMER_PO_NUMBER ,DELIVER_TO ,STATUS ,ITEM_NUMBER ,DESCRIPTION ,ORD_QTY ,SUM(QUANTITY) AS ON_HAND ,PACKAGE_ID ,PACKAGE_STATUS ,MAX(TRAN_DATE) AS LAST_TRANSACTION ,MIN(DAYS) AS DAYS FROM ( SELECT TRUNC(SH.ORDER_DATE) AS ORDER_DATE ,SH.SALES_ORDER_NO ,SH.CUSTOMER_PO_NUMBER ,SH.SHIP_CODE AS DELIVER_TO ,SH.STATUS ,SB.ITEM_NUMBER ,IM.DESCRIPTION ,SB.ORD_QTY ,BID.QUANTITY ,SPM.PACKAGE_ID ,CASE WHEN SPM.SHIPPED = 'Y' THEN 'SHIPPED' WHEN SPM.STATUS = 'C' THEN 'PACKED' WHEN BID.QUANTITY IS NOT NULL THEN 'AVAILABLE' WHEN BID.QUANTITY IS NULL THEN 'UNAVAILABLE' END AS PACKAGE_STATUS ,CASE WHEN SPM.SHIPPED = 'Y' THEN TRUNC(SPM.BILLING_DATE) WHEN SPM.STATUS = 'C' THEN TRUNC(SPM.END_TIME) WHEN BID.QUANTITY IS NOT NULL THEN TRUNC(BID.ACTIVATION_TIME) END AS TRAN_DATE ,CASE WHEN SPM.SHIPPED = 'Y' THEN ROUND(SYSDATE - SPM.BILLING_DATE, 0) WHEN SPM.STATUS = 'C' THEN ROUND(SYSDATE - SPM.END_TIME, 0) WHEN BID.QUANTITY IS NOT NULL THEN ROUND(SYSDATE - BID.ACTIVATION_TIME, 0) END AS DAYS FROM SO_HEADER SH LEFT JOIN SO_BODY SB ON SB.SO_HEADER_TAG = SH.SO_HEADER_TAG LEFT JOIN SO_PACKAGE_MASTER SPM ON SPM.PACKAGE_ID = SB.PACKAGE_ID LEFT JOIN ITEM_MASTER IM ON IM.ITEM_NUMBER = SB.ITEM_NUMBER LEFT JOIN V_BIN_ITEM_DETAIL BID ON BID.ITEM_NUMBER = SB.ITEM_NUMBER WHERE SH.ORDER_TYPE = 'MSR' AND ( SB.REASON_CODE IS NULL OR SB.REASON_CODE NOT LIKE 'CANCEL%' ) AND ( IM.DESCRIPTION NOT LIKE '%CKV%' OR IM.DESCRIPTION IS NULL AND IM.ITEM_NUMBER IS NOT NULL ) ) WHERE PACKAGE_STATUS <> 'SHIPPED' GROUP BY ORDER_DATE ,SALES_ORDER_NO ,CUSTOMER_PO_NUMBER ,DELIVER_TO ,STATUS ,ITEM_NUMBER ,DESCRIPTION ,ORD_QTY ,PACKAGE_ID ,PACKAGE_STATUS ORDER BY ( CASE PACKAGE_STATUS WHEN 'AVAILABLE' THEN 1 WHEN 'UNAVAILABLE' THEN 2 WHEN 'PACKED' THEN 3 END ) ,LAST_TRANSACTION; Is there any option I can select that will allow me to run this query?
Am getting operand clash error when running a datawarehouse compatible script
Hi I am getting an error like this: Operand type clash: date is incompatible with int. Below is my query which I am running on a SQL Server: CREATE TABLE val.census_last_month WITH( DISTRIBUTION = ROUND_ROBIN , CLUSTERED COLUMNSTORE INDEX ) AS SELECT dt_mydate AS dt_census, (SELECT count(DISTINCT encounter_id) FROM prod.encounter WHERE encounter_type = 'Inpatient' AND (ts_admit BETWEEN dt_mydate - 30 AND dt_mydate) AND (ts_discharge IS NULL OR ts_discharge > dt_mydate)) AS census, (SELECT count(DISTINCT encounter_id) FROM prod.encounter WHERE encounter_type = 'Inpatient' AND cast(ts_admit AS DATE) = dt_mydate) AS admits, (SELECT count(DISTINCT encounter_id) FROM prod.encounter WHERE encounter_type = 'Inpatient' AND cast(ts_discharge AS DATE) = dt_mydate) AS discharges FROM ref.calendar_day WHERE ref.calendar_day.dt_mydate BETWEEN (cast(getdate() as date) - 30) AND cast(getdate() as date);
You need to use dateadd function. See details here. https://learn.microsoft.com/en-us/sql/t-sql/functions/dateadd-transact-sql?view=sql-server-2017
There are multiple issues with this script, however can you confirm the datatypes starting "ts_" are dates stored as integers in the format yyyyMMdd and datatypes starting "dt_" are DATE? Based on these assumptions, this is my attempted rewrite: SELECT dt_mydate AS dt_census, ( SELECT COUNT( DISTINCT encounter_id ) FROM prod.encounter WHERE encounter_type = 'Inpatient' AND ( CAST( CAST( ts_admit AS CHAR(8) ) AS DATE ) BETWEEN DATEADD( day, -30, dt_mydate ) AND dt_mydate ) AND ( ts_discharge IS NULL OR CAST( CAST( ts_discharge AS CHAR(8) ) AS DATE ) > dt_mydate) ) AS census, ( SELECT COUNT( DISTINCT encounter_id ) FROM prod.encounter WHERE encounter_type = 'Inpatient' AND CAST( CAST( ts_admit AS CHAR(8) ) AS DATE ) = dt_mydate ) AS admits, ( SELECT COUNT( DISTINCT encounter_id ) FROM prod.encounter WHERE encounter_type = 'Inpatient' AND CAST( CAST( ts_discharge AS CHAR(8) ) AS DATE ) = dt_mydate ) AS discharges FROM ref.calendar_day WHERE ref.calendar_day.dt_mydate BETWEEN CAST ( DATEADD( day, -30, GETDATE() ) AS DATE ) AND CAST( GETDATE() AS DATE ); If either of my assumptions are incorrect, please let me know and I will update the script.
Recursive Relationship Query
I am looking to implement graph tables to map the role hierarchy for my application in Azure SQL. The graph will look like a tree if it is laid out. With the parent being able to manage any role that falls beneath it on the tree. So I have a roles node table and a canmanage edge table. I am familiar with querying the first level and the second level of relationships, however I need to have a query where I can put in any role and receive a list of all the children that fall under it. I am familiar with this sort of thing in NEO4J, but I have not found any documentation on how to accomplish this in Azure SQL. How do I go about running a recursive query to get all the child roles give a specific role name or id?
This is possible from SQL Server 2017 and Azure SQL DB using the new graph database capabilities and the new MATCH clause to model this type of relationship. Unfortunately in v1 polymorphism and transitive closure are not natively included but are possible using recursive queries. If you look at the last query, it keep the parameter you input as the top-level manager and iterates over the rest. A sample script: USE tempdb GO -- NODES DROP TABLE IF EXISTS dbo.roles -- EDGES DROP TABLE IF EXISTS dbo.canManage DROP TABLE IF EXISTS dbo.isManagedBy GO CREATE TABLE dbo.roles ( roleId INT PRIMARY KEY, roleName VARCHAR(20) UNIQUE NOT NULL ) AS NODE CREATE TABLE dbo.canManage AS EDGE; CREATE TABLE dbo.isManagedBy AS EDGE; GO -- Populate node table INSERT INTO dbo.roles ( roleId, roleName ) VALUES ( 1, 'CEO' ), ( 2, 'VP 1' ), ( 3, 'VP 2' ), ( 4, 'Sales Manager 1' ), ( 5, 'Sales Manager 2' ), ( 6, 'Ops Manager 1' ), ( 7, 'Ops Manager 2' ), ( 8, 'Sales Lead 1' ), ( 9, 'Salesperson 1' ), ( 10, 'Salesperson 2' ), ( 11, 'Salesperson 3' ) GO -- Populate edge table INSERT INTO dbo.canManage ( $from_id, $to_id ) SELECT ceo.$node_id, VPs.$node_id FROM dbo.roles ceo CROSS JOIN dbo.roles VPs WHERE ceo.roleName = 'CEO' AND VPs.roleName Like 'VP%' -- VP 1 manages Sales Managers INSERT INTO dbo.canManage ( $from_id, $to_id ) SELECT a.$node_id, b.$node_id FROM dbo.roles a CROSS JOIN dbo.roles b WHERE a.roleName = 'VP 1' AND b.roleName Like 'Sales Manager%' -- VP 2 manages Ops Managers INSERT INTO dbo.canManage ( $from_id, $to_id ) SELECT a.$node_id, b.$node_id FROM dbo.roles a CROSS JOIN dbo.roles b WHERE a.roleName = 'VP 2' AND b.roleName Like 'Ops Manager%' -- Sales Manger 1 manages Sales Leads INSERT INTO dbo.canManage ( $from_id, $to_id ) SELECT a.$node_id, b.$node_id FROM dbo.roles a CROSS JOIN dbo.roles b WHERE a.roleName = 'Sales Manager 1' AND b.roleName Like 'Sales Lead%' -- Sales Leads 1 manages all salespersons INSERT INTO dbo.canManage ( $from_id, $to_id ) SELECT a.$node_id, b.$node_id FROM dbo.roles a CROSS JOIN dbo.roles b WHERE a.roleName = 'Sales Lead 1' AND b.roleName Like 'Salesperson%' -- Create the inverse edge / relationship INSERT INTO dbo.isManagedBy ( $from_id, $to_id ) SELECT $to_id, $from_id FROM dbo.canManage GO -- Now write the graph queries: -- Manages SELECT FORMATMESSAGE( '%s manages %s', r1.roleName, r2.roleName ) manages FROM dbo.roles r1, dbo.canManage canManage, dbo.roles r2 WHERE MATCH ( r1-(canManage)->r2 ) -- Same manager SELECT FORMATMESSAGE( '%s and %s have the same manager %s', r1.roleName, r3.roleName, r2.roleName ) FROM dbo.roles r1, dbo.isManagedBy m1, dbo.roles r2, dbo.isManagedBy m2, dbo.roles r3 WHERE MATCH ( r1-(m1)->r2<-(m2)-r3 ) AND r1.$node_id < r3.$node_id -- Recursive -- walk the tree ... CEO manages everyone ... ;WITH cte AS ( SELECT 1 xlevel, r1.roleName manager, r2.roleName managed FROM dbo.roles r1, dbo.canManage canManage, dbo.roles r2 WHERE MATCH ( r1-(canManage)->r2 ) AND r1.roleName = 'CEO' UNION ALL SELECT c.xlevel + 1, r1.roleName, r2.roleName FROM cte c, dbo.roles r1, dbo.canManage canManage, dbo.roles r2 WHERE c.managed = r1.roleName AND MATCH ( r1-(canManage)->r2 ) ) SELECT * FROM cte ORDER BY xlevel, manager, managed ;WITH cte AS ( SELECT 1 xlevel, r1.roleName manager, r2.roleName managed FROM dbo.roles r1, dbo.canManage canManage, dbo.roles r2 WHERE MATCH ( r1-(canManage)->r2 ) AND r1.roleName = 'CEO' UNION ALL SELECT c.xlevel + 1, c.manager, r2.roleName FROM cte c, dbo.roles r1, dbo.canManage canManage, dbo.roles r2 WHERE c.managed = r1.roleName AND MATCH ( r1-(canManage)->r2 ) ) SELECT * FROM cte ORDER BY xlevel, manager, managed
Using Case to match strings in sql server?
I am trying to use CASE in a SQL Select statement that will allow me to get results where I can utilize the length of one string to produce the resutls of another string. These are for non-matched records from two data sets that share a common ID, but variant Data Source. Case statement is below: Select Column1, Column2, Case When Column1 = 'Something" and Len(Column2) = '35' Then Column1 = "Something Else" and substring(Column2, 1, 35) End as Column3 From dbo.xxx When I run it I get the following error: Msg 102, Level 15, State 1, Line 5 Incorrect syntax near '='.
You need to have a value for each WHEN, and ought to have an ELSE: Select Data_Source, CustomerID, CASE WHEN Data_Source = 'Test1' and Len(CustomerName) = 35 THEN 'First Value' WHEN Data_Source = 'Test2' THEN substring(CustomerName, 1, 35) ELSE 'Sorry, no match.' END AS CustomerName From dbo.xx FYI: Len() doesn't return a string. EDIT: A SQL Server answer that addresses some of the comments might be: declare #DataSource as Table ( Id Int Identity, CustomerName VarChar(64) ) declare #VariantDataSource as Table ( Id Int Identity, CostumerName VarChar(64) ) insert into #DataSource ( CustomerName ) values ( 'Alice B.' ), ( 'Bob C.' ), ( 'Charles D.' ) insert into #VariantDataSource ( CostumerName ) values ( 'Blush' ), ( 'Dye' ), ( 'Pancake Base' ) select *, -- Output the CostumerName padded or trimmed to the same length as CustomerName. NULLs are not handled gracefully. Substring( CostumerName + Replicate( '.', Len( CustomerName ) ), 1, Len( CustomerName ) ) as Clustermere, -- Output the CostumerName padded or trimmed to the same length as CustomerName. NULLs in CustomerName are explicitly handled. case when CustomerName is NULL then '' when Len( CustomerName ) > Len( CostumerName ) then Substring( CostumerName, 1, Len( CustomerName ) ) else Substring( CostumerName + Replicate( '.', Len( CustomerName ) ), 1, Len( CustomerName ) ) end as 'Crustymore' from #DataSource as DS inner join #VariantDataSource as VDS on VDS.Id = DS.Id
Select Column1, Column2, Case When Column1 = 'Something' and Len(Column2) = 35 Then 'Something Else' + substring(Column2, 1, 35) End as Column3 From dbo.xxx Update your query on use '+' for string concat len() returns int, no need to use '' remove "Column1 =" in the case when condition replace "" with '' Hope this help.