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).
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 2 years ago.
Improve this question
SELECT
SUM(a.totalsales) AS TotalSale,
SUM(a.cfee) + SUM(b.fee) * (TotalSale <> 0)
FROM
[dbo].[tbl1] AS a
INNER JOIN
[dbo].[tbl2] AS b ON a.[code] = b.[code]
WHERE
a.ID = '1234';
I'm getting this error:
Incorrect syntax near '<'
If you need condition on aggretated result then use HAVING
SELECT SUM(a.totalsales) AS TotalSale,
ISNULL(SUM(a.cfee) + SUM(b.fee)* SUM(a.totalsales),0) tot
FROM [dbo].[tbl1] AS a
INNER JOIN [dbo].[tbl2] AS b
ON a.[code]= b.[code]
WHERE a.ID= '1234'
HAVING SUM(a.totalsales) <>0;
and you can't use column name alias in select clause you must repeat the code
Try this:
SELECT SUM(a.totalsales) AS TotalSale,
(SUM(a.cfee)
+
SUM(b.fee)* SUM(a.totalsales) ) as sm
FROM [dbo].[tbl1] AS a
INNER JOIN [dbo].[tbl2] AS b
ON a.[code]= b.[code]
WHERE a.ID= '1234'
having SUM(a.totalsales)<>0;
I hope it helps
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 6 years ago.
Improve this question
I am having trouble with this piece of code, as it is saying "Error at line 22
ORA-00907: missing right parenthesis" when I execute it.
SELECT DC.ACJ_ID,
DC.MCI_UNIQ_ID,
DG.GENDER_COMMON_DESC,
DR.RACE_COMMON_DESC,
AD.ADDRESS_LINE_1,
AD.ADDRESS_LINE_2,
AD.CITY,
AD.STATE,
AD.ZIP_CD,
AD.ZIP_CD_9,
DC_RANK
FROM DW_CLNTVIEW.FACT_CLIENT_WEEK_LATEST FCAS,
DW.DIM_GENDER DG,
DW.DIM_RACE DR,
DW.DIM_ADDRESS AD,
KLISA.JAIL_IDS DOC,
(SELECT ACJ_ID,
MCI_UNIQ_ID,
DENSE_RANK ()
OVER (PARTITION BY ACJ_ID
ORDER BY END_DATE DESC, EFF_DATE DESC, MCI_UNIQ_ID DESC ---REMOVE “MCI_UNIQ_ID DESC” TO ALLOW DUPLICATES ON ACJ_ID
DC_RANK
FROM DW.DIM_CLIENT) DC
WHERE FCAS.RACE_KEY = DR.RACE_KEY(+)
AND FCAS.SRC_SYS_KEY(+) = 0
AND FCAS.GENDER_KEY = DG.GENDER_KEY(+)
AND FCAS.ADDRESS_KEY = AD.ADDRESS_KEY(+)
AND DC.MCI_UNIQ_ID = FCAS.MCI_UNIQ_ID(+)
AND ACJS.ACJ_ID = DC.ACJ_ID(+)
AND DC_RANK(+) = 1;
You have unbalanced parenthesis.
(SELECT ACJ_ID,
Left.
OVER (PARTITION BY ACJ_ID
Another left.
FROM DW.DIM_CLIENT) DC
Closed one pair.
Thus leaving one open.