Computer Webmaster Gaming Console Graphics Forum

Welcome to the Computer Webmaster Gaming Console Graphics Forum forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.

MK PitStop Main Earn $25 Earn Money Posting Extras Members Blogs Image Hosting User Pages
Go Back   Computer Webmaster Gaming Console Graphics Forum > Webmaster Forum > Website Coding > Database
Register FAQ/Rules Become A V.I.P. Member Search Today's Posts Mark Forums Read

Database Database problems or need to ask a question? maybe something to do with sql injections or a database software question. Database topics cover MySQL, PostgreSQL, Oracle, SQL Server or anything else related to databases.

Google
Reply
 
LinkBack Thread Tools Display Modes
Old 07-01-2007, 9:31 PM   #1
Lee
 
Lee's Avatar
 
Posts: n/a
My Photos: (0)

Banked:
MK Cash: $

I am Worth:
MK Cash: $
Donate

Recent Blog: None

Default SQL Sort by date

This is probably very trivial, but I'm stuck. I have a audit log table
like this:

Order_Id Status Timestamp
12345 READY 2006-04-28 09:03:21
43244 READY 2006-04-28 09:03:30
66434 READY 2006-04-28 09:04:17
12345 SET 2006-04-28 09:05:46
12345 GO 2006-04-28 09:10:49
43244 SET 2006-04-28 09:17:38
99999 READY 2006-04-29 03:12:33

How can I write a query that gives me all the order_ids who's status is
'SET'?

I've been trying to use the MAX,MIN and group by functions, but not
getting the results I would expect.

Thanks for any help,

Lee

 
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Reply With Quote
Advertisements
Old 07-01-2007, 9:31 PM   #2
Jerry Stuckle
 
Jerry Stuckle's Avatar
 
Posts: n/a
My Photos: (0)

Banked:
MK Cash: $

I am Worth:
MK Cash: $
Donate

Recent Blog: None

Default SQL Sort by date

Lee wrote:
> This is probably very trivial, but I'm stuck. I have a audit log table
> like this:
>
> Order_Id Status Timestamp
> 12345 READY 2006-04-28 09:03:21
> 43244 READY 2006-04-28 09:03:30
> 66434 READY 2006-04-28 09:04:17
> 12345 SET 2006-04-28 09:05:46
> 12345 GO 2006-04-28 09:10:49
> 43244 SET 2006-04-28 09:17:38
> 99999 READY 2006-04-29 03:12:33
>
> How can I write a query that gives me all the order_ids who's status is
> 'SET'?
>
> I've been trying to use the MAX,MIN and group by functions, but not
> getting the results I would expect.
>
> Thanks for any help,
>
> Lee
>


SELECT Order_Id FROM mytable where Status = 'SET';

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
 
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Reply With Quote
Old 07-01-2007, 9:31 PM   #3
Lee
 
Lee's Avatar
 
Posts: n/a
My Photos: (0)

Banked:
MK Cash: $

I am Worth:
MK Cash: $
Donate

Recent Blog: None

Default SQL Sort by date

Jerry: The problem with that is since this is an log, when someone
places an entry afterwards saying order_id is 'GO', your query will
always show that order_id as set. In my example table, 12345 would
still show as 'SET' even though it's actually 'GO'

I could do an update instead of an insert, but I want to track how long
each step takes. I could also write another table with order_ids and
just do update statements on it. It just bothers me I can't do it with
just this table.

 
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Reply With Quote
Old 07-01-2007, 9:31 PM   #4
Johan
 
Johan's Avatar
 
Posts: n/a
My Photos: (0)

Banked:
MK Cash: $

I am Worth:
MK Cash: $
Donate

Recent Blog: None

Default SQL Sort by date

Lee <lhenkel@gmail.com> wrote:

> This is probably very trivial, but I'm stuck. I have a audit log table
> like this:
>
> Order_Id Status Timestamp
> 12345 READY 2006-04-28 09:03:21
> 43244 READY 2006-04-28 09:03:30
> 66434 READY 2006-04-28 09:04:17
> 12345 SET 2006-04-28 09:05:46
> 12345 GO 2006-04-28 09:10:49
> 43244 SET 2006-04-28 09:17:38
> 99999 READY 2006-04-29 03:12:33
>
> How can I write a query that gives me all the order_ids who's status is
> 'SET'?
>
> I've been trying to use the MAX,MIN and group by functions, but not
> getting the results I would expect.
>
> Thanks for any help,
> Lee

Not tested this but using a subselect might be an option for you. Try
something like this:

Select l.order_id, min(l.status)
from
(select order_id, case status
when 'SET' then 1
when 'GO' then 2
when 'READY' then 3
else 4
end case as status
from log_table) l
group by l.order_id;

Again, no syntax check performed on this!
--
_____________________________________
Ing. Johan van Oostrum
chaos geordend - www.chaosgeordend.nl
_____________________________________
 
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Reply With Quote
Old 07-01-2007, 9:31 PM   #5
Bill Karwin
 
Bill Karwin's Avatar
 
Posts: n/a
My Photos: (0)

Banked:
MK Cash: $

I am Worth:
MK Cash: $
Donate

Recent Blog: None

Default SQL Sort by date

Lee wrote:
> This is probably very trivial, but I'm stuck. I have a audit log table
> like this:
>
> Order_Id Status Timestamp
> 12345 READY 2006-04-28 09:03:21
> 43244 READY 2006-04-28 09:03:30
> 66434 READY 2006-04-28 09:04:17
> 12345 SET 2006-04-28 09:05:46
> 12345 GO 2006-04-28 09:10:49
> 43244 SET 2006-04-28 09:17:38
> 99999 READY 2006-04-29 03:12:33
>
> How can I write a query that gives me all the order_ids who's status is
> 'SET'?


SELECT a1.order_id, a1.status
FROM audit_log AS a1
LEFT OUTER JOIN audit_log AS a2
ON a1.order_id = a2.order_id AND a1.`timestamp` < a2.`timestamp`
WHERE a2.order_id IS NULL AND a1.status = 'SET'

Regards,
Bill K.
 
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Reply With Quote
Old 07-01-2007, 9:31 PM   #6
Jerry Stuckle
 
Jerry Stuckle's Avatar
 
Posts: n/a
My Photos: (0)

Banked:
MK Cash: $

I am Worth:
MK Cash: $
Donate

Recent Blog: None

Default SQL Sort by date

Lee wrote:
> Jerry: The problem with that is since this is an log, when someone
> places an entry afterwards saying order_id is 'GO', your query will
> always show that order_id as set. In my example table, 12345 would
> still show as 'SET' even though it's actually 'GO'
>
> I could do an update instead of an insert, but I want to track how long
> each step takes. I could also write another table with order_ids and
> just do update statements on it. It just bothers me I can't do it with
> just this table.
>


OK, I see your problem now.

That's going a little harder. If you have a version of MySQL which supports
subselects, you could have something like:

SELECT Order_Id FROM mytable
WHERE Status = 'SET' AND
Order_Id NOT IN (SELECT Order_Id
FROM mytable
WHERE Status = 'GO');


This will get all orders with the status of SET which do not also have the
status of GO. You could add checks for other status values, also.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
 
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Reply With Quote
Old 07-01-2007, 9:31 PM   #7
Lee
 
Lee's Avatar
 
Posts: n/a
My Photos: (0)

Banked:
MK Cash: $

I am Worth:
MK Cash: $
Donate

Recent Blog: None

Default SQL Sort by date

Thanks for all the replies.. sadly I'm at home and can't try these out,
but at least these are some good starts if not solutions. I came up
with something that appears to work doing:

SELECT * FROM mytable
GROUP BY order_id
ORDER BY TIMESTAMP

Which gives the current status of all orders (I think). However, I
still have to filter in code for the status. Not terribly elegent.

Thanks!

 
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Reply With Quote
Old 07-01-2007, 9:32 PM   #8
Rik
 
Rik's Avatar
 
Posts: n/a
My Photos: (0)

Banked:
MK Cash: $

I am Worth:
MK Cash: $
Donate

Recent Blog: None

Default SQL Sort by date

Jerry Stuckle wrote:
> OK, I see your problem now.
>
> That's going a little harder. If you have a version of MySQL which
> supports subselects, you could have something like:
>
> SELECT Order_Id FROM mytable
> WHERE Status = 'SET' AND
> Order_Id NOT IN (SELECT Order_Id
> FROM mytable
> WHERE Status = 'GO');
>
> This will get all orders with the status of SET which do not also
> have the status of GO. You could add checks for other status values,
> also.



Hmmm, damned, thought I would nail it with an ENUM field, but no:
For MIN(), MAX(), and other aggregate functions, MySQL currently compares
ENUM and SET columns by their string value rather than by the string's
relative position in the set.

That could have solved a lot, but no....
:
SELECT Order_ID
FROM
(SELECT Order_ID, MAX(Status) AS 'max_status'
FROM mytable
GROUP BY Order_ID) as x
WHERE x.`max_status` = 'SET';

Let's wait for a correct implementation...

Grtz,
--
Rik Wasmus


 
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Reply With Quote
Old 07-01-2007, 9:32 PM   #9
Kai Ruhnau
 
Kai Ruhnau's Avatar
 
Posts: n/a
My Photos: (0)

Banked:
MK Cash: $

I am Worth:
MK Cash: $
Donate

Recent Blog: None

Default SQL Sort by date

Rik wrote:
> Hmmm, damned, thought I would nail it with an ENUM field, but no:
> For MIN(), MAX(), and other aggregate functions, MySQL currently compares
> ENUM and SET columns by their string value rather than by the string's
> relative position in the set.
>
> That could have solved a lot, but no....
> :
> SELECT Order_ID
> FROM
> (SELECT Order_ID, MAX(Status) AS 'max_status'
> FROM mytable
> GROUP BY Order_ID) as x
> WHERE x.`max_status` = 'SET';
>
> Let's wait for a correct implementation...


You can get the numeric index of the enum field as described in the manual.

http://dev.mysql.com/doc/refman/5.0/en/enum.html

<snip>

If you retrieve an ENUM value in a numeric context, the column value's
index is returned. For example, you can retrieve numeric values from an
ENUM column like this:

mysql> SELECT enum_col+0 FROM tbl_name;

</snip>

Greetings
Kai

--
This signature is left as an exercise for the reader.
 
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Reply With Quote
Old 07-01-2007, 9:32 PM   #10
Lee
 
Lee's Avatar
 
Posts: n/a
My Photos: (0)

Banked:
MK Cash: $

I am Worth:
MK Cash: $
Donate

Recent Blog: None

Default SQL Sort by date

Damnit Bill, that works and I have no idea why! That's going to bug
me now. I don't see why order_id should be NULL, but I'll have to
puzzle it out on my own nickel.

The other answers were good too; I never thought of case or ENUM.

Thanks all.

Lee

 
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Reply With Quote
Featured Websites
Free Space
Free Space
Free Space Free Space
Reply
Tags: , ,




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
date query problem, date in variable roy Database 1 07-01-2007 6:22 PM
PHP Search sort help please M.E. PHP 2 07-01-2007 5:31 PM
sort order Kurt Milligan PHP 2 07-01-2007 3:35 PM
How do I sort This array? Karl McAuley PHP 1 07-01-2007 3:18 PM
Does the PS3 have any sort of buzz about it being hot or a must have? Penguin Commandos for Gore. Computer Consoles 14 05-30-2007 10:03 PM


Featured Websites




All times are GMT +1. The time now is 3:32 AM.


Powered by: vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO 3.0.0
Cheap Computers
MK PitStop Copyright 2005 - 2008

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98