表的字段非常简单,创建表的sql语句如下:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Exam]') AND type in (N'U'))
BEGIN
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]
END
题目一: 写一条Sql语句查询前出100到199的记录
题目二: 写一条Sql语句删除重复[除时间外的所有字段字段相同]的记录,保留重复记录中时间最大的记录
题目三: 一条Sql语句查出年份,1月,2月,3月....12月的列表
题目四: 一条sql语句查询出年份,本月销量,上月销量,环比%,去年同期销量,同比%列表
-- 题一 一条Sql语句 查询前100到199的记录
select top 100 * from exam
where s_date<
(
select min(T.s_date) from ( select top 99 s_date from exam order by s_date desc ) as T
)
order by s_date desc
--
-- 题二 一条Sql语句 删除重复的记录[时间不重复,其它字段重复]
delete from exam
where s_date not in
(
select T.dt from
(
select order_id,product_id,amt,max(s_date) as dt from exam group by order_id,product_id,amt
) as T
)
--
--题三 一条Sql语句 查年份,1月,2月....12月
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实现一个功能:
如果不足8条纪录,添加至8条纪录。
超过8条纪录,剪切至8条纪录。
假设a表有列c1, c2, c3
select * from
(select c1, c2, c3 from a
union all
select null, null, null from all_objects where rownum<9
)
where rownum < 9;
------------------------------------------------------------
分享到:
相关推荐
SQL2KSP4 instcat.sqlSQL2KSP4 instcat.sqlSQL2KSP4 instcat.sqlSQL2KSP4 instcat.sql
sqlce20sql2ksp3a sqlce20sql2ksp3a sqlce20sql2ksp3a
SQl补丁chs_sql2ksp3.exe文件
chs_sql2ksp3
chs_sql2ksp3.exe
Microsoft® SQL Server™ 2000 安装程序创建新的 SQL Server 2000 安装或升级早期版本
Sql2.Train OfChina信息.sql,Sql2.Train OfChina信息.sql Sql2.Train OfChina信息.sql Sql2.Train OfChina信息.sql
Sql2Employee信息.sql Sql2Employee信息.sql Sql2Employee信息.sql Sql2Employee信息.sql
sql2查询汇总信息.sql sql2查询汇总信息.sql sql2查询汇总信息.sql sql2查询汇总信息.sql sql2查询汇总信息.sql
1、数据库客户端工具A5 sql 2、数据一览里可以根据注释直观的显示列明,可以通过表注释过滤表
SQL Server 2016 SP2 补丁 SQLServer2016SP2-KB4052908-x64-CHS
北大青鸟sql1.增加学生记录.sql 北大青鸟sql1.增加学生记录.sql 北大青鸟sql1.增加学生记录.sql
Microsoft's SQL Server 2008 -- this introductory guide will get you up and running with SQL quickly. Whether you need to write database applications, perform administrative tasks, or generate reports,...
sql大全sql大全sql大全sql大全sql大全sql大全sql大全sql大全sql大全sql大全sql大全sql大全sql大全sql大全sql大全sql大全sql大全sql大全sql大全sql大全sql大全sql大全sql大全sql大全sql大全sql大全sql大全sql大全sql...
sql 语句学习 sql sql sqlsql 语句学习 sql sql sql
Microsoft SQL Server 2000 Service Pack 3a复制组件(sql2kxxsp3a.msi)在IIS机器中安装 SQL Server CE 2.0 及 SQL Server 2000 SP4 复制组件。 该组件用于把移动设备中的SQL Server CE 2.0数据库连接到SQL Server ...
深入浅出sql headfist sql 第二部分
SQL 基础 SQL 首页 SQL 简介 SQL 语法 SQL select SQL distinct SQL where SQL AND & OR SQL Order By SQL insert SQL update SQL delete SQL 高级 SQL Top SQL Like SQL 通配符 SQL In SQL Between ...
Learning SQL, 2nd Edition by Alan Beaulieu,英文原版书籍