/**
第一题:
表中有以下資料
季別客戶款式單號品名
网络安全文章11Q3 FIL 25131640 10-0646 男梭織運動上衣
11Q3 FIL 25131640 10-0655 男梭織運動上衣(兩面穿)
SP10 HIN 8023609-0155 女平針背心
SP10 HIN 8023610-0120 女背心
FA09 HIN 8022109-0059 男平織上衣
FA09 HIN 8022109-0058 男針織上衣
怎麼得出的最大單號,結果為:
11Q3 FIL 25131640 10-0655 男梭織運動上衣(兩面穿)
SP10 HIN 8023610-0120 女背心
FA09 HIN 8022109-0059 男平織上衣
**/
--code:
--create tabletb
--(JB varcha r(20),
-- KH varcha r(20),
-- KS varcha r(20),
-- DH varcha r(20),
-- PM nvarch ar(50))
-
- inrt into tb
-- lect '11Q3','FIL','25131640','10-0646','男梭織運動上衣' unionall --lect '11Q3','FIL', '25131640', '10-0655', '男梭織運動上衣(兩面穿)' unionall
--lect 'SP10', 'HIN', '80236', '09-0155', '女平針背心' unionall
--lect 'SP10', 'HIN', '80236', '10-0120', '女背心' unionall
--lect 'FA09', 'HIN', '80221', '09-0059', '男平織上衣' unionall
--lect 'FA09', 'HIN', '80221', '09-0058', '男針織上衣'
--SELECT * FROM TB
--lect * from tb whereDH in (lect max(DH)from tb groupby JB)
/**
第二题:
--订单号,商品分类id,商品sku,销售数量
orderu id,classi d,sku,amount
so01,1,a1,10
so02,1,a2,5心理辅导记录
so03,1,a3,15
so04,2,b1,10
so05,2,b2,20
so06,2,b3,30
so07,3,c1,10
so08,3,c2,5
so09,3,c3,10
so10,4,d2,1
so11,1,a2,10 (不同的订单购商品明细一样)
so11,3,c2,5 (不同的订单购商品明细一样)
要求:按销售量统计,按分类分组,取销售前位
结果(横着排sql):
classi d,sku1,sku2
1,a2,a3
2,b2,b3
3,c1,c2
4,d2
结果(竖着排sql2)
classi d,sku
1,a2
1,a3
2,b2
2,b3
3,c1
3,c2
4,d2
CREATE TABLEGoods
(orderu id VARCHA R(10),
classi d INT,
sku VARCHA R(10),
amount INT)
GO
INSERT INTO GoodsVALUES('so01',1,'a1',10) INSERT INTO GoodsVALUES('so02',1,'a2',5) INSERT INTO GoodsVALUES('so03',1,'a3',15) INSERT INTO GoodsVALUES('so04',2,'b1',10) INSERT INTO GoodsVALUES('so05',2,'b2',20) INSERT INTO GoodsVALUES('so06',2,'b3',30) INSERT INTO GoodsVALUES('so07',3,'c1',10) INSERT INTO GoodsVALUES('so08',3,'c2',5)
INSERT INTO GoodsVALUES('so09',3,'c3',10)
INSERT INTO GoodsVALUES('so10',4,'d2',1)
INSERT INTO GoodsVALUES('so11',1,'a2',10)
INSERT INTO GoodsVALUES('so11',3,'c2',5)
解法一:
u studen ts3
;
with c as
(
lect sum(amount) as sums,classi d,sku
from Goods
属猴今日财运groupby classi d,sku)
lect classi d,sku
from(lect classi d,sku,row_nu mber() over(partit ion by classi d orderby sums desc) as row from c) t
whererow=1 or row=2
解法二:
u studen ts3
go
drop table#temp
lect sum(amount) as sum,classi d,sku into #temp from Goodsgroupby classi d,sku人人bt
lect classi d,sku from (lect classi d,sku ,row_nu mber()over(partit ion by classi d orderby sum desc)as rownum from #temp )t
whererownum=1 or rownum=2
**/
第三题:
-- 要查询出列C# 01课程比课程高信息
-- 表
/***
S# C# SCORE
01 01 80.0
01 02 90.0
01 03 99.0
02 01 70.0
02 02 60.0
02 03 80.0
03 01 80.0
素炒菠菜03 02 80.0
03 03 80.0
04 01 50.0
04 02 30.0
04 03 20.0
清淡饮食05 01 76.0
05 02 87.0
06 01 31.0
06 03 34.0
07 02 89.0
07 03 98.0
--想要输出的结果
S# C# SCORE
02 01 70.0
02 02 60.0
02 03 80.0
04 01 50.0
04 02 30.0
04 03 20.0
........
情感性
表
create tableSC(S# varcha r(10),C# varcha r(10),scoredecima l(18,1)) inrt into SC values('01' , '01' , 80)
inrt into SC values('01' , '02' , 90)
inrt into SC values('01' , '03' , 99)
inrt into SC values('02' , '01' , 70)
inrt into SC values('02' , '02' , 60)
inrt into SC values('02' , '03' , 80)
inrt into SC values('03' , '01' , 80)
inrt into SC values('03' , '02' , 80)
inrt into SC values('03' , '03' , 80)
inrt into SC values('04' , '01' , 50)
inrt into SC values('04' , '02' , 30)
inrt into SC values('04' , '03' , 20)
inrt into SC values('05' , '01' , 76)
inrt into SC values('05' , '02' , 87)
inrt into SC values('06' , '01' , 31)
inrt into SC values('06' , '03' , 34)
inrt into SC values('07' , '02' , 89)
inrt into SC values('07' , '03' , 98)
go
解法一:
SELECT *
FROM SC AS a
WHERE
NOT EXISTS(SELECT 1 FROM SC AS b WHERES#='01' AND NOT EXISTS(SELECT 1 FROM SC WHERES#=a.S# AND C#=b.C# AND score<b.score)
)
解法二:
lect * from SC whereS# in(
lect S# from SC a whereC#='01' and exists(lect 1 from Sc b wherea.s#=b.s# and a.score>b.scoreand b.C#='02')
)
u studen ts3
lect * from SC whereS# in
(lect S# from SC as a whereC#='01'and exists (lect S# from SC as b wherea.S#=b.S# and a.score>b.scoreand b.C#='02' ))
***/
/**
第四题:
我想建一个通过传递参数创建表的存储过程,现在存储过程创建好了,但是用命令执行的时候总出错,代码如下:
create proc proc1
(
@tnamevarcha r(50)
)
AS
BEGIN
Declar e @tablen ame As varcha r(50),@sql As varcha r(5000)
t @tablen ame = @tname
t @sql='create table'+@tablen ame+'(id int IDENTI TY (1,1) PRIMAR Y KEY ,unamenVarCh ar(50) null)'
exec(@sql)
END
一个一个造句这个是存储过程,存储过程本身是没有问题的
我执行的时候用exec dbo.proc1'test'可以实现,但execdbo.proc1'123'就不行
会报错说
消息102,级别15,状态1,第1 行