Netezza SQL query searching a concatenated value - sql

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.

Related

SQL Parameter to Include All on ID Column

I'm just taking a look at the following query
select * from tablename
where id like '%%';
So that it can handle parameters to include all of the data or filtered data like bellow
select * from tablename
where id like '%1%';
Which is fine for most parameters I use but this seems wrong for an ID because it will return all data that has IDs containing 1 which I don't want
To get around this I can only append the where clause if the ID is given but that seems like a pain in the butt
Is it possible to use a different type of where clause so that a wildcard can be used in a where equals clause instead of a where like clause, example
select * from tablename
where id = '*';
So that the same query can be used to return all or filtered data? Pass parameter '*' for all or parameter '1' for ID 1 specifically
(I'm not sure if it matters for this case but I'm using PostgreSQL 9.6.12 in this example)
This would often be expressed as:
where (id = :id or :id is null)
null is the "magic" value that represents all rows.

How to find list of columns from the given 10 table names?

We need to provide list of columns for around 50 tables/views names (which will be given in one excel). I am using below query for which I need to replace table name and run the query for 50 times. Is there a one shot query whose where clause will have all the table names? e.g.
Select table_name,column
from
where table_name in(Mytableq,Mytable,2...Mytable50)
Query I am using currently:
1.
SELECT *
FROM dbCFRMart.INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = N'vwPDPSubCase'
2.
SELECT *
FROM dbCFRMart.INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = N'vwDDPcase'
.
.
.
50.
SELECT *
FROM dbCFRMart.INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = N'vwXYZcase'

Completing a given SQL statement so that another column is displayed in the end

I'm given the following statement:
SELECT id FROM record_database WHERE id = <up to me to complete the statement>
The record database has different fields, among which are id and name.
I'm supposed to complete this select statement so that it displays all the ids and all the corresponding names side by side, and this should be done using this one line of SQL code. A hint was given that UNION or OR can be used.
I tried variations of the following:
SELECT id FROM record_database WHERE id = '*'
UNION
SELECT name FROM record_database WHERE name = '*';
But none of these worked. I tried doing this with AND, tried using display columns, but those didn't work either.
Any help would be appreciated.
This smells a great deal like homework, so I won't offer a complete answer, but you can't just union queries that return dissimilar result sets. I'm inferring that ID is an integer while NAME is some varchar, which won't union as you've listed in your hint.
When you say "complete," are you restricted to adding things to the end? If so, its a non-starter. You can't increase the list of fields being returned merely by adding things to the "WHERE" clause. You need to add things to the actual field list to get them to be returned, so you might clarify whether you are truly restricted to appending to the query you;ve given.
If you are looking for:
id
name
id next
name next
Then use this trick:
SELECT col2
FROM (
SELECT id, col2=convert ( varchar (size of name field),id)
FROM table
WHERE ....
UNION ALL
SELECT id, name
FROM table
WHERE ....
)
ORDER BY id
This order by will bring id and name side by side and col2 will contain id in first row and name in second row.
Cheating. Make the select return 0 rows and add another one that will show 2 columns. All in one and the same line:
SELECT id FROM record_database WHERE id = NULL;SELECT id,name FROM record_database;
No more time should be wasted on silly problems like this.
If both id and name are char (or varchar), you could also do this, concatting the two columns into one:
SELECT id FROM record_database WHERE id = NULL
UNION ALL
SELECT id || '--' || name FROM record_database ;
The id || '--' || name part differs from one DBMS to another. In some, the + is the concat operator, in others there are special functions. So you may need to use:
id + '--' + name
or:
CONCAT(id, '--', name)
Try this
SELECT * FROM record_database WHERE id = '*' OR name = '*'

How to select items with all possible id-s or just a particular one using the same query?

Is there a variable in SQL that can be used to represent ALL the possible values of a field? Something like this pseudo-code
SELECT name FROM table WHERE id = *ALL_EXISTING_ID-s*
I want to return all rows in this case, but later when I do a search and need only one item I can simply replace that variable with the id I'm looking for, i.e.
SELECT name FROM table WHERE id = 1
The simplest way is to remove the WHERE clause. This will return all rows.
SELECT name FROM table
If you want some "magic" value you can use for the ID that you can use in your existing query and it will return all rows, I think you're out of luck.
Though you could use something like this:
SELECT name FROM table WHERE id = IFNULL(?, id)
If the value NULL is provided, all rows will be returned.
If you don't like NULL then try the following query, which will return all rows if the value -1 is provided:
SELECT name FROM table WHERE id = IFNULL(NULLIF(?, -1), id)
Another approach that achieves the same effect (but requires binding the id twice) is:
SELECT name FROM table WHERE (id = ? OR ? = -1)

Handling different conditions present in table parameter to work as "AND" instead of "OR"

I have a page where user dynamically add search condition(s) to filter out records.
I am sending those conditions to stored proc in a TVP.
Now i need to write a query which applies those filters work as "AND" instead of "OR"
Consider an example of student table
ID Name Marks
----------- --------- -------
2 bab 65
4 bad 75
6 baf 85
I am getting conditions to Stored proc in TVP as (i am sending Ids for column and operator, but for clarity i used column name and operator in this example)
Column Operator Value
----------- --------- -------
Name Contains a
Marks >= 75
Now i need to use this TVP to filter out records from student table, in this example the last 2 rows of student table will be displayed to user.
Can anyone help me in writing a query for this, i tried making cross join between student table and TVP, but query is ORing instead of ANDing the conditions in TVP
Sample query:
DECLARE #tmpCond TABLE
(
ColumnId SMALLINT,
OperatorId SMALLINT,
Value VARCHAR(10)
)
INSERT INTO #tmpCond
( ColumnId ,
OperatorId ,
Value
)
VALUES ( 1,1,'a')
,(2,2,'75')
SELECT * FROM dbo.Student A
CROSS JOIN #tmpCond B
WHERE
(B.ColumnId = 1 AND B.OperatorId = 1 AND A.NAME LIKE '%'+B.Value+'%')
OR
(B.ColumnId = 2 AND B.OperatorId = 2 AND A.Marks >= B.Value)
You need to do the following:
Create the preliminary query as a string, missing the where parameters: query = 'select * from student where xxx order by name, marks'. We'll replace xxx with proper contents.
Create the a variable for the query part where_part = ''
Use a cursor to iterate through every row in TVP
For every column, operator, value in TVP, build up a string like name + operator + value + ' and ' and append it to where_part.
After having processed all rows from TVP, remove the last ' and ', in this sense: 'condition and condition and condition and '.
Replace 'xxx' in query with where_part
Run exec(#query) (tsql, other DBs have different syntax). See also this article and google for "dynamic sql in stored procedures ".