6 Replies - 1161 Views - Last Post: 27 December 2008 - 09:21 AM Rate Topic: -----

#1 Mangore   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 187
  • Joined: 11-October 08

Sql question about Last

Posted 25 December 2008 - 08:03 AM

I want to sue this Sql but I'm confusing in using Last to define the History
it's mean if the order of histoy in DB si : 1/12/2008, 2/12/2008,3/12/2008 will chose the 3/12/2008 but if the order is: 1/12/2008, 3/12/2008,2/12/2008 . will chose the 2/12/2008, how can I make it as second oder to chose the 3/12/2008..
Select .... from Table1 ORDER BY ... (History)..
Is This A Good Question/Topic? 0
  • +

Replies To: Sql question about Last

#2 mavrck   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 14
  • Joined: 23-December 08

Re: Sql question about Last

Posted 25 December 2008 - 09:42 AM

I'm willing to help you, but I can't make any sense from your question.

Are you wanting the second highest/lowest result?
Was This Post Helpful? 0
  • +
  • -

#3 Hary   User is offline

  • D.I.C Regular

Reputation: 44
  • View blog
  • Posts: 427
  • Joined: 23-September 08

Re: Sql question about Last

Posted 25 December 2008 - 09:45 AM

What is the data type of that column? Are you storing the date as a String (VARCHAR. CHAR()), a timestamp or a date? The last two sort 'normally'. If you store dates in a String-like field, the sorting cannot use date specific information.

SELECT * FROM table ORDER BY dateColumn ASC LIMIT 0, 1

This will give you the last date.
Was This Post Helpful? 0
  • +
  • -

#4 Mangore   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 187
  • Joined: 11-October 08

Re: Sql question about Last

Posted 25 December 2008 - 01:03 PM

I have Table1 in Db Ms Access
the Table has these columns A,B,C,D,E,F,G,H.. the typed of H is Date/time and the Typed of D is number and other columns is Text
I tried this Sql
"  Select A,B,C, SUM(D), (F,G ORDER BY max (H)) FROM Table1 Group by A,B,C.


The problem is in this line of Sql (F,G ORDER BY max (H))

How do I fix this problem ?

This post has been edited by Mangore: 25 December 2008 - 01:05 PM

Was This Post Helpful? 0
  • +
  • -

#5 mavrck   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 14
  • Joined: 23-December 08

Re: Sql question about Last

Posted 25 December 2008 - 01:25 PM

View PostMangore, on 25 Dec, 2008 - 12:03 PM, said:

I have Table1 in Db Ms Access
the Table has these columns A,B,C,D,E,F,G,H.. the typed of H is Date/time and the Typed of D is number and other columns is Text
I tried this Sql
"  Select A,B,C, SUM(D), (F,G ORDER BY max (H)) FROM Table1 Group by A,B,C.


The problem is in this line of Sql (F,G ORDER BY max (H))

How do I fix this problem ?



You don't/can't order by a column's value, you do it by the column's name. i.e. you could to the following if you've trying to get the largest value of h:

SELECT A,B,C,D,E,F,G,H ORDER BY H DESC


However, I noticed that you've included the 'SUM(D)' selection. For what purpose is this?


-mav
Was This Post Helpful? 0
  • +
  • -

#6 Mangore   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 187
  • Joined: 11-October 08

Re: Sql question about Last

Posted 26 December 2008 - 03:42 AM

I tried this but didn't succedd
Select A,B,C, SUM(D), (F,G  ORDER BY H DESC) FROM Table1 Group by A,B,C
Because I want to select the F,G according to the H DESC , and A,B,C as group A,B,C

This post has been edited by Mangore: 26 December 2008 - 03:45 AM

Was This Post Helpful? 0
  • +
  • -

#7 kpelchat   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 27-December 08

Re: Sql question about Last

Posted 27 December 2008 - 09:21 AM

View PostMangore, on 26 Dec, 2008 - 02:42 AM, said:

I tried this but didn't succedd
Select A,B,C, SUM(D), (F,G  ORDER BY H DESC) FROM Table1 Group by A,B,C
Because I want to select the F,G according to the H DESC , and A,B,C as group A,B,C


Because you are using the sum (sum(d)) in the select list you MUST include all other columns from the select list A, B, C, F, G in the GROUP BY clause. Additionally, you CANNOT use the order by clause in the select list. A more appropriate statement would look like this:

select a, b, c, sum(d), f, g
from table1
group by a, b, c, f, g
order by h desc
where h (put some criteria here to filter for the correct h's)



Hope this helps!

kpelchat
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1