Stupid GROUP BY question

Stupid GROUP BY question

am 25.09.2009 22:27:48 von Jerry Schwartz

------=_NextPart_000_0574_01CA3DFD.27013050
Content-Type: text/plain;
charset="utf-8"
Content-Transfer-Encoding: quoted-printable

It must be too late in the weekâ€=A6

=20

Suppose table Cust has one field, CustID. Suppose table Notes has four =
fields: NoteID (unique), CustID, NoteTime, and NoteText.

=20

A customer can have zero or more notes. Now hereâ€=99s the seemingly =
simple problem that Iâ€=99m trying to solve: I want to find the =
newest note (if any) for each customer.

=20

If all I want is the date, then I can do

=20

SELECT Cust.CustID, MAX(Notes.NoteTime) FROM Cust LEFT JOIN Notes ON =
Cust.CustID =3D Notes.Cust_ID GROUP BY Cust.CustID;

=20

That will work just fine, but now I also want the NoteText associated =
with the newest note. Obviously I canâ€=99t use MAX(NoteText). I =
could do this using a temporary table, but it seems like there should be =
another way.

=20

Regards,

=20

Jerry Schwartz

The Infoshop by Global Information Incorporated

195 Farmington Ave.

Farmington, CT 06032

=20

860.674.8796 / FAX: 860.674.8341

=20

www.the-infoshop.com

=20


------=_NextPart_000_0574_01CA3DFD.27013050--

Re: Stupid GROUP BY question

am 25.09.2009 22:33:41 von Peter Brawley

--------------040707040500060208040302
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

Jerry,

>I want to find the newest note (if any) for each customer.

See "Within-group aggregates" at
http://www.artfulsoftware.com/infotree/queries.php


PB

-----

Jerry Schwartz wrote:
> It must be too late in the week...
>
>
>
> Suppose table Cust has one field, CustID. Suppose table Notes has four fields: NoteID (unique), CustID, NoteTime, and NoteText.
>
>
>
> A customer can have zero or more notes. Now here's the seemingly simple problem that I'm trying to solve: I want to find the newest note (if any) for each customer.
>
>
>
> If all I want is the date, then I can do
>
>
>
> SELECT Cust.CustID, MAX(Notes.NoteTime) FROM Cust LEFT JOIN Notes ON Cust.CustID = Notes.Cust_ID GROUP BY Cust.CustID;
>
>
>
> That will work just fine, but now I also want the NoteText associated with the newest note. Obviously I can't use MAX(NoteText). I could do this using a temporary table, but it seems like there should be another way.
>
>
>
> Regards,
>
>
>
> Jerry Schwartz
>
> The Infoshop by Global Information Incorporated
>
> 195 Farmington Ave.
>
> Farmington, CT 06032
>
>
>
> 860.674.8796 / FAX: 860.674.8341
>
>
>
> www.the-infoshop.com
>
>
>
>
>
> ------------------------------------------------------------ ------------
>
>
> No virus found in this incoming message.
> Checked by AVG - www.avg.com
> Version: 8.5.409 / Virus Database: 270.13.112/2394 - Release Date: 09/25/09 05:51:00
>
>

--------------040707040500060208040302--

RE: Stupid GROUP BY question

am 25.09.2009 22:35:48 von Gavin Towey

Q29tbW9ubHkgcmVmZXJlZCB0byBhcyBhICJncm91cHdpc2UgbWF4Ig0KDQpo dHRwOi8vZGV2Lm15
c3FsLmNvbS9kb2MvcmVmbWFuLzUuMC9lbi9leGFtcGxlLW1heGltdW0tY29s dW1uLWdyb3VwLXJv
dy5odG1sDQpodHRwOi8vamFuLmtuZXNjaGtlLmRlL3Byb2plY3RzL215c3Fs L2dyb3Vwd2lzZS1t
YXgvDQoNClJlZ2FyZHMsDQpHYXZpbiBUb3dleQ0KDQotLS0tLU9yaWdpbmFs IE1lc3NhZ2UtLS0t
LQ0KRnJvbTogSmVycnkgU2Nod2FydHogW21haWx0bzpqc2Nod2FydHpAdGhl LWluZm9zaG9wLmNv
bV0NClNlbnQ6IEZyaWRheSwgU2VwdGVtYmVyIDI1LCAyMDA5IDE6MjggUE0N ClRvOiBteXNxbEBs
aXN0cy5teXNxbC5jb20NClN1YmplY3Q6IFN0dXBpZCBHUk9VUCBCWSBxdWVz dGlvbg0KDQpJdCBt
dXN0IGJlIHRvbyBsYXRlIGluIHRoZSB3ZWVr4oCmDQoNCg0KDQpTdXBwb3Nl IHRhYmxlIEN1c3Qg
aGFzIG9uZSBmaWVsZCwgQ3VzdElELiBTdXBwb3NlIHRhYmxlIE5vdGVzIGhh cyBmb3VyIGZpZWxk
czogTm90ZUlEICh1bmlxdWUpLCBDdXN0SUQsIE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