SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 SQL HELP
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

egemen_ates
Yak Posting Veteran

Turkey
65 Posts

Posted - 06/11/2012 :  10:25:47  Show Profile  Reply with Quote
I think this query very poor by performance,select a b c d e f from product cost table how can i control null control in one null or another operator.

SELECT ISNULL((
SELECT TOP 1

A+B
FROM
PRODUCT_COST WITH (NOLOCK)
WHERE
START_DATE <= {ts '2012-05-31 00:00:00'}
AND PRODUCT_ID = S.PRODUCT_ID

ORDER BY
START_DATE DESC,
RECORD_DATE DESC,
PRODUCT_COST_ID DESC
),0) EX_1,
ISNULL((
SELECT TOP 1

C+D

FROM
PRODUCT_COST WITH (NOLOCK)
WHERE
START_DATE <= {ts '2012-06-11 00:00:00'}
AND PRODUCT_ID = S.PRODUCT_ID

ORDER BY
START_DATE DESC,
RECORD_DATE DESC,
PRODUCT_COST_ID DESC
),0) EX2,
ISNULL((
SELECT TOP 1

D+E

FROM
PRODUCT_COST WITH (NOLOCK)
WHERE
START_DATE <= {ts '2012-06-11 00:00:00'}
AND PRODUCT_ID =S.PRODUCT_ID

ORDER BY
START_DATE DESC,
RECORD_DATE DESC,
PRODUCT_COST_ID DESC
),0) EX3

FROM
STOCKS S WITH (NOLOCK)

yosiasz
Flowing Fount of Yak Knowledge

USA
1610 Posts

Posted - 06/11/2012 :  12:53:51  Show Profile  Click to see yosiasz's MSN Messenger address  Reply with Quote
please post some sample data with ddl and dml

<><><><><><><><><><><><><><><><><>
If you don't have the passion to help people, you have no passion
Go to Top of Page

Lamprey
Flowing Fount of Yak Knowledge

3856 Posts

Posted - 06/11/2012 :  14:25:16  Show Profile  Reply with Quote
without knowing you data, table structure or indexing, it's hard to say. Here is a guess that may or may not be faster:
SELECT
	COALESCE(A.A + A.B, 0) AS EX_1,
	COALESCE(A.C + A.D, 0) AS EX2,
	...
FROM 
	STOCKS S WITH (NOLOCK)
LEFT OUTER JOIN
	(
		SELECT 
			A,
			B,
			C,
			D,
			E,
			PRODUCT_ID,
			ROW_NUMBER() OVER 
			(
				PARTITION BY 
					PRODUCT_ID 
				ORDER BY 
					START_DATE DESC, 
					RECORD_DATE DESC,
					PRODUCT_COST_ID DESC
			) AS RowNum
		FROM 
			PRODUCT_COST WITH (NOLOCK) 
		WHERE 
			START_DATE <= CAST('2012-06-11T00:00:00' AS DATETIME)
	) AS A
ON
	S.PRODUCT_ID = A.PRODUCT_ID
	AND RowNum = 1
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
48064 Posts

Posted - 06/11/2012 :  15:38:57  Show Profile  Reply with Quote
i didnt understand what you mean by
how can i control null control in one null or another operator

can you elaborate with an example

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000