Select DISTINCT issue (SQL Expert needed)

Ask a Question related to ASP Database, Design and Development.

  1. #1

    Default Select DISTINCT issue (SQL Expert needed)

    I have a database that I'm pulling out to an ASP page... I am looking for a
    wildcard string in a field. If the wildcard is found I just want ONE object
    (the one with the latest date) with the same name found. EXAMPLE:



    Table: DataVars

    Name QCode Alt TimeStamp

    John Q1324 HHH 9/25/03 12:01:09PM
    John Q9232 GGG 9/25/03 12:12:44PM
    Mark Q7326 JJJ 9/25/03 12:11:56PM
    Mike Q7322 III 9/25/03 12:08:42PM
    Mike Q3211 AAA 9/25/03 12:07:21PM
    Kelly Q6433 XXX 9/25/03 12:05:19PM

    'what I want (No Kelly because her QCode doesn't include a "32" in it)

    John Q1324 HHH 9/25/03 12:01:09PM
    Mark Q7326 JJJ 9/25/03 12:11:56PM
    Mike Q3211 AAA 9/25/03 12:07:21PM

    'These statements don't work

    SELECT * FROM Datavars WHERE Name = (SELECT DISTINCT Name FROM Datavars
    WHERE QCode LIKE '%32%') ORDER BY TimeStamp DESC;
    SELECT DISTINCT Name,QCode,Alt,TimeStamp FROM Datavars WHERE QCode LIKE
    '%32%' ORDER BY TimeStamp DESC;



    Thanks a billion in advance!!


    BP Prgm Guest

  2. Similar Questions and Discussions

    1. Using SELECT DISTINCT
      Thanks for the earlier help, I have got the SELECT DISTINCT to work but I would like to also bring in the ID (Primary Key) field as well. I have had...
    2. SELECT DISTINCT from two tables
      Hi I have two table which are related: table1 holds personellinformation table2 holds nodeInformation The nodes in table2 can have a...
    3. Select Distinct
      I feel a little stupid sending this, but I must be having a mental block or something. I am trying to do a Select Distinct on a column from a...
    4. SELECT DISTINCT + ORDER BY gives ERROR 145: ORDER BY items must appear in the select list if SELECT DISTINCT is specified.
      Following is my stored procedure. If I take the DISTINCT out then everything works fine. BUT I need the distinct because it returns duplicate...
    5. SELECT DISTINCT + ORDER BY gives ERROR 145: ORDER BY items mustappear in the select list if SELECT DISTINCT is specified.
      Dan, You should be able to do this: SELECT Id, FaxID, ReceivedTime, Pages FROM ( SELECT DISTINCT .Id AS Id,
  3. #2

    Default Re: Select DISTINCT issue (SQL Expert needed)

    When you say you want just one object, do you mean just one record? In your
    sample resultset, you have three records. I will assume you want just one.
    If so, this should do it.

    "SELECT TOP 1 [Name],[QCode],[Alt],[TimeStamp] FROM [Datavars] WHERE [QCode]
    LIKE '%32%' ORDER BY [TimeStamp]

    Ray at home


    "BP Prgm" <nospam@please.com> wrote in message
    news:OsNU%233HiDHA.604@TK2MSFTNGP10.phx.gbl...
    > I have a database that I'm pulling out to an ASP page... I am looking for
    a
    > wildcard string in a field. If the wildcard is found I just want ONE
    object
    > (the one with the latest date) with the same name found. EXAMPLE:
    >
    >
    >
    > Table: DataVars
    >
    > Name QCode Alt TimeStamp
    >
    > John Q1324 HHH 9/25/03 12:01:09PM
    > John Q9232 GGG 9/25/03 12:12:44PM
    > Mark Q7326 JJJ 9/25/03 12:11:56PM
    > Mike Q7322 III 9/25/03 12:08:42PM
    > Mike Q3211 AAA 9/25/03 12:07:21PM
    > Kelly Q6433 XXX 9/25/03 12:05:19PM
    >
    > 'what I want (No Kelly because her QCode doesn't include a "32" in it)
    >
    > John Q1324 HHH 9/25/03 12:01:09PM
    > Mark Q7326 JJJ 9/25/03 12:11:56PM
    > Mike Q3211 AAA 9/25/03 12:07:21PM
    >
    > 'These statements don't work
    >
    > SELECT * FROM Datavars WHERE Name = (SELECT DISTINCT Name FROM Datavars
    > WHERE QCode LIKE '%32%') ORDER BY TimeStamp DESC;
    > SELECT DISTINCT Name,QCode,Alt,TimeStamp FROM Datavars WHERE QCode LIKE
    > '%32%' ORDER BY TimeStamp DESC;
    >
    >
    >
    > Thanks a billion in advance!!
    >
    >

    Ray at Guest

  4. #3

    Default Re: Select DISTINCT issue (SQL Expert needed)

    I'm assuming you want to return the records with the earliest timestamp for
    each name. I am also going to assume you will never have two records for the
    same name with identical timestamps.

    Start by creating a query that returns only the records with the correct
    QCodes:

    Select [Name],[TimeStamp] from datavars
    where QCode like '%32%'

    Now use that query as the data source for a query that returns the minimum
    timestamp for each name:

    Select [Name],Min([TimeStamp]) As minTimeStamp
    FROM (
    Select [Name],[TimeStamp] from datavars
    where QCode like '%32%') As q1
    GROUP BY [Name]

    Now join this query back to the original table so you can get the rest of
    the information required:

    Select q2.[Name],t.QCode,t.Alt,q2.minTimestamp As [TimeStamp]
    FROM datavars t INNER JOIN (
    Select [Name],Min([TimeStamp]) As minTimeStamp
    FROM (
    Select [Name],[TimeStamp] from datavars
    where QCode like '%32%') As q1
    GROUP BY [Name]) As q2
    ON t.[Name] = q2.Name AND t.[Timestamp] = q2.minTimeStamp

    HTH,
    Bob Barrows

    BP Prgm wrote:
    > I have a database that I'm pulling out to an ASP page... I am looking
    > for a wildcard string in a field. If the wildcard is found I just
    > want ONE object (the one with the latest date) with the same name
    > found. EXAMPLE:
    >
    >
    >
    > Table: DataVars
    >
    > Name QCode Alt TimeStamp
    >
    > John Q1324 HHH 9/25/03 12:01:09PM
    > John Q9232 GGG 9/25/03 12:12:44PM
    > Mark Q7326 JJJ 9/25/03 12:11:56PM
    > Mike Q7322 III 9/25/03 12:08:42PM
    > Mike Q3211 AAA 9/25/03 12:07:21PM
    > Kelly Q6433 XXX 9/25/03 12:05:19PM
    >
    > 'what I want (No Kelly because her QCode doesn't include a "32" in it)
    >
    > John Q1324 HHH 9/25/03 12:01:09PM
    > Mark Q7326 JJJ 9/25/03 12:11:56PM
    > Mike Q3211 AAA 9/25/03 12:07:21PM
    >
    > 'These statements don't work
    >
    > SELECT * FROM Datavars WHERE Name = (SELECT DISTINCT Name FROM
    > Datavars WHERE QCode LIKE '%32%') ORDER BY TimeStamp DESC;
    > SELECT DISTINCT Name,QCode,Alt,TimeStamp FROM Datavars WHERE QCode
    > LIKE '%32%' ORDER BY TimeStamp DESC;
    >
    >
    >
    > Thanks a billion in advance!!


    Bob Barrows 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