Need of SQL query in selecting distinct values from two tables

hi,
I need a query for selecting distinct values from two tables with one condition.
for eg:
there are two tables a & b.
in table a there are values like age,sex,name,empno and in table b valuses are such as age,salary,DOJ,empno.
here what i need is with the help of empno as unique field,i need to select distinct values from two tables (ie) except age.
can anybody please help me.
Thanks in advance,
Ratheesh

Not sure what you mean either, but perhaps this will start a dialog:
SELECT DISTINCT a.empno,
                a.name,
                a.sex,
                b.salary,
                b.doj
FROM    a,
        b
WHERE   a.empno = b.empno;Greg

Similar Messages

  • How to select distinct values from a table when it has composite primary ke

    Hi
    I have the requirement like , I need to select distinct one column values from the table which has composite primary key. How to acheive this functioinality using view object.
    Eg : Table 1 has col1 and col2, col3
    col1 col2 col3
    1 A NA
    1 B NA
    2 A NA
    3 C NA
    2 D NA
    primary key (col1,col2)
    I have to select distinct col1.
    Thanks

    Hi
    I got the solution for above. By Creating the read only view object we can acheive this.
    thanks

  • Can we implement the custom sql query in CR for joining the two tables

    Hi All,
    Is there anyway to implement the custom sql query in CR for joining the two tables?
    My requirement here is I need to write sql logics for joining the two tables...
    Thanks,
    Gana

    In the Database Expert, expand the Create New Connection folder and browse the subfolders to locate your data source.
    Log on to your data source if necessary.
    Under your data source, double-click the Add Command node.
    In the Add Command to Report dialog box, enter an appropriate query/command for the data source you have opened.
    For example:
    SELECT
        Customer.`Customer ID`,
        Customer.`Customer Name`,
        Customer.`Last Year's Sales`,
        Customer.`Region`,
        Customer.`Country`,
        Orders.`Order Amount`,
        Orders.`Customer ID`,
        Orders.`Order Date`
    FROM
        Customer Customer INNER JOIN Orders Orders ON
            Customer.`Customer ID` = Orders.`Customer ID`
    WHERE
        (Customer.`Country` = 'USA' OR
        Customer.`Country` = 'Canada') AND
        Customer.`Last Year's Sales` < 10000.
    ORDER BY
        Customer.`Country` ASC,
        Customer.`Region` ASC
    Note: The use of double or single quotes (and other SQL syntax) is determined by the database driver used by your report. You must, however, manually add the quotes and other elements of the syntax as you create the command.
    Optionally, you can create a parameter for your command by clicking Create and entering information in the Command Parameter dialog box.
    For more information about creating parameters, see To create a parameter for a command object.
    Click OK.
    You are returned to the Report Designer. In the Field Explorer, under Database Fields, a Command table appears listing the database fields you specified.
    Note:
    To construct the virtual table from your Command, the command must be executed once. If the command has parameters, you will be prompted to enter values for each one.
    By default, your command is called Command. You can change its alias by selecting it and pressing F2.

  • CE function to get distinct values from Column table

    Hi All,
    Could you please let me know the appropriate CE function to get the distinct values from column table.
    IT_WORK = SELECT DISTINCT AUFNR FROM :IT_WO_DETAILS;
    Thank you.

    Hi,
    If you have 10g, you can use Model( with model performance is better than connect by )
    Solution
    ========================================================================
    WITH t AS
    (SELECT '0989.726332, 1234.567432, 3453.736379, 3453.736379, 0989.726332, 3453.736379, 1234.567432, 1234.567432, 0989.726332'
    txt
    FROM DUAL)
    SELECT DISTINCT TRIM(CHAINE)
    FROM T
    MODEL
    RETURN UPDATED ROWS
    DIMENSION BY (0 POSITION)
    MEASURES (CAST( ' ' AS VARCHAR2(50)) AS CHAINE ,txt ,LENGTH(REGEXP_REPLACE(txt,'[^,]+',''))+1 NB_MOT)
    RULES
    (CHAINE[FOR POSITION FROM  1 TO NVL(NB_MOT[0],1) INCREMENT 1] =
    CASE WHEN NB_MOT[0] IS NULL THEN TXT[0] ELSE REGEXP_SUBSTR(txt[0],'[^,]+',1,CV(POSITION)) END );
    =========================================================================
    Demo
    =======================================================================
    SQL> WITH t AS
    2 (SELECT '0989.726332, 1234.567432, 3453.736379, 3453.736379, 0989.726332, 3453.736379, 123
    4.567432, 1234.567432, 0989.726332'
    3 txt
    4 FROM DUAL)
    5 SELECT DISTINCT TRIM(CHAINE)
    6 FROM T
    7 MODEL
    8 RETURN UPDATED ROWS
    9 DIMENSION BY (0 POSITION)
    10 MEASURES (CAST( ' ' AS VARCHAR2(50)) AS CHAINE ,txt ,LENGTH(REGEXP_REPLACE(txt,'[^,]+',''))+1 NB_MOT)
    11 RULES
    12 (CHAINE[FOR POSITION FROM  1 TO NVL(NB_MOT[0],1) INCREMENT 1] =
    13 CASE WHEN NB_MOT[0] IS NULL THEN TXT[0] ELSE REGEXP_SUBSTR(txt[0],'[^,]+',1,CV(POSITION)) END );
    TRIM(CHAINE)
    3453.736379
    1234.567432
    0989.726332
    SQL>
    ========================================================================

  • To Select the data from two table one is transp table and onther is cluster

    Hi All,
    I want to select the data from two tables
    Here i am giving with an example.
    Fileds: kunnr belnr from bseg.  table bseg
    fields: adrnr from kna1     table: kna1.
    Know i want to put these into one internal table based on kunnr and belnr.
    Thanks in advance.
    Ramesh

    Hi,
       U cant use joins on cluster table and BSEG is a cluster table so use FOR  ALL ENTRIES for taht
    refer this code
    *&      Form  sub_read_bsak
          text
    -->  p1        text
    <--  p2        text
    FORM sub_read_bsak.
    *--Select data from BSAK Table
      SELECT lifnr
             augdt
             augbl
             gjahr
             belnr
             xblnr
             blart
             dmbtr
             mwskz
             mwsts
             sgtxt
             FROM bsak
             INTO CORRESPONDING FIELDS OF TABLE it_bsak
             WHERE belnr IN s_belnr
             AND   augdt IN s_augdt.
      IF sy-subrc EQ 0.
    *--Sort table by accounting document and vendor number
        SORT it_bsak BY belnr lifnr.
      ENDIF.
    ENDFORM.                    " sub_read_bsak
    *&      Form  sub_read_bseg
          text
    -->  p1        text
    <--  p2        text
    FORM sub_read_bseg.
      IF NOT it_bsak[] IS INITIAL.
    *--Select data from BSEG table
        SELECT belnr
               gjahr
               shkzg
               kostl
               hkont
               ebeln
               ebelp
               FROM bseg
               INTO CORRESPONDING FIELDS OF TABLE it_bseg
               FOR ALL ENTRIES IN it_bsak
               WHERE belnr EQ it_bsak-belnr
               AND   gjahr EQ it_bsak-gjahr
               AND   shkzg EQ 'S'.
        IF sy-subrc EQ 0.
    *--Sort table by accounting document
          SORT it_bseg BY belnr.
        ENDIF.
      ENDIF.
    ENDFORM.                    " sub_read_bseg

  • Dynamic SQL query to get single value from list in a column

    I have two tables: tblWorkers and tblSkills. tblWorkers has a
    column, Skills, which is populated from a multiple-checkbox form
    field with one or more skill_IDs from tblSkills, so each
    tblWorkers.Skills consists of a list of one or more comma-delimited
    values. For any Skill_ID, I need to generate a listing of all
    Workers with the corresponding skill, so I have tried to do
    something along the lines of SELECT WorkerName FROM tblWorkers
    WHERE Skills IN (Skills, #FORM.Skill_ID#) ... or WHERE Skills IN
    (ListFind(Skills, #FORM.Skill_ID#)) ... etc. ??? My results (once I
    got data type mismatches out of the way) return all Workers, not
    just those with the desired Skill. There must be an easy way to do
    this ... How do people with a bit more CF/SQL experience than I
    have do this???

    quote:
    Originally posted by:
    lucapac
    I have two tables: tblWorkers and tblSkills. tblWorkers has a
    column, Skills, which is populated from a multiple-checkbox form
    field with one or more skill_IDs from tblSkills, so each
    tblWorkers.Skills consists of a list of one or more comma-delimited
    values. For any Skill_ID, I need to generate a listing of all
    Workers with the corresponding skill, so I have tried to do
    something along the lines of SELECT WorkerName FROM tblWorkers
    WHERE Skills IN (Skills, #FORM.Skill_ID#) ... or WHERE Skills IN
    (ListFind(Skills, #FORM.Skill_ID#)) ... etc. ??? My results (once I
    got data type mismatches out of the way) return all Workers, not
    just those with the desired Skill. There must be an easy way to do
    this ... How do people with a bit more CF/SQL experience than I
    have do this???
    As Kronin implied, we normalize our databases. If you don't
    understand that answer, the book "Database Design for Mere Mortals"
    is often mentioned on this forum.

  • Select Distinct rows from 3 tables

    I need to retrive information from 3 different tables by applying a specific condition.
    I have the following query which works fine for retrieving desired data from 2 tables.
    SELECT
    a.userId
    FROM userGeneral
    a inner
    join userSpecific
    b on a.userId
    = b.userId
    WHERE
    a.userActive
    = 1
    userId
    111
    222
    333
    444
    Now I have a third table called userMgr which may contain multiple records for each userId with a corresponding mgrId value. It has a primary key identity column called userRecId and I would like to fetch the
    mgrId value corresponding to the MAX(userRecId) for the matching userId.
    userRecId  |  userId  | mgrId
    1 |    111    | 123
    2 | 111 | 234
    3 | 111 | 345
    4 | 333 | 345
    5 | 333 |  456
    The resultset should be as follows after joining all the 3 tables.
    userId | mgrId
    111 | 345
    222 | NULL
    333 | 456
    444 | NULL
    Can anyone please help with this query.

    I need to retrive information from 3 different tables by applying a specific condition.
    I have the following query which works fine for retrieving desired data from 2 tables.
    SELECT
    a.userId
    FROM userGeneral
    a inner
    join userSpecific
    b on a.userId
    = b.userId
    WHERE
    a.userActive
    = 1
    userId
    111
    222
    333
    444
    Now I have a third table called userMgr which may contain multiple records for each userId with a corresponding mgrId value. It has a primary key identity column called userRecId and I would like to fetch the
    mgrId value corresponding to the MAX(userRecId) for the matching userId.
    userRecId  |  userId  | mgrId
    1 |    111    | 123
    2 | 111 | 234
    3 | 111 | 345
    4 | 333 | 345
    5 | 333 |  456
    The resultset should be as follows after joining all the 3 tables.
    userId | mgrId
    111 | 345
    222 | NULL
    333 | 456
    444 | NULL
    Can anyone please help with this query.
    try this:
    select userId,userRecId,
    from userMgr,
    (SELECT a.userId, max(a.userRecId)
    FROM userGeneral a inner join userSpecific b on a.userId = b.userId
    group by a.userId
    WHERE a.userActive = 1) table1(userId,userRecId)
    where userMgr.userRecId = table1.userRecId

  • Cannot select different values from a table in a view object

    Hi everyone,
    I have two tables, Employee : ID, NAME, SALARY, NO_ROLE and role : ROLE_ID, ROLE_EMP. I join them with NO_ROLE and ROLE_ID. I created a view object with these two tables in JDeveloper, I made a LOV with ROLE_EMP and I want to be ale to create an employee with his ID, NAME, SALARY and by choosing a value in the ROLE_EMP list. The problem is that when I choose a ROLE_EMP, I cannot commit because the link between ROLE_EMP and ROLE_ID is not made correctly in ADF.
    In SQL, that's what I'm trying to do: INSERT INTO Employee VALUES (ID, NAME, SALARY, NO_ROLE) WHERE NO_ROLE = ROLE_ID of the ROLE_EMP I have selected.
    Thanks for your help ;)
    PS: I followed this tutorial to do this, but it is not exactly what I want to do: http://docs.oracle.com/cd/E18941_01/tutorials/jdtut_11r2_55/jdtut_11r2_55_2.html

    To be more generic, when I create an employee and select his role_emp (the name of the role) I want that the role_id of the role_emp to commit.

  • Selecting Max Value from Huge Table

    Dear Proffessionals
    I have a huge table (20,000,000+ records) with the following columns:
    [Time], [User], [Value]
    The values in [Value] column can recur for a single User at a Time e.g.
    2015-01-01, Me, X
    2015-01-01, Me, Y
    2015-01-01, Me, X
    2015-01-02, Me, Z
    2015-01-02, Me, X
    2015-01-02, Me, Z
    For each day, and for every user I want to have the maximum recurring value :
    2015-01-01, Me, X
    2015-01-02, Me, Z
    to be inserted into another table.
    PS: I want the MOST optimized way of achieving this functionality, bcause I am expecting a growth on the raw table over time, so PERFORMANCE is of great consideration.
    I would really appreciate it, if somebody can help me.
    Regards

    I can think of two techniques based on the data selecticity
    1) using row number function
    2) using cross apply operator
    USE Northwind;
    -- Solution 1
    SELECT S.SupplierID, S.CompanyName, CA.ProductID, CA.UnitPrice
    FROM dbo.Suppliers AS S
      CROSS APPLY
        (SELECT TOP (10) *
         FROM dbo.Products AS P
         WHERE P.SupplierID = S.SupplierID
         ORDER BY UnitPrice DESC, ProductID DESC) AS CA
    ORDER BY S.SupplierID, CA.UnitPrice DESC, CA.ProductID DESC;
    -- Solution 2
    WITH C AS
      SELECT S.SupplierID, S.CompanyName, P.ProductID, P.UnitPrice,
        ROW_NUMBER() OVER(
          PARTITION BY P.SupplierID
          ORDER BY P.UnitPrice DESC, P.ProductID DESC) AS RowNum
      FROM dbo.Suppliers AS S
        JOIN dbo.Products AS P
          ON P.SupplierID = S.SupplierID
    SELECT SupplierID, CompanyName, ProductID, UnitPrice
    FROM C
    WHERE RowNum <= 10
    ORDER BY SupplierID, ProductID DESC, UnitPrice DESC;
    Best Regards,Uri Dimant SQL Server MVP,
    http://sqlblog.com/blogs/uri_dimant/
    MS SQL optimization: MS SQL Development and Optimization
    MS SQL Consulting:
    Large scale of database and data cleansing
    Remote DBA Services:
    Improves MS SQL Database Performance
    SQL Server Integration Services:
    Business Intelligence

  • Select unique rows from two tables...

    Hi,
    I have two tables, replies1 and replies2.
    SQL> desc replies
    Name Null? Type
    URN VARCHAR2(36)
    ADDRESS VARCHAR2(18)
    FILESIZE NUMBER
    AS_NUM VARCHAR2(6)
    SQL> desc replies2
    Name Null? Type
    URN VARCHAR2(36)
    ADDRESS VARCHAR2(18)
    AS_NUM VARCHAR2(6)
    Both of the tables have no primary keys, but I have indixes on (urn, addrss) combination on both....
    I am trying to select the unique rows with (urn, address) from replies2, and then find the matching size from replies...
    I am using the following query:
    select distinct replies2.urn, replies2.address, replies.filesize from replies2, replies where replies2.AS_NUM like 'XYZ' and replies.urn = replies2.urn;
    I cannot figure out why it won't work. the way I understand it is that, distinct will give all distinct combination of all column names that follow, which is what I want...
    I know it is wrong, because the query:
    select count(*) from replies2 where AS_NUM like 'XYZ' returns less number of rows than the above query.
    Any help would be greatly appreciated.
    Thank you
    Oz.

    Thanks a lot Mohan for your reply.
    urn is not a unique key. Several rows could have the same (urn, address) pair in both tables. What I want is retrieve all (urn, address) rows from one table, and find the size from the other table to make a (urn, address, size). I want all unique combinations of (urn, address) to appear in the output.
    AS_NUM is an empty column in replies... It would've been a lot easier if it wasn't, since then I'll just say: select distinct urn, address, filesize from replies where AS_NUM like 'XYZ';
    I will try your query though and let u know how it goes. It takes quite a while to run since my tables are huge.

  • Distinct rows from two tables linked by a third one

    Hi,
    I need to write an SQL sentence to bring a group of DISTINCT rows, each row in a form of: A.X, A.Y, C.IDBOLETA. Tables A and C are linked through the table C, as follow:
    Table A: INDUSTRIAGEO
    NIT VARCHAR2(20) NOT NULL,
    CORRELAIGEO NUMERIC(2) NOT NULL,
    NOMBREAGENCIA VARCHAR2(255) NOT NULL,
    DIRECCION VARCHAR2(255),
    X NUMERIC(12,8) NOT NULL,
    Y NUMERIC(12,8) NOT NULL,
    CONSTRAINT PKGEOINDUST PRIMARY KEY(NIT, CORRELAIGEO),
    CONSTRAINT FKGEOINDUST FOREIGN KEY(NIT) REFERENCES INDUSTRIA(NIT)
    Table B: ANALISIS
    IDANALISIS NUMERIC(12) NOT NULL,
    NIT VARCHAR2(20) NOT NULL,
    CORRELAIGEO NUMERIC(2) NOT NULL,
    FECHA DATE NOT NULL,
    IDINSPECTOR NUMERIC(4) NOT NULL,
    CONSTRAINT PKANALISIS PRIMARY KEY(IDANALISIS),
    CONSTRAINT FKANALGEOI FOREIGN KEY(NIT, CORRELAIGEO) REFERENCES INDUSTRIAGEO(NIT, CORRELAIGEO),
    CONSTRAINT FKANALINSP FOREIGN KEY(IDINSPECTOR) REFERENCES INSPECTOR(IDINSPECTOR)
    Table C: ANALISISBOLETA
    IDANALISIS NUMERIC(12) NOT NULL,
    CORRELAANALISIS NUMERIC(2) NOT NULL,
    IDBOLETA NUMERIC(12) NOT NULL,
    IDCATEGORIAINDUSTRIA NUMERIC(3) NOT NULL,
    CONSTRAINT PKANALBOLE PRIMARY KEY (IDANALISIS, CORRELAANALISIS),
    CONSTRAINT UKANALBOLE UNIQUE (IDBOLETA),
    CONSTRAINT FKANALBOLE FOREIGN KEY(IDANALISIS) REFERENCES COPS.ANALISIS(IDANALISIS),
    CONSTRAINT FKANALCATI FOREIGN KEY(IDCATEGORIAINDUSTRIA) REFERENCES COPS.CATEGORIAINDUSTRIA(IDCATEGORIAINDUSTRIA)
    Thanks for your help!!!
    Mario

    Perhaps
    select distinct a.x,a.y,c.idboleta
      from industriaego a,
           cops.analisis b,
           analisisboleta c
    where a.nit = b.nit
       and a.correlaigeo = b.correlaigeo
       and b.idanalisis = c.idanalisisRegards
    Etbin
    Edited by: Etbin on 4.5.2010 21:42
    the copy/paste elf did it again: b.correlaigeo instead of b.a.correlaigeo (as in the OP's comment below)

  • Need a sql query to get the difference between two timestamp in the format of hh:mm:ss.msec

    I have a database table where it keeps record of the transaction when it starts at StartTime and when it ends at EndTime. Both these entries are having the timestamp entries. Say for example, I have a tuple with Entries like 'Transaction A' starts at '2014-05-07
    20:55:03.170' and ends at '2014-05-08 08:56:03.170'. I need to find the difference between these two timestamps and my expected output is 12:01:00.000. Let me know how to achieve this ? 

    Hi,
    You can use below script which calculates difference as DD:HH:MM:SS. You can modify the same:
    DECLARE @startTime DATETIME
    DECLARE @endTime DATETIME
    SET @startTime = '2013-11-05 12:20:35'
    SET @endTime = '2013-11-10 01:22:30'
    SELECT [DD:HH:MM:SS] =
    CAST((DATEDIFF(HOUR, @startTime, @endTime) / 24) AS VARCHAR)
    + ':' +
    CAST((DATEDIFF(HOUR, @startTime, @endTime) % 24) AS VARCHAR)
    + ':' +
    CASE WHEN DATEPART(SECOND, @endTime) >= DATEPART(SECOND, @startTime)
    THEN CAST((DATEDIFF(MINUTE, @startTime, @endTime) % 60) AS VARCHAR)
    ELSE
    CAST((DATEDIFF(MINUTE, DATEADD(MINUTE, -1, @endTime), @endTime) % 60)
    AS VARCHAR)
    END
    + ':' + CAST((DATEDIFF(SECOND, @startTime, @endTime) % 60) AS VARCHAR),
    [StringFormat] =
    CAST((DATEDIFF(HOUR , @startTime, @endTime) / 24) AS VARCHAR) +
    ' Days ' +
    CAST((DATEDIFF(HOUR , @startTime, @endTime) % 24) AS VARCHAR) +
    ' Hours ' +
    CASE WHEN DATEPART(SECOND, @endTime) >= DATEPART(SECOND, @startTime)
    THEN CAST((DATEDIFF(MINUTE, @startTime, @endTime) % 60) AS VARCHAR)
    ELSE
    CAST((DATEDIFF(MINUTE, DATEADD(MINUTE, -1, @endTime), @endTime) % 60)
    AS VARCHAR)
    END +
    ' Minutes ' +
    CAST((DATEDIFF(SECOND, @startTime, @endTime) % 60) AS VARCHAR) +
    ' Seconds '
    Reference:
    http://sqlandme.com/2013/12/23/sql-server-calculating-elapsed-time-from-datetime/
    - Vishal
    SqlAndMe.com

  • Need a Query to search a value from all tables.

    Hi,
    I would like to know, is there any way that we could search a string from all the tables.
    For example I need to find a STRING 'hello world', on which table it presents. I want to search it from all the tables, as there might be a situtation
    where we dont knwo which table the value gets stored.
    REgards
    Suresh

    Run this code ---this is only search for required value in VARCHAR2 column.as per ur requirement u can change it to oher dataype..
    Reply if its helpful to you
    DECLARE
    S SYS_REFCURSOR;
    CURSOR c_user_col IS
    SELECT TABLE_NAME,COLUMN_NAME,DATA_TYPE FROM USER_TAB_COLUMNS,TAB
    WHERE TABLE_NAME=TNAME AND TABTYPE='TABLE'
    ORDER BY TABLE_NAME;
    TYPE TAB_LIST
    IS
    RECORD
    TABLE_NAME VARCHAR2(1000),
    COLUMN_NAME VARCHAR2(1000),
    DATA_TYPE VARCHAR2(100));
    TYPE T_TAB_LIST
    IS
    TABLE OF TAB_LIST;
    L_TAB_LIST T_TAB_LIST := T_TAB_LIST();
    L_STMT CLOB;
    l_exists NUMBER;
    BEGIN
    FOR i IN c_user_col LOOP
    L_TAB_LIST.EXTEND;
    L_TAB_LIST(L_TAB_LIST.LAST).TABLE_NAME := I.TABLE_NAME;
    L_TAB_LIST(L_TAB_LIST.LAST).COLUMN_NAME := i.COLUMN_NAME;
    L_TAB_LIST(L_TAB_LIST.LAST).DATA_TYPE := i.DATA_TYPE;
    END LOOP;
    FOR i in 1..L_TAB_LIST.COUNT LOOP
    l_exists := NULL;
    IF L_TAB_LIST(I).DATA_TYPE = 'VARCHAR2' THEN
    L_STMT := 'SELECT 1 FROM '||L_TAB_LIST(I).TABLE_NAME||' WHERE '||L_TAB_LIST(I).COLUMN_NAME||'=''samplesdfsdfsdf''';
    OPEN S FOR L_STMT;
    FETCH S INTO l_exists;
    CLOSE S;
    IF l_exists IS NULL THEN
    NULL;
    ELSE
    DBMS_OUTPUT.PUT_LINE('Table name: '||L_TAB_LIST(I).TABLE_NAME||'--Column Name: '||L_TAB_LIST(I).COLUMN_NAME);
    DBMS_OUTPUT.PUT_LINE(L_STMT);
    END IF;
    END IF;
    END LOOP;
    END;

  • Select Distinct rows from multiple tables

    Table 1 is a List of Vendors - VID, PID, VName, VAddress, VPhone
    Table 2 is a list of Products - PID, PName, PPrice, PWeight, PColor
    I need to produce a list of unique PID's showing the following fields - PID, VName, PName, PColor
    So, Here is my failed attempt:
    SELECT P.PID, V.VName, P.PName, P.PColor
    FROM Products P INNER JOIN Vendors V ON P.PID = V.PID
    GROUP BY P.PID

    If you post DDL, sample data and your desired output based on that sample data, someone can probably create a query that does what you want. 
    And I have to tell you that Table 1 is not a list of vendors unless you have a system where a vendor provides one and only one product - something that is unusual. If your system is one where a vendor should provide any number of products (and if a product
    can be provided by any number of vendors), you have some fundamental schema issues to correct.
    Lastly, there are sticky posts at the top of the forum that provide suggestions for posting questions.  Please have a look - help your readers help you by providing sufficient information.  Phrases such as "failed" or "does not work"
    do not provide any useful detail.  And one good rule of thumb - any time you feel you need to use (or say) distinct in a query is an indication that something somewhere is wrong.  It could be a schema issue, a misunderstanding of the schema or the
    goal, an incorrect query, etc.  There are few instances where distinct is needed in a well-defined and implemented system, IMO.

  • Help needed to merge values from two tables -

    We have these three test tables shown, which have a common ID; And amount fields; I want the -------
    test1.val = test2.val + test3.val
    when they have common ids in them;
    I have shown the test data below: And the expected output;
    NOTE: In database with real table values - each of them have millions of rows in them. So effeciency is very important here;
    SQL> desc test1;
    Name Null? Type
    ID NUMBER
    VAL NUMBER
    SQL> desc test2;
    Name Null? Type
    ID NUMBER
    VAL NUMBER
    SQL> desc test3;
    Name Null? Type
    ID NUMBER
    VAL NUMBER
    SQL> select * from test1;
    ID VAL
    1 50
    2 50
    3 55
    4 60
    5 20
    5 rows selected.
    SQL> select * from test2;
    ID VAL
    1 25
    1 25
    2 5
    2 5
    4 75
    5 rows selected.
    SQL> select * from test3;
    ID VAL
    1 25
    1 25
    2 5
    2 25
    2 25
    5 10
    6 rows selected.
    I EXPECT the output to be:
    SQL> select * from test1;
    ID VAL
    1 100
    2 65
    3 20
    4 75
    3 20
    5 10
    6 rows selected.

    Need help with the update queries - joining three tables -

Maybe you are looking for