| Author |
Topic |
|
mike123
Master Smack Fu Yak Hacker
1462 Posts |
Posted - 2007-01-27 : 10:22:07
|
| Hi,I have the following SPROC below, which is working perfectly, however I would like to add just 1 more column. The column would be called "totalMapViews_toCurrentTime"This column would SELECT the same count as "totalMapViews" however it would add a WHERE clause that would limit by the current time. For instance if it was run on Jan,25,2007 at 330am it would select the count of total records for todays date, and all previous dates the total would be until 330am only.Can anyone help me figure out the best way to do this? Having problems with writing the query.Thank you very much once again for any help!!mike123CREATE PROCEDURE dbo.select_stats_TotalMapViews_Creations_Ratio ( @NumDays INT) ASSET NOCOUNT ONSELECT c.DateCreated AS DateCreated,TotalMapsCreated,TotalMapsCreated_UniqueIPS,TotalMapViews,TotalMapViews_UniqueIPS, CASE WHEN v.UniqueIPS = 0 THEN 0 ELSE 1.0 * v.UniqueIPS / c.UniqueIPS END as MapView_Create_Ratio FROM ( SELECT DATEADD(day, DATEDIFF(day, 0, dateCreated), 0) AS DateCreated, COUNT(DISTINCT(CreatorIP)) AS UniqueIPS, COUNT(MD.mapID) AS TotalMapsCreated, COUNT(distinct(MD.creatorIP)) AS TotalMapsCreated_UniqueIPS FROM tblMapDetails MD WHERE DateCreated >= DATEADD(day, -@NumDays, DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0)) GROUP BY DATEADD(day, DATEDIFF(day, 0, DateCreated), 0) ) AS cINNER JOIN ( SELECT DATEADD(day, DATEDIFF(day, 0, ViewDate), 0) AS ViewDate, COUNT(DISTINCT(IP)) AS UniqueIPS, COUNT(MVL.mapID) AS TotalMapViews, COUNT(DISTINCT(MVL.IP)) AS TotalMapViews_UniqueIPS FROM tblMapViews_Log MVL WHERE ViewDate >= DATEADD(day, -@NumDays, DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0)) GROUP BY DATEADD(day, DATEDIFF(day, 0, ViewDate), 0) ) AS v ON v.ViewDate = c.DateCreatedORDER BY DateCreated descGOCREATE TABLE [dbo].[tblMapViews_Log] ( [mapID] [int] NOT NULL , [IP] [varchar] (15) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [city] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [viewDate] [smalldatetime] NOT NULL ) ON [PRIMARY]GOCREATE TABLE [dbo].[tblMapDetails] ( [mapID] [int] IDENTITY (1, 1) NOT NULL , [dateCreated] [datetime] NOT NULL , [lastUpdated] [datetime] NOT NULL ) ON [PRIMARY]GO |
|
|
Vinnie881
Master Smack Fu Yak Hacker
1231 Posts |
Posted - 2007-01-28 : 01:55:33
|
| /*I would actually write the query creating a couple tmp tables to keep it neet rather then the way you have it, but to simply modify your query you could do what I illustrated below. Your query was a little difficult to decipher w/o knowing exactly what your db structure is, but you should be able to get the basic idea. I hope this helps*/SELECT c.DateCreated AS DateCreated,TotalMapsCreated,TotalMapsCreated_UniqueIPS,TotalMapViews,TotalMapViews_UniqueIPS,CASEWHEN v.UniqueIPS = 0 THEN 0ELSE 1.0 * v.UniqueIPS / c.UniqueIPSEND as MapView_Create_Ratio,--***Select COUNT(MVL.mapID) AS TotalMapViewsFROM tblMapViews_Log MVLWHERE ViewDate <= getdate()as totalMapViews_toCurrentTime--****FROM (SELECT DATEADD(day, DATEDIFF(day, 0, dateCreated), 0) AS DateCreated,COUNT(DISTINCT(CreatorIP)) AS UniqueIPS,COUNT(MD.mapID) AS TotalMapsCreated, COUNT(distinct(MD.creatorIP)) AS TotalMapsCreated_UniqueIPSFROM tblMapDetails MDWHERE DateCreated >= DATEADD(day, -@NumDays, DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0))GROUP BY DATEADD(day, DATEDIFF(day, 0, DateCreated), 0)) AS cINNER JOIN (SELECT DATEADD(day, DATEDIFF(day, 0, ViewDate), 0) AS ViewDate,COUNT(DISTINCT(IP)) AS UniqueIPS,COUNT(MVL.mapID) AS TotalMapViews, COUNT(DISTINCT(MVL.IP)) AS TotalMapViews_UniqueIPSFROM tblMapViews_Log MVLWHERE ViewDate >= DATEADD(day, -@NumDays, DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0))GROUP BY DATEADD(day, DATEDIFF(day, 0, ViewDate), 0)) AS v ON v.ViewDate = c.DateCreatedORDER BY DateCreated descGOCREATE TABLE [dbo].[tblMapViews_Log] ([mapID] [int] NOT NULL ,[IP] [varchar] (15) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,[city] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,[viewDate] [smalldatetime] NOT NULL) ON [PRIMARY]GOCREATE TABLE [dbo].[tblMapDetails] ([mapID] [int] IDENTITY (1, 1) NOT NULL ,[dateCreated] [datetime] NOT NULL ,[lastUpdated] [datetime] NOT NULL) ON [PRIMARY]GO |
 |
|
|
|
|
|