If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Help with "selection"

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-18-09, 11:24
mcnika mcnika is offline
Registered User
 
Join Date: Aug 2009
Posts: 2
Help with "selection"

Suppose I have a table

ID userName fyears tyears
1 nika 2007 2009
2 nika 2005 2009
3 nika 2000 2005
4 nika 1999 2000
5 nika 1996 1997


fyears is when the user nika started to work, tyears-ended
I need to select the total work experience, now it is =11

SELECT sum(tyears-fyears) FROM WE
I did in this way but it works inappropriately
Reply With Quote
  #2 (permalink)  
Old 08-18-09, 12:05
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
Code:
SELECT Sum(diff)
FROM   (
        SELECT tyears - fyears As diff
        FROM   WE
       ) As x
__________________
George
Twitter | Blog
Reply With Quote
  #3 (permalink)  
Old 08-18-09, 13:25
mcnika mcnika is offline
Registered User
 
Join Date: Aug 2009
Posts: 2
Quote:
Originally Posted by gvee
Code:
SELECT Sum(diff)
FROM   (
        SELECT tyears - fyears As diff
        FROM   WE
       ) As x

the result is 13 but it must be 11

2005-2009 is 4 year work experience
2000-2005 is 5 year work experience
1999-2000 is 1 year work experience
1996-1997 is 1 year work experience

2007-2009 must not be calculated, because they are exists in 2005-2009
Reply With Quote
  #4 (permalink)  
Old 08-18-09, 14:01
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,258
so how do you think you can exclude the problem you have, or have had created for yourself by using a suspect physical design.
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #5 (permalink)  
Old 08-19-09, 03:24
aflorin27 aflorin27 is offline
Registered User
 
Join Date: Apr 2008
Location: Iasi, Romania
Posts: 317
My opinion is that you have to do it procedurally
__________________
Florin Aparaschivei
Iasi, Romania
Reply With Quote
  #6 (permalink)  
Old 08-19-09, 03:58
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
Add a where clause to the derived table to exclude current records.

But to be honest - it's best if you redesign
__________________
George
Twitter | Blog
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

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