Write a Query For this

Hi friend,
I have a requirement...for complete this i want to your help... I create sample table and sample data here.
My table is like this :
create table test1
( docid number(10),
doctyp varchar2(10),
billno varchar2(10),
prnno varchar2(10),
amount number(10)
My data is :
insert into test1 values ( D0001, 'BNP05', '101', 'PRN01', 1000);
insert into test1 values ( D0001, 'BNP05', '101', 'PRN02', 1000);
insert into test1 values ( D0001, 'BNP05', '101', 'PRN03', 1000);
insert into test1 values ( D0002, 'BNP05', '102', 'PR01', 100);
My expected o/p :
DOCID DOCTYP BILLNO PRNNO AMOUNT
================================================
D0001 BNP05 101 PRN01 1000
D0001 BNP05 101 PRN02 0
D0001 BNP05 101 PRN03 0
D0002 BNP05 101 PR01 100
If u see it, i dont want to show amount repeatly.... for DOCID, DOCTYP, BILLNO Combination..
How to achieve this ... ??
Thanks in advance

Hi,
Do you want something like this:
SQL> select  DOCID, DOCTYP, BILLNO, prnno,
  2          case
  3            when row_number() over(partition
  4                  by
  5                  docid,doctyp,billno
  6                 order by prnno) = 1
  7                 then amount
  8            else null
  9           end   as amount
10  from test1
11  order by docid,doctyp,billno;
     DOCID DOCTYP     BILLNO     PRNNO          AMOUNT
         1 BNP05      101        PRN01            1000
         1 BNP05      101        PRN02
         1 BNP05      101        PRN03
         2 BNP05      102        PR01              100
SQL>

Similar Messages

  • How to write a query for this data??

    In this table i have column dob which is DATE type,for example 12-jan-89 I want output like this
       Year              Day
    12-jan-89         Thusrs_day
    12-jan-90          Friday
    upto
    12-jan-14        sunday

    krishnagopi wrote:
    No not like that ,I want up to 2014 year
    Ok, so lets say you have table like this
    SQL> create table my_table(dob date);
    Table created.
    SQL> insert into my_table (dob) values( to_date('12-jan-1989', 'dd-mon-yyyy'));
    1 row created.
    SQL> commit;
    Commit complete.
    SQL> select * from my_table;
    DOB
    12-JAN-89
    You can write a query like this
    select dob + (level -1) dob
          , to_char(dob + (level -1), 'fmDay') day_
       from my_table
    connect
         by level <= (dob + interval '25' year) - dob + 1;

  • How should i write a query for this?

    Hi i give the follwing query to implement the result,
    IT will gives you the result of one country machines information like Total machines failed machines and success machines of their count
    Countyr Name  CountryCode  Total Machines Failed Machines and Succes machines and their perentage
      IT  ITALY
    1000 20
    980    98% 
    like these i want to display the 16 countyr machines information all country information in one palce  like table format
    i will give you the query to the below here it is the combination of powershell code also used here with sql code.
    Code as Country 
    Name as Country
    And below i mentioned OU in bold every where i will put the country code according to the country
    like OU=FR
    Select
            'FR' as 'Code',
            'FRANCE' as 'Name',
            (select COUNT(*) from ADComputersInfo where
        ObjectPath like 'LDAP://eur.gad.schneider-electric.com/CN=%,OU=Workstations,OU=FR,OU=Countries,DC=eur,DC=gad,DC=schneider-electric,DC=com'
        (select COUNT(*) from ADComputersInfo where
        ObjectPath like 'LDAP://eur.gad.schneider-electric.com/CN=%,OU=Workstations_Indus,OU=FR,OU=Countries,DC=eur,DC=gad,DC=schneider-electric,DC=com'
        (select COUNT(*) from ADComputersInfo where
        ObjectPath like 'LDAP://eur.gad.schneider-electric.com/CN=%,OU=Servers,OU=FR,OU=Countries,DC=eur,DC=gad,DC=schneider-electric,DC=com'
        (select COUNT(*) from ADComputersInfo where
        ObjectPath like 'LDAP://eur.gad.schneider-electric.com/CN=%,OU=Workstations,OU=FR,OU=Countries,DC=eur,DC=gad,DC=schneider-electric,DC=com'
        or
        ObjectPath like 'LDAP://eur.gad.schneider-electric.com/CN=%,OU=Workstations_Indus,OU=FR,OU=Countries,DC=eur,DC=gad,DC=schneider-electric,DC=com'
        or
        ObjectPath like 'LDAP://eur.gad.schneider-electric.com/CN=%,OU=Servers,OU=FR,OU=Countries,DC=eur,DC=gad,DC=schneider-electric,DC=com'
        (select count(distinct(cn)) from ADComputersInfo where
        (ObjectPath like 'LDAP://eur.gad.schneider-electric.com/CN=%,OU=Workstations,OU=FR,OU=Countries,DC=eur,DC=gad,DC=schneider-electric,DC=com'
        or
        ObjectPath like 'LDAP://eur.gad.schneider-electric.com/CN=%,OU=Workstations_Indus,OU=FR,OU=Countries,DC=eur,DC=gad,DC=schneider-electric,DC=com'
        or
        ObjectPath like 'LDAP://eur.gad.schneider-electric.com/CN=%,OU=Servers,OU=FR,OU=Countries,DC=eur,DC=gad,DC=schneider-electric,DC=com')
        and cn in (select name0 from v_R_System)
        (select count(distinct(cn)) from ADComputersInfo where
        (ObjectPath like 'LDAP://eur.gad.schneider-electric.com/CN=%,OU=Workstations,OU=FR,OU=Countries,DC=eur,DC=gad,DC=schneider-electric,DC=com'
        or
        ObjectPath like 'LDAP://eur.gad.schneider-electric.com/CN=%,OU=Workstations_Indus,OU=FR,OU=Countries,DC=eur,DC=gad,DC=schneider-electric,DC=com'
        or
        ObjectPath like 'LDAP://eur.gad.schneider-electric.com/CN=%,OU=Servers,OU=FR,OU=Countries,DC=eur,DC=gad,DC=schneider-electric,DC=com')
        and cn not in (select name0 from v_R_System)
        select count(distinct(cn)) from ADComputersInfo where
        (ObjectPath like 'LDAP://eur.gad.schneider-electric.com/CN=%,OU=Workstations,OU=FR,OU=Countries,DC=eur,DC=gad,DC=schneider-electric,DC=com'
        or
        ObjectPath like 'LDAP://eur.gad.schneider-electric.com/CN=%,OU=Workstations_Indus,OU=FR,OU=Countries,DC=eur,DC=gad,DC=schneider-electric,DC=com'
        or
        ObjectPath like 'LDAP://eur.gad.schneider-electric.com/CN=%,OU=Servers,OU=FR,OU=Countries,DC=eur,DC=gad,DC=schneider-electric,DC=com')
        and cn in (select name0 from v_R_System where Client0 =1 and Obsolete0 =0)
        select count(distinct(cn)) from ADComputersInfo where
        (ObjectPath like 'LDAP://eur.gad.schneider-electric.com/CN=%,OU=Workstations,OU=FR,OU=Countries,DC=eur,DC=gad,DC=schneider-electric,DC=com'
        or
        ObjectPath like 'LDAP://eur.gad.schneider-electric.com/CN=%,OU=Workstations_Indus,OU=FR,OU=Countries,DC=eur,DC=gad,DC=schneider-electric,DC=com'
        or
        ObjectPath like 'LDAP://eur.gad.schneider-electric.com/CN=%,OU=Servers,OU=FR,OU=Countries,DC=eur,DC=gad,DC=schneider-electric,DC=com')
        and cn not in (select name0 from v_R_System where Client0 =1 and Obsolete0 =0)
    So here i pass the County code and country name and OU=FR(assigned country code ) 
    so hw we can all these 3 values in all the query can give me the r8 query for this . And i will get the all 16 country information 
    Ineed exact query hw we can implement..........................................
    I might be excepting lot from our side guys...
    Thanks ! 
    Advanced

    Hi 
    Can you simple post sample data + desired result? Always state what version you are using..
    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

  • How  to write select query for this

    Hi,
    I had a html form and in the for i had drop down box and it needs to select multiple values from the drop down box. When i select multiple values then i have to write the SQL select statement  query .
    When i try to write the select statement and trying to run i am getting error.
    select * from Table
    where emo_no = '1,2,3'
    this is how i write query please suggest me how  to write query for selecting multiple values from the drop down box.
    Thanks

    select * from Table
    where emo_no in ( 1,2,3)
    for integer values
    select * from Table
    where emo_no in ('1','2','3')
    for characters
    If we talk about large scale applications that may have millions of records, I would suggest this.
    declare @t table (v int)
    insert into t (v) values (1)
    insert into t (v) valves (2)
    insert into t (v) values (3)
    select *
    from table
         inner join @t t on table.emo_no = t.v
    Using "in" for a where clause is not so bad for filtering on a few values, but if you are filtering a lot of rows and a lot of values (emo_no) the performance degrades quickly for some reasons beyond the scope of this.
    This is just one solution, I'll through this out as well, instead of an in memory (@t) table, doing a disk based temp table (#t) and creating an index on the column "v".
    create table #t (v int)
    insert into #t (v) values (1)
    insert into #t (v) valves (2)
    insert into #t (v) values (3)
    create index ix_t on #t (v)
    select *
    from table
         inner join #t t on table.emo_no = t.v
    drop table #t
    Pardon any syntax errors and careful using a drop statement.
    Sometimes in memory tables work better than disk temp tables, it takes some testing and trial and error depending on your datasets to determine the best solution.
    Probably too much info  ;-)
    Byron Mann
    [email protected]
    [email protected]
    Software Architect
    hosting.com | hostmysite.com
    http://www.hostmysite.com/?utm_source=bb

  • How can I write a query for this table?

    Dear friends
    I have a table with the following structur and data
    ID Name Value
    1 book 100
    2 book 200
    3 car 25
    4 car 35
    5 book 300
    now I would like write a query with following out put
    Name value
    book 100 , 200 , 300
    car 25 , 35
    (with , Separator) (my oracle version is 10g)
    thanks

    Try this,
    SQL> WITH t AS(SELECT 'book' NAME, 100 VALUE FROM dual UNION ALL
      2  SELECT 'book', 200 FROM dual UNION ALL
      3  SELECT 'car', 25 FROM dual UNION ALL
      4  SELECT 'car', 35 FROM dual UNION ALL
      5  SELECT 'book', 300 FROM dual)
      6      SELECT NAME, LTRIM (MAX (SYS_CONNECT_BY_PATH (VALUE, ',')), ',') VALUE
      7        FROM (SELECT NAME, VALUE, ROW_NUMBER () OVER (PARTITION BY NAME ORDER BY VALUE) rn FROM t)
      8  START WITH rn = 1
      9  CONNECT BY PRIOR rn + 1 = rn
    10             AND PRIOR NAME = NAME
    11    GROUP BY NAME;
    NAME VALUE
    book 100,200,300
    car  25,35
    2 rows selected.
    SQL> G.

  • How to write a query for this situation

    Hi ,
    Can any one help me to write a query.I have a table with three columns like char_id_1,char_id_2,char_id_3,these data have to insert into some other table but in the target table the data should be comma seperated like (10,8,2),here blank space indicates null.In source table the columns are numbers,but in target table it is varchar2.
    source table:
    CHAR_ID_1 CHAR_ID_2 CHAR_ID_3
    10 8 2
    7 1 5
    4 11
    6 1
    2 8
    6 12 7
    Target table:
    CHAR_IDS
    10,8,2
    7,1,5
    4,11
    6,1
    2,8
    6,12,7

    I started doing it to_char way, and thought of some different version, and ended up with an ugly solution which works only if you have three columns. Thought of not posting, but posting it anyway:
    Connected to:
    Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - 64bit Production
    With the Partitioning, OLAP and Data Mining options
    SQL> with t as(
      2      select 10 c1,8 c2,2 c3 from dual union all
      3      select 7,1,5 from dual union all
      4      select 4,null,11 from dual union all
      5      select null,6,1 from dual union all
      6      select 2,8,null from dual union all
      7      select null,8,null from dual union all
      8      select null,null,10 from dual union all
      9      select 2,null,null from dual union all
    10      select null,null,null from dual union all
    11      select 6,12,7 from dual )
    12  --
    13  select nvl2(c1,
    14              c1 || nvl2(c2,',' || c2 || nvl2(c3,',' || c3,null),nvl2(c3,',' || c3,null)),
    15              nvl2(c2, c2 || nvl2(c3,','|| c3,null),nvl2(c3,c3,null))) col_concat
    16  from t;
    COL_CONCAT
    10,8,2
    7,1,5
    4,11
    6,1
    2,8
    8
    10
    2
    6,12,7
    10 rows selected.
    SQL>

  • How  to write  a  query  for this output

    I have a string ' if i know good acting , I am a hero '
    now if 'hero' is present in the string it will return ' i am hero' else
    'you are hero'
    How to write a sql query to return the same .

    SQL> select (case
    2 when '&a' like '%hero%' then 'I am hero'
    3 ELSE 'u r hero'
    4 end) output from dual;
    Enter value for a: if i know good acting , I am a hero
    old 2: when '&a' like '%hero%' then 'I am hero'
    new 2: when 'if i know good acting , I am a hero ' like '%hero%' then 'I am hero'
    OUTPUT
    I am hero
    SQL> /
    Enter value for a: jkhkh
    old 2: when '&a' like '%hero%' then 'I am hero'
    new 2: when 'jkhkh' like '%hero%' then 'I am hero'
    OUTPUT
    u r hero
    Hope this helps.

  • How to write a query for this

    I have 2 tables
    emp_master (emp_id, emp_name)
    dept_master (dept_id, dept_name, emp_id (references emp_id in emp_master)Now there are 30 departments and each department can have maximum 5 employees.
    Now I want a query that would list a department and all its employees in one line and if there are less than 5 employees it would show null for the rest of columns.
    Dept_id emp_1 emp_2 emp_3 emp_4 emp_5
    1 1 2 -- -- --
    2 3 4 5 6 7
    3 8 -- -- -- --
    Please help me out
    Thanks...

    One solution may be
    Connected to Oracle Database 10g Express Edition Release 10.2.0.1.0
    Connected as hr
    SQL>
    SQL> SELECT e.department_id, e.employee_id
      2    FROM employees e
      3   WHERE e.department_id IN (10, 20, 40, 60, 90)
      4  /
    DEPARTMENT_ID EMPLOYEE_ID
               90         100
               90         101
               90         102
               60         103
               60         104
               60         105
               60         106
               60         107
               10         200
               20         201
               20         202
               40         203
    12 rows selected
    SQL>
    SQL> SELECT ee.department_id, ee.emp_1, ee.emp_2, ee.emp_3, ee.emp_4, ee.emp_5
      2    FROM (SELECT MAX(CASE
      3                       WHEN MOD(r, 5) = 1 THEN
      4                        e.employee_id
      5                       ELSE
      6                        NULL
      7                     END) emp_1,
      8                 MAX(CASE
      9                       WHEN MOD(r, 5) = 2 THEN
    10                        e.employee_id
    11                       ELSE
    12                        NULL
    13                     END) emp_2,
    14                 MAX(CASE
    15                       WHEN MOD(r, 5) = 3 THEN
    16                        e.employee_id
    17                       ELSE
    18                        NULL
    19                     END) emp_3,
    20                 MAX(CASE
    21                       WHEN MOD(r, 5) = 4 THEN
    22                        e.employee_id
    23                       ELSE
    24                        NULL
    25                     END) emp_4,
    26                 MAX(CASE
    27                       WHEN MOD(r, 5) = 0 THEN
    28                        e.employee_id
    29                       ELSE
    30                        NULL
    31                     END) emp_5,
    32                 e.department_id
    33            FROM (SELECT row_number() over(PARTITION BY department_id ORDER BY employee_id) r,
    34                         department_id,
    35                         employee_id
    36                    FROM employees
    37                   WHERE department_id IN (10, 20, 40, 60, 90)) e
    38           GROUP BY e.department_id) ee
    39  /
    DEPARTMENT_ID      EMP_1      EMP_2      EMP_3      EMP_4      EMP_5
               10        200                                 
               20        201        202                      
               40        203                                 
               60        103        104        105        106        107
               90        100        101        102           
    SQL> Regards...

  • How can i write sql query for this result ?

    Hello Dear,
    Here is the script first.
    CREATE TABLE ACC_TEST(
    AD_ID NUMBER,
    AD_NAME VARCHAR2(50),
    AD_SPM_ID NUMBER);
    /data are
    Insert into ACC_TEST (AD_ID,AD_NAME,AD_SPM_ID) values (136,'Saleh Ahmed',129);
    Insert into ACC_TEST (AD_ID,AD_NAME,AD_SPM_ID) values (142,'Hamidur Rahman',136);
    Insert into ACC_TEST (AD_ID,AD_NAME,AD_SPM_ID) values (124,'Jasim Uddin',null);
    INSERT INTO ACC_TEST (AD_ID,AD_NAME,AD_SPM_ID) VALUES (129,'Sazib',124);I Need The Following Result When I Pass A Value Of Ad_Id. For Example I Pass 142 Then Result Should Be
    Select Ad_Id,Ad_Name
    From..
    where ad_id=142
    Ad_Id   Ad_Name
    136     Saleh Ahmed
    129     Sazib
    124     Jasim Uddin
    If I Pass Ad_Id=136 Then Result Should Be
    Ad_Id   Ad_Name
    129     Sazib
    124     Jasim Uddin
    If I Pass Ad_Id=129 Then Result Should Be
    Ad_Id   Ad_Name
    124     Jasim Uddin Database 10G XE
    Any help will be helpful

    Hi,
    HamidHelal wrote:
    WoW ! you acutely got my point. How did you understand that ? lLuck guess. Guessing is usually not the best way to solve problems. It's usually faster and more reliable to say exactly what you want, as well as give an example.
    ittle bit more i want to know, if i want to restrict the output not more then 2, what would be sql ?Now you're not even giviing an example!
    Maybe you want something like this:
    SELECT     ad_id
    ,     ad_name
    FROM     acc_test
    WHERE     LEVEL     BETWEEN 2 AND 3          -- Changed
    START WITH     ad_id     = :target_ad_id
    CONNECT BY     ad_id     = PRIOR ad_spm_id
    ;which will show jsut the parent and the grandparent of the given row.
    I do work with forms developer very much. Sql knowledge is as oracle sql book(cerfitication 9i). But this type of sql isn't available there.
    where can i learn this type of sql ? Different sql then ordinary ?Certification is a different topic entirely.
    There are books and web sites dealing with more advanced techniques. Sorry, I don't know any well enough to recommend them. Some authors (such as Tom Kyte) are consistently good.
    Here are a couple of sites that explain CONNECT BY queries:
    http://www.adp-gmbh.ch/ora/sql/connect_by.html
    http://www.oradev.com/connect_by.jsp

  • How can I write a query for this?

    I have a table as below ,
    room.........date.............inventory
    A..............01/01.................5
    A..............01/02.................5
    A..............01/03.................5
    A..............01/05.................5
    A..............01/06.................5
    A..............01/07.................8
    I want the output as below
    room.....begin_date........end_date......inventory
    A............01/01.................01/03..........5
    A............01/05.................01/06..........5
    A............01/07.................01/07..........8
    Is there any suggestion?
    Thanks
    null

    with x as
    (select 'A' room, to_date ('01/01/2007', 'mm/dd/yyyy') d, 5 i from dual union all
    select 'A' room, to_date ('01/02/2007', 'mm/dd/yyyy') d, 5 i from dual union all
    select 'A' room, to_date ('01/03/2007', 'mm/dd/yyyy') d, 7 i from dual union all
    select 'A' room, to_date ('01/04/2007', 'mm/dd/yyyy') d, 7 i from dual union all
    select 'A' room, to_date ('01/05/2007', 'mm/dd/yyyy') d, 5 i from dual union all
    select 'A' room, to_date ('01/06/2007', 'mm/dd/yyyy') d, 5 i from dual
    select min (d)
         , max (d)
         , min (i)
      from (
    select room
         , d
         , i
         , max (g) over (order by d) mg
      from
    (select room
         , d
         , i
         , case when lead (i) over (partition by grp order by d) = i
           then rownum
           end  g
    from (
    select room
    , d
    , i
    , d - rownum grp
    from x
    order by d
    group by mg

  • How to write  complex sql for this

    Hi ALL,
    I have a requirement like this
    I have 5 tables which i have to join to get the result
    but there no join column to 2 other table.
    I want to get all the applications using cobal,running on UNIX.
    How to write the query for this
    1.APP
    APP_i DESC
    1 Accounts
    2 Payments
    3 order transfer
    4 Order processing
    2.Techgy
    techid techdesc
    1 cobal
    2 Java
    3.APP_Techgy
    APP_I Techid
    1 1
    2 1
    3 1
    4 2
    4.Pltfrm
    pltfmid pltfrmdesc
    1 Windows NT
    2 UNIX
    5.APP_Pltfrm
    APP_I pltfrmid
    1 1
    2 1
    3 2
    4 2
    ouput must be
    APP_i Desc techDESC pltfrmdesc
    3 ordertranfer Cobal UNIX
    Thanks in advance

    This ('descr' in place of 'desc')?
    SQL> select a.app_i, a.descr, t.techdesc, p.pltfrmdesc
    from app_techgy atc,
       app a,
       techgy t,
       app_pltfrm ap,
       pltfrm p
    where atc.techid = t.techid
    and atc.app_i = a.app_i
    and atc.app_i = ap.app_i
    and ap.pltfrmid = p.pltfmid
    order by a.app_i
         APP_I DESCR                TECHDESC             PLTFRMDESC         
             1 accounts             cobal                windows nt         
             2 payments             cobal                windows nt         
             3 order transfer       cobal                unix               
             4 order processing     java                 unix               
    4 rows selected.

  • How to write sql query for counting pairs from below table??

    Below is my SQL table structure.
    user_id | Name | join_side | left_leg | right_leg | Parent_id
    100001 Tinku Left 100002 100003 0
    100002 Harish Left 100004 100005 100001
    100003 Gorav Right 100006 100007 100001
    100004 Prince Left 100008 NULL 100002
    100005 Ajay Right NULL NULL 100002
    100006 Simran Left NULL NULL 100003
    100007 Raman Right NULL NULL 100003
    100008 Vijay Left NULL NULL 100004
    It is a binary table structure.. Every user has to add two per id under him, one is left_leg and second is right_leg... Parent_id is under which user current user is added.. Hope you will be understand..
    I have to write sql query for counting pairs under id "100001". i know there will be important role of parent_id for counting pairs. * what is pair( suppose if any user contains  both left_leg and right_leg id, then it is called pair.)
    I know there are three pairs under id "100001" :-
    1.  100002 and 100003
    2.  100004 and 100005
    3.  100006 and 100007
        100008 will not be counted as pair because it does not have right leg..
     But i dont know how to write sql query for this... Any help will be appreciated... This is my college project... And tommorow is the last date of submission.... Hope anyone will help me...
    Suppose i have to count pair for id '100002'. Then there is only one pair under id '100002'. i.e 100004 and 100005

    Sounds like this to me
    DECLARE @ID int
    SET @ID = 100001--your passed value
    SELECT left_leg,right_leg
    FROM table
    WHERE (user_id = @ID
    OR parent_id = @ID)
    AND left_leg IS NOT NULL
    AND right_leg IS NOT NULL
    Please Mark This As Answer if it helps to solve the issue Visakh ---------------------------- http://visakhm.blogspot.com/ https://www.facebook.com/VmBlogs

  • I want sql query for this output

    hi guys
    could u tell how can i write sql query for this out put
    i have one table like this
    ID ACCOUTID TAX
    1 1 A
    2 1 B
    3 2 C
    4 2 D
    5 3 E
    7 NULL F
    8 NULL G
    MY OUT PUT MUST BE LIKE THIS
    ID AID TAX
    2 1 A
    4 2 D
    7 NULL F
    8 NULL G
    HERE IN THIS OUTPUT I SHOULD HAVE
    MAXIMAM ID VALUE FOR A REPEATED AID VALUES
    AND
    THE ROWS AID VALUES IS NULL ALSO MUST PAPULATED IN THE OUTPUT.
    I KNOW ONE SOLUTION LIKE THIS
    SELECT MAX(ID),AID,TAX
    FROM TABLE T
    GROUP BY AID,TAX
    UNION ALL
    SELECT ID, AIC,TAX
    FROM TABLE T
    WHERE AID IS NULL;
    BUT I WANT SAME RESULT WITH OUT USING LOGICAL OPERATORS.
    COULD U PLZ TELL A SOL.

    Will this help:
    SQL> with t as
      2    (
      3      select 1 ID, 1 ACCOUTID, 'A' TAX from dual union all
      4      select 2, 1, 'B' from dual union all
      5      select 3, 2, 'C' from dual union all
      6      select 4, 2, 'D' from dual union all
      7      select 5, 3, 'E' from dual union all
      8      select 7, NULL, 'F' from dual union all
      9      select 8, NULL, 'G' from dual
    10    )
    11  --
    12  select id, ACCOUTID AID, Tax
    13  from
    14  (
    15    select t.*
    16          ,count(1)       over (partition by t.ACCOUTID) cn
    17          ,row_number()   over (partition by t.ACCOUTID order by id desc) rn
    18    from t
    19  )
    20  where cn > 1
    21  and (rn = 1 or ACCOUTID is null)
    22  /
            ID        AID T
             2          1 B
             4          2 D
             8            G
             7            F
    -- If I leave out the OR condition then you'll get this:
    SQL> ed
    Wrote file afiedt.buf
      1  with t as
      2    (
      3      select 1 ID, 1 ACCOUTID, 'A' TAX from dual union all
      4      select 2, 1, 'B' from dual union all
      5      select 3, 2, 'C' from dual union all
      6      select 4, 2, 'D' from dual union all
      7      select 5, 3, 'E' from dual union all
      8      select 7, NULL, 'F' from dual union all
      9      select 8, NULL, 'G' from dual
    10    )
    11  --
    12  select id, ACCOUTID AID, Tax
    13  from
    14  (
    15    select t.*
    16          ,count(1)       over (partition by t.ACCOUTID) cn
    17          ,row_number()   over (partition by t.ACCOUTID order by id desc) rn
    18    from t
    19  )
    20  where cn > 1
    21* and rn = 1
    SQL> /
            ID        AID T
             2          1 B
             4          2 D
             8            G
    --which follows the description you've given, but not the output

  • How to write query for this in TopLink ?

    I am doing a simple search in jsp where the search will the based on the choices chosen by user.
    I had given 3 check boxes for those choices.
    The problem is, query will be based on the choice or choices chosed by the user.
    How to write query for this in TopLink ?
    Thanks in Advance..
    Jayaganesh

    Try below solution, it is NOT best solution but might work:
    Declare @Questions TABLE (QuestionID INT, QuestionText Varchar(100))
    INSERT INTO @Questions
    VALUES (1, 'Comment'), (2, 'Score')
    DECLARE @Answers TABLE (authkey INT, QuestionID INT, questiontext VARCHAR(100), answertext VARCHAR(100))
    INSERT INTO @Answers
    VALUES (101, 1, 'comment', 'hi!!'), (101, 2, 'score', '4'), (102, 1, 'comment', 'excellent'), (102, 2, 'score', '5'), (103, 2, 'score', '6'), (104, 2, 'score', '8')
    SELECT
    A.AuthKey
    ,Q.QuestionID
    ,Q.QuestionText
    ,A.AnswerText
    FROM
    @Questions Q
    INNER JOIN @Answers A ON Q.QuestionID = A.QuestionID
    UNION
    SELECT
    A.AuthKey
    ,Q.QuestionID
    ,Q.QuestionText
    ,Null
    FROM
    @Questions Q
    CROSS JOIN @Answers A
    WHERE
    NOT EXISTS (SELECT 1 FROM @Answers SubQry WHERE SubQry.AuthKey = A.AuthKey AND SubQry.QuestionID = Q.QuestionID)
    Output
    AuthKey | QuestionID
    | QuestionText
    | AnswerText
    101 | 1 | Comment | hi!!
    101 | 2 | Score | 4
    102 | 1 | Comment | excellent
    102 | 2 | Score | 5
    103 | 1 | Comment | NULL
    103 | 2 | Score | 6
    104 | 1 | Comment | NULL
    104 | 2 | Score | 8
    Best Wishes, Arbi; Please vote if you find this posting was helpful or Mark it as answered.

  • Write a query for which has its field name as input parameter

    Hi All,
    I need to write a query for SSRS report. StartDate, EndDate and ChangedField are the input parameters.
    Let's consider that we are taking employee details. The table contains lot of fields like emp name,add1,add2,city,state,country,designation,rank etc. We have almost 30 fields are there. These field name are coming as input parameter "ChangedField"
    . I need to show the records which belong to the start date and end date also data change for the field which is given in the input parameter "ChangedField".  That meand If I give ChangedField="city", we need to check for data which
    has been updated with new City name in the given time period.
    Please suggest.
    Regards,
    Julie

    Hi Julie,
    Per my understanding that you have many fields in the table to be displayed in the report, some of the fields will always show and others should be hide and show based on the selection of the parameters which include the columns names, right?
    By default, we can't display columns dynamically in the report automatically without insert this column in the report before. So, in your scenario, I will suggest you to add all the columns in the reports first and then add show/hide expression on
    the column visibility based on the selection of the parameter "ChangedField", when some of the fields selectted they will be display, otherwise they will be hidden. For the fields which you want to be always displayed will not need to add the expression
    in the column visibility.
    Details steps about how to acheive this, please referencet to the  article below:
    Displaying Dynamic Columns in SSRS Report
    If you still have any problem, please feel free to ask.
    Regards,
    Vicky Liu
    Vicky Liu
    TechNet Community Support

Maybe you are looking for

  • After new installing Solman 4.0 , the DSWP transaction not work

    hi gurut there, We did two new installing of solman 4.0 ( SQL Server database), after installing ,start up SAP and other  post-install steps... it seems ready,then I start to setup RFC with satellite systems ,and added server ,database,logical compon

  • My speakers are not working perfectly

    my speakers are not sounding perfectly. The sound is blearing. i just buyed 1 and half moths ago.i did not even droped. i have put Hard case cover on my ipod.

  • AIR 3.0 installation

    I am looking for a tutorial on how to setup air 3.0 for use within flash pro cs5.5, can someone point me in the right direction, I can't seem to find anything on integrating the SDK from Adobe. Thanks

  • Real slow bootup

    lately ive been having a slow bootup, it will hang at the following message for about 3 minutes then fail and proceed to boot as normal : Starting Hardware Abstraction Layer Error creating persisant state file: filename ='/var/lib/syslog-ng/syslog-ng

  • IMPORTANT: JARs and Sharing References: New Reference

    Hi Portal Developers, A new reference is available that lists ALL public portal APIs, and provides the following information: Class/interface name Package name JAR file for compilation Location of JAR file Required sharing reference for portalapp.xml