Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Modifying a WITH CTE AS statement

Trying to figure out how to modify this WITH CTE AS statement. What I have below will update my StackCustomerAltID numericly with the customers in ABC order that are not blank and Delflg = 0. But I need to figure out how to modify it to update my StackCustomerAltID numericly, customers in ABC order with the blank customers. But the problem seems to be the blanks. I tried removing the where clause but it puts the blanks first then the ones that have actual customers in them.

Existing Query

WITH CTE AS(
    SELECT StackCustomerAltID,
           ROW_NUMBER() OVER (ORDER BY Customer) AS RN
    FROM dbo.StackCustomer Where DelFlg = 0 and Customer <> '')
UPDATE CTE
SET StackCustomerAltID = RN;

without where clause

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

WITH CTE AS(
    SELECT StackCustomerAltID,
           ROW_NUMBER() OVER (ORDER BY Customer) AS RN
    FROM dbo.StackCustomer )
UPDATE CTE
SET StackCustomerAltID = RN;

>Solution :

How about:

WITH CTE AS
(
    SELECT 
        StackCustomerAltID,
        ROW_NUMBER() OVER 
        (
            ORDER BY 
                CASE WHEN Customer Is Null THEN 1 ELSE 0 END, 
                Customer
        ) AS RN
    FROM 
        dbo.StackCustomer 
)
UPDATE CTE
SET StackCustomerAltID = RN;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading