| View previous topic :: View next topic |
| Author |
Message |
Martin Beginner

Joined: 20 Mar 2006 Posts: 133 Topics: 58
|
Posted: Mon Jul 17, 2006 12:18 am Post subject: Insert columns based on whether another value exist or not. |
|
|
Hi All,
I will explain the problem with an example.
Table :-
| Code: |
P1
P2
P3
P4
V1
V2
V3
V4
|
If P1 = X1 then
Insert a new row P1=Y1 with the below values
| Code: |
P1 = Y1
P2 = Max (P2 +1 )
P3 = Max (P3 + 1)
P4 = Max (P4 + 1)
V1 = hardcoded 'abc'
V2 = hardcoded 'XYZ'
V3 = hardcoded 'efg'
V4 = hardcoded 'lmn'
|
can some one give me a SQL for this condn?
- Martin |
|
| Back to top |
|
 |
shekar123 Advanced
Joined: 22 Jul 2005 Posts: 528 Topics: 90 Location: Bangalore India
|
Posted: Mon Jul 17, 2006 4:58 am Post subject: |
|
|
Martin,
I am assuming table TABLEA containg the fields as shown by you which has a value 'X1' in its column P1 which means one row is already inserted in the table.You can first select the value of the column from TABLEA and then check if the value is 'X1' then u can insert into TABLEA with the new row.There could be other ways of doing it also by an query.Try this code:
| Code: |
SELECT THE VALUE OF THE COLUMN FROM TABLEA
EVALUATE SQLCODE
WHEN 0
IF VALUE OF COLUMN OF TABLEA = 'X1'
INSERT INTO TABLEA NEW VALUES
END-IF
WHEN 100
CONTINUE
WHEN OTHER
DISPLAY 'ABEND OCCURED'
END-EVALUATE.
|
_________________ Shekar
Grow Technically |
|
| Back to top |
|
 |
Martin Beginner

Joined: 20 Mar 2006 Posts: 133 Topics: 58
|
Posted: Mon Jul 17, 2006 7:03 am Post subject: |
|
|
Shekar123,
I do not want to use COBOL here. The idea is to run a SPUFI and insert the rows.
- Martin |
|
| Back to top |
|
 |
kolusu Site Admin

Joined: 26 Nov 2002 Posts: 12401 Topics: 75 Location: San Jose
|
Posted: Mon Jul 17, 2006 7:23 am Post subject: |
|
|
Martin,
Try this
| Code: |
INSERT INTO TABLE(SELECT CASE P1
WHEN 'X1' THEN CHAR('Y1')
ELSE CHAR(' ')
END
,CASE P1
WHEN 'X1' THEN MAX(P2) + 1
ELSE INT(0)
END
,CASE P1
WHEN 'X1' THEN MAX(P3) + 1
ELSE INT(0)
END
,CASE P1
WHEN 'X1' THEN MAX(P4) + 1
ELSE INT(0)
END
,CASE P1
WHEN 'X1' THEN CHAR('ABC')
ELSE CHAR(' ')
END
,CASE P1
WHEN 'X1' THEN CHAR('XYZ')
ELSE CHAR(' ')
END
,CASE P1
WHEN 'X1' THEN CHAR('EFG')
ELSE CHAR(' ')
END
,CASE P1
WHEN 'X1' THEN CHAR('LMN')
ELSE CHAR(' ')
END
FROM TABLE
GROUP BY P1);
|
Hope this helps...
Cheers
Kolusu _________________ Kolusu
www.linkedin.com/in/kolusu |
|
| Back to top |
|
 |
Martin Beginner

Joined: 20 Mar 2006 Posts: 133 Topics: 58
|
Posted: Mon Jul 17, 2006 9:57 am Post subject: |
|
|
kolusu,
Thanks so much for looking into this.
I need one more small info. How do I set the value of say :-
P7 of Y1 = P7 of X1
Could you please let me know?
Thanks,
Martin |
|
| Back to top |
|
 |
kolusu Site Admin

Joined: 26 Nov 2002 Posts: 12401 Topics: 75 Location: San Jose
|
Posted: Mon Jul 17, 2006 10:02 am Post subject: |
|
|
| Quote: |
I need one more small info. How do I set the value of say :-
P7 of Y1 = P7 of X1
Could you please let me know?
|
Martin,
Huh ? What is P7 ?
Kolusu _________________ Kolusu
www.linkedin.com/in/kolusu |
|
| Back to top |
|
 |
Martin Beginner

Joined: 20 Mar 2006 Posts: 133 Topics: 58
|
Posted: Mon Jul 17, 2006 10:07 am Post subject: |
|
|
Ooops !!
Sorry for the confusion. P7 is another column on the table.
Also,
In the SQL given above Is the case always P1?
"CASE P1"  |
|
| Back to top |
|
 |
kolusu Site Admin

Joined: 26 Nov 2002 Posts: 12401 Topics: 75 Location: San Jose
|
Posted: Mon Jul 17, 2006 10:14 am Post subject: |
|
|
| Quote: |
Sorry for the confusion. P7 is another column on the table.
Also,
In the SQL given above Is the case always P1?
"CASE P1"
|
martin,
Your first post says that you need to validate P1 for X1 and depending on that you need to insert rows. Now if you want to validate another column , then simply change the P1 to the column you want in the case statement.
Kolusu _________________ Kolusu
www.linkedin.com/in/kolusu |
|
| Back to top |
|
 |
Martin Beginner

Joined: 20 Mar 2006 Posts: 133 Topics: 58
|
Posted: Mon Jul 17, 2006 10:30 am Post subject: |
|
|
Kolusu,
Please correct me If am wrong here: -
Here's what I understand of the SQL you provided.
WHEN 'X1' THEN CHAR('Y1')
ELSE CHAR(' ')
END
,CASE P1 .
This will set the the value of P1 to Y1.
CASE P1
WHEN 'X1' THEN MAX(P2) + 1
ELSE INT(0)
END
This will set the value of P2 = P2 +1
But,
,CASE P1
WHEN 'X1' THEN CHAR('EFG')
ELSE CHAR(' ')
END
Will this set the the column V3 = EFG?
- Martin |
|
| Back to top |
|
 |
kolusu Site Admin

Joined: 26 Nov 2002 Posts: 12401 Topics: 75 Location: San Jose
|
Posted: Mon Jul 17, 2006 10:33 am Post subject: |
|
|
| Quote: |
Will this set the the column V3 = EFG?
|
Yes.
Kolusu _________________ Kolusu
www.linkedin.com/in/kolusu |
|
| Back to top |
|
 |
|
|
|