I have a lengthy query which I am trying to understand. I learned in this forum that the best way to understand complex queries is to split them, check their results separately and then combine. I am pasting a part of code here.
Please help me understand why its giving me an error: Unable to parse query text. Incorrect syntax near the keyword 'AS'.
(SELECT AppID, AppDetailID, AppDetailSavePointID, FieldID, Value AS Acct_Renewed
FROM v_RptAppDetailField_v001
WHERE (FieldID IN (- 87, - 88, - 152, - 179, - 258, - 263))
AND (AppDetailSavePointID = 0) AND (Value IS NOT NULL)) AS Acct_Renewed
ON
v_RptFlatAppDetail_v001.AppDetailID = Acct_Renewed.AppDetailID AND
v_RptFlatAppDetail_v001.AppID = Acct_Renewed.AppID
When I deleted this part of code from the above code. I got the results. Please help me understand the ON clause here. I am confused as it doesn't mention any join as well. Also please be aware that there was a left outer join right after this code.
AS Acct_Renewed
ON
v_RptFlatAppDetail_v001.AppDetailID = Acct_Renewed.AppDetailID AND
v_RptFlatAppDetail_v001.AppID = Acct_Renewed.AppID
Appreciate your help. Thank you in advance!
Your first block of SQL isn't a complete query. The part you delete is the extra piece that is part of the larger query.
What's happening is that this sql...
SELECT AppID, AppDetailID, AppDetailSavePointID, FieldID, Value AS Acct_Renewed
FROM v_RptAppDetailField_v001
WHERE (FieldID IN (- 87, - 88, - 152, - 179, - 258, - 263))
AND (AppDetailSavePointID = 0) AND (Value IS NOT NULL)
...is being used as a subquery. You can get more details here: http://technet.microsoft.com/en-us/library/ms189575(v=sql.105).aspx
As a subquery, it is used just like a table. That's why it has AS Acct_Renewed followed by an ON clause. This subquery is being joined to the previous table that you didn't copy using that ON clause.
The following part of the query, which gives you results, independently is called an inline view. You can give a name (alias) for inline views. Here, it is given the name Acct_Renewed.
(SELECT AppID, AppDetailID, AppDetailSavePointID, FieldID, Value AS Acct_Renewed
FROM v_RptAppDetailField_v001
WHERE (FieldID IN (- 87, - 88, - 152, - 179, - 258, - 263))
AND (AppDetailSavePointID = 0) AND (Value IS NOT NULL)
The following conditions indicate that the inline view is joined with another table / view / inline view named as v_RptFlatAppDetail_v001.
ON
v_RptFlatAppDetail_v001.AppDetailID = Acct_Renewed.AppDetailID AND
v_RptFlatAppDetail_v001.AppID = Acct_Renewed.AppID
References:
Inline views (Oracle) on MacLochlainns Weblog
A Visual Explanation of SQL Joins on Coding Horror
Select AppID,
AppDetailID,
AppDetailSavePointID,
FieldID,
Value AS Acct_Renew
(SELECT
AppID,
AppDetailID,
AppDetailSavePointID,
FieldID,
Value AS Acct_Renewed
FROM v_RptAppDetailField_v001 V
WHERE (V.FieldID IN (- 87, - 88, - 152, - 179, - 258, - 263))
AND (VAppDetailSavePointID = 0) AND (V.Value IS NOT NULL )
FROM v_RptAppDetailField_v001 VV
INNER JOIN v_RptAppDetailField_v001 V
ON
v.AppDetailID = vv.AppDetailID AND
v.AppID = vv.AppID
Related
I've create 3 computed columns as alias and then used the aliased columns to calculate the total cost. This is the query:
SELECT TOP 1000 [Id]
,[QuantityOfProduct]
,[Redundant_ProductName]
,[Order_Id]
,(CASE
WHEN [PriceForUnitOverride] is NULL
THEN [Redundant_PriceForUnit]
ELSE
[PriceForUnitOverride]
END
) AS [FinalPriceForUnit]
,(CASE
WHEN [QuantityUnit_Override] is NULL
THEN [Redundant_QuantityUnit]
ELSE
[QuantityUnit_Override]
END
) AS [FinalQuantityUnit]
,(CASE
WHEN [QuantityAtomic_Override] is NULL
THEN [Redundant_QuantityAtomic]
ELSE
[QuantityAtomic_Override]
END
) AS [Final_QuantityAtomic]
--***THIS IS WHERE THE QUERY CREATES AN ERROR***--
,([QuantityOfProduct]*[FinalPriceForUnit]*
([Final_QuantityAtomic]/[FinalQuantityUnit])) AS [Final_TotalPrice]
FROM [dbo].[ItemInOrder]
WHERE [IsSoftDeleted] = 0
ORDER BY [Order_Id]
The console returns this ERROR message:
Msg 207, Level 16, State 1, Line 55
Invalid column name 'FinalPriceForUnit'.
Msg 207, Level 16, State 1, Line 55
Invalid column name 'Final_QuantityAtomic'.
Msg 207, Level 16, State 1, Line 55
Invalid column name 'FinalQuantityUnit'.
If I remove the "AS [Final_TotalPrice]" alias computed column, no error occurs, but I need the total price. How can I solve this issue? It seems as the other aliases have not been created when the Final_TotalPrice is reached.
You can't use table aliases in the same select. The normal solution is CTEs or subqueries. But, SQL Server also offers APPLY. (Oracle also supports APPLY and other databases such as Postgres support lateral joins using the LATERAL keyword.)
I like this solution, because you can create arbitrarily nested expressions and don't have to worry about indenting:
SELECT TOP 1000 io.Id, io.QuantityOfProduct, io.Redundant_ProductName,
io.Order_Id,
x.FinalPriceForUnit, x.FinalQuantityUnit, x.Final_QuantityAtomic,
(x.QuantityOfProduct * x.FinalPriceForUnit * x.Final_QuantityAtomic / x.FinalQuantityUnit
) as Final_TotalPrice
FROM dbo.ItemInOrder io OUTER APPLY
(SELECT COALESCE(PriceForUnitOverride, Redundant_PriceForUnit) as FinalPriceForUnit,
COALESCE(QuantityUnit_Override, Redundant_QuantityUnit) as FinalQuantityUnit
COALESCE(QuantityAtomic_Override, Redundant_QuantityAtomic) as Final_QuantityAtomic
) x
WHERE io.IsSoftDeleted = 0
ORDER BY io.Order_Id ;
Notes:
I don't find that [ and ] help me read or write queries at all.
COALESCE() is much simpler than your CASE statements.
With COALESCE() you might consider just putting the COALESCE() expression in the final calculation.
You can't use an alias in the same select. What you can do is find the value in subquery and then use it outside in the expression (or may be repeated the whole case statement in your expression). Also, use COALESCE to instead of CASE.
select t.*,
([QuantityOfProduct] * [FinalPriceForUnit] * ([Final_QuantityAtomic] / [FinalQuantityUnit])) as [Final_TotalPrice]
from (
select top 1000 [Id],
[QuantityOfProduct],
[Redundant_ProductName],
[Order_Id],
coalesce([PriceForUnitOverride], [Redundant_PriceForUnit]) as [FinalPriceForUnit],
coalesce([QuantityUnit_Override], [Redundant_QuantityUnit]) as [FinalQuantityUnit],
coalesce([QuantityAtomic_Override], [Redundant_QuantityAtomic]) as [Final_QuantityAtomic]
from [dbo].[ItemInOrder]
where [IsSoftDeleted] = 0
order by [Order_Id]
) t;
SELECT p.pnum, p.pname
FROM professor p, class c
WHERE p.pnum = c.pnum AND c.cnum = CS245 AND (SELECT COUNT(*) FROM (SELECT MAX(m.grade), MAX(m.grade) - MIN(m.grade) AS diff
FROM mark m WHERE m.cnum = c.cnum AND m.term = c.term AND m.section = c.section AND diff <= 20)) = 3
Incorrect syntax near ')'. Expecting AS, FOR_PATH, ID, or QUOTED_ID.
Consider the following example:
SELECT COUNT(1)
FROM SYSCAT.TABLES T
WHERE
(
-- SELECT COUNT(1)
-- FROM
-- (
SELECT COUNT(1)
FROM SYSCAT.COLUMNS C
WHERE C.TABSCHEMA=T.TABSCHEMA AND C.TABNAME=T.TABNAME
-- )
) > 50;
The query above works as is. But the problem is, that if you uncomment the commented out lines, you get the following error message: "T.TABNAME" is an undefined name. and least in Db2 for Linux, Unix and Windows.
You can't push external to the sub-select column references too deeply.
So, your query is incorrect.
It's hard to correct it, until you provide the task description with data sample and the result expected.
I can see a potential syntax errors: It seems that CS245 refers to a value c.cnum may take and not a column name. If that is the case, it should be enclosed in single quotes.
I am writing a SQL Server Merge statement but can't seem to get the syntax correct. Would someone please take a look to see where I'm going wrong?
Any help you can give is most appreciated.
What I have is two tables that I'd like to merge (w_materialmarketprices2 and d_component). My source (d_component) table requires me to do a join to a tax table (d_tax).
Everything works fine except for when I try to add the additional tax table into my join. The reason I need the tax table is because it contains a tax rate which I don't have in my d_component table (although I do have the corresponding tax code).
My comparison criteria between w_materialmarketprices2 and d_component includes the tax rate in the calculation.
Here's my code:
MERGE [DWH].[dbo].[w_materialmarketprices2] AS A
USING
(SELECT
[comp_code], [comp_desc], [comp_o_un], [comp_type],
[comp_ccy], [comp_tx], [comp_net_price], [comp_per],
[comp_doc_date], [comp_last_update], [comp_latest],
D.[tax_rate] AS TaxRate
FROM
[DWH].[dbo].[d_component]) AS B
INNER JOIN
[DWH].[dbo].[d_tax] AS D ON D.[tax_code] = B.[comp_tx]
ON
A.[mp_comp_code] = B.[comp_code] AND A.[mp_valid_date] = B.[comp_doc_date] AND B.[comp_net_price]>0 AND A.[mp_price_inc_vat] = ROUND(((B.[comp_net_price]/B.[comp_per])*(1+TaxRate),3) AND A.[mp_budget_actual] = 'PO Actual' AND B.[comp_type] ='P100' AND (left(B.[comp_code],1)='S' OR left(B.[comp_code],1)='R')
WHEN NOT MATCHED BY TARGET
THEN
INSERT ([mp_budget_actual], [mp_comp_code], [mp_comp_desc], [mp_unit], [mp_unit_qty], [mp_qualified_supplier], [mp_ccy], [mp_price_inc_vat], [mp_valid_date], [mp_last_update], [mp_latest])
VALUES ('PO Actual', B.[comp_code], B.[comp_desc], B.[comp_o_un], 1, 'Y', B.[comp_ccy], ROUND(((B.[comp_net_price]/B.[comp_per])*(1+TaxRate),3), B.[comp_doc_date], B.[comp_last_update], B.[comp_latest])
;
The error I'm getting is:
Msg 4145, Level 15, State 1, Line 20
An expression of non-boolean type specified in a context where a condition is expected, near ','.
Msg 102, Level 15, State 1, Line 23
Incorrect syntax near 'B'.
,D.[tax_rate] AS TaxRate shows up as underlined in red so I reckon the problem is something to do with that. I also get the message
The multi-part identifier "D.tax_rate" could not be bound
Thanks for your help in advance. Honkonger.
There is no reason to use a subquery in the USING clause, ie: don't put a SELECT in there:
MERGE [DWH].[dbo].[w_materialmarketprices2] AS A
USING
[DWH].[dbo].[d_component] AS B
INNER JOIN [DWH].[dbo].[d_tax] AS D ON D.[tax_code] = B.[comp_tx]
ON
A.[mp_comp_code] = B.[comp_code] .......
I don't know what is wrong with this syntax.
SELECT Count (id_conv) AS NUM_CAMP
FROM csd_mx_mae_camp_dro
WHERE id_conv = (SELECT id_conv
FROM csd_mx_mae_conv_dro
WHERE num_cta = 60385300500)
AND id_cncpt = (SELECT A.id_cncpt
FROM csd_mx_mae_camp_dro A
INNER JOIN csd_mx_mae_cncpt_dro B
ON A.id_cncpt = B.id_cncpt
WHERE ( ( flg_tipo_camp = 'A'
AND txt_nombr_clase_logic IS NOT NULL )
OR ( flg_tipo_camp = 'C' ) )
AND txt_nom NOT IN ( 'Concepto' )
AND B.txt_cve = '84'
AND A.id_conv = (SELECT id_conv
FROM csd_mx_mae_conv_dro
WHERE num_cta = 60385300500)
AND rownum = 1
ORDER BY id_cmp)
AND flg_tipo_camp = 'A';
The expected result is 4, taking into account my records in the DB, however I have the error mentioned in the title (ORA-00907: missing the right parenthesis
00907. 00000 - "missing right parenthesis"
* Cause:
* Action:
Error in the line: 171, column: 90).
There are some subqueries where ORDER BY makes sense - and it is allowed by the syntax.
However, you use ORDER BY in a scalar subquery - one that is required to return a single value (one row / one column), and such subqueries do not allow ORDER BY.
You are using it incorrectly anyway (most likely) - you limit the number of rows to 1 by the condition ROWNUM = 1, which in conjunction with your ORDER BY probably means you wanted to order by ID_CMP and then take the first row from the result. That is not how it works; ORDER BY comes only after ROWNUM is assigned anyway. If that's what you were trying to do, remove ORDER BY as well as the condition on ROWNUM, and instead select MIN(ID_CMP) in the SELECT clause of the scalar subquery.
The specific error about the missing right parenthesis is caused by the ORDER BY clause: at that point, in a scalar subquery, the parser expects the closing parenthesis for the subquery, not any other token/clause/whatever.
When i execute below query in sql server i got below error
Incorrect syntax near the keyword 'in'
Query:
select projectid
from projects
where iif(1!=1,
projects.projectid in (1,16,17,18,19,20,21,22,23),
1);
This will work.
Explanation: you cannot use IN operator in THEN part of CASE Statement, that's why use nested CASE statements. Now, this query will give you no record because "1 != 1" always returns false and THEN part will not execute.
SELECT projectid
FROM projects
WHERE
projectid = CASE
WHEN 1 != 1
THEN
CASE
WHEN projectid IN (1,16,17,18,19,20,21,22,23)
THEN projectid
ELSE 1 END
END;
Now, there is no syntax error near IN keyword. You can modify this query according to your requirement.
The equivalent logic is:
select p.projectid
from projects p
where (1 = 1) or p.projectid in (1, 16, 17, 18, 19, 20, 21, 22, 23);
However, this is overkill, because the first expression involves only constants and is always true. More commonly, you would have something like:
where (#TakeAllFlag = 1) or p.projectid in (1, 16, 17, 18, 19, 20, 21, 22, 23);
Also, if you are learning SQL -- and not using MS Access -- learn the CASE statement, not IIF(). CASE is standard SQL and available in basically all databases.
This should be as simple as:
select projectid
from projects
where projects.projectid in (1,16,17,18,19,20,21,22,23)
Don't see any sense of putting an IIF function in the where clause.
iif(1!=1, projects.projectid in (1,16,17,18,19,20,21,22,23), 1)
In simple English the above line interoperates as if 1 is not equal to 1 then use the IN expression else return 1
1 is always equal to 1 and the rest doesn't make any sense .