Note that there are some explanatory texts on larger screens.

plurals
  1. POChanging WHERE clause in CTE stops it working
    primarykey
    data
    text
    <p>This works:</p> <pre><code>;WITH CTE AS ( SELECT PT.[description] AS [Transaction Type], Sum (P.original_amount) AS [AMOUNT (£) CREDIT], PR4.[description] AS [Product Type] FROM [X].[dbo].[PAYMENT] P join [X].[dbo].[T1] PT on P.PAYMENT_ID=PT.PAYMENT_TYPE_ID join [X].[dbo].[T2] CN on CN.PAYMENT_TYPE_ID=PT.PAYMENT_TYPE_ID join [X].[dbo].[T3] C on C.[Customer_ID]=CN.[Customer_ID] join [X].[dbo].[T4] SO on SO.PAYMENT_TYPE_ID=P.PAYMENT_TYPE_ID join [X].[dbo].[T5] OI on OI.ORDER_ID=SO.SITE_ORDER_ID join [X].[dbo].[T6] PR4 on PR4.Product_id=OI.PRODUCT_ID join [X].[dbo].[T7]PC4 on PC4.[PRODUCT_CATEGORY_ID]=PR4.[PRODUCT_CATEGORY_ID] Where PC4.DESCRIPTION is not null group by PT.DESCRIPTION, PR4.DESCRIPTION ) select * from CTE </code></pre> <p>This does NOT work:</p> <pre><code>;WITH CTE AS ( SELECT PT.[description] AS [Transaction Type], Sum (P.original_amount) AS [AMOUNT (£) CREDIT], PR4.[description] AS [Product Type] FROM [X].[dbo].[PAYMENT] P join [X].[dbo].[T1] PT on P.PAYMENT_ID=PT.PAYMENT_TYPE_ID join [X].[dbo].[T2] CN on CN.PAYMENT_TYPE_ID=PT.PAYMENT_TYPE_ID join [X].[dbo].[T3] C on C.[Customer_ID]=CN.[Customer_ID] join [X].[dbo].[T4] SO on SO.PAYMENT_TYPE_ID=P.PAYMENT_TYPE_ID join [X].[dbo].[T5] OI on OI.ORDER_ID=SO.SITE_ORDER_ID join [X].[dbo].[T6] PR4 on PR4.Product_id=OI.PRODUCT_ID join [X].[dbo].[T7]PC4 on PC4.[PRODUCT_CATEGORY_ID]=PR4.[PRODUCT_CATEGORY_ID] Where PC4.DESCRIPTION ='VALUEXXXXX' group by PT.DESCRIPTION, PR4.DESCRIPTION ) select * from CTE </code></pre> <p>Only difference is </p> <pre><code>Where PC4.DESCRIPTION ='VALUEXXXXX' </code></pre> <p>The error is</p> <pre><code>Msg 207, Level 16, State 1, Line 25 Invalid column name 'VALUEXXXXX'. </code></pre> <p>What is the syntax problem ?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload