create table #xs(vip varchar(20), date datetime, sale float)
insert into #xs(vip, date, sale)
select 'A' as vip,'2013-1-1' as date, 230 as sale union all
select 'A','2013-1-5', 400 union all
select 'A','2013-3-6', 500 union all
select 'A','2013-4-30',600 union all
select 'B','2013-2-3',200 union all
select 'B','2013-3-5',300 union all
select 'B','2013-4-24',400
多久每消费可以这个[code=sql]select datediff (DAY ,date,DATEADD(MONTH, 1, date - DAY(date) + 1) - 1) as 多久没消费 from #xs[code]
#3
把VIP 和date 这两列查出来都费劲,, 坐等大神
#4
if object_id('tempdb..#xs') is not null
drop table #xs;
create table #xs(vip varchar(20), date datetime, sale float)
insert into #xs(vip, date, sale)
select 'A' as vip,'2013-1-1' as date, 230 as sale union all
select 'A','2013-1-5', 400 union all
select 'A','2013-3-6', 500 union all
select 'A','2013-4-30',600 union all
select 'B','2013-2-3',200 union all
select 'B','2013-3-5',300 union all
select 'B','2013-4-24',400
;with t as
(
select dateadd(month, number,'20130101') MonthDay
from master.dbo.spt_values
where type='p'
)
,tRange as
(
select vip,convert(varchar(6),min(date),112)+'01' MinMonth,convert(varchar(6),max(date),112)+'01' MaxMonth
from #xs
group by vip
),tDimDate as
(
select tr.vip,t.MonthDay StartDay,dateadd(d,-1,DATEADD(month,1,t.MonthDay)) EndDay
from tRange tr
join t
on tr.MinMonth<=t.MonthDay
and tr.MaxMonth>=t.MonthDay
),tdata as (
select t.vip,EndDay
,DATEDIFF(d,(select top 1 date from #xs xs where xs.date <=t.EndDay and xs.vip=t.vip order by xs.date desc ),EndDay) NoVisitDays
,count(x.date) VisitDays
,sum(isnull(sale,0)) Sales
from tDimDate t
left join #xs x
on x.[date] between t.StartDay and t.EndDay
and t.vip=x.vip
group by t.vip,EndDay
)
select vip,EndDay,NoVisitDays,VisitDays
,sum(VisitDays) over(partition by vip order by EndDay ) sumVisitDays
,sum(Sales) over(partition by vip order by EndDay ) sumSales
from tdata
#5
额 我忘记说了 我还有一个日期的参照表:
#6
表字段没给全, 这两个表怎么关联?
#7
create table #xs(vip varchar(20), date datetime, sale float)
insert into #xs(vip, date, sale)
select 'A' as vip,'2013-1-1' as date, 230 as sale union all
select 'A','2013-1-5', 400 union all
select 'A','2013-3-6', 500 union all
select 'A','2013-4-30',600 union all
select 'B','2013-2-3',200 union all
select 'B','2013-3-5',300 union all
select 'B','2013-4-24',400
--select * from #xs
;WITH cte AS (
SELECT vip,[date],DATEADD(dd,-1,DATEADD(mm,DATEDIFF(m,0,[date]),0)) bomonth,DATEADD(dd,-1,DATEADD(mm,DATEDIFF(m,0,[date])+1,0)) AS eomonth,sale
FROM #xs
),c2 AS (
SELECT vip,eomonth,COUNT(1) AS monthCount,SUM(sale) AS sale FROM cte GROUP BY vip,[bomonth],eomonth
),c3 AS(
SELECT a.number,b.vip FROM master..spt_values a JOIN (SELECT DISTINCT vip FROM #xs) b ON 1=1 WHERE a.type='p' AND a.number BETWEEN 1 AND 12
),c4 AS (
SELECT a.*,b.number,b.vip AS vipx FROM c2 a RIGHT JOIN c3 b ON a.vip=b.vip AND MONTH(a.eomonth)=b.number
--ORDER BY b.vip,b.number
),c5 AS (
SELECT * FROM c4 a WHERE a.eomonth IS NOT NULL OR
EXISTS(SELECT 1 FROM c4 x WHERE a.vipx=x.vip AND a.number<x.number)
AND EXISTS(SELECT 1 FROM c4 x WHERE a.vipx =x.vip AND a.number>x.number)
),c6 AS (
SELECT vipx,CASE WHEN eomonth IS NOT NULL THEN eomonth ELSE (
SELECT DATEADD(mm,a.number-MAX(number),MAX(eomonth)) FROM c5 x WHERE x.vipx=a.vipx
) END AS eomonth
,CASE WHEN monthCount is NOT NULL THEN monthCount ELSE 0 END AS monthCount
,CASE WHEN sale is NOT NULL THEN sale ELSE 0 END AS sale
FROM c5 a
)
SELECT a.vipx,a.eomonth,a.monthCount
,(SELECT SUM(monthcount) FROM c6 x WHERE x.vipx=a.vipx AND x.eomonth<=a.eomonth) AS totalCount
,(SELECT SUM(sale) FROM c6 x WHERE x.vipx=a.vipx AND x.eomonth<=a.eomonth) AS TotalSale
FROM c6 a ORDER BY a.vipx,a.eomonth
--
/*
vipx eomonth monthCount totalCount TotalSale
-------------------- ----------------------- ----------- ----------- ----------------------
A 2013-01-31 00:00:00.000 2 2 630
A 2013-02-28 00:00:00.000 0 2 630
A 2013-03-31 00:00:00.000 1 3 1130
A 2013-04-30 00:00:00.000 1 4 1730
B 2013-02-28 00:00:00.000 1 1 200
B 2013-03-31 00:00:00.000 1 2 500
B 2013-04-30 00:00:00.000 1 3 900
*/
DROP TABLE #xs;
只能做为参考,太乱了.我去
#8
哥你这语句是不是漏掉了什么? 怎么我看sum(VisitDays) over(partition by vip order by EndDay ) sumVisitDays 这个怪怪的?
#9
你把order by EndDay去掉就不报错啦,
#10
if object_id('tempdb..#xs') is not null
drop table #xs;
create table #xs(vip varchar(20), date datetime, sale float)
insert into #xs(vip, date, sale)
select 'A' as vip,'2013-1-1' as date, 230 as sale union all
select 'A','2013-1-5', 400 union all
select 'A','2013-3-6', 500 union all
select 'A','2013-4-30',600 union all
select 'B','2013-2-3',200 union all
select 'B','2013-3-5',300 union all
select 'B','2013-4-24',400
;with t as
(
select dateadd(month, number,'20130101') MonthDay
from master.dbo.spt_values
where type='p'
)
,tRange as
(
select vip,convert(varchar(6),min(date),112)+'01' MinMonth,convert(varchar(6),max(date),112)+'01' MaxMonth
from #xs
group by vip
),tDimDate as
(
select tr.vip,t.MonthDay StartDay,dateadd(d,-1,DATEADD(month,1,t.MonthDay)) EndDay
from tRange tr
join t
on tr.MinMonth<=t.MonthDay
and tr.MaxMonth>=t.MonthDay
),tdata as (
select t.vip,EndDay
,DATEDIFF(d,(select top 1 date from #xs xs where xs.date <=t.EndDay and xs.vip=t.vip order by xs.date desc ),EndDay) NoVisitDays
,count(x.date) VisitDays
,sum(isnull(sale,0)) Sales
from tDimDate t
left join #xs x
on x.[date] between t.StartDay and t.EndDay
and t.vip=x.vip
group by t.vip,EndDay
)
select vip,EndDay,NoVisitDays,VisitDays
,sum(VisitDays) over(partition by vip order by EndDay ) sumVisitDays
,sum(Sales) over(partition by vip order by EndDay ) sumSales
from tdata
哥你这语句是不是漏掉了什么? 怎么我看sum(VisitDays) over(partition by vip order by EndDay ) sumVisitDays 这个怪怪的?
你把order by EndDay去掉就不报错啦,
去掉结果也错了
#11
if object_id('tempdb..#xs') is not null
drop table #xs;
create table #xs(vip varchar(20), date datetime, sale float)
insert into #xs(vip, date, sale)
select 'A' as vip,'2013-1-1' as date, 230 as sale union all
select 'A','2013-1-5', 400 union all
select 'A','2013-3-6', 500 union all
select 'A','2013-4-30',600 union all
select 'B','2013-2-3',200 union all
select 'B','2013-3-5',300 union all
select 'B','2013-4-24',400
;with t as
(
select dateadd(month, number,'20130101') MonthDay
from master.dbo.spt_values
where type='p'
)
,tRange as
(
select vip,convert(varchar(6),min(date),112)+'01' MinMonth,convert(varchar(6),max(date),112)+'01' MaxMonth
from #xs
group by vip
),tDimDate as
(
select tr.vip,t.MonthDay StartDay,dateadd(d,-1,DATEADD(month,1,t.MonthDay)) EndDay
from tRange tr
join t
on tr.MinMonth<=t.MonthDay
and tr.MaxMonth>=t.MonthDay
),tdata as (
select t.vip,EndDay
,DATEDIFF(d,(select top 1 date from #xs xs where xs.date <=t.EndDay and xs.vip=t.vip order by xs.date desc ),EndDay) NoVisitDays
,count(x.date) VisitDays
,sum(isnull(sale,0)) Sales
from tDimDate t
left join #xs x
on x.[date] between t.StartDay and t.EndDay
and t.vip=x.vip
group by t.vip,EndDay
)
select vip,EndDay,NoVisitDays,VisitDays
,sum(VisitDays) over(partition by vip order by EndDay ) sumVisitDays
,sum(Sales) over(partition by vip order by EndDay ) sumSales
from tdata
哥你这语句是不是漏掉了什么? 怎么我看sum(VisitDays) over(partition by vip order by EndDay ) sumVisitDays 这个怪怪的?
我这边是正常的啊。
没见过 sum() over() 这种写法吗
#12
if object_id('tempdb..#xs') is not null
drop table #xs;
create table #xs(vip varchar(20), date datetime, sale float)
insert into #xs(vip, date, sale)
select 'A' as vip,'2013-1-1' as date, 230 as sale union all
select 'A','2013-1-5', 400 union all
select 'A','2013-3-6', 500 union all
select 'A','2013-4-30',600 union all
select 'B','2013-2-3',200 union all
select 'B','2013-3-5',300 union all
select 'B','2013-4-24',400
;with t as
(
select dateadd(month, number,'20130101') MonthDay
from master.dbo.spt_values
where type='p'
)
,tRange as
(
select vip,convert(varchar(6),min(date),112)+'01' MinMonth,convert(varchar(6),max(date),112)+'01' MaxMonth
from #xs
group by vip
),tDimDate as
(
select tr.vip,t.MonthDay StartDay,dateadd(d,-1,DATEADD(month,1,t.MonthDay)) EndDay
from tRange tr
join t
on tr.MinMonth<=t.MonthDay
and tr.MaxMonth>=t.MonthDay
),tdata as (
select t.vip,EndDay
,DATEDIFF(d,(select top 1 date from #xs xs where xs.date <=t.EndDay and xs.vip=t.vip order by xs.date desc ),EndDay) NoVisitDays
,count(x.date) VisitDays
,sum(isnull(sale,0)) Sales
from tDimDate t
left join #xs x
on x.[date] between t.StartDay and t.EndDay
and t.vip=x.vip
group by t.vip,EndDay
)
select vip,EndDay,NoVisitDays,VisitDays
,sum(VisitDays) over(partition by vip order by EndDay ) sumVisitDays
,sum(Sales) over(partition by vip order by EndDay ) sumSales
from tdata
哥你这语句是不是漏掉了什么? 怎么我看sum(VisitDays) over(partition by vip order by EndDay ) sumVisitDays 这个怪怪的?
我这边是正常的啊。
没见过 sum() over() 这种写法吗
你用的是 2008吧? 05里面这样写不行,去掉order by EndDay才可以
#13
if object_id('tempdb..#xs') is not null
drop table #xs;
create table #xs(vip varchar(20), date datetime, sale float)
insert into #xs(vip, date, sale)
select 'A' as vip,'2013-1-1' as date, 230 as sale union all
select 'A','2013-1-5', 400 union all
select 'A','2013-3-6', 500 union all
select 'A','2013-4-30',600 union all
select 'B','2013-2-3',200 union all
select 'B','2013-3-5',300 union all
select 'B','2013-4-24',400
;with t as
(
select dateadd(month, number,'20130101') MonthDay
from master.dbo.spt_values
where type='p'
)
,tRange as
(
select vip,convert(varchar(6),min(date),112)+'01' MinMonth,convert(varchar(6),max(date),112)+'01' MaxMonth
from #xs
group by vip
),tDimDate as
(
select tr.vip,t.MonthDay StartDay,dateadd(d,-1,DATEADD(month,1,t.MonthDay)) EndDay
from tRange tr
join t
on tr.MinMonth<=t.MonthDay
and tr.MaxMonth>=t.MonthDay
),tdata as (
select t.vip,EndDay
,DATEDIFF(d,(select top 1 date from #xs xs where xs.date <=t.EndDay and xs.vip=t.vip order by xs.date desc ),EndDay) NoVisitDays
,count(x.date) VisitDays
,sum(isnull(sale,0)) Sales
from tDimDate t
left join #xs x
on x.[date] between t.StartDay and t.EndDay
and t.vip=x.vip
group by t.vip,EndDay
)
select vip,EndDay,NoVisitDays,VisitDays
,sum(VisitDays) over(partition by vip order by EndDay ) sumVisitDays
,sum(Sales) over(partition by vip order by EndDay ) sumSales
from tdata
哥你这语句是不是漏掉了什么? 怎么我看sum(VisitDays) over(partition by vip order by EndDay ) sumVisitDays 这个怪怪的?
我这边是正常的啊。
没见过 sum() over() 这种写法吗
你用的是 2008吧? 05里面这样写不行,去掉order by EndDay才可以
if object_id('tempdb..#xs') is not null
drop table #xs;
create table #xs(vip varchar(20), date datetime, sale float)
insert into #xs(vip, date, sale)
select 'A' as vip,'2013-1-1' as date, 230 as sale union all
select 'A','2013-1-5', 400 union all
select 'A','2013-3-6', 500 union all
select 'A','2013-4-30',600 union all
select 'B','2013-2-3',200 union all
select 'B','2013-3-5',300 union all
select 'B','2013-4-24',400
;with t as
(
select dateadd(month, number,'20130101') MonthDay
from master.dbo.spt_values
where type='p'
)
,tRange as
(
select vip,convert(varchar(6),min(date),112)+'01' MinMonth,convert(varchar(6),max(date),112)+'01' MaxMonth
from #xs
group by vip
),tDimDate as
(
select tr.vip,t.MonthDay StartDay,dateadd(d,-1,DATEADD(month,1,t.MonthDay)) EndDay
from tRange tr
join t
on tr.MinMonth<=t.MonthDay
and tr.MaxMonth>=t.MonthDay
),tdata as (
select t.vip,EndDay
,DATEDIFF(d,(select top 1 date from #xs xs where xs.date <=t.EndDay and xs.vip=t.vip order by xs.date desc ),EndDay) NoVisitDays
,count(x.date) VisitDays
,sum(isnull(sale,0)) Sales
from tDimDate t
left join #xs x
on x.[date] between t.StartDay and t.EndDay
and t.vip=x.vip
group by t.vip,EndDay
)
select vip,EndDay,NoVisitDays,VisitDays
,(select sum(VisitDays) from tdata b where a.vip=b.vip and b.EndDay<=a.EndDay ) sumVisitDays
,(select sum(Sales) from tdata c where a.vip=c.vip and c.EndDay<=a.EndDay ) sumSales
from tdata a
order by 1,2
那就还是用子查询吧!
我用的2012
#14
if object_id('tempdb..#xs') is not null
drop table #xs;
create table #xs(vip varchar(20), date datetime, sale float)
insert into #xs(vip, date, sale)
select 'A' as vip,'2013-1-1' as date, 230 as sale union all
select 'A','2013-1-5', 400 union all
select 'A','2013-3-6', 500 union all
select 'A','2013-4-30',600 union all
select 'B','2013-2-3',200 union all
select 'B','2013-3-5',300 union all
select 'B','2013-4-24',400
;with t as
(
select dateadd(month, number,'20130101') MonthDay
from master.dbo.spt_values
where type='p'
)
,tRange as
(
select vip,convert(varchar(6),min(date),112)+'01' MinMonth,convert(varchar(6),max(date),112)+'01' MaxMonth
from #xs
group by vip
),tDimDate as
(
select tr.vip,t.MonthDay StartDay,dateadd(d,-1,DATEADD(month,1,t.MonthDay)) EndDay
from tRange tr
join t
on tr.MinMonth<=t.MonthDay
and tr.MaxMonth>=t.MonthDay
),tdata as (
select t.vip,EndDay
,DATEDIFF(d,(select top 1 date from #xs xs where xs.date <=t.EndDay and xs.vip=t.vip order by xs.date desc ),EndDay) NoVisitDays
,count(x.date) VisitDays
,sum(isnull(sale,0)) Sales
from tDimDate t
left join #xs x
on x.[date] between t.StartDay and t.EndDay
and t.vip=x.vip
group by t.vip,EndDay
)
select vip,EndDay,NoVisitDays,VisitDays
,sum(VisitDays) over(partition by vip order by EndDay ) sumVisitDays
,sum(Sales) over(partition by vip order by EndDay ) sumSales
from tdata
哥你这语句是不是漏掉了什么? 怎么我看sum(VisitDays) over(partition by vip order by EndDay ) sumVisitDays 这个怪怪的?
我这边是正常的啊。
没见过 sum() over() 这种写法吗
你用的是 2008吧? 05里面这样写不行,去掉order by EndDay才可以
if object_id('tempdb..#xs') is not null
drop table #xs;
create table #xs(vip varchar(20), date datetime, sale float)
insert into #xs(vip, date, sale)
select 'A' as vip,'2013-1-1' as date, 230 as sale union all
select 'A','2013-1-5', 400 union all
select 'A','2013-3-6', 500 union all
select 'A','2013-4-30',600 union all
select 'B','2013-2-3',200 union all
select 'B','2013-3-5',300 union all
select 'B','2013-4-24',400
;with t as
(
select dateadd(month, number,'20130101') MonthDay
from master.dbo.spt_values
where type='p'
)
,tRange as
(
select vip,convert(varchar(6),min(date),112)+'01' MinMonth,convert(varchar(6),max(date),112)+'01' MaxMonth
from #xs
group by vip
),tDimDate as
(
select tr.vip,t.MonthDay StartDay,dateadd(d,-1,DATEADD(month,1,t.MonthDay)) EndDay
from tRange tr
join t
on tr.MinMonth<=t.MonthDay
and tr.MaxMonth>=t.MonthDay
),tdata as (
select t.vip,EndDay
,DATEDIFF(d,(select top 1 date from #xs xs where xs.date <=t.EndDay and xs.vip=t.vip order by xs.date desc ),EndDay) NoVisitDays
,count(x.date) VisitDays
,sum(isnull(sale,0)) Sales
from tDimDate t
left join #xs x
on x.[date] between t.StartDay and t.EndDay
and t.vip=x.vip
group by t.vip,EndDay
)
select vip,EndDay,NoVisitDays,VisitDays
,(select sum(VisitDays) from tdata b where a.vip=b.vip and b.EndDay<=a.EndDay ) sumVisitDays
,(select sum(Sales) from tdata c where a.vip=c.vip and c.EndDay<=a.EndDay ) sumSales
from tdata a
order by 1,2
那就还是用子查询吧!
我用的2012
我用的2005 难怪看上去这么奇怪的语句。。。
ok 结贴了 感谢ls各位大神! 尤其是stublue和jinfengyiye
多久每消费可以这个[code=sql]select datediff (DAY ,date,DATEADD(MONTH, 1, date - DAY(date) + 1) - 1) as 多久没消费 from #xs[code]
#3
把VIP 和date 这两列查出来都费劲,, 坐等大神
#4
if object_id('tempdb..#xs') is not null
drop table #xs;
create table #xs(vip varchar(20), date datetime, sale float)
insert into #xs(vip, date, sale)
select 'A' as vip,'2013-1-1' as date, 230 as sale union all
select 'A','2013-1-5', 400 union all
select 'A','2013-3-6', 500 union all
select 'A','2013-4-30',600 union all
select 'B','2013-2-3',200 union all
select 'B','2013-3-5',300 union all
select 'B','2013-4-24',400
;with t as
(
select dateadd(month, number,'20130101') MonthDay
from master.dbo.spt_values
where type='p'
)
,tRange as
(
select vip,convert(varchar(6),min(date),112)+'01' MinMonth,convert(varchar(6),max(date),112)+'01' MaxMonth
from #xs
group by vip
),tDimDate as
(
select tr.vip,t.MonthDay StartDay,dateadd(d,-1,DATEADD(month,1,t.MonthDay)) EndDay
from tRange tr
join t
on tr.MinMonth<=t.MonthDay
and tr.MaxMonth>=t.MonthDay
),tdata as (
select t.vip,EndDay
,DATEDIFF(d,(select top 1 date from #xs xs where xs.date <=t.EndDay and xs.vip=t.vip order by xs.date desc ),EndDay) NoVisitDays
,count(x.date) VisitDays
,sum(isnull(sale,0)) Sales
from tDimDate t
left join #xs x
on x.[date] between t.StartDay and t.EndDay
and t.vip=x.vip
group by t.vip,EndDay
)
select vip,EndDay,NoVisitDays,VisitDays
,sum(VisitDays) over(partition by vip order by EndDay ) sumVisitDays
,sum(Sales) over(partition by vip order by EndDay ) sumSales
from tdata
#5
额 我忘记说了 我还有一个日期的参照表:
#6
额 我忘记说了 我还有一个日期的参照表:
表字段没给全, 这两个表怎么关联?
#7
create table #xs(vip varchar(20), date datetime, sale float)
insert into #xs(vip, date, sale)
select 'A' as vip,'2013-1-1' as date, 230 as sale union all
select 'A','2013-1-5', 400 union all
select 'A','2013-3-6', 500 union all
select 'A','2013-4-30',600 union all
select 'B','2013-2-3',200 union all
select 'B','2013-3-5',300 union all
select 'B','2013-4-24',400
--select * from #xs
;WITH cte AS (
SELECT vip,[date],DATEADD(dd,-1,DATEADD(mm,DATEDIFF(m,0,[date]),0)) bomonth,DATEADD(dd,-1,DATEADD(mm,DATEDIFF(m,0,[date])+1,0)) AS eomonth,sale
FROM #xs
),c2 AS (
SELECT vip,eomonth,COUNT(1) AS monthCount,SUM(sale) AS sale FROM cte GROUP BY vip,[bomonth],eomonth
),c3 AS(
SELECT a.number,b.vip FROM master..spt_values a JOIN (SELECT DISTINCT vip FROM #xs) b ON 1=1 WHERE a.type='p' AND a.number BETWEEN 1 AND 12
),c4 AS (
SELECT a.*,b.number,b.vip AS vipx FROM c2 a RIGHT JOIN c3 b ON a.vip=b.vip AND MONTH(a.eomonth)=b.number
--ORDER BY b.vip,b.number
),c5 AS (
SELECT * FROM c4 a WHERE a.eomonth IS NOT NULL OR
EXISTS(SELECT 1 FROM c4 x WHERE a.vipx=x.vip AND a.number<x.number)
AND EXISTS(SELECT 1 FROM c4 x WHERE a.vipx =x.vip AND a.number>x.number)
),c6 AS (
SELECT vipx,CASE WHEN eomonth IS NOT NULL THEN eomonth ELSE (
SELECT DATEADD(mm,a.number-MAX(number),MAX(eomonth)) FROM c5 x WHERE x.vipx=a.vipx
) END AS eomonth
,CASE WHEN monthCount is NOT NULL THEN monthCount ELSE 0 END AS monthCount
,CASE WHEN sale is NOT NULL THEN sale ELSE 0 END AS sale
FROM c5 a
)
SELECT a.vipx,a.eomonth,a.monthCount
,(SELECT SUM(monthcount) FROM c6 x WHERE x.vipx=a.vipx AND x.eomonth<=a.eomonth) AS totalCount
,(SELECT SUM(sale) FROM c6 x WHERE x.vipx=a.vipx AND x.eomonth<=a.eomonth) AS TotalSale
FROM c6 a ORDER BY a.vipx,a.eomonth
--
/*
vipx eomonth monthCount totalCount TotalSale
-------------------- ----------------------- ----------- ----------- ----------------------
A 2013-01-31 00:00:00.000 2 2 630
A 2013-02-28 00:00:00.000 0 2 630
A 2013-03-31 00:00:00.000 1 3 1130
A 2013-04-30 00:00:00.000 1 4 1730
B 2013-02-28 00:00:00.000 1 1 200
B 2013-03-31 00:00:00.000 1 2 500
B 2013-04-30 00:00:00.000 1 3 900
*/
DROP TABLE #xs;
只能做为参考,太乱了.我去
#8
if object_id('tempdb..#xs') is not null
drop table #xs;
create table #xs(vip varchar(20), date datetime, sale float)
insert into #xs(vip, date, sale)
select 'A' as vip,'2013-1-1' as date, 230 as sale union all
select 'A','2013-1-5', 400 union all
select 'A','2013-3-6', 500 union all
select 'A','2013-4-30',600 union all
select 'B','2013-2-3',200 union all
select 'B','2013-3-5',300 union all
select 'B','2013-4-24',400
;with t as
(
select dateadd(month, number,'20130101') MonthDay
from master.dbo.spt_values
where type='p'
)
,tRange as
(
select vip,convert(varchar(6),min(date),112)+'01' MinMonth,convert(varchar(6),max(date),112)+'01' MaxMonth
from #xs
group by vip
),tDimDate as
(
select tr.vip,t.MonthDay StartDay,dateadd(d,-1,DATEADD(month,1,t.MonthDay)) EndDay
from tRange tr
join t
on tr.MinMonth<=t.MonthDay
and tr.MaxMonth>=t.MonthDay
),tdata as (
select t.vip,EndDay
,DATEDIFF(d,(select top 1 date from #xs xs where xs.date <=t.EndDay and xs.vip=t.vip order by xs.date desc ),EndDay) NoVisitDays
,count(x.date) VisitDays
,sum(isnull(sale,0)) Sales
from tDimDate t
left join #xs x
on x.[date] between t.StartDay and t.EndDay
and t.vip=x.vip
group by t.vip,EndDay
)
select vip,EndDay,NoVisitDays,VisitDays
,sum(VisitDays) over(partition by vip order by EndDay ) sumVisitDays
,sum(Sales) over(partition by vip order by EndDay ) sumSales
from tdata
哥你这语句是不是漏掉了什么? 怎么我看sum(VisitDays) over(partition by vip order by EndDay ) sumVisitDays 这个怪怪的?
#9
if object_id('tempdb..#xs') is not null
drop table #xs;
create table #xs(vip varchar(20), date datetime, sale float)
insert into #xs(vip, date, sale)
select 'A' as vip,'2013-1-1' as date, 230 as sale union all
select 'A','2013-1-5', 400 union all
select 'A','2013-3-6', 500 union all
select 'A','2013-4-30',600 union all
select 'B','2013-2-3',200 union all
select 'B','2013-3-5',300 union all
select 'B','2013-4-24',400
;with t as
(
select dateadd(month, number,'20130101') MonthDay
from master.dbo.spt_values
where type='p'
)
,tRange as
(
select vip,convert(varchar(6),min(date),112)+'01' MinMonth,convert(varchar(6),max(date),112)+'01' MaxMonth
from #xs
group by vip
),tDimDate as
(
select tr.vip,t.MonthDay StartDay,dateadd(d,-1,DATEADD(month,1,t.MonthDay)) EndDay
from tRange tr
join t
on tr.MinMonth<=t.MonthDay
and tr.MaxMonth>=t.MonthDay
),tdata as (
select t.vip,EndDay
,DATEDIFF(d,(select top 1 date from #xs xs where xs.date <=t.EndDay and xs.vip=t.vip order by xs.date desc ),EndDay) NoVisitDays
,count(x.date) VisitDays
,sum(isnull(sale,0)) Sales
from tDimDate t
left join #xs x
on x.[date] between t.StartDay and t.EndDay
and t.vip=x.vip
group by t.vip,EndDay
)
select vip,EndDay,NoVisitDays,VisitDays
,sum(VisitDays) over(partition by vip order by EndDay ) sumVisitDays
,sum(Sales) over(partition by vip order by EndDay ) sumSales
from tdata
哥你这语句是不是漏掉了什么? 怎么我看sum(VisitDays) over(partition by vip order by EndDay ) sumVisitDays 这个怪怪的?
你把order by EndDay去掉就不报错啦,
#10
if object_id('tempdb..#xs') is not null
drop table #xs;
create table #xs(vip varchar(20), date datetime, sale float)
insert into #xs(vip, date, sale)
select 'A' as vip,'2013-1-1' as date, 230 as sale union all
select 'A','2013-1-5', 400 union all
select 'A','2013-3-6', 500 union all
select 'A','2013-4-30',600 union all
select 'B','2013-2-3',200 union all
select 'B','2013-3-5',300 union all
select 'B','2013-4-24',400
;with t as
(
select dateadd(month, number,'20130101') MonthDay
from master.dbo.spt_values
where type='p'
)
,tRange as
(
select vip,convert(varchar(6),min(date),112)+'01' MinMonth,convert(varchar(6),max(date),112)+'01' MaxMonth
from #xs
group by vip
),tDimDate as
(
select tr.vip,t.MonthDay StartDay,dateadd(d,-1,DATEADD(month,1,t.MonthDay)) EndDay
from tRange tr
join t
on tr.MinMonth<=t.MonthDay
and tr.MaxMonth>=t.MonthDay
),tdata as (
select t.vip,EndDay
,DATEDIFF(d,(select top 1 date from #xs xs where xs.date <=t.EndDay and xs.vip=t.vip order by xs.date desc ),EndDay) NoVisitDays
,count(x.date) VisitDays
,sum(isnull(sale,0)) Sales
from tDimDate t
left join #xs x
on x.[date] between t.StartDay and t.EndDay
and t.vip=x.vip
group by t.vip,EndDay
)
select vip,EndDay,NoVisitDays,VisitDays
,sum(VisitDays) over(partition by vip order by EndDay ) sumVisitDays
,sum(Sales) over(partition by vip order by EndDay ) sumSales
from tdata
哥你这语句是不是漏掉了什么? 怎么我看sum(VisitDays) over(partition by vip order by EndDay ) sumVisitDays 这个怪怪的?
你把order by EndDay去掉就不报错啦,
去掉结果也错了
#11
if object_id('tempdb..#xs') is not null
drop table #xs;
create table #xs(vip varchar(20), date datetime, sale float)
insert into #xs(vip, date, sale)
select 'A' as vip,'2013-1-1' as date, 230 as sale union all
select 'A','2013-1-5', 400 union all
select 'A','2013-3-6', 500 union all
select 'A','2013-4-30',600 union all
select 'B','2013-2-3',200 union all
select 'B','2013-3-5',300 union all
select 'B','2013-4-24',400
;with t as
(
select dateadd(month, number,'20130101') MonthDay
from master.dbo.spt_values
where type='p'
)
,tRange as
(
select vip,convert(varchar(6),min(date),112)+'01' MinMonth,convert(varchar(6),max(date),112)+'01' MaxMonth
from #xs
group by vip
),tDimDate as
(
select tr.vip,t.MonthDay StartDay,dateadd(d,-1,DATEADD(month,1,t.MonthDay)) EndDay
from tRange tr
join t
on tr.MinMonth<=t.MonthDay
and tr.MaxMonth>=t.MonthDay
),tdata as (
select t.vip,EndDay
,DATEDIFF(d,(select top 1 date from #xs xs where xs.date <=t.EndDay and xs.vip=t.vip order by xs.date desc ),EndDay) NoVisitDays
,count(x.date) VisitDays
,sum(isnull(sale,0)) Sales
from tDimDate t
left join #xs x
on x.[date] between t.StartDay and t.EndDay
and t.vip=x.vip
group by t.vip,EndDay
)
select vip,EndDay,NoVisitDays,VisitDays
,sum(VisitDays) over(partition by vip order by EndDay ) sumVisitDays
,sum(Sales) over(partition by vip order by EndDay ) sumSales
from tdata
哥你这语句是不是漏掉了什么? 怎么我看sum(VisitDays) over(partition by vip order by EndDay ) sumVisitDays 这个怪怪的?
我这边是正常的啊。
没见过 sum() over() 这种写法吗
#12
if object_id('tempdb..#xs') is not null
drop table #xs;
create table #xs(vip varchar(20), date datetime, sale float)
insert into #xs(vip, date, sale)
select 'A' as vip,'2013-1-1' as date, 230 as sale union all
select 'A','2013-1-5', 400 union all
select 'A','2013-3-6', 500 union all
select 'A','2013-4-30',600 union all
select 'B','2013-2-3',200 union all
select 'B','2013-3-5',300 union all
select 'B','2013-4-24',400
;with t as
(
select dateadd(month, number,'20130101') MonthDay
from master.dbo.spt_values
where type='p'
)
,tRange as
(
select vip,convert(varchar(6),min(date),112)+'01' MinMonth,convert(varchar(6),max(date),112)+'01' MaxMonth
from #xs
group by vip
),tDimDate as
(
select tr.vip,t.MonthDay StartDay,dateadd(d,-1,DATEADD(month,1,t.MonthDay)) EndDay
from tRange tr
join t
on tr.MinMonth<=t.MonthDay
and tr.MaxMonth>=t.MonthDay
),tdata as (
select t.vip,EndDay
,DATEDIFF(d,(select top 1 date from #xs xs where xs.date <=t.EndDay and xs.vip=t.vip order by xs.date desc ),EndDay) NoVisitDays
,count(x.date) VisitDays
,sum(isnull(sale,0)) Sales
from tDimDate t
left join #xs x
on x.[date] between t.StartDay and t.EndDay
and t.vip=x.vip
group by t.vip,EndDay
)
select vip,EndDay,NoVisitDays,VisitDays
,sum(VisitDays) over(partition by vip order by EndDay ) sumVisitDays
,sum(Sales) over(partition by vip order by EndDay ) sumSales
from tdata
哥你这语句是不是漏掉了什么? 怎么我看sum(VisitDays) over(partition by vip order by EndDay ) sumVisitDays 这个怪怪的?
我这边是正常的啊。
没见过 sum() over() 这种写法吗
你用的是 2008吧? 05里面这样写不行,去掉order by EndDay才可以
#13
if object_id('tempdb..#xs') is not null
drop table #xs;
create table #xs(vip varchar(20), date datetime, sale float)
insert into #xs(vip, date, sale)
select 'A' as vip,'2013-1-1' as date, 230 as sale union all
select 'A','2013-1-5', 400 union all
select 'A','2013-3-6', 500 union all
select 'A','2013-4-30',600 union all
select 'B','2013-2-3',200 union all
select 'B','2013-3-5',300 union all
select 'B','2013-4-24',400
;with t as
(
select dateadd(month, number,'20130101') MonthDay
from master.dbo.spt_values
where type='p'
)
,tRange as
(
select vip,convert(varchar(6),min(date),112)+'01' MinMonth,convert(varchar(6),max(date),112)+'01' MaxMonth
from #xs
group by vip
),tDimDate as
(
select tr.vip,t.MonthDay StartDay,dateadd(d,-1,DATEADD(month,1,t.MonthDay)) EndDay
from tRange tr
join t
on tr.MinMonth<=t.MonthDay
and tr.MaxMonth>=t.MonthDay
),tdata as (
select t.vip,EndDay
,DATEDIFF(d,(select top 1 date from #xs xs where xs.date <=t.EndDay and xs.vip=t.vip order by xs.date desc ),EndDay) NoVisitDays
,count(x.date) VisitDays
,sum(isnull(sale,0)) Sales
from tDimDate t
left join #xs x
on x.[date] between t.StartDay and t.EndDay
and t.vip=x.vip
group by t.vip,EndDay
)
select vip,EndDay,NoVisitDays,VisitDays
,sum(VisitDays) over(partition by vip order by EndDay ) sumVisitDays
,sum(Sales) over(partition by vip order by EndDay ) sumSales
from tdata
哥你这语句是不是漏掉了什么? 怎么我看sum(VisitDays) over(partition by vip order by EndDay ) sumVisitDays 这个怪怪的?
我这边是正常的啊。
没见过 sum() over() 这种写法吗
你用的是 2008吧? 05里面这样写不行,去掉order by EndDay才可以
if object_id('tempdb..#xs') is not null
drop table #xs;
create table #xs(vip varchar(20), date datetime, sale float)
insert into #xs(vip, date, sale)
select 'A' as vip,'2013-1-1' as date, 230 as sale union all
select 'A','2013-1-5', 400 union all
select 'A','2013-3-6', 500 union all
select 'A','2013-4-30',600 union all
select 'B','2013-2-3',200 union all
select 'B','2013-3-5',300 union all
select 'B','2013-4-24',400
;with t as
(
select dateadd(month, number,'20130101') MonthDay
from master.dbo.spt_values
where type='p'
)
,tRange as
(
select vip,convert(varchar(6),min(date),112)+'01' MinMonth,convert(varchar(6),max(date),112)+'01' MaxMonth
from #xs
group by vip
),tDimDate as
(
select tr.vip,t.MonthDay StartDay,dateadd(d,-1,DATEADD(month,1,t.MonthDay)) EndDay
from tRange tr
join t
on tr.MinMonth<=t.MonthDay
and tr.MaxMonth>=t.MonthDay
),tdata as (
select t.vip,EndDay
,DATEDIFF(d,(select top 1 date from #xs xs where xs.date <=t.EndDay and xs.vip=t.vip order by xs.date desc ),EndDay) NoVisitDays
,count(x.date) VisitDays
,sum(isnull(sale,0)) Sales
from tDimDate t
left join #xs x
on x.[date] between t.StartDay and t.EndDay
and t.vip=x.vip
group by t.vip,EndDay
)
select vip,EndDay,NoVisitDays,VisitDays
,(select sum(VisitDays) from tdata b where a.vip=b.vip and b.EndDay<=a.EndDay ) sumVisitDays
,(select sum(Sales) from tdata c where a.vip=c.vip and c.EndDay<=a.EndDay ) sumSales
from tdata a
order by 1,2
那就还是用子查询吧!
我用的2012
#14
if object_id('tempdb..#xs') is not null
drop table #xs;
create table #xs(vip varchar(20), date datetime, sale float)
insert into #xs(vip, date, sale)
select 'A' as vip,'2013-1-1' as date, 230 as sale union all
select 'A','2013-1-5', 400 union all
select 'A','2013-3-6', 500 union all
select 'A','2013-4-30',600 union all
select 'B','2013-2-3',200 union all
select 'B','2013-3-5',300 union all
select 'B','2013-4-24',400
;with t as
(
select dateadd(month, number,'20130101') MonthDay
from master.dbo.spt_values
where type='p'
)
,tRange as
(
select vip,convert(varchar(6),min(date),112)+'01' MinMonth,convert(varchar(6),max(date),112)+'01' MaxMonth
from #xs
group by vip
),tDimDate as
(
select tr.vip,t.MonthDay StartDay,dateadd(d,-1,DATEADD(month,1,t.MonthDay)) EndDay
from tRange tr
join t
on tr.MinMonth<=t.MonthDay
and tr.MaxMonth>=t.MonthDay
),tdata as (
select t.vip,EndDay
,DATEDIFF(d,(select top 1 date from #xs xs where xs.date <=t.EndDay and xs.vip=t.vip order by xs.date desc ),EndDay) NoVisitDays
,count(x.date) VisitDays
,sum(isnull(sale,0)) Sales
from tDimDate t
left join #xs x
on x.[date] between t.StartDay and t.EndDay
and t.vip=x.vip
group by t.vip,EndDay
)
select vip,EndDay,NoVisitDays,VisitDays
,sum(VisitDays) over(partition by vip order by EndDay ) sumVisitDays
,sum(Sales) over(partition by vip order by EndDay ) sumSales
from tdata
哥你这语句是不是漏掉了什么? 怎么我看sum(VisitDays) over(partition by vip order by EndDay ) sumVisitDays 这个怪怪的?
我这边是正常的啊。
没见过 sum() over() 这种写法吗
你用的是 2008吧? 05里面这样写不行,去掉order by EndDay才可以
if object_id('tempdb..#xs') is not null
drop table #xs;
create table #xs(vip varchar(20), date datetime, sale float)
insert into #xs(vip, date, sale)
select 'A' as vip,'2013-1-1' as date, 230 as sale union all
select 'A','2013-1-5', 400 union all
select 'A','2013-3-6', 500 union all
select 'A','2013-4-30',600 union all
select 'B','2013-2-3',200 union all
select 'B','2013-3-5',300 union all
select 'B','2013-4-24',400
;with t as
(
select dateadd(month, number,'20130101') MonthDay
from master.dbo.spt_values
where type='p'
)
,tRange as
(
select vip,convert(varchar(6),min(date),112)+'01' MinMonth,convert(varchar(6),max(date),112)+'01' MaxMonth
from #xs
group by vip
),tDimDate as
(
select tr.vip,t.MonthDay StartDay,dateadd(d,-1,DATEADD(month,1,t.MonthDay)) EndDay
from tRange tr
join t
on tr.MinMonth<=t.MonthDay
and tr.MaxMonth>=t.MonthDay
),tdata as (
select t.vip,EndDay
,DATEDIFF(d,(select top 1 date from #xs xs where xs.date <=t.EndDay and xs.vip=t.vip order by xs.date desc ),EndDay) NoVisitDays
,count(x.date) VisitDays
,sum(isnull(sale,0)) Sales
from tDimDate t
left join #xs x
on x.[date] between t.StartDay and t.EndDay
and t.vip=x.vip
group by t.vip,EndDay
)
select vip,EndDay,NoVisitDays,VisitDays
,(select sum(VisitDays) from tdata b where a.vip=b.vip and b.EndDay<=a.EndDay ) sumVisitDays
,(select sum(Sales) from tdata c where a.vip=c.vip and c.EndDay<=a.EndDay ) sumSales
from tdata a
order by 1,2
那就还是用子查询吧!
我用的2012
我用的2005 难怪看上去这么奇怪的语句。。。
ok 结贴了 感谢ls各位大神! 尤其是stublue和jinfengyiye