Re: Using Function Table ? using Sql Server 2008
- From: "Luqman" <pearlsoft@xxxxxxxxxxxx>
- Date: Sat, 27 Jun 2009 23:14:59 +0500
Hi,
Thanks very much for the help, it really worked out.
As I have no sound knowledge about performance and tuning of queries, however I have heard that performance can be evaluated using Execution Plan, is it true? if so, how ?
I will appreciate if you please point me to the right direction.
Take care!
Luqman
"Dan Guzman" <guzmanda@xxxxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message news:Od0oAQ09JHA.1492@xxxxxxxxxxxxxxxxxxxxxxx
I want to retrieve all Employees who are missing in particular Dates.
I suggest you change the function to use strongly-typed dates as this will make the query clearer. Better yet, create a permanent Calendar table to facilitate these types of queries. Below is a refactored example that I think will provide the desired results and also perform better:
ALTER FUNCTION dbo.GetDates
(
@StartDate datetime,
@EndDate datetime
)
RETURNS @ret TABLE(DateValue datetime)
AS
BEGIN
WITH
L0 AS (SELECT 0 AS N UNION ALL SELECT 1),
L1 AS (SELECT A.N+(B.N*2) AS N FROM L0 AS A, L0 AS B),
L2 AS (SELECT A.N+(B.N*4) AS N FROM L1 AS A, L1 AS B),
L3 AS (SELECT A.N+(B.N*16) AS N FROM L2 AS A, L2 AS B),
NUM AS (SELECT A.N+(B.N*256) AS N FROM L3 AS A, L3 AS B)
INSERT INTO @ret(DateValue)
SELECT DATEADD(day, N, @StartDate)
FROM NUM
WHERE DATEADD(day, N, @StartDate) BETWEEN @StartDate AND @EndDate
RETURN
END
GO
DECLARE
@FirstDate datetime,
@LastDate datetime
SELECT
@FirstDate = '20090601',
@LastDate = '20090630'
SELECT
A.EMPLOYEEID,
B.DateValue
FROM EMPLOYEE A
CROSS JOIN dbo.GetDates(@FirstDate,@LastDate) B
WHERE NOT EXISTS(
SELECT *
FROM transactionold
WHERE
employeeid = A.employeeid
AND transactionold.cDate = B.DateValue
)
--
Hope this helps.
Dan Guzman
SQL Server MVP
http://weblogs.sqlteam.com/dang/
"Luqman" <pearlsoft@xxxxxxxxxxxx> wrote in message news:%23wAZmZz9JHA.4692@xxxxxxxxxxxxxxxxxxxxxxxI have created a function to find missing Dates.
The function name is GetDates.
CREATE FUNCTION dbo.GetDates
(
@Start BIGINT,
@End BIGINT
)
RETURNS @ret TABLE(Number BIGINT)
AS
BEGIN
WITH
L0 AS (SELECT 1 AS C UNION ALL SELECT 1), L1 AS (SELECT 1 AS C FROM L0 AS A, L0 AS B),
L2 AS (SELECT 1 AS C FROM L1 AS A, L1 AS B),
L3 AS (SELECT 1 AS C FROM L2 AS A, L2 AS B),
L4 AS (SELECT 1 AS C FROM L3 AS A, L3 AS B),
L5 AS (SELECT 1 AS C FROM L4 AS A, L4 AS B),
num AS (SELECT ROW_NUMBER() OVER(ORDER BY C) AS N FROM L5)
INSERT INTO @ret(Number) SELECT N FROM NUM WHERE N BETWEEN @Start AND @End
RETURN
END
I want to retrieve all Employees who are missing in particular Dates.
I tried following query but could not get the desired results.
SELECT EMPLOYEEID,CAST(@nfirstdate AS DATETIME) + Number-1
FROM EMPLOYEE A, dbo.GetDates(@nFirstDay,@nLastDay) B
WHERE NOT EXISTS
(SELECT * FROM transactionold
WHERE employeeid=A.employeeid and cDate=CAST(@nfirstdate AS DATETIME) + Number-1)
Any idea please ?
Thanks and Regards,
Luqman
.
- Follow-Ups:
- Re: Using Function Table ? using Sql Server 2008
- From: Dan Guzman
- Re: Using Function Table ? using Sql Server 2008
- From: Erland Sommarskog
- Re: Using Function Table ? using Sql Server 2008
- From: Plamen Ratchev
- Re: Using Function Table ? using Sql Server 2008
- References:
- Using Function Table ? using Sql Server 2008
- From: Luqman
- Re: Using Function Table ? using Sql Server 2008
- From: Dan Guzman
- Using Function Table ? using Sql Server 2008
- Prev by Date: Re: Using Function Table ? using Sql Server 2008
- Next by Date: Re: Query Cursor from Another Cursor
- Previous by thread: Re: Using Function Table ? using Sql Server 2008
- Next by thread: Re: Using Function Table ? using Sql Server 2008
- Index(es):
Relevant Pages
|
Loading