Solved

select / insert distinct record running from aqua data against oracle

Posted on 2006-11-07
4
1,162 Views
Last Modified: 2013-12-12
I am trying to write sql statement to select / insert a distinct value (metername) and other info from a table where pointname is not distinct.   metername = pointname

insert into METER(METERNAME,METERTYPE,DESCRIPTION,METERID,LANGCODE,HASLD)
select distinct POINTNAME,'GAUGE',DESCRIPTION,meterseq.nextval,'EN',0
from mydatabase

I'm using aqua data studio against a oracle 9i db

PLEASE HELP!!!
0
Comment
Question by:brainsprocket
4 Comments
 
LVL 12

Accepted Solution

by:
jwahl earned 250 total points
ID: 17896059
in this statement meterseq.nextval changes with every selected row, so POINTNAME is never distinct.

assuming POINTNAME and DESCRIPTION are distinct, you could try this:

insert into METER(METERNAME,METERTYPE,DESCRIPTION,METERID,LANGCODE,HASLD)
select POINTNAME,'GAUGE',DESCRIPTION,meterseq.nextval,'EN',0
from (
    select DISTINCT POINTNAME, DESCRIPTION
    from mydatabase);
 
0
 
LVL 7

Expert Comment

by:lappins
ID: 17896532
I have not tested this, but try distinct on individual columns instread of the row:

insert into METER(METERNAME,METERTYPE,DESCRIPTION,METERID,LANGCODE,HASLD)
select (distinct POINTNAME),'GAUGE',(distinct DESCRIPTION),meterseq.nextval,'EN',0
from mydatabase
0
 
LVL 14

Assisted Solution

by:GGuzdziol
GGuzdziol earned 250 total points
ID: 17896846
Try this:

insert into METER(METERNAME,METERTYPE,DESCRIPTION,LANGCODE,HASLD,METERID)
  select a.*, meterseq.nextval
    from (
      select POINTNAME,'GAUGE',MAX(DESCRIPTION),'EN',0
        from mydatabase
        group by pointname
    ) a

You have to decide which description You want in case when several same pointname's are present - I've chosen last one alphabetically (this is the MAX function -if You want first just use MIN. If You need concatenation of all of them follow this link for details: http://asktom.oracle.com/pls/ask/f?p=4950:8:14499142677306489710::NO::F4950_P8_DISPLAYID,F4950_P8_CRITERIA:2196162600402).
0
 
LVL 28

Expert Comment

by:Naveen Kumar
ID: 17939593
lappins, the one which you gave is not working. It is giving missing expression error. May be that is the reason you didn't want to test it :)

I think the easiest way is to go as per jwahl.

Thanks
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

This article started out as an Experts-Exchange question, which then grew into a quick tip to go along with an IOUG presentation for the Collaborate confernce and then later grew again into a full blown article with expanded functionality and legacy…
Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…
This video shows how to recover a database from a user managed backup

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now