需要在此输出中的SQL查询

时间:2012-11-23 09:19:05

标签: sql sql-server

这是我的疑问:

select * 
from dbo.tblHRIS_Experience 
where intsid=1115 
order by expyear desc 

intEID      intSID      nvrCompany                                                                                                                                                                                                                                                 nvrDesignation                                                                                                                                                                                                                                             ExpMonth                                           ExpYear                                            ExpMonthTo                                         ExpYearTo                                          nvrReason                                                                                                                                                                                                                                                        dttCreatedon            nvrCreatedBy                                       dttModifiedOn           nvrModifiedby
----------- ----------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ----------------------- -------------------------------------------------- ----------------------- --------------------------------------------------
11          1115        Sundaram Clayton Limited                                                                                                                                                                                                                                   Senior Executive Public reletion                                                                                                                                                                                                                           November                                           2009                                               August                                             2012                                               Better job                                                                                                                                                                                                                                                       2012-08-23 12:02:10.527 1115                                               NULL                    NULL
10          1115        Harita Finance Limited                                                                                                                                                                                                                                     Asst Manager Bonds                                                                                                                                                                                                                                         September                                          1988                                               August                                             2009                                               Better job                                                                                                                                                                                                                                                       2012-08-23 11:59:43.057 1115                                               NULL                    NULL
9           1115        Remedies India                                                                                                                                                                                                                                             Medical Representative                                                                                                                                                                                                                                     July                                               1982                                               August                                             1988                                               Better job                                                                                                                                                                                                                                                       2012-08-23 11:57:23.277 1115                                               NULL                    NULL

在同一个查询中需要这样的输出:

[current company]                    [old Company]                   [Exp In perivious Organisation]   [Total yrs Exp]
Sundaram Clayton Limited          Harita Finance Limited                             3                     30      

3 个答案:

答案 0 :(得分:1)

这是代码。但请记住,您需要将ExpYear和ExpMonth以及ExpYearTo和ExpMonthTo合并为2个单独的日期字段,因为现在它不会占用几个月。

;with a as (select * from dbo.tblHRIS_Experience where intsid=1115 order by expyear desc)
select (select top 1 nvrCompany from a) as 'current company',
    (select top 1 nvrCompany from a where ExpYear<(select top 1 ExpYear from a)) as 'old Company',
    (select count(*) from a) as 'Exp In perivious Organisation',
    (select max(ExpYearTo)-min(ExpYear) from a) as 'Total yrs Exp'

答案 1 :(得分:0)

试试此代码

create table #test (
    intsid int,
    nvrCompany varchar(255),
    ExpMonth varchar(15),
    ExpYear smallint,
    ExpMonthTo varchar(15),
    ExpYearTo smallint
)

insert into #test values (1115, 'Sundaram Clayton Limited', 'November',2009,'August',2012)
insert into #test values (1115, 'Harita Finance Limited', 'September',1988,'August',2009)
insert into #test values (1115, 'Remedies India', 'July',1982,'August',1988)
insert into #test values (222, 'test','July',1982,'August',1988)

;with a as (
    select nvrCompany,
    CONVERT(datetime, ExpMonth+' 01 '+cast(ExpYear as char(4))) as datefrom,
    CONVERT(datetime, ExpMonthTo+' 01 '+cast(ExpYearTo as char(4))) as dateto
    from #test where intsid=1115
)
select (select nvrCompany from a where datefrom=(select max(datefrom) from a)) as 'current company',
    (select top 1 nvrCompany from a where datefrom<(select max(datefrom) from a) order by datefrom desc) as 'old Company',
    (select count(*) from a) as 'Exp In perivious Organisation',
    (select datediff(year, min(datefrom),max(dateto)) from a) as 'Total yrs Exp'

答案 2 :(得分:0)

请检查查询:

with T as(
    select cast(('01-'+ExpMonth+'-'+ExpYear) as datetime) ExpFrom, 
        cast(('01-'+ExpMonthTo+'-'+ExpYearTo) as datetime) ExpTo,
        row_number() over (partition by intSID order by cast(('01-'+ExpMonthTo+'-'+ExpYearTo) as datetime) desc) Ord, 
    * from dbo.tblHRIS_Experience
)
select [1] as [CurrComp], [2] as [OldComp], Expr2, TotalExpr From(
select 
    nvrCompany, 
    (select datediff(year, ExpFrom, ExpTo) from T as T2 where T2.intSID=T1.intSID and Ord=2) Expr2,
    datediff(year, Min(ExpFrom) over(partition by intSID), Max(ExpTo) over(partition by intSID)) TotalExpr, Ord
    from T as T1
)x pivot (min(nvrCompany) for Ord IN ([1], [2])) as vh