T-SQL Multiple grouping(T-SQL 多重分组)
本文介绍了T-SQL 多重分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下数据:
Product Price StartDate EndDate
Apples 4.9 2010-03-01 00:00:00.000 2010-03-01 00:00:00.000
Apples 4.9 2010-03-02 00:00:00.000 2010-03-02 00:00:00.000
Apples 2.5 2010-03-03 00:00:00.000 2010-03-03 00:00:00.000
Apples 4.9 2010-03-05 00:00:00.000 2010-03-05 00:00:00.000
Apples 4.9 2010-03-06 00:00:00.000 2010-03-06 00:00:00.000
Apples 4.9 2010-03-09 00:00:00.000 2010-03-09 00:00:00.000
Apples 2.5 2010-03-10 00:00:00.000 2010-03-10 00:00:00.000
Apples 4.9 2010-03-11 00:00:00.000 2010-03-11 00:00:00.000
Apples 4.9 2010-03-12 00:00:00.000 2010-03-12 00:00:00.000
Apples 4.9 2010-03-13 00:00:00.000 2010-03-13 00:00:00.000
Apples 4.9 2010-03-15 00:00:00.000 2010-03-15 00:00:00.000
Apples 4.9 2010-03-16 00:00:00.000 2010-03-16 00:00:00.000
想要像 product, price, min(startdate), max(startdate) 这样进行分组,但也应该在开始日期和结束日期中进行分组............如下所示
want to group like product, price, min(startdate), max(startdate) but should have grouping in start date and end date as well........ something like below
想要的结果
Apples 4.9 2010-03-01 00:00:00.000 2010-03-02 00:00:00.000
Apples 2.5 2010-03-03 00:00:00.000 2010-03-03 00:00:00.000
Apples 4.9 2010-03-05 00:00:00.000 2010-03-09 00:00:00.000
Apples 2.5 2010-03-10 00:00:00.000 2010-03-10 00:00:00.000
Apples 4.9 2010-03-11 00:00:00.000 2010-03-16 00:00:00.000
推荐答案
我的方法.
数据:
create table t ( producte varchar(50),
price money,
start_date date,
end_date date);
insert into t values
( 'apple', 4.9, '2012-01-01', '2012-01-01' ),
( 'apple', 4.9, '2012-01-02', '2012-01-02' ),
( 'apple', 8, '2012-01-04', '2012-01-04' ),
( 'cat', 5, '2012-01-01', '2012-01-01' ),
( 'cat', 6, '2012-01-02', '2012-01-02' ),
( 'cat', 6, '2012-01-03', '2012-01-03' );
查询:
with start_dates as (
select
t.producte, t.price, t.start_date, t.end_date, t.start_date as gr_date
from
t left outer join
t t1 on
t.price = t1.price and --new
t.producte = t1.producte and
t.start_date = dateadd(day,1, t1.end_date )
where t1.producte is null
union all
select
t.producte, t.price, t.start_date,t. end_date, gr_date
from
t inner join
start_dates t1 on
t.price = t1.price and --new
t.producte = t1.producte and
t.start_date = dateadd(day,1, t1.end_date )
)
select t.producte, t.price , min( t.start_date ), max( t.end_date )
from start_dates t
group by t.producte, gr_date ,t.price
结果:
| PRODUCTE | PRICE | COLUMN_2 | COLUMN_3 |
----------------------------------------------
| apple | 4.9 | 2012-01-01 | 2012-01-02 |
| apple | 8 | 2012-01-04 | 2012-01-04 |
| cat | 5 | 2012-01-01 | 2012-01-01 |
| cat | 6 | 2012-01-02 | 2012-01-03 |
说明
这是一个递归 CTE 表达式.基本查询获取每组价格的初始日期.递归查询查找具有此价格的最后一条数据.
This is a recursive CTE expression. Base query take inital dates for each group of prices. Recursive query looks for last data with this price.
这篇关于T-SQL 多重分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
织梦狗教程
本文标题为:T-SQL 多重分组
基础教程推荐
猜你喜欢
- 在 MySQL 中:如何将表名作为存储过程和/或函数参数传递? 2021-01-01
- 二进制文件到 SQL 数据库 Apache Camel 2021-01-01
- 什么是 orradiag_<user>文件夹? 2022-01-01
- 如何根据该 XML 中的值更新 SQL 中的 XML 2021-01-01
- 在多列上分布任意行 2021-01-01
- 表 './mysql/proc' 被标记为崩溃,应该修复 2022-01-01
- mysql选择动态行值作为列名,另一列作为值 2021-01-01
- oracle区分大小写的原因? 2021-01-01
- MySQL 中的类型:BigInt(20) 与 Int(20) 2021-01-01
- 如何在 SQL 中将 Float 转换为 Varchar 2021-01-01
