I am writing a select statement to use in a view. The first column of data I need is retrieved with the first case statement below; I call that column TxnCode. Then I need to write data to a second column that depends on the TxnCode returned in the first column. The script below tells me 'invalid column mame 'TxnCode''. It is not recognizing the alias from the first case statement.
Is there an easy way to re-write this so I can use the TxnCode in the second case statement? Thanks.
SELECT
CASE
WHEN doctype = 6 THEN 'SPPC'
WHEN doctype = 4 THEN 'RPPC'
WHEN doctype = 1 AND trxqty < 1 THEN 'ANPC'
WHEN doctype = 1 AND trxqty > 1 THEN 'APPC'
ELSE 'HELP' END AS TxnCode,
CASE TxnCode
WHEN 'RPPC' THEN 'I'
WHEN 'APPC' THEN 'I'
WHEN 'RNPC' then ''
WHEN 'SPPC' THEN ''
WHEN 'ANPC' THEN ''
ELSE 'HELP' END as NewColumn
FROM dbo.IV30300
Is there an easy way to re-write this so I can use the TxnCode in the second case statement? Thanks.
SELECT
CASE
WHEN doctype = 6 THEN 'SPPC'
WHEN doctype = 4 THEN 'RPPC'
WHEN doctype = 1 AND trxqty < 1 THEN 'ANPC'
WHEN doctype = 1 AND trxqty > 1 THEN 'APPC'
ELSE 'HELP' END AS TxnCode,
CASE TxnCode
WHEN 'RPPC' THEN 'I'
WHEN 'APPC' THEN 'I'
WHEN 'RNPC' then ''
WHEN 'SPPC' THEN ''
WHEN 'ANPC' THEN ''
ELSE 'HELP' END as NewColumn
FROM dbo.IV30300