anyone????
Anyone any good at SQL?
-
andyc209
- Posts: 614
- Joined: Wed Sep 25, 2002 10:21 am
- Location: Biddulph
I have an SQL database with a table with Fax Logs in. Each row as a number which corresponds to a faxed order and a date and time it was sent.
If the fax has had to be resent I get a new record with a duplicate faxed order number as with the original record and a new date/time.
How can I query this so thatI return only the newest fax record and not the older one in my table of records.
SO far i have
SELECT dbo.faxlog.Col001 AS FaxDate, dbo.faxlog.Col002 AS FaxTime, dbo.faxlog.Col003 AS FaxUser, dbo.faxlog.Col004 AS FaxSPOID,
dbo.faxlog.Col005 AS FaxOrganisation, dbo.faxlog.Col007 AS FaxNumber, dbo.faxlog.Col009 AS FaxPages, dbo.faxlog.Col010 AS FaxDuration,
dbo.faxlog.Col012 AS FaxFrom, dbo.ZFAXerror.Description AS FaxErrorDescription, dbo.faxlog.Col008 AS FaxError
FROM dbo.ZFAXerror INNER JOIN
dbo.faxlog ON dbo.ZFAXerror.Hex = dbo.faxlog.Col008
WHERE (dbo.faxlog.Col003 = 'MPP')
and i need it to show all the fax logs but only the newest fax where FaxSOPID might be the same.
cheers
:rolleyes: :rolleyes:
If the fax has had to be resent I get a new record with a duplicate faxed order number as with the original record and a new date/time.
How can I query this so thatI return only the newest fax record and not the older one in my table of records.
SO far i have
SELECT dbo.faxlog.Col001 AS FaxDate, dbo.faxlog.Col002 AS FaxTime, dbo.faxlog.Col003 AS FaxUser, dbo.faxlog.Col004 AS FaxSPOID,
dbo.faxlog.Col005 AS FaxOrganisation, dbo.faxlog.Col007 AS FaxNumber, dbo.faxlog.Col009 AS FaxPages, dbo.faxlog.Col010 AS FaxDuration,
dbo.faxlog.Col012 AS FaxFrom, dbo.ZFAXerror.Description AS FaxErrorDescription, dbo.faxlog.Col008 AS FaxError
FROM dbo.ZFAXerror INNER JOIN
dbo.faxlog ON dbo.ZFAXerror.Hex = dbo.faxlog.Col008
WHERE (dbo.faxlog.Col003 = 'MPP')
and i need it to show all the fax logs but only the newest fax where FaxSOPID might be the same.
cheers
:rolleyes: :rolleyes:
Duuhhh.... Peugeot Monkey Business.......
-
MattB
- Site Admin
- Posts: 1609
- Joined: Fri Dec 27, 2002 8:07 pm
- Location: Leeds
You need to select the MAX date in the SELECT, then GROUP BY the id.
Untested, but shuold work:
SELECT MAX(dbo.faxlog.Col001) AS FaxDate, dbo.faxlog.Col002 AS FaxTime, dbo.faxlog.Col003 AS FaxUser, dbo.faxlog.Col004 AS FaxSPOID,
dbo.faxlog.Col005 AS FaxOrganisation, dbo.faxlog.Col007 AS FaxNumber, dbo.faxlog.Col009 AS FaxPages, dbo.faxlog.Col010 AS FaxDuration,
dbo.faxlog.Col012 AS FaxFrom, dbo.ZFAXerror.Description AS FaxErrorDescription, dbo.faxlog.Col008 AS FaxError
FROM dbo.ZFAXerror INNER JOIN
dbo.faxlog ON dbo.ZFAXerror.Hex = dbo.faxlog.Col008
WHERE (dbo.faxlog.Col003 = 'MPP') GROUP BY FaxSPOID
Untested, but shuold work:
SELECT MAX(dbo.faxlog.Col001) AS FaxDate, dbo.faxlog.Col002 AS FaxTime, dbo.faxlog.Col003 AS FaxUser, dbo.faxlog.Col004 AS FaxSPOID,
dbo.faxlog.Col005 AS FaxOrganisation, dbo.faxlog.Col007 AS FaxNumber, dbo.faxlog.Col009 AS FaxPages, dbo.faxlog.Col010 AS FaxDuration,
dbo.faxlog.Col012 AS FaxFrom, dbo.ZFAXerror.Description AS FaxErrorDescription, dbo.faxlog.Col008 AS FaxError
FROM dbo.ZFAXerror INNER JOIN
dbo.faxlog ON dbo.ZFAXerror.Hex = dbo.faxlog.Col008
WHERE (dbo.faxlog.Col003 = 'MPP') GROUP BY FaxSPOID
-
jim
- Posts: 230
- Joined: Sat Jun 01, 2002 1:00 am
- Location: Bristol
-
MattB
- Site Admin
- Posts: 1609
- Joined: Fri Dec 27, 2002 8:07 pm
- Location: Leeds
Beware though - if you have a lot of data, DISTINCT in SQL Server is painfully slow.I think you'll get either, as the distinct used in that way will only look at FaxSOPID.
Who'd have thought i'd end up having an SQL converstion on this site?!? Or even that theres so many people here who know it. Maybe we should have an sql area so i know where to come next time i am stuck.
Maybe we should get some car stickers made up... "SELECT owner FROM 206cc WHERE geek='yes'"
Or something...
-
andyc209
- Posts: 614
- Joined: Wed Sep 25, 2002 10:21 am
- Location: Biddulph
-
MattB
- Site Admin
- Posts: 1609
- Joined: Fri Dec 27, 2002 8:07 pm
- Location: Leeds
What error are you getting when trying to use MAX then?
I've setup a quick test DB here and it works fine. The table I'm using is:
id d
----------- ----------------------------
1 2003-02-13 09:00:00.000
1 2003-02-14 09:00:00.000
1 2003-02-15 09:00:00.000
2 2003-02-13 10:00:00.000
2 2003-02-13 11:00:00.000
3 2003-02-16 14:00:00.000
And the query:
SELECT id, MAX(d) AS d FROM andy GROUP BY id;
returns:
id d
----------- ----------------------------
1 2003-02-15 09:00:00.000
2 2003-02-13 11:00:00.000
3 2003-02-16 14:00:00.000
Which is correct as far as I can see...
I've setup a quick test DB here and it works fine. The table I'm using is:
id d
----------- ----------------------------
1 2003-02-13 09:00:00.000
1 2003-02-14 09:00:00.000
1 2003-02-15 09:00:00.000
2 2003-02-13 10:00:00.000
2 2003-02-13 11:00:00.000
3 2003-02-16 14:00:00.000
And the query:
SELECT id, MAX(d) AS d FROM andy GROUP BY id;
returns:
id d
----------- ----------------------------
1 2003-02-15 09:00:00.000
2 2003-02-13 11:00:00.000
3 2003-02-16 14:00:00.000
Which is correct as far as I can see...
-
CB
- Posts: 4312
- Joined: Mon Apr 08, 2002 1:00 am
- Location: Classified
-
andyc209
- Posts: 614
- Joined: Wed Sep 25, 2002 10:21 am
- Location: Biddulph
Works fine..up to a point
I have :
SELECT TOP 100 PERCENT MAX(Col004) AS Expr1, Col003
FROM dbo.faxlog
GROUP BY Col004, Col003
HAVING (MAX(Col004) < '999999') AND (Col003 = 'mpp')
ORDER BY Col004
which does filter duplicates from the Col004 colum BUT
this only shows the Col004 and Col003
(col004 being the now filtered ID and col003 being all 'MPP')
I need to also show the rest of the data as well ...col001,2,3 etc which contains names and addresses etc
e.g
SOPID Col003 Col001 Col002
123 MPP Joe blogs London
124 MPP Fred Smith Liverpool
if I add these other columns to the SELECT clause i get
Column Col001 is invalid in the select list as it is not contained in either an aggregate function or in the Group by clause
cheers
I have :
SELECT TOP 100 PERCENT MAX(Col004) AS Expr1, Col003
FROM dbo.faxlog
GROUP BY Col004, Col003
HAVING (MAX(Col004) < '999999') AND (Col003 = 'mpp')
ORDER BY Col004
which does filter duplicates from the Col004 colum BUT
this only shows the Col004 and Col003
(col004 being the now filtered ID and col003 being all 'MPP')
I need to also show the rest of the data as well ...col001,2,3 etc which contains names and addresses etc
e.g
SOPID Col003 Col001 Col002
123 MPP Joe blogs London
124 MPP Fred Smith Liverpool
if I add these other columns to the SELECT clause i get
Column Col001 is invalid in the select list as it is not contained in either an aggregate function or in the Group by clause
cheers
Duuhhh.... Peugeot Monkey Business.......