Page 1 of 2 12 LastLast
Results 1 to 10 of 20
  1. #1

    Default need help on a sql query: :)


    Hi, All i hope someone can help me out

    so heres the situation...

    i got a table lets call it 'history', and freight_bill

    Code:
    table:freight_bill
    ------------------------------------------------
    ID     | FreightBill     |
    ------------------------------------------------
    1      | frght0001     |
    2      | frght0002     |
    3      | frght0003     |
    4      | frght0004     |
    5      | frght0005     |
    6      | frght0006     |
    ------------------------------------------------
    
    table: history
    ------------------------------------------------
    ID     | FreightBill     | TimeStamp     | status     |
    ------------------------------------------------
    1      | frght0001     | 5pm              | pending    |
    2      | frght0001     | 6pm              | analysis    |
    3      | frght0001     | 7pm              | analysis    |
    4      | frght0002     | 5pm              | requeue    |
    5      | frght0003     | 5pm              | NULL        |
    6      | frght0004     | 8pm              | analysis    |
    7      | frght0004     | 8:30pm         | requeue    |
    ------------------------------------------------
    so my concern is, how do i get all the freight bill with its latest time stamps or information?
    so end result would be :

    Code:
    ------------------------------------------------
    ID     | FreightBill     | TimeStamp     | status     |
    ------------------------------------------------
    1      | frght0001     | 7pm              | analysis     |
    2      | frght0002     | 5pm              | requeue    |
    3      | frght0003     | 5pm              | null    |
    4      | frght0004     | 8:30pm         | requeue    |
    5      | frght0005     | NULL             | NULL        |
    6      | frght0006     | NULL             | NULL        |

  2. #2
    Try this:

    SELECT FreightBill,max(TimeStamp),status FROM history GROUP BY FreightBill ORDER BY FreightBill

  3. #3
    ^^ from your query this was the result:

    Code:
    +-------------+----------------+----------+
    | FreightBill | MAX(TimeStamp) | STATUS   |
    +-------------+----------------+----------+
    | frght0001   | 7pm            | pending  |
    | frght0002   | 5pm            | requeue  |
    | frght0003   | 5pm            | null     |
    | frght0004   | 9pm            | analysis |
    +-------------+----------------+----------+
    it didn't display the intended output silent-kill required.

  4. #4
    Quote Originally Posted by javapenguin View Post
    Try this:

    SELECT FreightBill,max(TimeStamp),status FROM history GROUP BY FreightBill ORDER BY FreightBill
    I think ok na na cya. pero I refine nalang para pag left outer join. Try lang. dugay nako wla SQL.

    SELECT
    freight_bill.ID,
    freight_bill.FreightBill,
    max(history.TimeStamp),
    history.status
    FROM history
    LEFT OUTER JOIN freight_bill ON history.FreightBill=freight_bill.FreightBill
    GROUP BY freight_bill.ID, freight_bill.FreightBill,history.status
    ORDER BY freight_bill.ID

  5. #5
    pwede nimo i modify ang suggestion ni javapenguin
    e union lang na nimo sa records with null timestamp

  6. #6
    Quote Originally Posted by spikes View Post
    I think ok na na cya. pero I refine nalang para pag left outer join. Try lang. dugay nako wla SQL.

    SELECT
    freight_bill.ID,
    freight_bill.FreightBill,
    max(history.TimeStamp),
    history.status
    FROM history
    LEFT OUTER JOIN freight_bill ON history.FreightBill=freight_bill.FreightBill
    GROUP BY freight_bill.ID, freight_bill.FreightBill,history.status
    ORDER BY freight_bill.ID
    from your query, this is the output:

    Code:
    +------+-------------+------------------------+----------+
    | ID   | FreightBill | max(history.TimeStamp) | status   |
    +------+-------------+------------------------+----------+
    |    1 | frght0001   | 7pm                    | analysis |
    |    1 | frght0001   | 5pm                    | pending  |
    |    2 | frght0002   | 5pm                    | requeue  |
    |    3 | frght0003   | 5pm                    | null     |
    |    4 | frght0004   | 8pm                    | analysis |
    |    4 | frght0004   | 9pm                    | requeue  |
    +------+-------------+------------------------+----------+

  7. #7
    With mysql you have to use a temporary table and user variables.
    Try this:
    Code:
    create temporary table temp (FreightBill, TimeStamp, status)
    	select if(@fb=FreightBill, "xRepeatedx", @fb:=FreightBill), TimeStamp, status 
    	from history order by FreightBill, TimeStamp desc;
    
    select f.ID, f.FreightBill, h.TimeStamp, h.Status
    	from freight_bill as f 
    	left join temp as h on f.FreightBill = h.Freightbill
    	order by f.ID
    There maybe syntax errors for this is not tested on actual sql server but the principle here is shown.

  8. #8
    Quote Originally Posted by ChaosOrb View Post
    ^^ from your query this was the result:

    Code:
    +-------------+----------------+----------+
    | FreightBill | MAX(TimeStamp) | STATUS   |
    +-------------+----------------+----------+
    | frght0001   | 7pm            | pending  |
    | frght0002   | 5pm            | requeue  |
    | frght0003   | 5pm            | null     |
    | frght0004   | 9pm            | analysis |
    +-------------+----------------+----------+
    it didn't display the intended output silent-kill required.

    hehehe, klaro jud wala nako gi test akong sql, karon ako na jud gi testing sa mysql, it's a "dirty" solution but the output is right (I think).

    try this:

    SELECT b.FreightBill,time_format(substring(max(concat(a.T imeStamp,a.status is not null)),1,,'%r') as time,substring(max(concat(a.TimeStamp,a.status)),9 ) as status FROM freight_bill as b left join history as a on b.FreightBill = a.FreightBill group by b.FreightBill;

    +-------------+-------------+----------+
    | FreightBill | time | status |
    +-------------+-------------+----------+
    | frght0001 | 07:00:00 PM | analysis |
    | frght0002 | 05:00:00 PM | requeue |
    | frght0003 | 05:00:00 PM | NULL |
    | frght0004 | 08:30:00 PM | requeue |
    | frght0005 | NULL | NULL |
    | frght0006 | NULL | NULL |
    +-------------+-------------+----------+
    6 rows in set (0.00 sec)

  9. #9
    ooops! ni sulod man c smily, 8 ) (eight then close parenthesis)

  10. #10
    what say you silent-kill??

  11.    Advertisement

Page 1 of 2 12 LastLast

Similar Threads

 
  1. need help on ipod shuffle
    By apart in forum Gizmos & Gadgets (Old)
    Replies: 7
    Last Post: 08-19-2009, 08:13 AM
  2. need help on my video card
    By dudoyjoie in forum Computer Hardware
    Replies: 9
    Last Post: 11-25-2005, 09:59 AM
  3. needs help on HTML
    By tagaisla in forum Websites & Multimedia
    Replies: 11
    Last Post: 11-16-2005, 03:32 AM
  4. MOVED: needs help on HTML
    By vern in forum Networking & Internet
    Replies: 0
    Last Post: 11-14-2005, 04:52 AM
  5. I need help on CPU Air flow suggestions
    By HLadik2 in forum Computer Hardware
    Replies: 8
    Last Post: 09-12-2005, 05:20 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
about us
We are the first Cebu Online Media.

iSTORYA.NET is Cebu's Biggest, Southern Philippines' Most Active, and the Philippines' Strongest Online Community!
follow us
#top