研究了一个星期了,没做的出来,希望路过这里的大侠想想办法,可能讲不清楚,最好提供一下实现的SQL代码, 谢谢!
要求如下: 从SQL中自带的Northwind数据库的Products表中
找出各种产品类别(CatagoryID)中库存数量(UnitsInStock)在前3位的
产品的代号(ProductID)和名称(ProductName)。
(提示,需要使用临时表和游标)
唉 小弟困惑了一星期 .... 万望相助 3Q! 有请大侠!
要求如下: 从SQL中自带的Northwind数据库的Products表中
找出各种产品类别(CatagoryID)中库存数量(UnitsInStock)在前3位的
产品的代号(ProductID)和名称(ProductName)。
(提示,需要使用临时表和游标)
唉 小弟困惑了一星期 .... 万望相助 3Q! 有请大侠!
解决方案 »
- 昨天去面试,对面非说1000W条数据分页查询要2秒以上,贴测试结果。
- 求一条updatesql语句。希望你们能进来帮帮我。谢谢你们了。
- 求一条更新数据的sql语句
- 配置过复制又删除复制后,数据库不能分离?
- Excel 导入到Sql,同时,想用变量设定文件路径。发生错误,求助
- 怎么得到日期
- 打开SQL后,提示要联接服务器,但是我就一台电脑,怎办呢?请高手赐都!
- 如何用sql语句检查email字段的有效(是否是有效的email)?
- 批删除时有问题
- 请问哪里有ms sql7中各种数据类型的详细描述啊?
- 這句sql怎麼寫,有點算法哦
- 打开一个ASP站点后,SQLServer进程持续占用90%以上和内存持续上涨N百兆
(select top 3 CategoryID , UnitsInStock = sum(UnitsInStock) from Products group by CategoryID order by UnitsInStock desc) n
where m.CategoryID = n.CategoryID
order by m.CategoryID/*
ProductID ProductName
----------- ----------------------------------------
1 Chai
2 Chang
24 Guaraná Fantástica
34 Sasquatch Ale
35 Steeleye Stout
38 Côte de Blaye
39 Chartreuse verte
43 Ipoh Coffee
67 Laughing Lumberjack Lager
70 Outback Lager
75 Rhönbräu Klosterbier
76 Lakkalikööri
3 Aniseed Syrup
4 Chef Anton's Cajun Seasoning
5 Chef Anton's Gumbo Mix
6 Grandma's Boysenberry Spread
8 Northwoods Cranberry Sauce
15 Genen Shouyu
44 Gula Malacca
61 Sirop d'érable
63 Vegie-spread
65 Louisiana Fiery Hot Pepper Sauce
66 Louisiana Hot Spiced Okra
77 Original Frankfurter grüne Soße
10 Ikura
13 Konbu
18 Carnarvon Tigers
30 Nord-Ost Matjeshering
36 Inlagd Sill
37 Gravad lax
40 Boston Crab Meat
41 Jack's New England Clam Chowder
45 Rogede sild
46 Spegesild
58 Escargots de Bourgogne
73 Röd Kaviar(所影响的行数为 36 行)*/
select catagoryid, row_number() over (order by catagoryid desc) as SubSeq, unitsinstock from products
row_number() over (partition by categoryid order by unitsinstock desc) as subseq,
unitsinstock into #
from products/*
categoryid subseq unitsinstock
----------- -------------------- ------------
1 1 125
1 2 111
1 3 69
1 4 57
1 5 52
1 6 39
1 7 20
1 8 20
1 9 17
1 10 17
1 11 17
1 12 15
2 1 120
2 2 113
2 3 76
2 4 53
2 5 39
2 6 32
2 7 27
2 8 24
2 9 13
2 10 6
2 11 4
2 12 0
3 1 76
3 2 65
3 3 49
3 4 40
3 5 36
3 6 29
3 7 25
3 8 17
3 9 15
3 10 15
3 11 10
3 12 6
3 13 3
4 1 112
4 2 86
4 3 79
4 4 26
4 5 26
4 6 22
4 7 19
4 8 14
4 9 9
4 10 0
5 1 104
5 2 61
5 3 38
5 4 36
5 5 26
5 6 22
5 7 21
6 1 115
6 2 29
6 3 21
6 4 0
6 5 0
6 6 0
7 1 35
7 2 26
7 3 20
7 4 15
7 5 4
8 1 123
8 2 112
8 3 101
8 4 95
8 5 85
8 6 62
8 7 42
8 8 31
8 9 24
8 10 11
8 11 10
8 12 5(77 row(s) affected)*/select categoryid,unitsinstock from # where subseq <4
go/*
categoryid unitsinstock
----------- ------------
1 125
1 111
1 69
2 120
2 113
2 76
3 76
3 65
3 49
4 112
4 86
4 79
5 104
5 61
5 38
6 115
6 29
6 21
7 35
7 26
7 20
8 123
8 112
8 101(24 row(s) affected)*/
select aaa.ProductID,aaa.ProductName,bbb.库存总数 from Products aaa,
(select top 3 CategoryID ,sum(UnitsInStock) as 库存总数 from Products group by CategoryID order by 库存总数 desc) bbb
where aaa.CategoryID=bbb.CategoryID
right join (select top 3 CategoryID , UnitsInStock = sum(UnitsInStock)
from Products
group by CategoryID
order by UnitsInStock desc) n
on m.CategoryID = n.CategoryID
order by m.CategoryID
select aaa.CategoryID,aaa.ProductID,aaa.ProductName,sum(aaa.UnitsInStock) as 产品总数,bbb.分类总数 from Products aaa,
(select top 3 CategoryID ,sum(UnitsInStock) as 分类总数 from Products group by CategoryID order by 分类总数 desc) bbb
where aaa.CategoryID=bbb.CategoryID group by aaa.ProductID,aaa.ProductName,aaa.CategoryID,bbb.分类总数
order by bbb.分类总数 desc,产品总数 desc