Sql query union double rows

Hi,
i have a problem with my sql query.
I need the difference from query 2 - query 1.
the red marked row should be ignored because its already in table 1.
Union compares all colums not just the first 3.
Here's a screen of the problem:
http://c60.img-up.net/?up=screenoracskt6.JPG
thx for all

Hi,
Welcome to the forum!
Whenever you have a problem, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only), and the results you want from that data.
Explain how you get those results from that data.
Always say what version of oracle you're using.
user6754335 wrote:
Hi,
i have a problem with my sql query.
I need the difference from query 2 - query 1.
the red marked row should be ignored because its already in table 1.
Union compares all colums not just the first 3.That's right; UNION compare all the columns selected. If you're only interested in 3 of the columns, then only select those 3.
If you need to display more columns, but not where all 3 of them match, then the best way is probably an outer join; but there are lots of other ways, including NOT EXISTS, NOT IN, and MINUS (in a sub-query).
Here's a screen of the problem:
http://c60.img-up.net/?up=screenoracskt6.JPG
Post anything relevant on this site.

Similar Messages

  • [Excel] Running a SQL Query to delete rows

    Hello Experts,
    Background: I am attempting to use a dba of my companies time keeping system and implement it with Power BI tools. Given the file size restrictions within Power Bi itself I need to lower my file size by removing all time logs from
    inactive employees.
    I have a question regarding whether or not you can use a sql query to delete rows in excel. I have roughly 200,000+ rows in my excel spreadsheet. I am attempting to delete all rows where an employee equals inactive. I have attempted to
    delete these rows by sorting them and doing a bulk delete and clear contents, but it seems to crash my excel every time.  My thought process is that using a query that does a timed delete might put less of a burden on deleting the massive amount of data.
    I would like to use this: DELETE * FROM [Table_(...)_Actual$] WHERE [Current] = "Inactive" (Will add more once I know it is possible to use sql queries in Excel.
    Any information on whether or not this is possible would be appreciated.
    Regards,
    Link

    Running SQL Query in Excel is possible, however, the delete query is not supported in Excel.
    You are more restricted in deleting Excel data than   data from a relational data source. In a relational database, "row" has no   meaning or existence apart from "record"; in an Excel worksheet, this is not   true. You can delete values
    in fields (cells). Please see:http://support.microsoft.com/kb/257819/en-us
    One workaround : Use update query to set the rows as null, then use select query.
    e.g. 
    SQL = "update [sheet2$A1:B5] set name=null,age=null where name='andy'"
    cnn.Execute SQL
    SQL = "select name,age from [sheet2$A1:B5] where name is not null"
    Wind Zhang
    TechNet Community Support

  • Need help in SQL Query: Update a row in a table & insert the same row into another table

    I want to update a row in a table say Table A and the updated row should be inserted into another table say Table B. I need to do it in a single SQL query and i don't want to do it in PL/SQL with triggers. And i tried with MERGE statement but its working with this scenario. (Note: I'm using Oracle Database 10g Enterprise Edition Release 10.2.0.1.0).
    Thanks in Advance.

    Using Sven's code as an example, you could save the updated row in a sql plus variable. (also untested):
    SQL> var v_id number
    update tableA  
    set colB='ABC' 
    where colC='XYZ' 
    returning id into :v_id;
    insert into table A_History (ID, colA, colB, ColC)  
    select id, ColA, ColB, ColC  
    from tableA  
    where id = :v_id;   

  • Need sql query to convert rows to columns

    Im using oracle 11g.
    I have data in a table as below: Column names are type,month,percentage
    TYPE      MONTH   PERCENTAGE
    A1         JAN-2013     100
    A2         JAN-2013     100
    A3         JAN-2013     95
    A4         JAN-2013     98
    A5         JAN-2013     99
    A6         JAN-2013     90
    A7         JAN-2013     92
    A1         FEB-2013     100
    A2         FEB-2013     99
    A3         FEB-2013     88
    A4         FEB-2013     67
    A5         FEB-2013     98
    A6         FEB-2013     95
    A7         FEB-2013     84
    The desired output using a sql query is as below
    TYPE     JAN-2013     FEB-2013
    A1         100             100
    A2         100             99
    A3         95               88
    A4         98               67
    A5         99               98
    A6         90               95
    A7         92               84

    Try this:
    select * from table_name
    PIVOT(   max(Percentage)
                     for MONTH
                     in ('JAN-2013','FEB-2013')
                     ) ;with data
    with t(TYPE,MONTH,PERCENTAGE) as
    SELECT 'A1', 'JAN-2013', 100
      FROM DUAL
    UNION ALL
    SELECT 'A2', 'JAN-2013', 100
      FROM DUAL
    UNION ALL
    SELECT 'A3', 'JAN-2013', 95
      FROM DUAL
    UNION ALL
    SELECT 'A4', 'JAN-2013', 98
      FROM DUAL
    UNION ALL
    SELECT 'A1', 'FEB-2013', 100
      FROM DUAL
    UNION ALL
    SELECT 'A2', 'FEB-2013', 99
      FROM DUAL
    UNION ALL
    SELECT 'A3', 'FEB-2013', 88
      FROM DUAL
    UNION ALL
    SELECT 'A4', 'FEB-2013', 67
      FROM DUAL
    select * from t PIVOT(   max(Percentage)
                     for MONTH
                     in ('JAN-2013','FEB-2013')
                     ) ;

  • Displaying SQL Query results in rows instead of Columns

    Hi,
    I'm fairly new to Oracle so forgive me if this is a really stupid question.
    I used Mysql for a while and if I wanted to display query results in rows instead of columns I could end the SQL command with '\G' instead of a semicolon.
    This would give me output like...
    Column_1: AAAA
    Column_2: BBBB
    Column_3: CCCC
    Instead of the normal
    Column_1 Column_2 Column_3
    AAAAAA BBBBBBB CCCCCCC
    Is there an equivalent in SQLPlus to the MySQL \G termination?
    Thanks.
    John

    > so forgive me if this is a really stupid question.
    It is certainly not a stupid question, but pivoting is a very frequently asked and answered question:
    [url http://forums.oracle.com/forums/search.jspa?threadID=&q=pivot&objID=f75&dateRange=all&userID=&numResults=15]http://forums.oracle.com/forums/search.jspa?threadID=&q=pivot&objID=f75&dateRange=all&userID=&numResults=15
    In 11g you have special PIVOT and UNPIVOT functions.
    Regards,
    Rob.

  • Sql query to add row id without loops

    I am aware of the statement SELECT ROW_NUMBER() OVER (ORDER ...
    But we need more functionality. Can we make the row_number to repeat counting from 1 after a specific field changes in value.  I know how to achieve what I want using a cursor within a stored procedure. Is there a way we
    achieve the same just using sql statements and without stored procedure/loop
    For example, we have built the following resultset using an sql query and the result is already sorted by the first field, CompanyName:
    CompanyName, department, ManagerName
    A-company, Account, Steven
    A-company, HR, Mark
    A-company, IT, Susan
    B-Company,IT,Ross
    B-Company,Customer Service, Jack
    C-Company,Finance, Matthew
    C-Company,Finance, Helen
    C-Company,Finance, Ron
    I want to have a rew result with one additional column which is incremented automatically but the value must re-start from 1 once the Companyname changes.
    1, A-company, Account, Steven
    2, A-company, HR, Mark
    3, A-company, IT, Susan
    1, B-Company,IT,Ross
    2, B-Company,Customer Service, Jack
    1, C-Company,Finance, Matthew
    2, C-Company,Finance, Helen
    3, C-Company,Finance, Ron
    Please enlighten me.

    You have to program the numbering yourself. There is no built-in automatic numbering like identity or sequence but only OVER PARTITION BY.
    Reference:
    http://www.sqlusa.com/bestpractices2005/overpartitionby/
    Kalman Toth Database & OLAP Architect
    SQL Server 2014 Design & Programming
    New Book / Kindle: Exam 70-461 Bootcamp: Querying Microsoft SQL Server 2012

  • SQL query, generating several rows by a value in a field

    I am using Oracle database. Imaging I have a table with a row as following
         col1  |  col2  | col3
        'Str1' | 'Str2' | 4
    The value from col3=4 should generate 4 rows as following:
         col1  |  col2  | col3
        'Str1' | 'Str2' | 1
        'Str1' | 'Str2' | 2
        'Str1' | 'Str2' | 3
        'Str1' | 'Str2' | 4
    After several hours in front of the screen and stil no luck - how can I create such a select query???

    You're mixing up the 'standard' WITH clause and Recursive Subquery Factoring (which NEEDS a UNION ALL)  (see: http://docs.oracle.com/cd/E11882_01/server.112/e26088/statements_10002.htm#i2129904).
    This should work on 10.2:
    SQL> select * from v$version;
    BANNER
    Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bi
    PL/SQL Release 10.2.0.4.0 - Production
    CORE    10.2.0.4.0      Production
    TNS for Linux: Version 10.2.0.4.0 - Production
    NLSRTL Version 10.2.0.4.0 - Production
    SQL> with t as (select 'str1' col1, 'str2' col2, 4 col3 from dual
      2            )
      3  select col1
      4  ,      col2
      5  ,      level col3
      6  from   t
      7  connect by level <= col3;
    COL1 COL2       COL3
    str1 str2          1
    str1 str2          2
    str1 str2          3
    str1 str2          4
    See here as well: http://technology.amis.nl/2009/09/01/oracle-rdbms-11gr2-goodbye-connect-by-or-the-end-of-hierarchical-querying-as-we-know-it/

  • SQL query for repeating rows

    Hi I joined two table and created the new table called FMW_ANALY. Which has some repeating rows. I used the following Query to list the top 10 customers by value
    SELECT *
    FROM
    (SELECT CUSTOMER, OPP_VALUE, RANK() OVER (order by OPP_VALUE DESC) opp_val_rank
    FROM FMW_ANALY)
    WHERE opp_val_rank<=10;
    I'm getting the following result
    Customer Opp Value Opp Val Rank
    Department of Education and Training 8991776.99 1
    Department of Education and Training 8991776.99 1
    Department of Education and Training 8991776.99 1
    Department of Education and Training 8991776.99 1
    Department of Education and Training 8991776.99 1
    Department of Education and Training 8991776.99 1
    Department of Education and Training 8991776.99 1
    Department of Education and Training 8991776.99 1
    Department of Education and Training 8991776.99 1
    Department of Education and Training 8991776.99 1
    Department of Education and Training 8991776.99 1
    Department of Education and Training 8991776.99 1
    Department of Education and Training 8991776.99 1
    Department of Education and Training 8991776.99 1
    Department of Education and Training 8991776.99 1
    Investments Commission 7447419.84 22
    Bank Limited 4803000 23
    Bank Limited 4803000 23
    Bank Limited 4803000 23
    Bank Limited 4803000 23
    Bank Limited 4803000 23
    Bank Limited 4803000 23
    Bank Limited 4803000 23
    Bank Limited 4803000 23
    How can I Query without these repeating rows?
    Thank you
    Vick

    Example which will help you understand Rank, Dense Rank
    SQL> ed
    Wrote file afiedt.buf
      1  select deptno, ename,sal,
      2       rank()
      3         over (order by sal desc)rnk,
      4       dense_rank()
      5         over (order by sal desc)densernk
      6       from emp2
      7*     order by sal desc
    SQL> /
        DEPTNO ENAME             SAL        RNK   DENSERNK
            10 KING2            5000          1          1
            10 KING             5000          1          1
            20 SCOTT2           3630          3          2
            20 SCOTT            3630          3          2
            20 JONES2           3272          5          3
            20 JONES            3272          5          3
            20 FORD2            3000          7          4
            20 FORD             3000          7          4
            30 BLAKE2           2850          9          5
            30 BLAKE            2850          9          5
            10 CLARK2           2450         11          6
            10 CLARK            2450         11          6
            30 ALLEN2           1600         13          7
            30 ALLEN            1600         13          7
            30 TURNER2          1500         15          8
            30 TURNER           1500         15          8
            10 MILLER2          1300         17          9
            10 MILLER           1300         17          9
            30 WARD2            1250         19         10
            30 WARD             1250         19         10
            30 MARTIN2          1250         19         10
            30 MARTIN           1250         19         10
            20 ADAMS2           1100         23         11
            20 ADAMS            1100         23         11
            30 JAMES2            950         25         12
            30 JAMES             950         25         12
            20 SMITH2            800         27         13
            20 SMITH             800         27         13
    28 rows selected.
    SQL> ed
    Wrote file afiedt.buf
      1  SELECT * FROM
      2  (
      3  select deptno, ename,sal,
      4       rank()
      5         over (order by sal desc)rnk,
      6       dense_rank()
      7         over (order by sal desc)densernk
      8       from emp2
      9      order by sal desc
    10  )
    11* WHERE DENSERNK <=5
    SQL> /
        DEPTNO ENAME             SAL        RNK   DENSERNK
            10 KING2            5000          1          1
            10 KING             5000          1          1
            20 SCOTT2           3630          3          2
            20 SCOTT            3630          3          2
            20 JONES2           3272          5          3
            20 JONES            3272          5          3
            20 FORD2            3000          7          4
            20 FORD             3000          7          4
            30 BLAKE2           2850          9          5
            30 BLAKE            2850          9          5
    10 rows selected.

  • SQL query returns few rows, but holds on with several Gbs of RAM memory used

    If I perform the following query:
    SELECT d.DocumentGUID, d.DocumentID
    FROM ImportDataBase.dbo.Document d
    LEFT OUTER JOIN ContentDataBase.dbo.Document d2 ON (d.DocumentGUID = d2.DocumentGUID)
    WHERE ( d2.DocumentGUID IS NULL ) -- new document in the ImportDB
    OR ( d2.DocumentGUID IS NOT NULL AND d.QueryContent <> d2.QueryContent ) -- modified document in the ImportDB
    It returns around 1000 rows and takes about 3 minutes to complete
    It also raises up the RAM memory used from 2GB to 9GBs. 
    This memory used will remain used untill I restart the server. I have no need to make use of that memory, I already copied the returned rows of that query into a note pad for example. And any other reason of SQL has to  to keep that memory stored by
    that query, I  dont want it.
    Is there a way to release that memory I really dont need my SQL server to keep, without having to restart the SQL server?
    and without topping the max memory the sql server uses, since I need my SQL server to use as much as it needs in other tasks(I dont think it needs to hold on to the above query memory used)
    Thank You very much.

    Is there a way to release that memory I really dont need my SQL server to keep, without having to restart the SQL server?
    and without topping the max memory the sql server uses, since I need my SQL server to use as much
    A query can be resource intensive even if it returns a single row. Generally it is a good idea to set SQL Server MAX memory.
    BOL: "Optimizing Server Performance Using Memory Configuration Options
    The memory manager component of Microsoft SQL Server eliminates the need for manual management of the memory available to SQL Server. When SQL Server starts, it dynamically determines how much memory to allocate based on how much memory the operating system
    and other applications are currently using. As the load on the computer and SQL Server changes, so does the memory allocated. For more information, see Memory Architecture.
    The following server configuration options can be used to configure memory usage and affect server performance:
    •min server memory
    •max server memory
    •max worker threads
    •index create memory
    •min memory per query
    The min server memory server configuration option can be used to ensure that SQL Server does not release memory below the configured minimum server memory once that threshold is reached. This configuration option can be set to a specific value based on the
    size and activity of your SQL Server. If you choose to set this value, set it to some reasonable value to ensure that the operating system does not request too much memory from SQL Server, which can affect SQL Server performance.
    The max server memory server configuration option can be used to specify the maximum amount of memory SQL Server can allocate when it starts and while it runs. This configuration option can be set to a specific value if you know there are multiple applications
    running at the same time as SQL Server and you want to guarantee that these applications have sufficient memory to run. If these other applications, such as Web or e-mail servers, request memory only as needed, then do not set the max server memory server
    configuration option, because SQL Server releases memory to them as needed. However, applications often use whatever memory is available when they start and do not request more if needed. If an application that behaves in this manner runs on the same computer
    at the same time as SQL Server, set the max server memory server configuration option to a value that guarantees that the memory required by the application is not allocated by SQL Server."
    LINK: 
    http://technet.microsoft.com/en-us/library/ms177455(v=sql.105).aspx
    Memory configuration:
    http://www.sqlusa.com/bestpractices/memory-configuration/
    Kalman Toth Database & OLAP Architect
    SQL Server 2014 Database Design
    New Book / Kindle: Beginner Database Design & SQL Programming Using Microsoft SQL Server 2014

  • SQL Query returning no rows, please help!!

    I have a table that contains user audits for a particular procedures alongwith the date stamp. Now, I want to list all the procedures not accessed by the users within the last 6 months. Or, all the procedures that have not been used/accessed during the last 6 months.
    This is what I am trying but is not returning any rows:
    SELECT DISTINCT proc_name,
    TRUNC (entry_date)
    FROM log_web
    WHERE proc_name NOT IN (SELECT proc_name
    FROM log_web
    WHERE TRUNC (entry_date) > TRUNC (SYSDATE - 180))
    ORDER BY 2 DESC
    Please advise.
    Thank you in advance.

    Two possibilities leap to mind.
    First, are you sure that there are any prodcedures not accessed in the last six months?
    Second, is there a chance that there could be records with a null value in proc_name? If the sub-query used in a NOT IN predicate returns even one null value, the query will return no rows.
    Does this return rows?
    SELECT DISTINCT proc_name, TRUNC(entry_date)
    FROM log_web
    WHERE proc_name NOT IN (SELECT proc_name
                            FROM log_web
                            WHERE TRUNC (entry_date) > TRUNC (SYSDATE - 180) and
                                  proc_name IS NOT NULL)
    ORDER BY 2 DESCJohn

  • Whats wrong with the sql query-- giving no rows selected

    i have a table audit_data with 4 columns , eff_m & end_m are declared as timestamp and the time stamp data is different for all the records
    Please advice as soon as possible as this is a production problem
    ORG_GRP_I grn_n ED_EFF_m (timestamp) ED_END_M OFV
    1 999 01-Jan-06 1 03-Aug-06 0 0
    1 999 28-Jul-06 0 04-Aug-06 1 0
    1 999 04-Aug-06 1 04-Aug-06 1 0
    1 999 04-Aug-06 0 6.08
    This query is giving no rows returned,even though there is data in database
    SELECT NVL(OFV,0)
    FROM audit_data ag
    WHERE ag.ORG_GRP_I = 1
    AND ag.GRN_N = 999
    AND (ag.ED_EFF_M,NVL(ag.ED_END_M,SYSDATE)) IN (SELECT MAX(ED_EFF_M),MAX(NVL(ED_END_M,SYSDATE))
    FROM audit_data ag2
    WHERE ag2.ORG_GRP_I= ag.org_grp_i
    AND ag2.grn_n=ag.grn_n
    AND To_Date(To_Char(ag2.ED_EFF_M,'MM/DD/YYYY'),'MM/DD/YYYY') <= TO_DATE(TO_CHAR('08/04/06','MM/DD/YYYY'),'MM/DD/YYYY')
    AND To_Date(To_Char(NVL(ag2.ED_END_M,SYSDATE),'MM/DD/YYYY'),'MM/DD/YYYY') >= TO_DATE(TO_CHAR('08/04/06','MM/DD/YYYY'),'MM/DD/YYYY'));
    This query is giving value 6.08, when made type conversions.
    what wrong with the above query.please advice
    SELECT NVL(OFV,0)
    FROM audit_data ag
    WHERE ag.ORG_GRP_I = 1
    AND ag.GRN_n = 999
    AND (to_date(to_char(ag.ED_EFF_M,'mm/dd/yyyy'),'mm/dd/yyyy'),NVL(to_date(to_char(ag.ED_END_M,'mm/dd/yyyy'),'mm/dd/yyyy'),SYSDATE)) IN
    (SELECT MAX(to_date(to_char(ED_EFF_M,'mm/dd/yyyy'),'mm/dd/yyyy')),MAX(NVL(ED_END_M,SYSDATE))
    FROM audit_data ag2
    WHERE ag2.ORG_GRP_I= ag.org_grp_i
    AND ag2.grn_n=ag.grn_n
    AND To_Date(To_Char(ag2.ED_EFF_M,'MM/DD/YYYY'),'MM/DD/YYYY') <= TO_date('08/04/2006','MM/DD/YYYY')
    AND To_Date(To_Char(NVL(ag2.ED_END_M,SYSDATE),'MM/DD/YYYY'),'MM/DD/YYYY') >= TO_DATE('08/04/2006','MM/DD/YYYY'));

    In the second query you convert a string to a date correctly using TO_DATE(string,date format)
    In the first query you try to make a char of something that already is a char, e.g. TO_CHAR('08/04/06','MM/DD/YYYY').
    The TO_CHAR functions expects either a number or a date as its first parameter. That's why it's giving you a ORA-01722.

  • SQL query - select one row from each date group

    Hi,
    I have data as follows.
    Visit_Date Visit_type Consultant
    05/09/2009 G name1
    05/09/2009 G name2
    05/09/2009 G name3
    06/09/2009 I name4
    07/09/2009 G name5
    07/09/2009 G name6
    How to select data as follows
    05/09/2009 G name1
    06/09/2009 G name4
    07/09/2009 G name5
    i.e one row from every visit_date
    Thanks,
    MK Nathan
    Edited by: k_murali on Oct 7, 2009 10:44 PM

    Are you after this (one row per date per visit_type)
    with dd as (select to_date('05/09/2009','MM/DD/YYYY') Visit_Date, 'G' Visit_type, 'name1' Consultant from dual
                union all
                select to_date('05/09/2009','MM/DD/YYYY') Visit_Date, 'G' Visit_type, 'name2' Consultant from dual
                union all
                select to_date('05/09/2009','MM/DD/YYYY') Visit_Date, 'G' Visit_type, 'name3' Consultant from dual
                union all
                select to_date('06/09/2009','MM/DD/YYYY') Visit_Date, 'G' Visit_type, 'name4' Consultant from dual
                union all
                select to_date('07/09/2009','MM/DD/YYYY') Visit_Date, 'G' Visit_type, 'name5' Consultant from dual
                union all
                select to_date('07/09/2009','MM/DD/YYYY') Visit_Date, 'G' Visit_type, 'name6' Consultant from dual
                union all
                select to_date('07/09/2009','MM/DD/YYYY') Visit_Date, 'F' Visit_type, 'name7' Consultant from dual)
    select trunc(visit_date) visit_date, visit_type, min(consultant)
    from   dd
    group by trunc(visit_date), visit_type
    order by trunc(visit_date);
    VISIT_DAT V MIN(C
    09/MAY/09 G name1
    09/JUN/09 G name4
    09/JUL/09 G name5
    09/JUL/09 F name7or are you after only one row per date?:
    with dd as (select to_date('05/09/2009','MM/DD/YYYY') Visit_Date, 'G' Visit_type, 'name1' Consultant from dual
                union all
                select to_date('05/09/2009','MM/DD/YYYY') Visit_Date, 'G' Visit_type, 'name2' Consultant from dual
                union all
                select to_date('05/09/2009','MM/DD/YYYY') Visit_Date, 'G' Visit_type, 'name3' Consultant from dual
                union all
                select to_date('06/09/2009','MM/DD/YYYY') Visit_Date, 'G' Visit_type, 'name4' Consultant from dual
                union all
                select to_date('07/09/2009','MM/DD/YYYY') Visit_Date, 'G' Visit_type, 'name5' Consultant from dual
                union all
                select to_date('07/09/2009','MM/DD/YYYY') Visit_Date, 'G' Visit_type, 'name6' Consultant from dual
                union all
                select to_date('07/09/2009','MM/DD/YYYY') Visit_Date, 'F' Visit_type, 'name7' Consultant from dual)
    select trunc(visit_date) visit_date, min(visit_type) visit_type, min(consultant)
    from   dd
    group by trunc(visit_date)
    order by trunc(visit_date);
    VISIT_DAT V MIN(C
    09/MAY/09 G name1
    09/JUN/09 G name4
    09/JUL/09 F name5

  • Query issue / double rows

    Hi,
    I have the following query helping my to have Sales by item, invoice, BP and some UDF.
    My point is, when i execute it, some invoice rows appears twice while some other appears only once (as it should be).
    Can you help to identify what is causing this and how to remedy it?
    SELECT OINV.[CardCode], OINV.[CardName], CRD1.[Country], OINV.[DocNum], OINV.[DocDate],OITM.[U_ACW_FAMILYA], OITM.[U_ACW_FAMILYB], OITM.[U_ACW_FAMILYC],  INV1.[ItemCode], INV1.[Dscription], (INV1.[Quantity] * OITM.[SalFactor2]) AS PackQty, INV1.[unitMsr], INV1.[TotalSumSy] FROM OINV INNER JOIN INV1 ON OINV.DocEntry = INV1.DocEntry INNER JOIN OITM ON INV1.ItemCode = OITM.ItemCode INNER JOIN CRD1 ON OINV.CardCode = CRD1.CardCode
    On top of this, if some of you experts can help me to add the code so i can choose a period and a customer group only to filter this query i would really appreciate, i am new to this.

    Hi
    delete the las inner join.
    I think when the BP has two addresses the invoice will be appear twice. Even, if you have the same item twice you will have two results.
    To choose a period:
    where OINV.docdate >= [%0] and OINV.docdate <= [%1]
    To filter by customer group:
    1. you have to add the OCRG table
    2. you have to add the condition WHERE ocrg.groupname = [%2]
    3. you have to add a new inner join: inner join ocrg.groupcode = ocrd.groupcode
    Kind regards
    Agustín Marcos Cividanes

  • Sql Query Returns repeating rows

    Oracle Version : 10g R2.
    DDL & DML
    CREATE TABLE T1
       (     CCODE VARCHAR2(4 BYTE),
         PNUM VARCHAR2(16 BYTE),
         CTYPCDE VARCHAR2(4 BYTE)
    INSERT INTO T1 VALUES ('0001','003526892','0007');
    INSERT INTO T1 VALUES ('0001','06767019','0006');
    INSERT INTO T1 VALUES ('0001','14787-10-900','0007');
    INSERT INTO T1 VALUES ('0002','003445803','0009');
    INSERT INTO T1 VALUES ('0002','000000','0018');
    INSERT INTO T1 VALUES ('0002','001645156','0008');
    INSERT INTO T1 VALUES ('0002','001646283','0008');
    INSERT INTO T1 VALUES ('0025','002587509','0008');
    INSERT INTO T1 VALUES ('0025','02507462', '0008');
    INSERT INTO T1 VALUES ('0025','02565229', '0008');
    INSERT INTO T1 VALUES ('0025','000943965','0007');
    CREATE TABLE XWLK
       (     CTYPCDE VARCHAR2(4 BYTE),
         CODE VARCHAR2(85 BYTE)
    INSERT INTO XWLK  VALUES ( '0004','70');
    INSERT INTO XWLK  VALUES ( '0005','05');
    INSERT INTO XWLK  VALUES ( '0006','05');
    INSERT INTO XWLK  VALUES ( '0007','05');
    INSERT INTO XWLK  VALUES ( '0007','10');
    INSERT INTO XWLK  VALUES ( '0008','15');
    INSERT INTO XWLK  VALUES ( '0008','20');
    INSERT INTO XWLK  VALUES ( '0009','25');
    INSERT INTO XWLK  VALUES ( '0010','75');
    INSERT INTO XWLK  VALUES ( '0010','80');
    INSERT INTO XWLK  VALUES ( '0011','75');
    INSERT INTO XWLK  VALUES ( '0011','80');
    INSERT INTO XWLK  VALUES ( '0012','75');
    INSERT INTO XWLK  VALUES ( '0012','80');
    INSERT INTO XWLK  VALUES ( '0013','75');
    INSERT INTO XWLK  VALUES ( '0013','80');
    INSERT INTO XWLK  VALUES ( '0014','75');
    INSERT INTO XWLK  VALUES ( '0014','80');
    INSERT INTO XWLK  VALUES ( '0015','75');
    INSERT INTO XWLK  VALUES ( '0015','80');
    INSERT INTO XWLK  VALUES ( '0016','75');
    INSERT INTO XWLK  VALUES ( '0016','80');
    INSERT INTO XWLK  VALUES ( '0017','15');
    INSERT INTO XWLK  VALUES ( '0017','70');
    INSERT INTO XWLK  VALUES ( '0018','15');
    INSERT INTO XWLK  VALUES ( '0018','25');
    INSERT INTO XWLK  VALUES ( '0018','70');
    INSERT INTO XWLK  VALUES ( '0019','25');
    INSERT INTO XWLK  VALUES ( '0019','70');
    INSERT INTO XWLK  VALUES ( '0020','70');
    INSERT INTO XWLK  VALUES ( '0021','75');
    INSERT INTO XWLK  VALUES ( '0021','80');
    INSERT INTO XWLK  VALUES ( '0022','75');
    INSERT INTO XWLK  VALUES ( '0022','80');
    INSERT INTO XWLK  VALUES ( '0023','10');
    INSERT INTO XWLK  VALUES ( '0025','05');
    INSERT INTO XWLK  VALUES ( '0025','10');
    INSERT INTO XWLK  VALUES ( '0025','25');
    INSERT INTO XWLK  VALUES ( '0025','30');
    INSERT INTO XWLK  VALUES ( '0025','35');
    INSERT INTO XWLK  VALUES ( '0025','40');
    INSERT INTO XWLK  VALUES ( '0025','45');
    INSERT INTO XWLK  VALUES ( '0025','50');
    INSERT INTO XWLK  VALUES ( '0025','55');
    INSERT INTO XWLK  VALUES ( '0025','60');
    INSERT INTO XWLK  VALUES ( '0025','65');
    INSERT INTO XWLK  VALUES ( '0026','75');
    INSERT INTO XWLK  VALUES ( '0026','80');
    INSERT INTO XWLK  VALUES ( '0027','05');
    INSERT INTO XWLK  VALUES ( '0027','10');
    INSERT INTO XWLK  VALUES ( '0027','25');
    INSERT INTO XWLK  VALUES ( '0027','30');
    INSERT INTO XWLK  VALUES ( '0027','35');
    INSERT INTO XWLK  VALUES ( '0027','40');
    INSERT INTO XWLK  VALUES ( '0027','45');
    INSERT INTO XWLK  VALUES ( '0027','50');
    INSERT INTO XWLK  VALUES ( '0027','55');
    INSERT INTO XWLK  VALUES ( '0027','60');
    INSERT INTO XWLK  VALUES ( '0027','65');
    INSERT INTO XWLK  VALUES ( '0028','15');
    INSERT INTO XWLK  VALUES ( '0069','75');
    CREATE TABLE PTB
       ( PNUM VARCHAR2(20 BYTE));
    INSERT INTO PTB VALUES('003526892');
    INSERT INTO PTB VALUES('06767019');
    INSERT INTO PTB VALUES('14787-10-900');
    INSERT INTO PTB VALUES('003445803');
    INSERT INTO PTB VALUES('000000');
    INSERT INTO PTB VALUES('001645156');
    INSERT INTO PTB VALUES('001646283');
    INSERT INTO PTB VALUES('002587509');
    INSERT INTO PTB VALUES('02507462');
    INSERT INTO PTB VALUES('02565229');
    INSERT INTO PTB VALUES('000943965');
    Query
    SELECT
    SAK.CCODE,
    SAK.CTYPCDE,
    CASE
         WHEN SAK.CCODE = '0001' and SAK.CTYPCDE != '0088' THEN '75'
         WHEN SAK.CCODE = '0002' and SAK.CTYPCDE != '0088' THEN '75'
         WHEN SAK.CCODE = '0002' and SAK.CTYPCDE != '0088' THEN '80'
         WHEN SAK.CCODE = '0003' and SAK.CTYPCDE != '0088' THEN '80'
         ELSE XWLK.CODE
    END AS PCD,
    PTB.PNUM
    FROM T1 SAK  INNER JOIN XWLK ON SAK.CTYPCDE = XWLK.CTYPCDE
    INNER JOIN PTB  ON SAK.PNUM = PTB.PNUM
    Order by 4
    Output
    CCODE     CTYPCODE PCD     PNUM
    0002     0018     75     000000
    0002     0018     75     000000
    0002     0018     75     000000
    0025     0007     05     000943965
    0025     0007     10     000943965
    0002     0008     75     001645156
    0002     0008     75     001645156
    0002     0008     75     001646283
    0002     0008     75     001646283
    0025     0008     15     002587509
    0025     0008     20     002587509
    0002     0009     75     003445803
    0001     0007     75     003526892
    0001     0007     75     003526892
    0025     0008     20     02507462
    0025     0008     15     02507462
    0025     0008     15     02565229
    0025     0008     20     02565229
    0001     0006     75     06767019
    0001     0007     75     14787-10-900
    0001     0007     75     14787-10-900
    But the Output is not the expected one if we see for the PNUM = 000000, the records are displayed
    3 times it should be only ones displayed and also for PNUM = 001645156.  My Query is why is it displaying
    more than once, is there anyway that we can display it only once. Can some please advice me where am making
    wrong. Thanks in advance.

    Hi Frank,
    Thanks for your responce. My earlier Insert scripts the sample data had few issues, Please find below the updated one....
    Oracle Version : 10g R2.
    DDL & DML
    CREATE TABLE T1
       (     CCODE VARCHAR2(4 BYTE),
         PNUM VARCHAR2(16 BYTE),
         CTYPCDE VARCHAR2(4 BYTE)
    INSERT INTO T1 VALUES ('0512',     '000-06-5393-05',  '0023');
    INSERT INTO T1 VALUES ('0305',     '000-06-5393-05',  '0006');
    INSERT INTO T1 VALUES ('0611',     '0000',            '0006');
    INSERT INTO T1 VALUES ('0806',     '0000',            '0008');
    INSERT INTO T1 VALUES ('0606',     '0000',            '0006');
    INSERT INTO T1 VALUES ('0099',     '0000',            '0007');
    INSERT INTO T1 VALUES ('0025',     '000000324',       '0008');
    CREATE TABLE XWLK
       (     CTYPCDE VARCHAR2(4 BYTE),
         CODE VARCHAR2(85 BYTE)
    INSERT INTO XWLK  VALUES ( '0004','70');
    INSERT INTO XWLK  VALUES ( '0005','05');
    INSERT INTO XWLK  VALUES ( '0006','05');
    INSERT INTO XWLK  VALUES ( '0007','05');
    INSERT INTO XWLK  VALUES ( '0007','10');
    INSERT INTO XWLK  VALUES ( '0008','15');
    INSERT INTO XWLK  VALUES ( '0008','20');
    INSERT INTO XWLK  VALUES ( '0009','25');
    INSERT INTO XWLK  VALUES ( '0010','75');
    INSERT INTO XWLK  VALUES ( '0010','80');
    INSERT INTO XWLK  VALUES ( '0011','75');
    INSERT INTO XWLK  VALUES ( '0011','80');
    INSERT INTO XWLK  VALUES ( '0012','75');
    INSERT INTO XWLK  VALUES ( '0012','80');
    INSERT INTO XWLK  VALUES ( '0013','75');
    INSERT INTO XWLK  VALUES ( '0013','80');
    INSERT INTO XWLK  VALUES ( '0014','75');
    INSERT INTO XWLK  VALUES ( '0014','80');
    INSERT INTO XWLK  VALUES ( '0015','75');
    INSERT INTO XWLK  VALUES ( '0015','80');
    INSERT INTO XWLK  VALUES ( '0016','75');
    INSERT INTO XWLK  VALUES ( '0016','80');
    INSERT INTO XWLK  VALUES ( '0017','15');
    INSERT INTO XWLK  VALUES ( '0017','70');
    INSERT INTO XWLK  VALUES ( '0018','15');
    INSERT INTO XWLK  VALUES ( '0018','25');
    INSERT INTO XWLK  VALUES ( '0018','70');
    INSERT INTO XWLK  VALUES ( '0019','25');
    INSERT INTO XWLK  VALUES ( '0019','70');
    INSERT INTO XWLK  VALUES ( '0020','70');
    INSERT INTO XWLK  VALUES ( '0021','75');
    INSERT INTO XWLK  VALUES ( '0021','80');
    INSERT INTO XWLK  VALUES ( '0022','75');
    INSERT INTO XWLK  VALUES ( '0022','80');
    INSERT INTO XWLK  VALUES ( '0023','10');
    INSERT INTO XWLK  VALUES ( '0025','05');
    INSERT INTO XWLK  VALUES ( '0025','10');
    INSERT INTO XWLK  VALUES ( '0025','25');
    INSERT INTO XWLK  VALUES ( '0025','30');
    INSERT INTO XWLK  VALUES ( '0025','35');
    INSERT INTO XWLK  VALUES ( '0025','40');
    INSERT INTO XWLK  VALUES ( '0025','45');
    INSERT INTO XWLK  VALUES ( '0025','50');
    INSERT INTO XWLK  VALUES ( '0025','55');
    INSERT INTO XWLK  VALUES ( '0025','60');
    INSERT INTO XWLK  VALUES ( '0025','65');
    INSERT INTO XWLK  VALUES ( '0026','75');
    INSERT INTO XWLK  VALUES ( '0026','80');
    INSERT INTO XWLK  VALUES ( '0027','05');
    INSERT INTO XWLK  VALUES ( '0027','10');
    INSERT INTO XWLK  VALUES ( '0027','25');
    INSERT INTO XWLK  VALUES ( '0027','30');
    INSERT INTO XWLK  VALUES ( '0027','35');
    INSERT INTO XWLK  VALUES ( '0027','40');
    INSERT INTO XWLK  VALUES ( '0027','45');
    INSERT INTO XWLK  VALUES ( '0027','50');
    INSERT INTO XWLK  VALUES ( '0027','55');
    INSERT INTO XWLK  VALUES ( '0027','60');
    INSERT INTO XWLK  VALUES ( '0027','65');
    INSERT INTO XWLK  VALUES ( '0028','15');
    INSERT INTO XWLK  VALUES ( '0069','75');
    CREATE TABLE PTB
       (  PSAK Number(10,0),  
          PNUM VARCHAR2(20 BYTE),
          Seq_num NUMBER(5,0));
    INSERT INTO PTB VALUES('9462003',     '000-06-5393-05', '1');
    INSERT INTO PTB VALUES('9462004',     '000-06-5393-05', '2');
    INSERT INTO PTB VALUES('9462008',     '0000',            '1');
    INSERT INTO PTB VALUES('9462009',     '0000',            '2');
    INSERT INTO PTB VALUES('9462010',     '0000',            '3');
    INSERT INTO PTB VALUES('9462017',     '000000324',       '1');
    Query
    SELECT
    PTB.PSAK,
    CASE
         WHEN SAK.CCODE = '0001' and SAK.CTYPCDE != '0088' THEN '75'
         WHEN SAK.CCODE = '0002' and SAK.CTYPCDE != '0088' THEN '75'
         WHEN SAK.CCODE = '0002' and SAK.CTYPCDE != '0088' THEN '80'
         WHEN SAK.CCODE = '0003' and SAK.CTYPCDE != '0088' THEN '80'
         ELSE XWLK.CODE
    END AS PCD,
    PTB.PNUM,
    PTB.Seq_num
    FROM T1 SAK  INNER JOIN XWLK ON SAK.CTYPCDE = XWLK.CTYPCDE
    INNER JOIN PTB  ON SAK.PNUM = PTB.PNUM
    Order by 4,3
    Query output without distinct
    PSAK     PCD     PNUM          SEQ_NUM
    9462003     05     000-06-5393-05     1
    9462003     10     000-06-5393-05     1
    9462008     05     0000          1
    9462008     10     0000          1
    9462008     20     0000          1
    9462008     15     0000          1
    9462008     05     0000          1
    9462008     05     0000          1
    9462017     20     000000324     1
    9462017     15     000000324     1
    9462004     10     000-06-5393-05     2
    9462004     05     000-06-5393-05     2
    9462009     05     0000          2
    9462009     20     0000          2
    9462009     05     0000          2
    9462009     15     0000          2
    9462009     05     0000          2
    9462009     10     0000          2
    9462010     05     0000          3
    9462010     05     0000          3
    9462010     15     0000          3
    9462010     10     0000          3
    9462010     05     0000          3
    9462010     20     0000          3
    Query output with distinct
    PSAK     PCD     PNUM          SEQ_NUM
    9462003     05     000-06-5393-05     1
    9462003     10     000-06-5393-05     1
    9462008     05     0000          1
    9462008     10     0000          1
    9462008     15     0000          1
    9462008     20     0000          1
    9462017     15     000000324     1
    9462017     20     000000324     1
    9462004     05     000-06-5393-05     2
    9462004     10     000-06-5393-05     2
    9462009     05     0000          2
    9462009     10     0000          2
    9462009     15     0000          2
    9462009     20     0000          2
    9462010     05     0000          3
    9462010     10     0000          3
    9462010     15     0000          3
    9462010     20     0000          3
    But both the output is wrong is there any way that i can get the result set as below, from the above query i can see duplicates
    if the PNUM has more SEQ_NUM then it is again doing a cross join i think and hence the result set is coming wrong. The Below
    is the expected output. Can some one help me out in getting this. Thanks in advance.
    PSAK     PCD     PNUM          SEQ_NUM
    9462003     05     000-06-5393-05     1
    9462004     10     000-06-5393-05     2
    9462008     05     0000          1
    9462009     05     0000          2
    9462010     15     0000          3
    9462010     20     0000          3
    9462017     15     000000324                     1
    9462017     20     000000324                     1

  • SQL Query for getting Row No for each group

    Dear Oracle Gurus,
    I want to allocate Rownum to each group. How we can do this with single SQL. Help me on this.
    Thanks

    select deptno, ename, row_number() over (partition by deptno order by ename) from emp order by 1,3;

Maybe you are looking for