下面是创建一个存储过程:
USE [dnn6_1] GO /****** Object: StoredProcedure [dbo].[WNKAdvise_UpdateItemsLog_GetTemplatesUsingInfo] Script Date: 2013/4/28 9:22:58 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO --IF EXISTS(SELECT * FROM sysobjects WHERE name='WNKAdvise_UpdateItemsLog_GetTemplatesUsingInfo') -- DROP PROCEDURE WNKAdvise_UpdateItemsLog_GetTemplatesUsingInfo
ALTER PROCEDURE [dbo].[WNKAdvise_UpdateItemsLog_GetTemplatesUsingInfo] -- 定义存储过程的参数-- @TemplateType int, @TemplateAddType int, @StartDate DateTime, @EndDate DateTime AS BEGIN SET DATEFORMAT ymd; --定义时间变量-- declare @EndDate0 DateTime declare @StartDate0 DateTime declare @StartDate1 DateTime declare @StartDate2 DateTime
set @StartDate = null set @EndDate = null
--获取系统的当前时间 select @EndDate0 = getdate() --select @StartDate0 = dateadd(dd,-1,getdate()) --120的作用是将获取的当前时间按2006-05-16 10:57:49格式显示 select @StartDate0 = convert(varchar(10),dateadd(hh,-24,getdate()),120) --声明将当前时间退回到七天前 select @StartDate1 = dateadd(dd,-7,getdate()) --声明将当前时间回退到一个月前 select @StartDate2 = dateadd(mm,-1,getdate())
--当天 SELECT lg.TemplateId,count(lg.TemplateId) as DayCount INTO #DayAccess FROM dbo.WNKAdvise_UpdateItemsLog lg inner join WNKAdvise_AdviseTemplates t ON t.TemplateId = lg.TemplateId WHERE (CreatedTime > @StartDate0 OR @StartDate0 is null) AND (CreatedTime <= @EndDate0 OR @EndDate0 is null) AND (TemplateType = @TemplateType OR @TemplateType=-1) AND (TemplateAddType = @TemplateAddType OR @TemplateAddType = -1) GROUP BY lg.TemplateId
--最近一周 SELECT lg.TemplateId,COUNT(lg.TemplateId) as WeekCount INTO #WeekAccess FROM dbo.WNKAdvise_UpdateItemsLog lg inner join WNKAdvise_AdviseTemplates t ON t.TemplateId = lg.TemplateId WHERE (CreatedTime >= @StartDate OR @StartDate is null) AND (CreatedTime <= @EndDate OR @EndDate is null) AND (TemplateType = @TemplateType OR @TemplateType=-1) AND (TemplateAddType = @TemplateAddType OR @TemplateAddType = -1) GROUP BY lg.TemplateId
--最近一月 SELECT lg.TemplateId,COUNT(lg.TemplateId) as MonthCount INTO #MonthAccess FROM dbo.WNKAdvise_UpdateItemsLog lg inner join WNKAdvise_AdviseTemplates t ON t.TemplateId = lg.TemplateId WHERE (CreatedTime >= @StartDate2 OR @StartDate2 is null) AND (CreatedTime <= @EndDate0 OR @EndDate0 is null) AND (TemplateType = @TemplateType OR @TemplateType=-1) AND (TemplateAddType = @TemplateAddType OR @TemplateAddType = -1) GROUP BY lg.TemplateId
--总计 SELECT lg.TemplateId,Count(lg.TemplateId) as ToatalCount INTO #HistoryAccess FROM dbo.WNKAdvise_UpdateItemsLog lg inner join WNKAdvise_AdviseTemplates t ON t.TemplateId = lg.TemplateId WHERE (TemplateType = @TemplateType OR @TemplateType=-1) AND (TemplateAddType = @TemplateAddType OR @TemplateAddType = -1) GROUP BY lg.TemplateId
SELECT h.TemplateId,ToatalCount FROM #WeekAccess w,#HistoryAccess h
END
执行后有很多的重复项,如何删除重复项值保留一项!俺是第一次写存储过程,请大虾门赐教!
假设一个表test(ID,a,b),其中ID是主键,a和b是要判断是否重复的列,写法如下:
delete test where (a,b) in ( select a,b from ( select a,b,count(*) as rowc from test group by a,b ) where rowc > 0 ) and id not in ( select min(id) from test group by a,b )
这是oracle的写法,如果是sqlserver,请考虑使用top关键字
查询的时候distinct。上面的代码有点头晕,不知道对你有用没!
Group By 列名
直接distinct吧,这个看着头晕啊