| Author |
Topic |
|
mcnika
Starting Member
6 Posts |
Posted - 2009-08-18 : 08:19:25
|
| 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 1997fyears is when the user nika started to work, tyears-endedI need to select the total work experience, now it is =11SELECT sum(tyears-fyears) FROM WE I did in this way but it works inappropriately |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-08-18 : 08:33:43
|
how do you get 11 ? KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
Nageswar9
Aged Yak Warrior
600 Posts |
Posted - 2009-08-18 : 08:40:43
|
| I think , he has for id = 1 ( 2007 - 2009 ) and id = 2 (2005 - 2009) this case already count is there. so 2 years minus. that's why he want 11 |
 |
|
|
mcnika
Starting Member
6 Posts |
Posted - 2009-08-18 : 08:43:56
|
| 2005-2009 is 4 year work experience2000-2005 is 5 year work experience1999-2000 is 1 year work experience1996-1997 is 1 year work experience2007-2009 I don't calculate, because they are exists in 2005-2009 |
 |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-08-18 : 08:58:13
|
| declare @ta table( ID int, userName varchar(32),fyears int,tyears int)insert into @ta select 1, 'nika', 2007, 2009 union all select 2, 'nika', 2005, 2009 union all select 3, 'nika', 2000 ,2005 union all select 4, 'nika', 1999, 2000 union all select 5, 'nika', 1996, 1997select username, count(distinct fyears+number) as valfrom @ta inner join master..spt_values m on type = 'p' and number > 0where fyears+number <= tyearsgroup by username |
 |
|
|
mcnika
Starting Member
6 Posts |
Posted - 2009-08-18 : 13:32:41
|
| Please explain what is written here I don't understand "number" and "master..spt_values m on type = 'p' " |
 |
|
|
Nageswar9
Aged Yak Warrior
600 Posts |
Posted - 2009-08-18 : 23:49:42
|
| master..spt_values is system table , and it used for Loop purposeselect * from master..spt_values where type = 'p' |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-08-19 : 00:02:31
|
[code]declare @ta table( ID int, username varchar(32),fyears int,tyears int)insert into @ta select1, 'nika', 2007, 2009 union all select6, 'nika', 2005, 2007 union all select2, 'nika', 2005, 2009 union all select3, 'nika', 2000 ,2005 union all select4, 'nika', 1999, 2000 union all select5, 'nika', 1996, 1997select username, sum(tyears - fyears)from @ta twhere not exists ( select * from @ta x where x.username = t.username and x.ID <> t.ID and t.fyears >= x.fyears and t.tyears <= x.tyears )group by username[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
mcnika
Starting Member
6 Posts |
Posted - 2009-08-19 : 02:37:05
|
| Thanks a lot for your reply.In this casewhen table contains only this values1, 'nika', 2007, 2009 2, 'nika', 2007, 2009Returned an empty result row.In other cases it works perfectly.Please help with this too.Thanks. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-08-19 : 03:38:27
|
[code]; with dataas( select ID = min(ID), username, fyears, tyears from @ta group by username, fyears, tyears)select username, sum(tyears - fyears)from data twhere not exists ( select * from data x where x.username = t.username and x.ID <> t.ID and t.fyears >= x.fyears and t.tyears <= x.tyears )group by username[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
mcnika
Starting Member
6 Posts |
Posted - 2009-08-19 : 03:56:43
|
| I don't understand this; with dataas(...... |
 |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-08-19 : 04:34:55
|
| hi, did u verify the query given by me;with data as ( see commontableexpression then u can understand this |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-08-19 : 04:42:07
|
quote: Originally posted by mcnika I don't understand this; with dataas(......
that is CTE (Common Table Expression). See http://msdn.microsoft.com/en-us/library/ms190766.aspxYou are using SQL 2005 / 2008 right ? KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
mcnika
Starting Member
6 Posts |
Posted - 2009-08-19 : 05:12:22
|
| I must use it in PHP. |
 |
|
|
|