SQL query to partition over entire data but show specific rows of them
I have a table with fields
1: Visit Place 2: Visit Date
I have to calculate the visit No. of any visit in specific date range.Visit range will be calculate on month basis means how many times a place is visited but data should be shown only of required date.开发者_JS百科
My query is as:
SELECT visit_date,
VisitPlaceId,
[Visit Frequency]
FROM (SELECT visit_date,
VisitPlaceId,
'Visit-' + CONVERT(VARCHAR, ROW_NUMBER() OVER ( PARTITION BY
VisitPlaceId ORDER
BY
VisitPlaceId))AS [Visit Frequency]
FROM tbl_Visits
WHERE visit_date >= '01/Sep/2011'
AND visit_date <= '30/Sep/2011'
GROUP BY visit_date,
VisitPlaceId) tbl
WHERE visit_date >= '01/Sep/2011'
AND visit_date <= '05/Sep/2011'
Can it be modified to result faster.
Looking for your kind and precious solutions.
Thanks. Supriya
Ya,it is like
CREATE TABLE dbo.tbl_Visits
(
Report_ID INT IDENTITY NOT NULL,
User_ID INT NOT NULL,
City_ID INT NOT NULL,
VisitPlaceId INT NOT NULL,
CategoryId INT NOT NULL,
Area VARCHAR (512) NULL,
Remarks VARCHAR (264) NULL,
visit_date SMALLDATETIME NULL,
POSM_Used VARCHAR (250) NULL,
PosmBanner INT NULL,
PosmBooklet INT NULL,
PosmBunting INT NULL,
PosmDangler INT NULL,
PosmDummyBox INT NULL,
PosmTearPad INT NULL,
PosmPoster INT NULL,
PosmShelfStrip INT NULL,
PosmStandee INT NULL,
PosmTableTop INT NULL,
PosmWobbler INT NULL,
PosmLeafLet INT NULL,
PosmBrochure INT NULL,
Exe_Remarks VARCHAR (200) NULL,
ApprovalRemarks VARCHAR (50) NULL,
ApprovalStatus VARCHAR (10) NULL,
ApprovalDate SMALLDATETIME NULL,
ApprovedBy INT NULL
)
GO
And no index I have created yet.
You could add a non clustered index as below
CREATE NONCLUSTERED INDEX ix_name On tbl_Visits(visit_date,VisitPlaceId)
This covers the query and allows the visit_date
range of interest to be seeked to directly.
精彩评论