Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
obinna
Starting Member
26 Posts |
Posted - 2007-12-12 : 10:34:24
|
| I have a View with a structure like thisVisitTimes Company2007-07-10 14:24:38.000 Microsoft2007-03-10 11:14:38.000 Microsoft2007-12-01 13:04:56.000 SQLTeam2007-12-13 12:54:52.000 GoldMan Sac2007-08-11 02:15:38.000 Oracle2007-02-11 12:45:04.000 SAP LtdI am asked to write a stored procedure that get a count of each count of visit on a START and END dateI wrote the below SP but am not getting the right result I think the **where VisitTime >= @Start AND VisitTime <= @End)** is not being evaluated. Help plsCREATE procedure dbo.GetVisits@Start varchar(50),@End varchar(50)asSELECT TOP 100 PERCENT COUNT(company) AS VisitCount, companyFROM visits.dbo.IViewVisits where EXISTS (SELECT * FROM VISITS.dbo.IViewVisits where VisitTime >= @Start AND VisitTime <= @End)GROUP BY company ORDER BY COMPANY ASC |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-12-12 : 10:35:16
|
Because there is no relation between exist and outer query. E 12°55'05.25"N 56°04'39.16" |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-12-12 : 10:35:56
|
change toVisitTime < dateadd(day, 1, @End) make sure your @start and @end does not contain time KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-12-12 : 10:37:06
|
Also, the EXISTS is absolutely unnecessary!CREATE PROCEDURE dbo.GetVisits( @Start VARCHAR(50), @End VARCHAR(50))ASSET NOCOUNT ONSELECT COUNT(*) AS VisitCount, CompanyFROM Visits.dbo.IViewVisitsWHERE VisitTime >= @Start AND VisitTime <= @EndGROUP BY CompanyORDER BY Company E 12°55'05.25"N 56°04'39.16" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-12-12 : 10:41:30
|
quote: Originally posted by khtan change toVisitTime < dateadd(day, 1, @End) make sure your @start and @end does not contain time
A dangerous assumption. OP may already have this "next day value" in the variable. E 12°55'05.25"N 56°04'39.16" |
 |
|
|
obinna
Starting Member
26 Posts |
Posted - 2007-12-12 : 10:47:28
|
| Peso all the best, it worked like charm - thanks people |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-12-13 : 01:02:10
|
| Also use proper DATETIME datatype instead of varchar in the procedureMadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|
|
|