create sql query

Hi Everyone,

Can anyone pls help me out......with my requirement, i am struggling since 3 days. Please find the requirement below

my file contains below data

R1|Array/Network Resistor - VIP|V_RES_CLASS|V_MOUNT_FEATURE|SURFACE MOUNT|AND|8533.10.00.20|8533.10.00.20|
R1|Array/Network Resistor - VIP|V_RES_NW_CLASS|V_TECHNOLOGY|CARBON*||8533.10.00.20|8533.10.00.20|
R4|Array/Network Resistor - VIP|V_RES_CLASS|V_MOUNT_FEATURE|THROUGH HOLE MOUNT|AND|8533.10.00.57|8533.10.00.57|
R4|Array/Network Resistor - VIP|V_RES_NW_CLASS|V_TECHNOLOGY|CARBON*||8533.10.00.57|8533.10.00.57|
R7|Array/Network Resistor - VIP|V_RES_CLASS|V_MOUNT_FEATURE|SURFACE MOUNT|AND|8533.21.00.10|8533.21.00.25|
R7|Array/Network Resistor - VIP|V_RES_NW_CLASS|V_TECHNOLOGY|!CARBON*|AND|8533.21.00.10|8533.21.00.25|
R7|Array/Network Resistor - VIP|V_PRT_CLASS|V_MFR_PKG_DESC|DIP*||8533.21.00.10|8533.21.00.25|

i need a script to generate single sql statement for the unique column 1 attribute

if you see R7 there are 3 lines, there are 3 table names in column 3 and 3 attrributes from these tables in column 4.

my tool has to generate a sql line for R7 is

select a.V_MOUNT_FEATURE, b.V_TECHNOLOGY, c.V_MFR_PKG_DESC from V_RES_CLASS a, V_RES_NW_CLASS b, V_PRT_CLASS c where a.V_MOUNT_FEATURE='SURFACE MOUNT' and b.V_TECHNOLOGY not like 'CARBON%' and c.V_MFR_PKG_DESC like 'DIP*';

Please help me out.

Thanks,
Jam

---------- Post updated at 01:06 PM ---------- Previous update was at 12:37 PM ----------

Please provide the reply.....its very critical

Try and adapt the following AWK script :

nawk -F'|' -v Q="'" '
{
    req       = $1;
    table     = $3;
    column    = $4;
    attribute = $5;
    nbTbl     = ++Requests[req];     # Tables count
    Tables[req, nbTbl]     = table;
    Columns[req, nbTbl]    = column;
    Attributes[req, nbTbl] = attribute;
}
END {
    tblIds = "abcdefghijklmnopqrstuvwxyz";
    for (req in Requests) {
        nbTbl  = Requests[req]
        select = "";
        where  = "";
        from   = "";
        sep    = "";
        for (i=1; i<=nbTbl; i++) {
            id        = substr(tblIds,i,1);
            select    = select sep id "." Columns[req, i];
            from      = from   sep Tables[req, i] " " id;     
            attribute = Attributes[req, i];
            if (attribute ~ /[*%]/) {
                not = "";
                if (substr(attribute,1,1) == "!") {
                    not = "NOT ";
                    attribute = substr(attribute,2);
                }
                where = where (sep ? " and " : "") id "." Columns[req, i] " " not "LIKE " Q attribute Q;
            } else {
                where = where (sep ? " and " : "") id "." Columns[req, i] "=" Q attribute Q;
            }
            sep = ",";
            }
        print "\nselect",select,"\nfrom  ",from,"\nwhere ",where,";";
    }
}
' inputfile

Inputfile:

R1|Array/Network Resistor - VIP|V_RES_CLASS|V_MOUNT_FEATURE|SURFACE MOUNT|AND|8533.10.00.20|8533.10.00.20|
R1|Array/Network Resistor - VIP|V_RES_NW_CLASS|V_TECHNOLOGY|CARBON*||8533.10.00.20|8533.10.00.20|
R4|Array/Network Resistor - VIP|V_RES_CLASS|V_MOUNT_FEATURE|THROUGH HOLE MOUNT|AND|8533.10.00.57|8533.10.00.57|
R4|Array/Network Resistor - VIP|V_RES_NW_CLASS|V_TECHNOLOGY|CARBON*||8533.10.00.57|8533.10.00.57|
R7|Array/Network Resistor - VIP|V_RES_CLASS|V_MOUNT_FEATURE|SURFACE MOUNT|AND|8533.21.00.10|8533.21.00.25|
R7|Array/Network Resistor - VIP|V_RES_NW_CLASS|V_TECHNOLOGY|!CARBON*|AND|8533.21.00.10|8533.21.00.25|
R7|Array/Network Resistor - VIP|V_PRT_CLASS|V_MFR_PKG_DESC|DIP*||8533.21.00.10|8533.21.00.25|

Output:

select a.V_MOUNT_FEATURE,b.V_TECHNOLOGY
from   V_RES_CLASS a,V_RES_NW_CLASS b
where  a.V_MOUNT_FEATURE='SURFACE MOUNT' and b.V_TECHNOLOGY LIKE 'CARBON*' ;

select a.V_MOUNT_FEATURE,b.V_TECHNOLOGY
from   V_RES_CLASS a,V_RES_NW_CLASS b
where  a.V_MOUNT_FEATURE='THROUGH HOLE MOUNT' and b.V_TECHNOLOGY LIKE 'CARBON*' ;

select a.V_MOUNT_FEATURE,b.V_TECHNOLOGY,c.V_MFR_PKG_DESC
from   V_RES_CLASS a,V_RES_NW_CLASS b,V_PRT_CLASS c
where  a.V_MOUNT_FEATURE='SURFACE MOUNT' and b.V_TECHNOLOGY NOT LIKE 'CARBON*' and c.V_MFR_PKG_DESC LIKE 'DIP*' ;

Jean-Pierre.

1 Like

Hi aigles,

Your code works superb!!!!!!!! but unfortunately i need perl code to do this as i am not able to understand awk........

Thanks a lot for your quick reply.........!

---------- Post updated at 03:48 PM ---------- Previous update was at 02:18 PM ----------

Hi friends......

Please provide ur support......i need it very urgent......

---------- Post updated 06-30-11 at 10:03 AM ---------- Previous update was 06-29-11 at 03:48 PM ----------

Hi.......

I need this very urgent.........please help me out.

Sorry, i don't use perl so I can't help you anymore.

Jean-Pierre.

Try using a2p

Hi aigles,

Thanks for your help!!!!!! I will call your awk scipt in my perl script. I need some modification with reference to below file

R1|Array/Network Resistor - VIP|V_RES_CLASS|V_MOUNT_FEATURE|SURFACE MOUNT|AND|8533.10.00.20|8533.10.00.20|
R1|Array/Network Resistor - VIP|V_RES_NW_CLASS|V_TECHNOLOGY|CARBON*||8533.10.00.20|8533.10.00.20|
R4|Array/Network Resistor - VIP|V_RES_CLASS|V_MOUNT_FEATURE|THROUGH HOLE MOUNT|AND|8533.10.00.57|8533.10.00.57|
R4|Array/Network Resistor - VIP|V_RES_NW_CLASS|V_TECHNOLOGY|CARBON*||8533.10.00.57|8533.10.00.57|
R7|Array/Network Resistor - VIP|V_RES_CLASS|V_MOUNT_FEATURE|SURFACE MOUNT|AND|8533.21.00.10|8533.21.00.25|
R7|Array/Network Resistor - VIP|V_RES_NW_CLASS|V_TECHNOLOGY|!CARBON*|AND|8533.21.00.10|8533.21.00.25|
R7|Array/Network Resistor - VIP|V_PRT_CLASS|V_MFR_PKG_DESC|DIP*||8533.21.00.10|8533.21.00.25|

in the created sql query i need to add two static table v_prt_class and v_vmf_class in all the query as i need to extract one attribute from each of these two tables,
condition here is if v_prt_class part of the it should not duplicate

If u take the first output it does not contain V_PRT_CLASS table in the query, if u take 3rd output it contains V_PRT_CLASS table.

new sqlquery for the 3rd output should be

select a.v_prt_nbr, b.v_vmf_name, c.V_MOUNT_FEATURE, d.V_TECHNOLOGY, a.V_MFR_PKG_DESC from v_prt_class a, v_vmf_class b, V_RES_CLASS c, V_RES_NW_CLASS d where c.V_MOUNT_FEATURE='SURFACE MOUNT' and d.V_TECHNOLOGY NOT LIKE 'CARBON%' and a.V_MFR_PKG_DESC LIKE 'DIP%' and a.obj_id=c.obj_id and c.obj_id=d.obj_id and a.V_PRT_MFG_PTR=b.obj_id;

here a.obj_id=c.obj_id and c.obj_id=d.obj_id and a.V_PRT_MFG_PTR=b.obj_id; is a static line
for 1st output

select a.v_prt_nbr, b.v_vmf_name, c.V_MOUNT_FEATURE, d.V_TECHNOLOGY from v_prt_class a, v_vmf_class b, V_RES_CLASS c, V_RES_NW_CLASS d where c.V_MOUNT_FEATURE='SURFACE MOUNT' and d.V_TECHNOLOGY LIKE 'CARBON%' and a.obj_id=c.obj_id and c.obj_id=d.obj_id and a.V_PRT_MFG_PTR=b.obj_id;

Instead of writing the output to terminal I need to write it to some output file, Also I need to retain clolumn 1 ,7 and 8 along with the corresponding query.

Try this new version :

nawk -F'|' -v Q="'" '

{
    req       = $1;
    table     = $3;
    column    = $4;
    attribute = $5;
	nbTbl     = ++Requests[req];     # Tables count
	Tables[req, nbTbl]     = toupper(table);
    Columns[req, nbTbl]    = column;
    Attributes[req, nbTbl] = attribute;
}

function getTableId(table) {
    table = toupper(table);
    if ( ! (table in TablesIds) ) {
        Tables[++TablesCount] = table;
        TablesIds = substr("abcdefghijklmnopqrstuvwxyz", TablesCount, 1);
    }
    return TablesIds;
}

END {
    for (req in Requests) {

        select = "";
        where  = "";
        from   = "";
        
        #
        # Fixed part of select columns statement
        #
        
        id = getTableId("V_PRT_CLASS");
        select = id ".V_PRT_NBR";
        
        id = getTableId("V_VMF_CLASS");
        select = select ", " id ".V_VMF_NAME";
        
        #
        # Select columns and where clause from request definition
        #
                
        req_len = Requests[req];
        wsep = "";
        for (i=1; i<=req_len; i++) {
            id     = getTableId(Tables[req, i]);
            select = select ", " id "." Columns[req, i];
            attribute = Attributes[req, i];
            if (attribute ~ /[*%]/) {
                not = "";
                if (substr(attribute,1,1) == "!") {
                    not = "not ";
                    attribute = substr(attribute,2);
                }
                where = where wsep id "." Columns[req, i] " " not "like " Q attribute Q;
            } else {
                where = where wsep id "." Columns[req, i] "=" Q attribute Q;
            }
            wsep = "\n  and  ";
            }
        
        #
        # Fixed part of where clause
        #
        
        id1 = getTableId("V_PRT_CLASS");
        id2 = getTableId("V_VMF_CLASS");
        id3 = getTableId("V_RES_CLASS");
        id4 = getTableId("V_RES_NW_CLASS");
        
        where = where wsep id1 ".OBJ_ID="        id3 ".OBJ_ID";
        where = where wsep id3 ".OBJ_ID="        id4 ".OBJ_ID";
        where = where wsep id1 ".V_PRT_MFG_PTR=" id2 ".OBJ_ID";
        
        #
        # From clause
        #

        fsep = "";
        for (i=1; i<=TablesCount; i++) {
            table = Tables;
            id    = getTableId(table);
            from  = from fsep table " " id;
            fsep  = ", "
        }
        
        #
        # Print SQL statement
        #
        
        print "\nselect",select,"\nfrom  ",from,"\nwhere ",where,";";

    }
 }
' jam.dat

Inputfile:

R1|Array/Network Resistor - VIP|V_RES_CLASS|V_MOUNT_FEATURE|SURFACE MOUNT|AND|8533.10.00.20|8533.10.00.20|
R1|Array/Network Resistor - VIP|V_RES_NW_CLASS|V_TECHNOLOGY|CARBON*||8533.10.00.20|8533.10.00.20|
R4|Array/Network Resistor - VIP|V_RES_CLASS|V_MOUNT_FEATURE|THROUGH HOLE MOUNT|AND|8533.10.00.57|8533.10.00.57|
R4|Array/Network Resistor - VIP|V_RES_NW_CLASS|V_TECHNOLOGY|CARBON*||8533.10.00.57|8533.10.00.57|
R7|Array/Network Resistor - VIP|V_RES_CLASS|V_MOUNT_FEATURE|SURFACE MOUNT|AND|8533.21.00.10|8533.21.00.25|
R7|Array/Network Resistor - VIP|V_RES_NW_CLASS|V_TECHNOLOGY|!CARBON*|AND|8533.21.00.10|8533.21.00.25|
R7|Array/Network Resistor - VIP|V_PRT_CLASS|V_MFR_PKG_DESC|DIP*||8533.21.00.10|8533.21.00.25|

Output:

select a.V_PRT_NBR, b.V_VMF_NAME, c.V_MOUNT_FEATURE, d.V_TECHNOLOGY
from   V_PRT_CLASS a, V_VMF_CLASS b, V_RES_CLASS c, V_RES_NW_CLASS d
where  c.V_MOUNT_FEATURE='SURFACE MOUNT'
  and  d.V_TECHNOLOGY like 'CARBON*'
  and  a.OBJ_ID=c.OBJ_ID
  and  c.OBJ_ID=d.OBJ_ID
  and  a.V_PRT_MFG_PTR=b.OBJ_ID ;

select a.V_PRT_NBR, b.V_VMF_NAME, c.V_MOUNT_FEATURE, d.V_TECHNOLOGY
from   V_PRT_CLASS a, V_VMF_CLASS b, V_RES_CLASS c, V_RES_NW_CLASS d
where  c.V_MOUNT_FEATURE='THROUGH HOLE MOUNT'
  and  d.V_TECHNOLOGY like 'CARBON*'
  and  a.OBJ_ID=c.OBJ_ID
  and  c.OBJ_ID=d.OBJ_ID
  and  a.V_PRT_MFG_PTR=b.OBJ_ID ;

select a.V_PRT_NBR, b.V_VMF_NAME, c.V_MOUNT_FEATURE, d.V_TECHNOLOGY, a.V_MFR_PKG_DESC
from   V_PRT_CLASS a, V_VMF_CLASS b, V_RES_CLASS c, V_RES_NW_CLASS d
where  c.V_MOUNT_FEATURE='SURFACE MOUNT'
  and  d.V_TECHNOLOGY not like 'CARBON*'
  and  a.V_MFR_PKG_DESC like 'DIP*'
  and  a.OBJ_ID=c.OBJ_ID
  and  c.OBJ_ID=d.OBJ_ID
  and  a.V_PRT_MFG_PTR=b.OBJ_ID ;

if you want to write the result of the script (for example named 'generate_sql.ksh') in the file 'request.sql' :

generate_sql.ksh > request.sql

I don't understand what you want to do.
Give us an example.

Jean-Pierre.

Hi Aigles,

My requierement is is....

i need to print column1|Sql Query|column7|column8 as the output of this awk script.

like below

R1|select a.V_PRT_NBR, b.V_VMF_NAME, c.V_MOUNT_FEATURE, d.V_TECHNOLOGY from   V_PRT_CLASS a, V_VMF_CLASS b, V_RES_CLASS c, V_RES_NW_CLASS d where c.V_MOUNT_FEATURE='THROUGH HOLE MOUNT' and d.V_TECHNOLOGY like 'CARBON%' and a.OBJ_ID=c.OBJ_ID and c.OBJ_ID=d.OBJ_ID and a.V_PRT_MFG_PTR=b.OBJ_ID ;|8533.10.00.20|8533.10.00.20|

Also in query instead of , in the select attribute it should be ||'|'||

like below

select a.V_PRT_NBR||'|'||b.V_VMF_NAME||'|'||c.V_MOUNT_FEATURE||'|'||d.V_TECHNOLOGY
from   V_PRT_CLASS a, V_VMF_CLASS b, V_RES_CLASS c, V_RES_NW_CLASS d
where  c.V_MOUNT_FEATURE='THROUGH HOLE MOUNT'
  and  d.V_TECHNOLOGY like 'CARBON*'
  and  a.OBJ_ID=c.OBJ_ID
  and  c.OBJ_ID=d.OBJ_ID
  and  a.V_PRT_MFG_PTR=b.OBJ_ID ;

and * should be replaced with %

Thanks a ton for your quick help

Try this new version :

nawk -F'|' -v Q="'" -v OFS="|" '

{
    req       = $1;
    table     = $3;
    column    = $4;
    attribute = $5;
    Column7[req] = $7;
    Column8[req] = $8;
    nbTbl     = ++Requests[req];     # Tables count
    Tables[req, nbTbl]     = toupper(table);
    Columns[req, nbTbl]    = column;
    Attributes[req, nbTbl] = attribute;
}

function getTableId(table) {
    table = toupper(table);
    if ( ! (table in TablesIds) ) {
        Tables[++TablesCount] = table;
        TablesIds = substr("abcdefghijklmnopqrstuvwxyz", TablesCount, 1);
    }
    return TablesIds;
}

END {
    for (req in Requests) {

        select = "";
        where  = "";
        from   = "";
        
        #
        # Fixed part of select columns statement
        #
        
        id = getTableId("V_PRT_CLASS");
        select = id ".V_PRT_NBR";
        
        id = getTableId("V_VMF_CLASS");
        select = select " || " Q "|" Q " || " id ".V_VMF_NAME";
        
        #
        # Select columns and where clause from request definition
        #
                
        req_len = Requests[req];
        wsep = "";
        for (i=1; i<=req_len; i++) {
            id     = getTableId(Tables[req, i]);
            select = select " || " Q "|" Q " || " id "." Columns[req, i];
            attribute = Attributes[req, i];
            if (attribute ~ /[*%]/) {
                gsub(/\*/, "%", attribute);
                not = "";
                if (substr(attribute,1,1) == "!") {
                    not = "not ";
                    attribute = substr(attribute,2);
                }
                where = where wsep id "." Columns[req, i] " " not "like " Q attribute Q;
            } else {
                where = where wsep id "." Columns[req, i] "=" Q attribute Q;
            }
            wsep = "and ";
        }
        
        #
        # Fixed part of where clause
        #
        
        id1 = getTableId("V_PRT_CLASS");
        id2 = getTableId("V_VMF_CLASS");
        id3 = getTableId("V_RES_CLASS");
        id4 = getTableId("V_RES_NW_CLASS");
        
        where = where wsep id1 ".OBJ_ID="        id3 ".OBJ_ID";
        where = where wsep id3 ".OBJ_ID="        id4 ".OBJ_ID";
        where = where wsep id1 ".V_PRT_MFG_PTR=" id2 ".OBJ_ID";
        
        #
        # From clause
        #

        fsep = "";
        for (i=1; i<=TablesCount; i++) {
            table = Tables;
            id    = getTableId(table);
            from  = from fsep table " " id;
            fsep  = ", "
        }
        
        #
        # Print SQL statement
        #
        
        print req, "\"select " select " from " from " where " where " ;\"", Column7[req],Column8[req];

    }
 }
' jam.dat

Inputfile (jam.dat):

R1|Array/Network Resistor - VIP|V_RES_CLASS|V_MOUNT_FEATURE|SURFACE MOUNT|AND|8533.10.00.20|8533.10.00.20|
R1|Array/Network Resistor - VIP|V_RES_NW_CLASS|V_TECHNOLOGY|CARBON*||8533.10.00.20|8533.10.00.20|
R4|Array/Network Resistor - VIP|V_RES_CLASS|V_MOUNT_FEATURE|THROUGH HOLE MOUNT|AND|8533.10.00.57|8533.10.00.57|
R4|Array/Network Resistor - VIP|V_RES_NW_CLASS|V_TECHNOLOGY|CARBON*||8533.10.00.57|8533.10.00.57|
R7|Array/Network Resistor - VIP|V_RES_CLASS|V_MOUNT_FEATURE|SURFACE MOUNT|AND|8533.21.00.10|8533.21.00.25|
R7|Array/Network Resistor - VIP|V_RES_NW_CLASS|V_TECHNOLOGY|!CARBON*|AND|8533.21.00.10|8533.21.00.25|
R7|Array/Network Resistor - VIP|V_PRT_CLASS|V_MFR_PKG_DESC|DIP*||8533.21.00.10|8533.21.00.25|

Output:

R1|"select a.V_PRT_NBR || '|' || b.V_VMF_NAME || '|' || c.V_MOUNT_FEATURE || '|' || d.V_TECHNOLOGY from V_PRT_CLASS a, V_VMF_CLASS b, V_RES_CLASS c, V_RES_NW_CLASS d where c.V_MOUNT_FEATURE='SURFACE MOUNT'and d.V_TECHNOLOGY like 'CARBON%'and a.OBJ_ID=c.OBJ_IDand c.OBJ_ID=d.OBJ_IDand a.V_PRT_MFG_PTR=b.OBJ_ID ;"|8533.10.00.20|8533.10.00.20
R4|"select a.V_PRT_NBR || '|' || b.V_VMF_NAME || '|' || c.V_MOUNT_FEATURE || '|' || d.V_TECHNOLOGY from V_PRT_CLASS a, V_VMF_CLASS b, V_RES_CLASS c, V_RES_NW_CLASS d where c.V_MOUNT_FEATURE='THROUGH HOLE MOUNT'and d.V_TECHNOLOGY like 'CARBON%'and a.OBJ_ID=c.OBJ_IDand c.OBJ_ID=d.OBJ_IDand a.V_PRT_MFG_PTR=b.OBJ_ID ;"|8533.10.00.57|8533.10.00.57
R7|"select a.V_PRT_NBR || '|' || b.V_VMF_NAME || '|' || c.V_MOUNT_FEATURE || '|' || d.V_TECHNOLOGY || '|' || a.V_MFR_PKG_DESC from V_PRT_CLASS a, V_VMF_CLASS b, V_RES_CLASS c, V_RES_NW_CLASS d where c.V_MOUNT_FEATURE='SURFACE MOUNT'and d.V_TECHNOLOGY not like 'CARBON%'and a.V_MFR_PKG_DESC like 'DIP%'and a.OBJ_ID=c.OBJ_IDand c.OBJ_ID=d.OBJ_IDand a.V_PRT_MFG_PTR=b.OBJ_ID ;"|8533.21.00.10|8533.21.00.25

Jean-Pierre.

Hi aigles,

Thanks a ton for your help.