1.
表A
月份 部门编号 业绩
一月 01 34
一月 02 55
二月 03 21
二月 02 78
三月 03 553
表B
部门编号 部门名称
01 财政部
02 门市部
03 销售部
根据表A表B得出 表C
部门名称 一月 二月 三月
2.表T有A,B,C(int类型)三列,求查询语句输出一列,每行中 A>B输出A,如果B>C为B,其他为C3.有一张会员表有Id,Name,CreateTime,Vip四个字段,其中Vip有两个状态yes与no,yes表是高级会员,no表示普通会员,要求的结果是:最早创建的高级会员与普通会员各五名,并且高级会员要排在普通会员前面4.表A 
编号 职位名称 第二职位
01 员工
02 技术支持 03
03 经理 01
04 老总 02
求  编号,职位名称,第二职位名称

解决方案 »

  1.   

    --第一题
    EXEC   sp_dbcmptlevel 数据库名,90declare @t1 table(月份 varchar(10),部门编号 varchar(10),业绩 int)
    insert into @t1 select '一月','01',34
    union all
    select '一月','02',55
    union all
    select '二月','03',21
    union all
    select '二月','02',78
    union all
    select '三月','03',553declare @t2 table(部门编号 varchar(10),部门名称 varchar(20))
    insert into @t2 
    select '01','财政部'
    union all
    select '02','门市部'
    union all
    select '03','销售部'
    select * from
    (
    select tb.部门名称,ta.月份,ta.业绩
    from @t1 ta,@t2 tb
    where ta.部门编号=tb.部门编号
    ) tb
    pivot
    (
    sum(业绩)
    for 月份 in ([一月],[二月],[三月])
    ) as pt
    /*(5 行受影响)(3 行受影响)
    部门名称                 一月          二月          三月
    -------------------- ----------- ----------- -----------
    财政部                  34          NULL        NULL
    门市部                  55          78          NULL
    销售部                  NULL        21          553(3 行受影响)*/ 
      

  2.   

    --1.
    declare @表A table([月份] nvarchar(2),[部门编号] nvarchar(2),[业绩] int)
    Insert @表A
    select N'一月',N'01',34 union all
    select N'一月',N'02',55 union all
    select N'二月',N'03',21 union all
    select N'二月',N'02',78 union all
    select N'三月',N'03',553
    --Select * from @表A
     
    declare @表B table([部门编号] nvarchar(2),[部门名称] nvarchar(3))
    Insert @表B
    select N'01',N'财政部' union all
    select N'02',N'门市部' union all
    select N'03',N'销售部'
    --Select * from @表BSELECT B.[部门名称] , 
           SUM(CASE WHEN [月份] ='一月' THEN [业绩] ELSE 0 END) AS '一月',
           SUM(CASE WHEN [月份] ='二月' THEN [业绩] ELSE 0 END) AS '二月',
           SUM(CASE WHEN [月份] ='三月' THEN [业绩] ELSE 0 END) AS '三月'
    FROM @表A A 
    LEFT JOIN @表B B ON B.[部门编号] = A.[部门编号]
    GROUP BY B.[部门名称]
    /*
    部门名称 一月          二月          三月
    ---- ----------- ----------- -----------
    财政部  34          0           0
    门市部  55          78          0
    销售部  0           21          553
    */
    --2.
    SELECT 
    CASE WHEN A>B THEN A 
         WHEN B>C THEN B
         ELSE C END AS COL
    FROM T--3.
    SELECT TOP 5 Id,[Name],CreateTime,Vip FROM @tb WHERE Vip ='yes'
    UNION ALL
    SELECT TOP 5 Id,[Name],CreateTime,Vip FROM @tb WHERE Vip ='no'
    --4.
    SELECT [编号], [职位名称], [第二职位] FROM 表A
      

  3.   

    以下是鄙人的第四题答案:
    select a1.编号 a1.职位名称 a2.职位名称 as 第二职位名称from A as a1 , A as a2where a1.第二职位 = a2.编号 
      

  4.   


    --第二题select case when A>B then A 
    when B>C then B
    else C
    end from 表
      

  5.   

    我也是刚学SQL,试着写一下,仅供参考,有好的方法,共同交流^_^第一题:
    ---------------------------创建a表
    create table a(monthy char(4),depa char(4),score int)
    insert a
    select '一月','01',34 union all
    select '一月','02',55 union all
    select '二月','03',21 union all
    select '二月','02',78 union all
    select '三月','03',55 
    ---------------------------创建b表
    create table b(depa char(4),depaname varchar(6))
    insert b
    select '01','财政部'union all
    select '02','门市部'union all
    select '03','销售部'
    ----------------------------查询语句
    select distinct b.depaname ,一月=isnull((select sum(score)from a where monthy='一月'and a.depa=b.depa),0),
           二月=isnull((select sum(score)from a where monthy='二月'and a.depa=b.depa),0),
           三月=isnull((select sum(score)from a where monthy='三月'and a.depa=b.depa),0)
    from b,a
    where b.depa=a.depa
    ----------------------------结果如下:depaname 一月          二月          三月          
    -------- ----------- ----------- ----------- 
    财政部      34          0           0
    门市部      55          78          0
    销售部      0           21          55(所影响的行数为 3 行)
      

  6.   


    --第三题
    select * from 
    (
    SELECT TOP 5 Id,[Name],CreateTime,Vip FROM 表 WHERE Vip ='yes'
    UNION ALL
    SELECT TOP 5 Id,[Name],CreateTime,Vip FROM 表 WHERE Vip ='no'
    ) order by (case when Vip='yes' then 2 else 0 end ) desc, CreateTime
      

  7.   


    --第四题
    select ta.编号,ta.职位名称,第二职位名称=(select 职位名称 from 表 where 编号=ta.第二职位)
    from 表 ta
      

  8.   

    我的第四题是错的,没有看到最后第二职位名称哪个名称.
    zlp321002这个答案是正确的.
      

  9.   

    汇总一下罢--1 
    --表A 
    --月份 部门编号 业绩 
    --一月 01 34 
    --一月 02 55 
    --二月 03 21 
    --二月 02 78 
    --三月 03 553 
    --表B 
    --部门编号 部门名称 
    --01 财政部 
    --02 门市部 
    --03 销售部 
    --根据表A表B得出 表C 
    --部门名称 一月 二月 三月 
    --创建数据
    create table t1([月份] nvarchar(2),[部门编号] nvarchar(2),[业绩] int)
    Insert t1
    select N'一月',N'01',34 union all
    select N'一月',N'02',55 union all
    select N'二月',N'03',21 union all
    select N'二月',N'02',78 union all
    select N'三月',N'03',553
    --Select * from t1
     
    create table t2([部门编号] nvarchar(2),[部门名称] nvarchar(3))
    Insert t2
    select N'01',N'财政部' union all
    select N'02',N'门市部' union all
    select N'03',N'销售部'
    --select * from t2--执行
    declare @sqlstr varchar(8000)
    select  @sqlstr = ''
    select  @sqlstr = @sqlstr +',sum(case 月份 when '''+月份+''' then 业绩 else 0 end) as '+ 月份 
    from t1 
    group by 月份
    exec('select t2.部门名称'+@sqlstr+' from t1 left join t2 on t1.部门编号=t2.部门编号 group by t2.部门名称')
    --drop table t1,t2
    --2.表T有A,B,C(int类型)三列,求查询语句输出一列,每行中 A>B输出A,如果B>C为B,其他为C 
    --创建数据
    create table t3(A int,B int,C int)
    insert into t3 select 1,2,3
    union all
    select 6,5,14
    union all
    select 4,8,6
    --select * from t3--执行
    select case when A>B then A when B>C then B else C end as Col from t3--drop table t3--3.有一张会员表有Id,Name,CreateTime,Vip四个字段,其中Vip有两个状态yes与no,yes表是高级会员,no表示普通会员
    --要求的结果是:最早创建的高级会员与普通会员各五名,并且高级会员要排在普通会员前面 
    --创建数据
    create table t4([Id] varchar(3),[Name] varchar(20),[CreateTime] datetime,[Vip] varchar(3))
    insert into t4 
    select '06','','2003-05-23','yes' union all
    select '07','well','2005-12-11','yes' union all
    select '01','m001','2007-02-13','yes' union all
    select '03','mtob','2008-01-14','yes' union all
    select '05','joys','2007-09-09','no' union all
    select '02','yhou','2006-12-19','no' union all
    select '04','feio','2002-10-06','no' union all
    select '08','feif','2001-03-21','no' 
    --select * from t4--执行
    declare @t4 table([Id] varchar(3),[Name] varchar(20),[CreateTime] datetime,[Vip] varchar(3))
    insert into @t4 
    select top 2 [Id],[Name],[CreateTime],[Vip] from t4 where [Vip]='yes'  order by [CreateTime] asc
    insert into @t4
    select top 2 [Id],[Name],[CreateTime],[Vip]  from t4 where [Vip]='no' order by [CreateTime] asc 
    select * from @t4 order by (case when [Vip]='yes' then 2 else 0 end) desc,[CreateTime]--drop table t4--4.表A 
    --编号 职位名称 第二职位 
    --01 员工 
    --02 技术支持 03 
    --03 经理 01 
    --04 老总 02 
    --求  编号,职位名称,第二职位名称  
    --创建数据
    create table t5(编号 varchar(2),职位名称 varchar(20),第二职位 varchar(2))
    insert into t5 select '01','员工',''
    union all
    select '02','技术支持','03'
    union all
    select '03','经理','01' 
    union all
    select '04','老总','02' 
    --select * from t5--执行
    select a.编号,a.职位名称,b.职位名称 as 第二职位名称 from  t5 a left join t5 b on a.第二职位=b.编号--drop table t5
      

  10.   

    虽然top 是取最早插入的记录,也不能排除修改数据的可能,还是加上order by [CreateTime] asc保险些,11楼考虑全面
      

  11.   

    感谢大家对本帖的支持,再说声谢谢!又有一道面试题:表A 
    有两列   姓名   ,工资             小王         2000 
                小张         1200 
                小非         2000 
                小赵         5000 问题是找出工资排第二的人(包含重复)。 请大家用标准的SQL写,不要用针对那种数据库的SQL
      

  12.   

    请教
    11楼写的插入语句
    insert into t5 select '01','员工',''
    union all
    select '02','技术支持','03'与 insert into t5 
    values('02','技术支持','03')有什么区别吗?
      

  13.   

    11楼第3题
    select * from @t4 order by (case when [Vip]='yes' then 2 else 0 end) desc,[CreateTime]
    case when [Vip]='yes' then 2 else 0 end这句怎么理解呀?
      

  14.   


    -->生成测试数据
     
    declare @tb table([姓名] nvarchar(2),[工资] int)
    Insert @tb
    select N'小王',2000 union all
    select N'小张',1200 union all
    select N'小非',2000 union all
    select N'小赵',5000
    Select * from @tb where 工资 = (select top 1 [工资] from @tb where [工资]< (select max([工资]) from @tb))
    /*
    姓名   工资
    ---- -----------
    小王   2000
    小非   2000
    */
      

  15.   

    -->生成测试数据
    declare @tb table([姓名] nvarchar(2),[工资] int)
    Insert @tb
    select N'小马',1500 union all
    select N'小王',2000 union all
    select N'小张',1200 union all
    select N'小非',2000 union all
    select N'小赵',5000Select * from @tb 
    where 工资= (select top 1 [工资] from @tb where [工资]< (select max([工资]) from @tb) order by [工资] desc)
    /*
    姓名  工资
    ---- -----------
    小王  2000
    小非  2000
    */
      

  16.   

    题一
    create table mobt(monthname varchar(10),deptno varchar(10),业绩 int)
    insert into mobt select '1','01',34
    union all
    select '1','02',55
    union all
    select '2','03',21
    union all
    select '2','02',78
    union all
    select '3','03',553
    create table dept(deptno varchar(10),dname varchar(20))
    insert into dept 
    select '01','fiscal'
    union all
    select '02','city'
    union all
    select '03','sal'
    select * from mobt
    select * from deptthnam
    truncate table deptno
    select dname,[1] as 一,[2] as 二,[3] as 三 from(
    select a.dname,b.monthname,sum([业绩]) as dmt from dept a,mobt b where a.deptno=b.deptno group by a.dname,b.monthname) as test
    pivot (
    min(dmt) for monthname in ([1],[2],[3])
    )as piv
      

  17.   

    题二
    create table t4([Id] varchar(3),[Name] varchar(20),[CreateTime] datetime,[Vip] varchar(3))
    insert into t4 
    select '09','','2004-05-23','yes' union all
    select '10','well','2003-12-11','yes' union all
    select '11','m001','2003-02-13','yes' union all
    select '13','mtob','2004-01-14','yes' union all
    select '15','joys','2006-09-09','no' union all
    select '12','yhou','2002-12-19','no' union all
    select '14','feio','2005-10-06','no' union all
    select '18','feif','2001-03-21','no' union all
    select '06','','2003-05-23','yes' union all
    select '07','well','2005-12-11','yes' union all
    select '01','m001','2007-02-13','yes' union all
    select '03','mtob','2008-01-14','yes' union all
    select '05','joys','2007-09-09','no' union all
    select '02','yhou','2006-12-19','no' union all
    select '04','feio','2002-10-06','no' union all
    select '08','feif','2001-03-21','no' select * from t4
    select id,name,Createtime,vip from (
    select id,name,Createtime,vip,rank()over (partition by VIp order by Createtime desc) as run from t4

    )as tt where run<6 order by vip desc
      

  18.   

    表A 
    有两列  姓名  ,工资             小王        2000 
                小张        1200 
                小非        2000 
                小赵        5000 问题是找出工资排第二的人(包含重复)。 如果我要的是排名第三或第四的呢?Select * from @tb where 工资 = (select top 1 [工资] from @tb where [工资]< (select max([工资]) from @tb))
    这样的写法就不行了
      

  19.   

    排名的时候你加RANK就行 
      

  20.   

    可惜没有分啊!................
    /*
    表A
    月份 部门编号 业绩
    一月 01 34
    一月 02 55
    二月 03 21
    二月 02 78
    三月 03 553
    表B
    部门编号 部门名称
    01 财政部
    02 门市部
    03 销售部
    根据表A表B得出 表C
    部门名称 一月 二月 三月 
    */
    create table tableA
    (
      tbmonth varchar(20),
      tbdepartment varchar(20),
      tbscore int
    )
    insert into tableA select '一月','01',34
    insert into tableA select '一月','02',55
    insert into tableA select '二月','03',21
    insert into tableA select '二月','02',78
    insert into tableA select '三月','03',553create table tableB
    (
      tbdepart varchar(20),
      tbdepartname varchar(20)
    )
    insert into tableB select '01','财政部'
    insert into tableB select '02','门市部'
    insert into tableB select '03','销售部'
    select B.tbdepartname,
    sum(case when A.tbmonth='一月' then tbscore else 0 end) as '一月',
    sum(case when A.tbmonth='二月' then tbscore else 0 end) as '二月',
    sum(case when A.tbmonth='三月' then tbscore else 0 end) as '三月'
    from tableB B,tableA A where B.tbdepart=A.tbdepartment group by B.tbdepartname
      

  21.   

    第二题太简单了
    做第四题
    /*
    4.表A
    编号 职位名称 第二职位
    01 员工
    02 技术支持 03
    03 经理 01
    04 老总 02
    求  编号,职位名称,第二职位名称 
    */
    create table ta
    (
      tid varchar(10),
      tname varchar(10),
      td varchar(10)
    )
    insert into ta select '01','员工',''
    insert into ta select '02','技术支持','03'
    insert into ta select '03','经理','01'
    insert into ta select '04','老总','02'
    select tid as'编号',tname as '职位',(select tname from ta  where tid=tt.td) as '第二职业'
    from ta as tt
    /*01 员工 NULL
    02 技术支持 经理
    03 经理 员工
    04 老总 技术支持*/
      

  22.   

    [align=center]LZ
    你可以非常满意的接蛋贴了
    下次发帖的时候
    多给点分我
    知道吗?.....................
    我睡觉去的明天要上班
    [/align]
      

  23.   

    create table t1([月份] nvarchar(2),[部门编号] nvarchar(2),[业绩] int)
    Insert t1
    select N'一月',N'01',34 union all
    select N'一月',N'02',55 union all
    select N'二月',N'03',21 union all
    select N'二月',N'02',78 union all
    select N'三月',N'03',553
    --Select * from t1
     
    create table t2([部门编号] nvarchar(2),[部门名称] nvarchar(3))
    Insert t2
    select N'01',N'财政部' union all
    select N'02',N'门市部' union all
    select N'03',N'销售部'declare @sql  nvarchar(4000)
    select @sql='select 部门名称'
    select @sql=@sql+','''+[月份]+'''=sum(case when 月份='''+[月份]+''' then 业绩 else null end)' from (select distinct [月份] from t1 )A 
    select @sql=@sql+' from (select 部门名称,月份,业绩  from t1,t2 where t1.部门编号=t2.部门编号)B group by 部门名称 order by 1 '
    print @sql
    exec (@sql)SELECT 
    CASE WHEN A>B THEN A 
         WHEN B>C THEN B
         ELSE C END AS COL
    FROM Tselect * from 表 where id in (select top 5 id  from 表 A where a.vip=表.vip order by createtime)declare @a table (编号 nvarchar(10),职位名称 nvarchar(100),第二职位 nvarchar(10))
    insert into @a select '01','员工',null  union select '02','技术支持','03' union
    select '03','经理','01' union select '04','老总','02'
    select * from @a 
    select 编号,职位名称,第二职位名称=(select isnull(职位名称,'无') from @a a where a.编号 =b.第二职位) from @a b
      

  24.   

    请问一下,
    1. 
    表A 
    月份 部门编号 业绩 
    一月 01 34 
    一月 02 55 
    二月 03 21 
    二月 02 78 
    三月 03 553 
    表B 
    部门编号 部门名称 
    01 财政部 
    02 门市部 
    03 销售部 
    根据表A表B得出 表C 
    部门名称 一月 二月 三月 能不能用标准SQL写出来?
    表A 
    有两列  姓名  ,工资             小王        2000 
                小张        1200 
                小非        2000 
                小赵        5000 问题是找出工资排第二的人(包含重复)。 如果我要的是排名第三或第四的呢?Select * from @tb where 工资 = (select top 1 [工资] from @tb where [工资] < (select max([工资]) from @tb)) 
    这样的写法就不行了
      

  25.   

    题一为什么你们都是这样写的呢?如果数据一万条怎么办
    select 部门名称,
            (select 业绩 from a where 部门编号= b.部门编号 and 月份= '一月') as '一月',
            isnull((select 业绩 from a where a.部门编号= b.部门编号 and 月份 = '二月'),0) as '二月',
    isnull((select 业绩 from a where a.部门编号= b.部门编号 and 月份 = '三月'),0) as '三月'
    into c
    from   b
      

  26.   

    -----------------------------------------------
    create  table tb([name] nvarchar(2),[salary] int)
    Insert tb
    select N'小王',1200 union all
    select N'小张',18000 union all
    select N'小非',5000 union all
    select N'小赵',5000
    用存储过程和游标可以查询各个名次的工资
    -------------------------------------------------------------------
    create procedure p_salary
      @num int,
       @outcursor cursor varying output
     as
       declare @salary int
       set @outcursor = cursor scroll for
       select distinct  salary 
       from tb
       order by salary desc 
       open @outcursor
      fetch absolute  @num from @outcursor into @salary
       select [name],salary from tb where salary = @salary
      close @outcursor
      deallocate @outcursor 
    ------------------------------------------------
    调用游标
    ------------------------------------------------
    declare @output cursor 
    execute p_salary 1,@output output
      

  27.   

    1.
    select 部门名称,一月=max(case when 月份='一月' then 业绩 else 0 end),
                   二月=max(case when  月份='二月' then 业绩 else 0 end),
                   三月=max(case when  月份='三月' then 业绩 else 0 end)
    from t1 join t2 on t1.部门编号=t2.部门编号 group by 部门名称
      

  28.   

    2.2.表T有A,B,C(int类型)三列,求查询语句输出一列,每行中 A>B输出A,如果B>C为B,其他为C 
    if object_id('t3') is not null
    drop  table t3
    go
    create table t3 (A int ,B int,C int)
    insert t3 
    select 23,43,52 
    union all
    select 24,23,42select  a=case when A>B then A when B>C then B else  C  end from t3
      

  29.   

    3.有一张会员表有Id,Name,CreateTime,Vip四个字段,其中Vip有两个状态yes与no,yes表是高级会员,no表示普通会员,要求的结果是:最早创建的高级会员与普通会员各五名,并且高级会员要排在普通会员前面 
    select * from 会员表 where vip='yes' order by createtime 
    union all
    select * from 会员表 where vip='no' order by createtime
      

  30.   

    var result = from c in db.month
                             group c by c.department.Name into gr                         select new 
                             {
                               部门 = gr.Key,
                               一月 = Convert.ToBoolean(((gr.SingleOrDefault(p => p.months == "一月")).volume)) ? (gr.SingleOrDefault(p => p.months == "一月").volume):0,
                               二月 = Convert.ToBoolean(((gr.SingleOrDefault(p => p.months == "二月")).volume)) ? (gr.SingleOrDefault(p => p.months == "二月").volume):0,
                               三月 = Convert.ToBoolean(((gr.SingleOrDefault(p => p.months == "三月")).volume)) ? (gr.SingleOrDefault(p => p.months == "三月").volume):0
                             };