Solved

select / insert distinct record running from aqua data against oracle

Posted on 2006-11-07
4
1,174 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:Stephen Lappin
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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Oracle SQL Select unique values from two columns 4 55
Oracle - SQL Query with Function 3 38
PL/SQL Two changes 7 29
Oracle Insert not working 10 28
Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

832 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