Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 months ago.
Improve this question
I have a table in SQL Server, which has a column, which stores json object.
For example:
{
"definition":{
"analysisDefinitionId":16136,
"ptfProcLinkId":34959,
"version":2,
"dataVersion":1655821735508,
"originType":"uw",
"originPeriodFrequency":"an",
"developmentPeriodFrequency":"qu",
"startPeriod":"201001",
"endPeriod":"202109",
"calendarEnd":"202109",
"largeLosses":{
"excludeLargeLosses":[
],
"excludeLargeLossesEvents":[
"300063",
"302292"
]
},
"statisticalAdjustments":[
],
"includeCurrentPeriod":false,
"distributionMethod":"BF"
},
"includeCedentShift":true,
"calculateCedentShift":true,
"reportedClaimsType":"excluding_acr",
"userId":"195b47d7-6b8f-4ccd-a35b-eed688c5abc7",
"unit":"ones"
}
I want to retrieve values in this path: $.definition.largeLosses.excludeLargeLossesEvents, so basically to get two values: 300063, 302292, in two rows.
to recreate this scenario, you can run this:
CREATE TABLE [dbo].[bu_json_test]
( [id] INT IDENTITY (1, 1) NOT NULL,
[js_state] NVARCHAR (MAX) NULL
);
insert into [dbo].[bu_json_test] (js_state)
values ('{"definition":{"analysisDefinitionId":16136,"ptfProcLinkId":34959,"version":2,"dataVersion":1655821735508,"originType":"uw","originPeriodFrequency":"an","developmentPeriodFrequency":"qu","startPeriod":"201001","endPeriod":"202109","calendarEnd":"202109","largeLosses":{"excludeLargeLosses":[],"excludeLargeLossesEvents":["300063","302292"]},"statisticalAdjustments":[],"includeCurrentPeriod":false,"distributionMethod":"BF"},"includeCedentShift":true,"calculateCedentShift":true,"reportedClaimsType":"excluding_acr","userId":"195b47d7-6b8f-4ccd-a35b-eed688c5abc7","unit":"ones"}');
Now my select looks like this, but getting NULL values, altough select recognizes, that the result should have 2 rows:
SELECT
tbl.id,
gle.excludeLargeLossesEvents
FROM [dbo].[bu_json_test] tbl
OUTER APPLY OPENJSON(tbl.js_state, '$.definition.largeLosses.excludeLargeLossesEvents')
WITH (excludeLargeLossesEvents varchar(200)) gle
WHERE tbl.id = 1
So I expect
1 300063
1 302292
But getting
1 NULL
1 NULL
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last year.
Improve this question
I need to combine the results from two different CTEs into a single table. Hence, I have written the following SQL query wherein I attempted to create a left join on two different fields (patient_id, quarter in example below).
Unfortunately, I am getting a SQL compilation error (Unknown user defined function A.YR_QTR). But I have declared this variable in the code so am at a loss to why this error is cropping up. My code follows below:
WITH op_clms AS
(
SELECT
MC.UNIV_MBR_ID AS univ_mbr_id,
MC.YR_QTR AS YR_QTR,
COUNT DISTINCT MC.ENC_KEY AS op_clms
FROM
CRF.MED_CLAIMS AS MC
WHERE
MC.SERVICE BETWEEN '2016-01-01' AND '2020-12-31'
AND PLACE_OF_SERVICE ! = '23'
),
ed_clms AS
(
SELECT
MC.UNIV_MBR_ID AS univ_mbr_id,
MC.YR_QTR AS YR_QTR,
COUNT DISTINCT MC.ENC_KEY AS ed_clms
FROM
CRF.MED_CLAIMS AS MC
WHERE
MC.SERVICE BETWEEN '2016-01-01' AND '2020-12-31'
AND PLACE_OF_SERVICE = '23'
)
SELECT
a.UNIV_MBR_ID, a.YR_QTR
(IFNULL(op_clms, 0)) AS op_clms,
(IFNULL(ed_clms,0)) AS ed_clms,
1 as cohort
FROM
(SELECT UNIV_MBR_ID, YR_QTR
FROM op_clms
UNION
SELECT UNIV_MBR_ID, YR_QTR
FROM ed_clms) a
LEFT JOIN
op_clms ON a.UNIV_MBR_ID = op_clms.UNIV_MBR_ID
AND a.YR_QTR = op_clms.YR_QTR
LEFT JOIN
ed_clms ON a.UNIV_MBR_ID = ed_clms.UNIV_MBR_ID
AND a.YR_QTR = ed_clms.YR_QTR
Can somebody please guide me on what I am doing wrong? Each of the individual CTEs check out fine on their own. And I cant find anything else syntactically/logically wrong with my code.
It appears in my original code, I inadvertently omitted a comma after a.YR_QTR. Adding that as shown below, fixes the issue!
a.UNIV_MBR_ID, a.YR_QTR,
(IFNULL(op_clms, 0)) AS op_clms,
(IFNULL(ed_clms,0)) AS ed_clms,
1 as cohort
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I have the following SQL Statement, which gives me an error.
UPDATE GNGRB.BS_CLOSING
SET ENDINGDATE + STATUS + STATUSDATE = NULL
WHERE UNIT = '231296' AND BMON = '2020114';
What is wrong with that code?
You can update multiple columns like that:
UPDATE GNGRB.BS_CLOSING
SET ENDINGDATE = NULL
,STATUS = NULL
,STATUSDATE = NULL
WHERE UNIT = '231296'
AND BMON = '2020114';
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
Junior here with no one to help me but the void of the internet and my googling skills are mediocre at best.
The following syntax returns the information I need however, it's giving me ALL dates in the table and ignoring the WHERE clause. There are 3 tables I'm trying to Join: 'ProductHistory' is the main one, with one column needed from 'Product' and a date field from the third table 'MainJobDetails'.
SELECT [ProductHistory].[Type]
,[ProductHistory].[Ref]
,[ProductHistory].[JobNo]
,[ProductHistory].[Quantity]
,[ProductHistory].[PurchasePrice]
,[ProductHistory].[UnitPrice]
,[ProductHistory].[UnitDesc]
,[ProductHistory].[ProductID]
,[ProductHistory].[Location]
,[ProductHistory].[UserID]
,[Product].[Description]
,[Product].[StkRef1]
,[MainJobDetails].[DespatchDate]
FROM [ProductHistory]
JOIN [Product] ON [ProductHistory].[ProductID] = [Product].[ID]
JOIN [MainJobDetails] ON [ProductHistory].[JobNo] = [MainJobDetails].[JobNo]
WHERE YEAR([MainJobDetails].[DespatchDate]) = 2020
AND MONTH([MainJobDetails].[DespatchDate]) = 11
AND [ProductHistory].[Type] = 5
OR [ProductHistory].[Type] = 6
ORDER BY [MainJobDetails].[DespatchDate] DESC
I've tried changing the WHERE clause to:
WHERE [MainJobDetails].[DespatchDate] BETWEEN '2020/10/31' AND '2020/11/30'
But it made no difference.
This is another similar query I've used previously and it works fine:
SELECT [MainJobDetails].[JobNo]
,[MainJobDetails].[EstimateHeaderRef]
,[MainJobDetails].[InvoiceCustomerName]
,[MainJobDetails].[JobDesc]
,[MainJobDetails].[DespatchDate]
,[FinishingInput].[Code]
,[FinishingInput].[Description]
,[FinishingInput].[Runs]
,[FinishingInput].[Timedb]
FROM [MainJobDetails]
LEFT JOIN [FinishingInput]
ON [MainJobDetails].[EstimateHeaderRef]=[FinishingInput].[EstimateHeaderRef]
WHERE [MainJobDetails].[DespatchDate] BETWEEN '2020/11/01' AND '2020/12/31'
ORDER BY [MainJobDetails].[DespatchDate] DESC
What am I getting wrong in the first statement?
Many thanks in advance for your help.
Put the OR condition within parentheses:
WHERE
YEAR([MainJobDetails].[DespatchDate]) = 2020
AND MONTH([MainJobDetails].[DespatchDate]) = 11
AND ([ProductHistory].[Type] = 5 OR [ProductHistory].[Type] = 6)
--^-- here and here --^--
Why you need that is because OR has lower priority than AND in logical expressions. So your original code (without the parentheses) is equivalent to:
WHERE
(
YEAR([MainJobDetails].[DespatchDate]) = 2020
AND MONTH([MainJobDetails].[DespatchDate]) = 11
AND ([ProductHistory].[Type] = 5
)
OR [ProductHistory].[Type] = 6
You can see that this allows any product with Type 6, regardless of other conditions.
I would also suggest further optimizations:
use direct filtering on the date rather than applying date functions no the stored value - this is much more efficient
use IN instead of ORed conditions
So:
WHERE
[MainJobDetails].[DespatchDate] >= '20201101'
AND [MainJobDetails].[DespatchDate] < '20201201'
AND [ProductHistory].[Type] IN (5, 6)
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
If I add custom column in select, I get this error:
20018 Invalid column name '2'.
Here is query example
SELECT
[msg].[MessageTo],
[msg].[MessageFrom],
[msg].[SendTime],
[msg].[ReceiveTime],
[msg].[id],
'2' AS source,
[kat].[id] AS [CategoriId],
[kat].[naziv] AS [CategoriName]
FROM
[SMSServer_1].[dbo].[MessageIn] AS [msg]
LEFT JOIN [Tekijanka].[dbo].[crm_poruka] AS [por] ON [por].[fk_poruka] = [msg].[id]
AND [por].[fk_source] = [2]
LEFT JOIN [Tekijanka].[dbo].[crm_kategorije_poruka] AS [kat] ON [kat].[id] = [por].[fk_kategorija]
WHERE
msg.id NOT IN (
SELECT
fk_poruka
FROM
Tekijanka.dbo.crm_poruka
WHERE
fk_status <> 1
)
ORDER BY
[SendTime] DESC
Is there any way to fix it?
The problem is in LEFT JOIN, not in the SELECT section:
AND [por].[fk_source] = [2]
This condition tries to join fk_source and column named [2]. Of course, there's no such column in tables MessageIn and crm_poruka. You have to change this part of the code (remove the condition or change it to AND [por].[fk_source] = 2).
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Hello i need to know when (date) the 10 (tenth) item as been sold, the ID item is passed per parameter,
this is the sales tables:
Thks in advance
Please try the following (I believe* that I used the correct columns):
// This example finds the date that the 10th occurrence of Part #1001 was sold.
// #idArtigo is the placeholder for your incoming parameter
DECLARE #idArtigo int;
SET #idArtigo = 1001;
WITH Artigos AS (
SELECT
Row_Number() OVER(ORDER BY v.[DataHora] ASC, lv.[IdLinhaVenda] ASC) AS RowNumber,
v.[DataHora],
v.[IdVenda],
lv.[IdLinhaVenda]
FROM
[Vendas] AS v
INNER JOIN [LinhasVenda] AS lv
ON ( v.[IdVenda] = lv.[IdVenda] )
WHERE
lv.[IdArtigo] = #idArtigo
)
SELECT
[DataHora],
[IdVenda],
[IdLinhaVenda]
FROM
Artigos
WHERE
RowNumber = 10;
*NOTE: I do not know Portuguese (other than via Google Translate), so I made some educated guesses (& assumptions) as to which columns to use.