最里层rownumber over partition by,外层rownumber = 1,最外层sum求和
解决方案 »
- oracle有类似sql server的事件探查器么?就是程序前台操作所执行的sql都记录下来了。
- 是高手的进来一下,帮帮忙?
- 怎样知道我的数据库的字符集是按什么排序的?
- 求助SQL语法(删掉表的内容,保留表的结构)
- 修改没有命名的表级或列级约束条件
- 请教在调用存储过程时报"从字符串向 datetime 转换时失败"的错误如何解决?
- 请问有什么方法可以令VFP的界面变得与漂亮点,别老是win98的标准界面?
- 如何在oracle 加个自动增加的字段(指的是字段值)
- 如何建立一个作业(或事务),定时将另一台机器的Sybase数据库里的数据导入?
- 请问如何在sql中实现模糊查询?(急,给分!)
- sql 单列转行
- sql 能不能先判断值,在确定需要UPDATE的字段
-- Author :DBA_HuangZJ(發糞塗牆)
-- Date :2014-07-23 17:22:59
-- Version:
-- Microsoft SQL Server 2012 - 11.0.5058.0 (X64)
-- May 14 2014 18:34:29
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition: Core-based Licensing (64-bit) on Windows NT 6.3 <X64> (Build 9600: ) (Hypervisor)
--
----------------------------------------------------------------
--> 测试数据:[huang]
if object_id('[huang]') is not null drop table [huang]
go
create table [huang]([ProID] int,[AttrCateName] varchar(4),[AttrName] varchar(4),[Price] numeric(4,2),[Sort] int)
insert [huang]
select 1,'颜色','纯白',10.00,2 union all
select 1,'颜色','酒色',15.00,1 union all
select 1,'颜色','青色',12.00,3 union all
select 1,'装订','平装',5.00,5 union all
select 1,'装订','精装',15.00,4
--------------开始查询--------------------------
SELECT SUM([Price])[Price]
FROM (
select *,ROW_NUMBER()OVER(PARTITION BY [AttrCateName] ORDER BY sort)id
from [huang])a
WHERE id=1
----------------结果----------------------------
/*
Price
---------------------------------------
30.00
*/