|
|

|
Porsche, and the Porsche crest are registered trademarks of Dr. Ing. h.c. F. Porsche AG.
This site is not affiliated with Porsche in any way. Its only purpose is to provide an online forum for car enthusiasts. All other trademarks are property of their respective owners. |
|
|
| SirAndy |
Mar 26 2007, 04:41 PM
Post
#1
|
|
Resident German ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Admin Posts: 42,385 Joined: 21-January 03 From: Oakland, Kalifornia Member No.: 179 Region Association: Northern California |
it's time for another SQL question ...
here's the problem: i have a table with event log information. it has a unique id for each entry (primary key), a user id, timestamp and other info. getting the info out of the table is easy except for one particular way. what i need is a page where i list all the last actions taken by any user per day. meaning, if users xyz created 30 logentries today, i only want to show the latest for any given day. so, the list should contain exactly ONE entry for each distinct user per day, showing his/her latest action ... something like this, except, the query below doesn't work ... (IMG:style_emoticons/default/rolleyes.gif) CODE SELECT * FROM tbl_EventLog WHERE iID IN ( SELECT DISTINCT el.iUserID, dummy.iID FROM tbl_EventLog as el INNER JOIN tbl_EventLog as dummy ON dummy.iUserID = el.iUserID WHERE (el.dtCreated >= '3/26/2007 0:00:01 AM') ) anyone? (IMG:style_emoticons/default/confused24.gif) Andy |
![]() ![]() |
| bigkensteele |
Mar 26 2007, 10:18 PM
Post
#2
|
|
Major Member ![]() ![]() ![]() ![]() Group: Members Posts: 2,200 Joined: 30-August 04 From: Cincinnati, OH Member No.: 2,660 Region Association: South East States
|
I have kicked this around for a half hour, and I think that the most efficient way to do it would be a to create a work table using something like this:
SELECT xxx.user, xxx.date, Max(xxx.time) AS MaxTime INTO WORKTABLE FROM xxx GROUP BY xxx.user, xxx.date HAVING xxx.date = 03/26/2007; Then do a join of table xxx and WORKTABLE using user, date, and time = MaxTime. SELECT xxx.* FROM WORKTABLE INNER JOIN xxx ON (WORKTABLE.user = xxx.user) and (WORKTABLE.date = xxx.date) and (WORKTABLE.MaxTime = xxx.time); With the above solution, I would create an additional index on your table over the user, date and time columns. However, if your primary key is a sequentially assigned numeric, you could include that in the first query as SELECT Last(xxx.key) AS LastKey, xxx.user, xxx.date, Max(xxx.time) AS MaxTime INTO WORKTABLE FROM xxx GROUP BY xxx.user, xxx.date HAVING xxx.date = 03/26/2007; and then your second query would only need to be SELECT xxx.* FROM WORKTABLE INNER JOIN xxx ON (WORKTABLE.LastKey = xxx.key); Hope this helps, please let us know what you end up doing. BKS |
| SirAndy |
Mar 26 2007, 10:29 PM
Post
#3
|
|
Resident German ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Admin Posts: 42,385 Joined: 21-January 03 From: Oakland, Kalifornia Member No.: 179 Region Association: Northern California |
I have kicked this around for a half hour, and I think that the most efficient way to do it would be a to create a work table using something like this: hmmm, i thought about using a temp table as well. that might work ... (IMG:style_emoticons/default/idea.gif) too bad Microsoft never got over the fact that "DISTINCT ON" is not part of the SQL standard. "DISTINCT ON" let's you specify multiple columns to return but the distinct clause is only used on the column you specify with the ON param ... really too bad MS does not support it. but many others don't either. would have been nice to have, as it would solve my problem and let me get away with just one simple query ... for now, i'm doing to two nested queries in the code. it works fine and speed is not an issue here as it is in the admin section of the B2B site. the page only gets maybe 30 hits a day ... thanks for all the input guys! (IMG:style_emoticons/default/pray.gif) Andy |
SirAndy WOT: SQL query help needed again ... Mar 26 2007, 04:41 PM
Air_Cooled_Nut Can you use a MAX clause on the date/time stamp? ... Mar 26 2007, 05:14 PM
SirAndy
Can you use a MAX clause on the date/time stamp? ... Mar 26 2007, 05:24 PM
fiid :agree: He's right - we have to do something s... Mar 26 2007, 05:25 PM
fiid I think you might group by userid - but not sure..... Mar 26 2007, 05:25 PM
blitZ The Max(dtCreated ) would probably work, then grou... Mar 26 2007, 05:26 PM
SirAndy
The Max(dtCreated ) would probably work, then gro... Mar 26 2007, 06:01 PM
drewvw Not sure what DB you are using, but any thoughts o... Mar 26 2007, 06:10 PM
SirAndy
Not sure what DB you are using, but any thoughts ... Mar 26 2007, 06:29 PM
drewvw
yeah, i could. MS-SQL 8 Server.
problem is, the ... Mar 26 2007, 06:42 PM
SirAndy
Do we have any true DBAs in the club or just prog... Mar 26 2007, 06:53 PM
SirAndy plus, i just noticed, with my hacked solution, i c... Mar 26 2007, 06:31 PM
watsonrx13 Andy, what's the fields that you're trying... Mar 26 2007, 06:52 PM
SirAndy
Andy, what's the fields that you're tryin... Mar 26 2007, 07:02 PM
drewvw I am the same way....I know enough DB/SQL to be da... Mar 26 2007, 06:59 PM
SirAndy well, at least i got the list ordered by time now ... Mar 26 2007, 07:38 PM
blitZ Hmm, wouldn't something like this work?
SEL... Mar 26 2007, 08:09 PM
SirAndy
Hmm, wouldn't something like this work?
SE... Mar 26 2007, 08:22 PM
ClayPerrine Andy,
Look up a tool on Microsoft's website c... Mar 26 2007, 08:47 PM
SirAndy
Andy,
Look up a tool on Microsoft's website ... Mar 26 2007, 09:00 PM
ClayPerrine
Andy,
Look up a tool on Microsoft's website... Mar 26 2007, 09:46 PM
drewvw good response...avoiding IN clauses is nice becaus... Mar 26 2007, 10:23 PM![]() ![]() |
|
Lo-Fi Version | Time is now: 26th December 2025 - 12:21 PM |
| All rights reserved 914World.com © since 2002 |
|
914World.com is the fastest growing online 914 community! We have it all, classifieds, events, forums, vendors, parts, autocross, racing, technical articles, events calendar, newsletter, restoration, gallery, archives, history and more for your Porsche 914 ... |