Sponsored Content
Top Forums Shell Programming and Scripting Request: How to Parse dynamic SQL query to pad extra columns to match the fixed number of columns Post 302930910 by vikas_trl on Thursday 8th of January 2015 01:52:54 PM
Old 01-08-2015
Request: How to Parse dynamic SQL query to pad extra columns to match the fixed number of columns

Hello All,

I have a requirement in which i will be given a sql query as input in a file with dynamic number of columns. For example some times i will get 5 columns, some times 8 columns etc up to 20 columns.
So my requirement is to generate a output query which will have 20 columns all the times irrespective of number of input columns.

Sample input file
Code:
SELECT
A AS COL1,
COALESCE(B ,'123') AS COL2,
CAST(C AS TIMESTAMP(0)) AS COL3
FROM TABLE
WHERE X = Y AND Z > (SELECT A FROM TABLE2);

Expected output
Code:
SELECT
A AS COL1,
COALESCE(B ,'123') AS COL2,
CAST(C AS TIMESTAMP(0)) AS COL3
,NULL AS COL4
,NULL AS COL5
..
..
,NULL AS COL20
FROM TABLE
WHERE X = Y AND Z > (SELECT A FROM TABLE2);


Can someone one please help me achieve this in UNIX KSH/BASH?

Thanks in Advance
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Dynamic SQL query based on shell script parameters

Hi, I need a script that will run a dynamic Oracle SQL. Dynamic meaning the SQL statement depends on the parameter. For instance, something like this: #!/bin/ksh -x # Set environment . /home/mine/set_vars sqlplus $LOGINID <<! >> /home/mine/log.txt select count(1) from $1 where... (2 Replies)
Discussion started by: laiko
2 Replies

2. Shell Programming and Scripting

Remove extra characters and sum the columns

I have data extracted like this: A=%123% B=%456% C=%789% A=%111% B=%222% C=%333% A=%777% B=%888% C=%999% Can someone please help me with a script to remove all the % signs and get the totals for A, B and C. So output will be: A=1368 B=666 C=2664 Thank you! (2 Replies)
Discussion started by: tatchel
2 Replies

3. Shell Programming and Scripting

splitting a huge line of file into multiple lines with fixed number of columns

Hi, I have a huge file with a single line. But I want to break that line into lines of with each line having five columns. My file is like this: code: "hi","there","how","are","you?","It","was","great","working","with","you.","hope","to","work","you." I want it like this: code:... (1 Reply)
Discussion started by: rajsharma
1 Replies

4. UNIX for Dummies Questions & Answers

Add extra columns help

Hi Gurus, This below script adds a column extra to my flat file..But how can i add another column, Say if i just put mention an other column beside the first column..it does get generated but as one column only while IFS="" read r; do printf "dummy\t%s\n" "$r" done < xxx.txt > zzz.txt ... (9 Replies)
Discussion started by: saggiboy10
9 Replies

5. Shell Programming and Scripting

How to parse fixed-width columns which may include empty fields?

I am trying to selectively display several columns from a db2 query, which gives me a fixed-width output (partial output listed here): --------- -------------------------- ------------ ------ 000 0000000000198012 702 29 000 0000000000198013 ... (9 Replies)
Discussion started by: ahsh79
9 Replies

6. Shell Programming and Scripting

Match on columns and replace other columns

Hi Friends, I have the following input file cat input chr1 100 200 0.1 0.2 na 1 na nd chr1 105 200 0.1 0.2 1 1 na 98 chr1 110 290 nf 1 na nd na 1 chr2 130 150 12 3 na 1 na 1 chr3 450 600 nf nf na 10 na nd chr4 300 330 1 1 10 11 23 34 My requirement is 1. If $6 is na make $7 nd and... (2 Replies)
Discussion started by: jacobs.smith
2 Replies

7. UNIX for Dummies Questions & Answers

Getting values of 2 columns from sql query in UNIX variables

Hi, I have connected to oracle database with sqlplus -s / <<EOF select ename, age from emp where empid=1234; EOF I want to save the values of ename and age in unix shell variables. Any pointers would be welcome.. Thanks in advance!!1 Cheers :):):):) (1 Reply)
Discussion started by: gonchusirsa
1 Replies

8. Shell Programming and Scripting

Evaluate 2 columns, add sum IF two columns match on two rows

Hi all, I know this sounds suspiciously like a homework course; but, it is not. My goal is to take a file, and match my "ID" column to the "Date" column, if those conditions are true, add the total number of minutes worked and place it in this file, while not printing the original rows that I... (6 Replies)
Discussion started by: mtucker6784
6 Replies

9. Programming

Query SQL get two values differents from the same columns

Hi, I have 2 different values in the same column and two different values in other column Query 1 ins name value 1 Test 12345 1 TestV1 12/10/2014 8 Test 85435 8 TestV1 11/11/2005 9 Test 42232 9 TestV1 19/10/2000 6 Test 54321... (6 Replies)
Discussion started by: faka
6 Replies

10. Shell Programming and Scripting

Adding columns from 2 files with variable number of columns

I have two files, file1 and file2 who have identical number of rows and columns. However, the script is supposed to be used for for different files and I cannot know the format in advance. Also, the number of columns changes within the file, some rows have more and some less columns (they are... (13 Replies)
Discussion started by: maya3
13 Replies
CREATE TABLE 
AS(7) SQL Commands CREATE TABLE AS(7) NAME
CREATE TABLE AS - define a new table from the results of a query SYNOPSIS
CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } ] TABLE table_name [ (column_name [, ...] ) ] [ WITH ( storage_parameter [= value] [, ... ] ) | WITH OIDS | WITHOUT OIDS ] [ ON COMMIT { PRESERVE ROWS | DELETE ROWS | DROP } ] [ TABLESPACE tablespace ] AS query [ WITH [ NO ] DATA ] DESCRIPTION
CREATE TABLE AS creates a table and fills it with data computed by a SELECT command. The table columns have the names and data types asso- ciated with the output columns of the SELECT (except that you can override the column names by giving an explicit list of new column names). CREATE TABLE AS bears some resemblance to creating a view, but it is really quite different: it creates a new table and evaluates the query just once to fill the new table initially. The new table will not track subsequent changes to the source tables of the query. In contrast, a view re-evaluates its defining SELECT statement whenever it is queried. PARAMETERS
GLOBAL or LOCAL Ignored for compatibility. Refer to CREATE TABLE [create_table(7)] for details. TEMPORARY or TEMP If specified, the table is created as a temporary table. Refer to CREATE TABLE [create_table(7)] for details. table_name The name (optionally schema-qualified) of the table to be created. column_name The name of a column in the new table. If column names are not provided, they are taken from the output column names of the query. If the table is created from an EXECUTE command, a column name list cannot be specified. WITH ( storage_parameter [= value] [, ... ] ) This clause specifies optional storage parameters for the new table; see Storage Parameters [create_table(7)] for more information. The WITH clause can also include OIDS=TRUE (or just OIDS) to specify that rows of the new table should have OIDs (object identi- fiers) assigned to them, or OIDS=FALSE to specify that the rows should not have OIDs. See CREATE TABLE [create_table(7)] for more information. WITH OIDS WITHOUT OIDS These are obsolescent syntaxes equivalent to WITH (OIDS) and WITH (OIDS=FALSE), respectively. If you wish to give both an OIDS set- ting and storage parameters, you must use the WITH ( ... ) syntax; see above. ON COMMIT The behavior of temporary tables at the end of a transaction block can be controlled using ON COMMIT. The three options are: PRESERVE ROWS No special action is taken at the ends of transactions. This is the default behavior. DELETE ROWS All rows in the temporary table will be deleted at the end of each transaction block. Essentially, an automatic TRUNCATE [truncate(7)] is done at each commit. DROP The temporary table will be dropped at the end of the current transaction block. TABLESPACE tablespace The tablespace is the name of the tablespace in which the new table is to be created. If not specified, default_tablespace is con- sulted, or temp_tablespaces if the table is temporary. query A SELECT [select(7)], TABLE, or VALUES [values(7)] command, or an EXECUTE [execute(7)] command that runs a prepared SELECT, TABLE, or VALUES query. WITH [ NO ] DATA This clause specifies whether or not the data produced by the query should be copied into the new table. If not, only the table structure is copied. The default is to copy the data. NOTES
This command is functionally similar to SELECT INTO [select_into(7)], but it is preferred since it is less likely to be confused with other uses of the SELECT INTO syntax. Furthermore, CREATE TABLE AS offers a superset of the functionality offered by SELECT INTO. Prior to PostgreSQL 8.0, CREATE TABLE AS always included OIDs in the table it created. As of PostgreSQL 8.0, the CREATE TABLE AS command allows the user to explicitly specify whether OIDs should be included. If the presence of OIDs is not explicitly specified, the default_with_oids configuration variable is used. As of PostgreSQL 8.1, this variable is false by default, so the default behavior is not identical to pre-8.0 releases. Applications that require OIDs in the table created by CREATE TABLE AS should explicitly specify WITH (OIDS) to ensure proper behavior. EXAMPLES
Create a new table films_recent consisting of only recent entries from the table films: CREATE TABLE films_recent AS SELECT * FROM films WHERE date_prod >= '2002-01-01'; To copy a table completely, the short form using the TABLE command can also be used: CREATE TABLE films2 AS TABLE films; Create a new temporary table films_recent, consisting of only recent entries from the table films, using a prepared statement. The new ta- ble has OIDs and will be dropped at commit: PREPARE recentfilms(date) AS SELECT * FROM films WHERE date_prod > $1; CREATE TEMP TABLE films_recent WITH (OIDS) ON COMMIT DROP AS EXECUTE recentfilms('2002-01-01'); COMPATIBILITY
CREATE TABLE AS conforms to the SQL standard. The following are nonstandard extensions: o The standard requires parentheses around the subquery clause; in PostgreSQL, these parentheses are optional. o In the standard, the WITH [ NO ] DATA clause is required; in PostgreSQL it is optional. o PostgreSQL handles temporary tables in a way rather different from the standard; see CREATE TABLE [create_table(7)] for details. o The WITH clause is a PostgreSQL extension; neither storage parameters nor OIDs are in the standard. o The PostgreSQL concept of tablespaces is not part of the standard. Hence, the clause TABLESPACE is an extension. SEE ALSO
CREATE TABLE [create_table(7)], EXECUTE [execute(7)], SELECT [select(7)], SELECT INTO [select_into(7)], VALUES [values(7)] SQL - Language Statements 2010-05-14 CREATE TABLE AS(7)
All times are GMT -4. The time now is 04:21 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy