How to figure out the column names programmatically using Netezza SQL, where the column values matches a preset value
Basically you want to transpose data .
These kind of questions were already answered in SO.
Transpose rows to columns based on ID column.
Netezza aggregation issue (pivot)
Hope this helps! If you want more help please reply.
How to list similar column names with select statement?
You'll have to use dynamic nzplsql in a stored procedure. Stored procedures don't easily return tables, though, but you can have it easily output a select statement that builds from _v_relation_column
via a cursor. Capture that in bash and feed it to nzsql
to select from a table. Either that or you can just return a reftable.
/* Stored procedure header. */
declare sql varchar;
declare col record;
begin_proc
sql := 'select ';
for col in select * from _v_relation_column where name = 'TABLE_NAME' loop
if col.attname like 'year%'
sql := sql || attname || ',';
end if;
sql := substring(sql,1,length(sql)-1); --To strip the last comma. Could probably be more elegant.
sql := sql || ' from table_name;';
raise notice '%',sql;
end_proc;
/* Stored procedure footer. */
Related Topics
Postgresql:How to Select Top N Percent(%) Entries from Each Group/Category
Create Temp Table with Range of Numbers
Performing a Where - in Query in Couchdb
Inserting a Variable in a Raw SQL Query Laravel
SQL - Pivot Table and Group by Not Working
Retrieve Rank from SQLite Table
Create Unqiue Case-Insensitive Constraint on Two Varchar Fields
SQL Query to Count() Multiple Tables
How to Calculate Running Multiplication
What Would Be the Best Way to Store Records Order in SQL
Ssrs - Keep a Table the Same Width When Hiding Columns Dynamically
Split/Explode Comma Delimited String with Sybase SQL Anywhere
How to Group by and Return Sum Row in Postgres
Filter Duplicate Rows Based on a Field
Group by Every N Records in T-Sql
What Are the Conditions for Encountering a Serialization Failure