Firebird SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesFirebird SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 7th, 2006, 04:51 AM
AlLeX_ AlLeX_ is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2006
Posts: 4 AlLeX_ User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 15 m 30 sec
Reputation Power: 0
SQL query help(Firebird)

hi
I want to manipulate over 3 fields (fromdate, todate and dep) wich belong to a table called 'let's say' X.

fromdate - datetime type
todate- datetime type
dep - integer type Int - integer
---------------------------------------------------
dep Int fromdate todate
100 2 01.01.2006 10:00 01.01.2006 20:00
200 2 01.01.2006 20:10 02.01.2006 10:30
100 2 02.01.2006 10:40 02.01.2006 11:00

100 3 01.01.2006 10:00 01.01.2006 20:00
200 3 01.01.2006 20:10 02.01.2006 10:30
100 3 02.01.2006 10:40 02.01.2006 11:00

So, my query has to select a given DEP (for example -100) but with latest TODATE field for a given Int field (for ex.- 2)
More precisely - i don't know how to make relations between todate (and fromdate)fields with the given "int" and given (or group of) "dep" filed. The important latest 'todate'.

thanks very much for the HELP!!!

Reply With Quote
  #2  
Old October 7th, 2006, 05:38 AM
shammat shammat is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Oct 2003
Location: Munich, Bavaria
Posts: 1,007 shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 13 h 20 m 10 sec
Reputation Power: 67
Try this:
Code:
SELECT *
FROM x
WHERE INT = 2
AND   dep = 100
AND   todate = (SELECT MAX(todate) 
                FROM X x2
                WHERE x2.int = x.int
                AND   x2.dep = x.int)


As a side note: "int" as the name for a column is not a very good idea as it leads to confusion (could be mistaken for a data type) and is actually a reserved word in some DBMS and last but not least does not tell you anything about the contents. Column names should be as descriptive as possible.

Reply With Quote
  #3  
Old October 9th, 2006, 04:51 AM
AlLeX_ AlLeX_ is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2006
Posts: 4 AlLeX_ User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 15 m 30 sec
Reputation Power: 0
thanks Shammat
The real column title is not Int. I just put it because is short.

A little more help....what if i want all rows from X,
not only where INT = 2 AND dep = 100

Reply With Quote
  #4  
Old October 9th, 2006, 05:38 AM
shammat shammat is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Oct 2003
Location: Munich, Bavaria
Posts: 1,007 shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 13 h 20 m 10 sec
Reputation Power: 67
Quote:
Originally Posted by AlLeX_
A little more help....what if i want all rows from X,
not only where INT = 2 AND dep = 100

Then leave out the condition on int and dep
Code:
SELECT *
FROM x
WHERE todate = (SELECT MAX(todate) 
                FROM X x2
                WHERE x2.int = x.int
                AND   x2.dep = x.int)

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesFirebird SQL Development > SQL query help(Firebird)


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT