I am using Aginity workbench to query a netezza database and trying to create simple query. Basically, within a table there are two columns that I want to combine to create an ID (they do make up two parts of actual unique IDs). Within the query I want to search unique IDs e.g. the IDRef value. So in the below example, I want to return all rows within the table where column X equals 282 and Z equals 55. The below code isnt returning anything.
SELECT T_ROUTE || CSC as IDRef, *
FROM HUB_DATABASE
WHERE POSTCODE like 'A%'
AND CURRENT_RECORD_FLAG = '1'
AND IDRef = 28255
LIMIT 100
;
So the below code works ok in the same way, but just trying to use a smarter method of doing this and of course furthering my sql knowledge.
SELECT * FROM HUB_DATABASE
WHERE T_ROUTE = '282'
AND CSC = '55'
AND POSTCODE like 'A%'
AND CURRENT_RECORD_FLAG = 1
LIMIT 100;
Use a subquery. You cannot refer to a column alias in the same level where it is defined:
SELECT hb.*
FROM (SELECT T_ROUTE || CSC as IDRef, hd.*
FROM HUB_DATABASE hd
WHERE POSTCODE like 'A%' AND
CURRENT_RECORD_FLAG = '1'
) hb
WHERE IDRef = 28255
LIMIT 100 ;
You will get an error if IDRef
is already defined in HUB_DATABASE
. If so, you need to use a different name or remove that column from the subquery.