Ask a Question related to Microsoft SQL / MS SQL Server, Design and Development.

  1. #1

    Default Query problem.

    column 1 column2 column 3 column 4

    1 current 1.0 yr1
    1 previous 1.2 yr0
    2 current 1.3 yr1
    2 previous 2.0 yr0
    3 previous 1.4 yr0
    4. current 2.0 yr1
    4. previous 1.5 yr0
    .................................................


    I want to use a query to return the following, basically, if there is no
    "current", then it shouldn't return "previous" .
    is it possible?


    column 1 column2 column 3 column 4

    1 current 1.0 yr1
    1 previous 1.2 yr0
    2 current 1.3 yr1
    2 previous 2.0 yr0
    4. current 2.0 yr1
    4. previous 1.5 yr0


    Mike Guest

  2. Similar Questions and Discussions

    1. Query problem, please help.
      mysql Ver 12.22 Distrib 4.0.24, for pc-linux-gnu (i386) gives me: The following database Error occured: You have an error in your SQL syntax....
    2. ***Sql Query problem
      Randy Webb wrote: SELECT DISTINCT OrderID FROM trans WHERE Trans_ID = 1 AND OrderID NOT IN (SELECT DISTINCT OrderID FROM trans WHERE Trans_ID =...
    3. Query of Query problem
      Error Executing Database Query. Query Of Queries runtime error. Table named "DATA" was not found in Memory. It is misspelled, or the table is...
    4. Query on Query and CF casting problem
      I am using a custom tag in MX7 that was working fine in 5 that renders a table. The input to the custom tag is a query and it's columns along with...
    5. query problem
      SELECT top5.PRODID, p.PRODNAME FROM ( SELECT TOP 5 SALES = COUNT(*), PRODID FROM SALES GROUP BY PRODID ORDER BY SALES DESC ) AS top5 INNER JOIN...
  3. #2

    Default Re: Query problem.

    try this, should work (untested though)

    select a.* from table a inner join
    (select column1 from table where column2 in('current', 'previous')
    group by column1 having count(*) >= 2) b
    on a.column1= b.column1

    --
    -Vishal

    "Mike" <pearl_77@hotmail.com> wrote in message
    news:#tTy7tyRDHA.940@TK2MSFTNGP11.phx.gbl...
    > column 1 column2 column 3 column 4
    >
    > 1 current 1.0 yr1
    > 1 previous 1.2 yr0
    > 2 current 1.3 yr1
    > 2 previous 2.0 yr0
    > 3 previous 1.4 yr0
    > 4. current 2.0 yr1
    > 4. previous 1.5 yr0
    > ................................................
    >
    >
    > I want to use a query to return the following, basically, if there is no
    > "current", then it shouldn't return "previous" .
    > is it possible?
    >
    >
    > column 1 column2 column 3 column 4
    >
    > 1 current 1.0 yr1
    > 1 previous 1.2 yr0
    > 2 current 1.3 yr1
    > 2 previous 2.0 yr0
    > 4. current 2.0 yr1
    > 4. previous 1.5 yr0
    >
    >

    Vishal Parkar Guest

  4. #3

    Default Re: Query problem.

    You can do this in many ways, using self joins, derived tables or even with
    subqueries like:

    SELECT *
    FROM tbl
    WHERE EXISTS( SELECT *
    FROM tbl t1
    INNER JOIN tbl t2
    ON t1.col1 = t2.col1
    AND t1.col1 = tbl.col1
    WHERE t1.col2 = 'current'
    AND t2.col2 = 'previous' ) ;

    --
    - Anith
    ( Please reply to newsgroups only )


    Anith Sen Guest

  5. #4

    Default Re: Query problem.

    okay... but how about this,
    if there is current but no previous, then we should not eliminate
    current.
    is that possible?


    "Anith Sen" <anith@bizdatasolutions.com> wrote in message
    news:uUs$U0yRDHA.1624@tk2msftngp13.phx.gbl...
    > You can do this in many ways, using self joins, derived tables or even
    with
    > subqueries like:
    >
    > SELECT *
    > FROM tbl
    > WHERE EXISTS( SELECT *
    > FROM tbl t1
    > INNER JOIN tbl t2
    > ON t1.col1 = t2.col1
    > AND t1.col1 = tbl.col1
    > WHERE t1.col2 = 'current'
    > AND t2.col2 = 'previous' ) ;
    >
    > --
    > - Anith
    > ( Please reply to newsgroups only )
    >
    >

    Mike Guest

  6. #5

    Default Re: Query problem.

    Just remove the last condition in the WHERE clause like:

    SELECT *
    FROM tbl
    WHERE EXISTS( SELECT *
    FROM tbl t1
    INNER JOIN tbl t2
    ON t1.col1 = t2.col1
    AND t1.col1 = tbl.col1
    WHERE t1.col2 = 'current' ) ;

    --
    - Anith
    ( Please reply to newsgroups only )


    Anith Sen Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139