Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Insert Decode help

Status
Not open for further replies.

kss444

Programmer
Sep 19, 2006
306
0
0
US
I have a decode statment:
select B.account_number
, sum(decode(NLS_UPPER(db_cr_type), 'D', A.amount)) Total_Debits
, sum(decode(NLS_UPPER(db_cr_type), 'C', A.amount)) Total_Credits
, '0' OutStanding_Beginning
, count(A.amount) as New_Items
, count(decode(NLS_UPPER(A.state), 'R', A.state )) Items_Mathched
, count(decode(NLS_UPPER(A.state), 'O', A.state )) OutStanding_Items
, sum(decode(NLS_UPPER(db_cr_type), 'D', A.amount)) - sum(decode(NLS_UPPER(db_cr_type), 'C', A.amount)) Net

--, sum( decode( c2, 'col3', c3 ) ) col3
FROM ITEM A
LEFT JOIN ACCOUNT B
ON A.ACCOUNT = B.OBJECT_ID
WHERE B.ACCOUNT_NUMBER LIKE '25710%'
--and A.accepted_date between sysdate - 7 and sysdate
--and A.accepted_date between '28-Nov-2006' and '30-NOV-2006'
group by B.account_number
order by B.account_number

This returns me everything I need, now I want to convert this to an insert so I can insert it into a table called archnet.
Can anyone help me, thanks
 
Code:
INSERT INTO archnet (column,column.....)
SELECT your_select_statement
/

The ORDER BY is redundant.
 
Didn't work, also I created seq_arch_net I am using as autonumber, sequence in oracle.
Error is ORA-00936 missing expression, thats all I get.

insert into arch_net values
select seq_arch_net_id.NEXTVAL,
select B.account_number
, sum(decode(NLS_UPPER(db_cr_type), 'D', A.amount)) Total_Debits
, sum(decode(NLS_UPPER(db_cr_type), 'C', A.amount)) Total_Credits
, '0' OutStanding_Beginning
, count(A.amount) as New_Items
, count(decode(NLS_UPPER(A.state), 'R', A.state )) Items_Mathched
, count(decode(NLS_UPPER(A.state), 'O', A.state )) OutStanding_Items
, sum(decode(NLS_UPPER(db_cr_type), 'D', A.amount)) - sum(decode(NLS_UPPER(db_cr_type), 'C', A.amount)) Net

FROM ITEM A
LEFT JOIN ACCOUNT B
ON A.ACCOUNT = B.OBJECT_ID
WHERE B.ACCOUNT_NUMBER LIKE '25710%'

group by B.account_number
order by B.account_number
 
Look carefully at my code. It doesn't use VALUES.
 
I understand but if I remove the "Values" word then I get an error that says Missing VALUES keyword.
insert into arch_net (acct_id,total_debits,total_credits,outstanding_items_beginning,new_items_added,items_macthed
,outstanding_items_end,netamount)
seq_arch_net_id.NEXTVAL,
select B.account_number
, sum(decode(NLS_UPPER(db_cr_type), 'D', A.amount)) Total_Debits
, sum(decode(NLS_UPPER(db_cr_type), 'C', A.amount)) Total_Credits
, '0' OutStanding_Beginning
, count(A.amount) as New_Items
, count(decode(NLS_UPPER(A.state), 'R', A.state )) Items_Mathched
, count(decode(NLS_UPPER(A.state), 'O', A.state )) OutStanding_Items
, sum(decode(NLS_UPPER(db_cr_type), 'D', A.amount)) - sum(decode(NLS_UPPER(db_cr_type), 'C', A.amount)) Net

FROM ITEM A
LEFT JOIN ACCOUNT B
ON A.ACCOUNT = B.OBJECT_ID
WHERE B.ACCOUNT_NUMBER LIKE '25710%'

group by B.account_number
order by B.account_number
 
Here I get insert into arch_net
ORA-00936: missing expression

insert into arch_net
(acct_id,
account_number,
total_debits,
total_credits,
outstanding_items_beginning,
new_items_added,
items_macthed,
outstanding_items_end,
netamount) VALUES

select seq_arch_net_id.NEXTVAL,
B.account_number
, sum(decode(NLS_UPPER(db_cr_type), 'D', A.amount)) Total_Debits
, sum(decode(NLS_UPPER(db_cr_type), 'C', A.amount)) Total_Credits
, '0' OutStanding_Beginning
, count(A.amount) as New_Items
, count(decode(NLS_UPPER(A.state), 'R', A.state )) Items_Mathched
, count(decode(NLS_UPPER(A.state), 'O', A.state )) OutStanding_Items
, sum(decode(NLS_UPPER(db_cr_type), 'D', A.amount)) - sum(decode(NLS_UPPER(db_cr_type), 'C', A.amount)) Net

FROM ITEM A
LEFT JOIN ACCOUNT B
ON A.ACCOUNT = B.OBJECT_ID
WHERE B.ACCOUNT_NUMBER LIKE '25710%'

group by B.account_number
 
Code:
insert into arch_net
(acct_id,
account_number,
total_debits,
total_credits,
outstanding_items_beginning,
new_items_added,
items_macthed,
outstanding_items_end,
netamount)   
SELECT seq_arch_net_id.NEXTVAL,
B.account_number
     , sum(decode(NLS_UPPER(db_cr_type), 'D', A.amount)) Total_Debits
     , sum(decode(NLS_UPPER(db_cr_type), 'C', A.amount)) Total_Credits
     , '0' OutStanding_Beginning
     , count(A.amount) as New_Items
     , count(decode(NLS_UPPER(A.state), 'R', A.state )) Items_Mathched
     , count(decode(NLS_UPPER(A.state), 'O', A.state )) OutStanding_Items
     , sum(decode(NLS_UPPER(db_cr_type), 'D', A.amount)) - sum(decode(NLS_UPPER(db_cr_type), 'C', A.amount)) Net    
FROM ITEM A
LEFT JOIN ACCOUNT B
ON    A.ACCOUNT = B.OBJECT_ID
WHERE  B.ACCOUNT_NUMBER LIKE '25710%'
group by B.account_number
 
You use SELECT or VALUES, bot not both or neither.
 
Thanks for the help, I finally got it but I had to take out the seq_arch_net_id.NEXTVAL, it gave me errors if I put it before the select and it gave me errors when I had it in the select.

When in the select it gave me 2:51:33 PM ORA-02287: sequence number not allowed here.

But I believe I can get by with out it, I was just using it as my Primary Key, and having it autonumber. I can also play around with it.
Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top