Jump to content

nealgseattle

New Members
  • Posts

    4
  • Joined

  • Last visited

    Never

Profile Information

  • Gender
    Not Telling

nealgseattle's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. There are a couple of ways to do this. Use a BETWEEN in your WHERE clause. BETWEEN includes the values that are included: SELECT * FROM database WHERE ID BETWEEN 2 AND 3; Use an IN in your WHERE clause: SELECT * FROM database WHERE ID IN (2,3);
  2. I think I have a solution, if I understand correctly. I'll set up a set of sample data, which I will then query. I'm using the Query Analyzer in SQL Server Management Studio for running the code in the sample. First, I'll set up a table: CREATE TABLE clienttable ( client VARCHAR(25), producer VARCHAR(25), submitdate INT ); Then, I'll populate the table with fictitious data: INSERT INTO clienttable VALUES ('Ceres','Zeus',20100115), ('Hespera','Athena',20100221), ('Ceres','Zeus',20100102), ('Juno','Poseiden',20100105), ('Jupiter','Apollo',20100316), ('Minerva','Hestia',20100225), ('Hespera','Athena',20100325), ('Nemesis','Vesta',20100310), ('Neptune','Hera',20100111), ('Neptune','Hestia',20100212), ('Jupiter','Apollo',20100320), ('Hespera','Athena',20100226); I need to get a subset of the data from this table based on the first time a client is entered. To get that client entry record, I run this query: SELECT client, MIN(submitdate) AS firstPolicy FROM clienttable GROUP BY client; And I get 7 rows: client firstPolicy ------------------------- ----------- Ceres 20100102 Hespera 20100221 Juno 20100105 Jupiter 20100316 Minerva 20100225 Nemesis 20100310 Neptune 20100111 Then, I use this query to join against the actual table in order to get the producer - I'm joining a subset of the table to the table itself: SELECT T2.client, T2.firstPolicy, T1.producer FROM clienttable T1 INNER JOIN ( SELECT client, MIN(submitdate) AS firstPolicy FROM clienttable GROUP BY client ) AS T2 ON T1.client = T2.client AND T1.submitdate = T2.firstPolicy; And this give me this result: client firstPolicy producer ------------------------- ----------- ------------------------- Ceres 20100102 Zeus Hespera 20100221 Athena Juno 20100105 Poseiden Jupiter 20100316 Apollo Minerva 20100225 Hestia Nemesis 20100310 Vesta Neptune 20100111 Hera Is this what you're looking for?
  3. Actually, let me be a little more accurate: DISTINCT is designed to return a result set that eliminates duplicate rows based on the columns in the SELECT list, not necessarily in the table. Sorry about that.
  4. It would also be helpful to see the comparable MySQL code. DISTINCT in Transact-SQL is designed to return only one copy of duplicate rows. If you don't have duplicate rows in your table, which you most likely won't have in a normalized database, you will get back all rows in the table.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.