表结构:
CREATE TABLE [dbo].[Exam](
    [S_date] [datetime] NOT NULL,
    [Order_Id] [varchar](50) NOT NULL,
    [Product_Id] [varchar](50) NOT NULL,
    [Amt] [numeric](18, 0) NOT NULL
) ON [PRIMARY]

题目一: 写一条Sql语句查询前出100到199的记录
题目二: 写一条Sql语句删除重复[除时间外的所有字段字段相同]的记录,保留重复记录中时间最大的记录
题目三: 一条Sql语句查出年份,1月,2月,3月....12月的订单总数列表
题目四: 一条sql语句查询出年份,本月销量,上月销量,环比%,去年同期销量,同比%列表

第一题比较简单,前面也讲过,就不赘述。
第二题:
delete from Exam where S_date not in (
    select e2.maxdt from
    ( 
         select Order_Id,Product_Id,Amt,MAX(S_date) as maxdt from Exam 
         group by Order_Id,Product_Id,Amt
    ) as e2
)
第三题:分两次组
select y,sum(c1) as m1,sum(c2) as m2,sum(c3) as m3,sum(c4) as m4,sum(c5) as m5,sum(c6) as m6,
sum(c7) as m7,sum(c8) as m8,sum(c9) as m9,sum(c10) as m10,sum(c11) as m11,sum(c12) as m12
from 
(
select 
  y, 
  case m when 1 then c else 0 end as c1,
  case m when 2 then c else 0 end as c2,
  case m when 3 then c else 0 end as c3,
  case m when 4 then c else 0 end as c4, 
  case m when 5 then c else 0 end as c5,
  case m when 6 then c else 0 end as c6,
  case m when 7 then c else 0 end as c7,
  case m when 8 then c else 0 end as c8,
  case m when 9 then c else 0 end as c9,
  case m when 10 then c else 0 end as c10,
  case m when 11 then c else 0 end as c11,
  case m when 12 then c else 0 end as c12
from 
  (
   select y,m,count(s_date) as c from 
   (
    select datepart(year,convert(DateTime,s_date)) as y,
     datepart(month,convert(DateTime,s_date)) as m ,
     s_date from exam
   )  as T1
   group by T1.y,T1.m 
  )
as T2
) as T3
group by T3.y

第三题:SQL相当复杂,不过只要理清了思路就很容易上手了,这里主要用到了left join on,语句如下:

select y1 年,m1 月,c1 本月销售额,
       c2 上月销售额,
       case when c2 is null or c2=0 then '无穷大' 
       else cast(cast((isnull(c1, 0)-isnull(c2,0))*100/isnull(c2, 0) as decimal(10,2)) as varchar(50))+'%' end as 环比增长 ,
       c3 去年本月销售额,
       case when c3 is null or c3=0 then '无穷大' 
       else cast(cast((isnull(c1, 0)-isnull(c3,0))*100/isnull(c3, 0) as decimal(10,2)) as varchar(50))+'%' end as 同比增长 
from 
(
select y1,m1,c1,c2,c3
from 
(
   select y1,m1,c1,c2 from 
   (
    select y1,m1,sum(Amt) as c1 from 
     (
          select datepart(year,convert(DateTime,s_date)) as y1,
                   datepart(month,convert(DateTime,s_date)) as m1 ,
                  Amt 
         from orders
     )  as T1
     group by T1.y1,T1.m1 
    ) o2

left join
   (
     select y2,m2,sum(Amt) as c2 from 
     (
         select datepart(year,convert(DateTime,s_date)) as y2,
                  datepart(month,convert(DateTime,s_date)) as m2 ,
                  Amt from orders
       )  as T1
       group by T1.y2,T1.m2
     ) o3
   on o2.y1 = o3.y2 and o2.m1 = o3.m2 - 1
  ) as o4 
  left join 
  (
     select y3,m3,sum(Amt) as c3 from 
     (
         select datepart(year,convert(DateTime,s_date)) as y3,
                  datepart(month,convert(DateTime,s_date)) as m3,
                  Amt from orders
      )  as T1
      group by T1.y3,T1.m3
   ) as o5
   on o4.y1 = o5.y3 - 1 and o4.m1 = o5.m3
) as o6

年   月    本月销售额           上月销售额   环比        去年同期   同比

2012 3     230                         420   -45.24%         NULL    无穷大
    2010 4     270                         NULL   无穷大          300      -10.00%
    2011 4     300                         353   -15.01%         420      -28.57%
    2012 4     420                         463   -9.29%           NULL     无穷大
    2011 5     353                         NULL   无穷大          463       -23.76%
    2012 5     463                         NULL   无穷大          NULL     无穷大
    2011 11    492                        240    105.00%        NULL    无穷大
    2011 12    240                        NULL    无穷大          NULL     无穷大

工欲善其事,必先利其器。
转载自:http://www.cnblogs.com/zhangzhu/archive/2012/05/10/2495034.html

几条复杂的SQL语句的更多相关文章

  1. 不同数据库,查询前n条数据的SQL语句

    不同的数据库,支持的SQL语法略有不同,以下是不同数据库查询前n条数据的SQl语句 SQL Server(MSSQL) SELECT TOP n * FROM table_name ORACLE SE ...

  2. sql插入多条数据的sql语句

    sql插入多条数据的sql语句 有三种方法:1.InSert Into <表名>(列名)Select <列名>From <源表名>如:INSERT INTO Ton ...

  3. Mybatis 删除多条数据XML SQL语句删除

    Mybatis 删除多条数据XML SQL语句删除 1.删除多条数据SQL写法 <delete id="deleteParamsByIds"> delete from ...

  4. “取出数据表中第10条到第20条记录”的sql语句+selecttop用法

    1.首先,select top用法: 参考问题 select top n * from和select * from的区别 select * from table -- 取所有数据,返回无序集合 sel ...

  5. “取出数据表中第10条到第20条记录”的sql语句+select top 使用方法

    1.首先.select top使用方法: 參考问题  select top n * from和select * from的差别 select * from table --  取全部数据.返回无序集合 ...

  6. 各数据库查询前N条记录的SQL语句

    sql在不同数据库查询前几条数据 1. ORACLE SELECT * FROM TABLE_NAME WHERE ROWNUM <= N;  HQL: from table_name t or ...

  7. 如何随机从数据库表中抽一条数据的SQL语句

    NewID() 方法返回一个 GUID,如:EE95A489-B721-4E8A-8171-3CA8CB6AD9E4 在 select 表的时候,再增加一列为 NewID() 就可以了. SQL 语句 ...

  8. mysql中删除重复记录,并保留重复数据中的一条数据的SQL语句

    正好想写一条删除重复语句并保留一条数据的SQL,网上查了一部分资料写的很详细,但还是在这里写下自己的理解,以遍后续学习 .如下: 表字段和数据: SQL语句: [sql] view plain cop ...

  9. 一条分页的SQL语句

    1.概述 在网页中如果显示的数据太多就会占据过多的页面,而且显示速度也会很慢.为了控制每次在页面上显示数据的数量,就可以利用分页来显示数据. 2.技术要点 在SQL Server中要实现SQL分页,需 ...

随机推荐

  1. JS 学习笔记--3--数据类型

    1.typeof 操作符 用来获取变量或者字面量的类型,也可以typeof(x);但是typeof并非内置函 数,而是一个操作符变量2.JS 一共6种类型 Undefined/Null/Boolean ...

  2. 2011斯坦福大学iOS应用开发教程学习笔记(第一课)MVC.and.Introduction.to.Objective-C

    blog.csdn.net/totogo2010/article/details/8205810  目录(?)[-] 第一课名称 MVC and Introduction to Objective-C ...

  3. 4-Highcharts 3D图之3D普通饼图

    <!DOCTYPE> <html lang='en'> <head> <title>4-Highcharts 3D图之3D普通饼图</title& ...

  4. VC5509的通用GEL代码

    GEL是通用扩展语言(General Extension Language)的英文缩写,GEL是一个大小写敏感但缺少类型检测的解释性语言,只有int类型,在语法上可看作是C语言的一个子集.GEL主要用 ...

  5. Hello world,Hello 2015,Bye 2014

    序 在我写下“在”这个字的时候已经是2014-12-31 19:59,14年最后一天了,总觉得不写点东西祭奠一下那些被自己虐死的脑细胞,心里就不舒服. 那就从生活,工作,学习三个方面,总结一下吧. 生 ...

  6. 谈谈arm下的函数栈

    引言 这篇文章简要说说函数是怎么传入参数的,我们都知道,当一个函数调用使用少量参数(ARM上是少于等于4个)时,参数是通过寄存器进行传值(ARM上是通过r0,r1,r2,r3),而当参数多于4个时,会 ...

  7. chrome 网络面板

    Chrome Timeline的指标说明:Blocked.Connect.Send.Wait.Receive Blocked time includes any pre-processing time ...

  8. (转)Eclipse平台技术概述

    转载:周金根 http://zhoujg.blog.51cto.com/1281471/516833    Eclipse:Eclipse平台技术概述 2010-10-19 13:35:00 标签:E ...

  9. iOS视频录制裁剪合成

    网址链接: 视频裁剪合并:http://blog.sina.com.cn/s/blog_64ea868501018jx3.html 视频之定义裁剪高宽度:http://www.cocoachina.c ...

  10. sql server 函数

    1.Fun_Split 分割字符串,形成返回表 CREATE FUNCTION [dbo].[Fun_Split] ( @Items VARCHAR(MAX), @SplitStr VARCHAR(M ...