PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Hébergement serveur > comp.db.ms-sqlserver > needed with TSQL statement
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
needed with TSQL statement

Réponse
 
LinkBack Outils de la discussion
Vieux 01/04/2008, 23h34   #1
Greg Hines
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut needed with TSQL statement

Hi all,

I have a booking table that records changes to bookings to give an audit
trail.

Table1

ID - is an Identity column.
BookingID - is a varchar column containing the booking id of each booking.
Comment - is a varchar column containing details of the booking changes.
dtTime - a datetime column containing the date and time of the booking
change.

eg:

ID BookingID Comment dtTime
1 30 Initial booking
2004-07-08 13:36:23.363
4 31 Initial booking
2004-07-08 13:37:23.363
5 30 First change
2004-07-08 13:38:23.363
9 31 First change
2004-07-08 13:39:23.363
11 30 Second change
2004-07-08 13:40:23.363

Note there has been 2 changes to the intial booking for bookingID 30, and
one change for bookingID 31.

What I want is an SQL SELECT statement that will give me all the ID's for
the initial booking. In this example 1 and 4 should be the only results
that the statement will return.

Any appreciated and TIA

Greg


  Réponse avec citation
Vieux 01/04/2008, 23h41   #2
Erland Sommarskog
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: needed with TSQL statement

Greg Hines (ghines@aussiemail.com.au_NO_SPAM) writes:
> What I want is an SQL SELECT statement that will give me all the ID's for
> the initial booking. In this example 1 and 4 should be the only results
> that the statement will return.


SELECT ID, BookingID FROM tbl WHERE Comment = 'Initial booking'?

SELECT MIN(ID), BookingID FROM tbl GROUP BY BookingID?

--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
  Réponse avec citation
Vieux 02/04/2008, 00h09   #3
Greg Hines
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: needed with TSQL statement

> SELECT ID, BookingID FROM tbl WHERE Comment = 'Initial booking'?
Cannot use this as Initial booking comment may not always be the same, just
that it was in my example.

> SELECT MIN(ID), BookingID FROM tbl GROUP BY BookingID?


Gives the error:-

Column 'tbl.BookingID is invalid in the select list because it is not
contained in either an aggregate function or the GROUP BY clause.



  Réponse avec citation
Vieux 02/04/2008, 01h25   #4
Greg Hines
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: needed with TSQL statement

Sorry, my typo.

Your second SELECT statement does do the trick.

Thanks.


  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 02h36.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,09277 seconds with 12 queries