|
manasa601
Starting Member
1 Post |
Posted - 2010-05-21 : 12:44:40
|
| I am trying to write a sql query that will give me the usage details between a start date and end dateHere is the query DECLARE @T1 TABLE (T1Months Datetime,NewUsers int)INSERT INTO @T1 (T1Months,NewUsers)SELECTDateAdd(Month, DateDiff(Month, 0, RegisteredOn), 0) as Months,COUNT(users.Id) AS NewUsersFROM [TCV_11_PROD].[dbo].[Users] WHERE RegisteredOn is not Null and RegisteredOn > '2008-06-01 12:00 AM' and RegisteredOn < '2010-05-31 12:00 AM'GROUP BY DateAdd(Month, DateDiff(Month, 0, RegisteredOn), 0)DECLARE @T2 TABLE( Months Datetime,Newusers int,TotalUsers int)INSERT INTO @T2 ( Months, Newusers, TotalUsers)Select T1Months, NewUsers ,(Select sum(NewUsers) from @T1where T1Months <= T.T1Months) As TotalUsersfrom @T1 Torder by T1months descDECLARE @T3 TABLE (T3Months Datetime,Newcerts int)INSERT INTO @T3 (T3Months,Newcerts)SELECT DateAdd(Month, DateDiff(Month, 0,CertificationDate), 0) AS Months,COUNT(*) AS NewCertsFROM [TCV_11_PROD].[dbo].[Projects] INNER JOIN [TCV_11_PROD].[dbo].[Users] ON [TCV_11_PROD].[dbo].Projects.BuilderID = [TCV_11_PROD].[dbo].Users.ID WHERE RegisteredOn is not null And CertificationDate is not Null and CertificationDate > '2008-06-01 12:00 AM' and CertificationDate < '2010-05-31 12:00 AM' GROUP BY DateAdd(Month, DateDiff(Month, 0, CertificationDate), 0)ORDER BY DateAdd(Month, DateDiff(Month, 0, CertificationDate), 0) DESCDECLARE @T4 TABLE( Months Datetime, Newcerts int,Totalcerts int)INSERT INTO @T4 ( Months, Newcerts, TotalCerts)Select T3Months, Newcerts ,(Select sum(Newcerts) from @T3where T3Months <= T.T3Months) As Totalcertsfrom @T3 Torder by T3months descSelect T4.Months,TotalUsers, TotalCertsfrom @T4 T4 inner join @T2 T2 on T4.Months=T2.Monthsorder by T4.months desc as we want to see user details even if it is "0" (for the graph)my boss wanted me to work as shown below--Find the Range of Days Available--* Need to convert this to the start of the monthdeclare @maxmonth datetimedeclare @minmonth datetimeset @maxmonth = (Select MAX(CreatedOn) From Projects)print @maxmonthset @minmonth = (Select Min(CreatedOn) From Projects)print @minmonth/* Populate a Month ListUsing the previously found dates create a list of onlyselectable months by filtering based on what you find formaxmonth and minmonth *//* Join the month list with any metric queries so thatempty months still show up in your result sets *//* 2008 R2 join the metric sets together so you get County, Monthly_Users,Total_users,Montly_Projects,Total_Projects,Montly_Certs,Total_Certsand City, Monthly_Users,Total_users,Montly_Projects,Total_Projects,Montly_Certs,Total_Certs *//* 2008 R2 - if you join all the metric sets together, you can parameterize a subselectas Select ? and use a custom table to list available metrics. You can then select individual metrics and show them by themselves(this will be key in doing this in GIS/mapping as the GIS object onlywants 2 columns - county & value) *//* 2008 R2 - In reporting services, you can take the top two queries that populatea month list and use them all by themselve to populate a rangethat you can use as a paramter ... This will let you create a reporttool that you can use to select any individual month .. this will alsohelp in GIS/mapping as you can drilldown to a map that shows only the current month and pairs with the previous parameter you can builda report that you can select a metric and select a month and visuallycompare the metrics*/ |
|