Click here to Skip to main content
15,906,558 members
Home / Discussions / Database
   

Database

 
GeneralRe: concat multi rows field Pin
sepel22-Nov-08 5:21
sepel22-Nov-08 5:21 
QuestionSql bitwise in pervaisve DataBase Pin
Thaer Hamael22-Nov-08 2:19
Thaer Hamael22-Nov-08 2:19 
Questiondoubt in cross join Pin
deepthy.p.m21-Nov-08 22:01
deepthy.p.m21-Nov-08 22:01 
AnswerRe: doubt in cross join Pin
Mycroft Holmes21-Nov-08 23:26
professionalMycroft Holmes21-Nov-08 23:26 
GeneralRe: doubt in cross join Pin
deepthy.p.m23-Nov-08 17:48
deepthy.p.m23-Nov-08 17:48 
QuestionSql reporting Pin
member2721-Nov-08 20:25
member2721-Nov-08 20:25 
QuestionUDF's in Stored Procedures (not using)... Pin
Andy_L_J21-Nov-08 17:04
Andy_L_J21-Nov-08 17:04 
AnswerRe: UDF's in Stored Procedures (not using)... Pin
Wendelius21-Nov-08 23:15
mentorWendelius21-Nov-08 23:15 
Disclaimer: The following codes may contain several typos etc. They are just provided to give some ideas Smile | :)

Perhaps the easiest transformation would using a scalar instead of UDF. The following won't be the best version when considering performance, but it allows the optimizer to reorganize the query if possible:
SELECT p.Name,
	Case When pt.Name LIKE 'IN%'
		Then (SELECT Value 	  
			FROM PropVal pv1
			JOIN Prop pr1	ON pv1.PropID	= pr1.ID	    
			JOIN Prod p1	ON pv1.ProdID	= p1.ID	    
			JOIN PCat pc1	ON p1.PCatID	= pc1.ID	   
			JOIN PType pt1	ON pc1.PTypeID	= pt1.ID	  
			WHERE	p1.Name	= p.Name
			AND	pr1.Name = 'SHOT')
		Else 0
	End As 'SHOT',
	Case When pt.Name LIKE 'EX%'   
		Then (SELECT Value 	  
			FROM PropVal pv1
			JOIN Prop pr1	ON pv1.PropID	= pr1.ID	    
			JOIN Prod p1	ON pv1.ProdID	= p1.ID	    
			JOIN PCat pc1	ON p1.PCatID	= pc1.ID	   
			JOIN PType pt1	ON pc1.PTypeID	= pt1.ID	  
			WHERE	p1.Name	= p.Name
			AND	pr1.Name = 'LEN')
		Else 0
	End As 'LEN',
	...
FROM 	Prod p
JOIN 	PCat pc		ON p.PCatID = pc.ID,
JOIN 	PType pt	ON pc.PTypeID = pt.ID
...	


The next version could be using joins. Those scalars can be dropped to inline views. However, without knowledge about the amount of rows, logic etc, it's hard to describe the correct transformation. It coud be something like:
SELECT p.Name,
	Case When pt.Name LIKE 'IN%'
		Then iv1.Value
		Else 0
	End As 'SHOT',
	Case When pt.Name LIKE 'EX%'   
		Then iv2.Value
		Else 0
	End As 'LEN',
	...
FROM 	Prod p
JOIN 	PCat pc		ON p.PCatID = pc.ID,
JOIN 	PType pt	ON pc.PTypeID = pt.ID
LEFT OUTER JOIN (SELECT Value 	  
		FROM PropVal pv1
		JOIN Prop pr1	ON pv1.PropID	= pr1.ID	    
		JOIN Prod p1	ON pv1.ProdID	= p1.ID	    
		JOIN PCat pc1	ON p1.PCatID	= pc1.ID	   
		JOIN PType pt1	ON pc1.PTypeID	= pt1.ID
		WHERE pr1.Name = 'SHOT') iv1 ON iv1.Name = p.Name
LEFT OUTER JOIN (SELECT Value 	  
		FROM PropVal pv1
		JOIN Prop pr1	ON pv1.PropID	= pr1.ID	    
		JOIN Prod p1	ON pv1.ProdID	= p1.ID	    
		JOIN PCat pc1	ON p1.PCatID	= pc1.ID	   
		JOIN PType pt1	ON pc1.PTypeID	= pt1.ID
		WHERE pr1.Name = 'LEN') iv2 ON iv2.Name	= p.Name
...	

Hope this helps you.

The need to optimize rises from a bad design.

My articles[^]

AnswerRe: UDF's in Stored Procedures (not using)... Pin
Mycroft Holmes21-Nov-08 23:20
professionalMycroft Holmes21-Nov-08 23:20 
GeneralRe: UDF's in Stored Procedures (not using)... Pin
Wendelius21-Nov-08 23:58
mentorWendelius21-Nov-08 23:58 
GeneralRe: UDF's in Stored Procedures (not using)... Pin
Ben Fair25-Nov-08 4:16
Ben Fair25-Nov-08 4:16 
QuestionKeeping track of Pin
CodingYoshi21-Nov-08 9:16
CodingYoshi21-Nov-08 9:16 
AnswerRe: Keeping track of Pin
Mycroft Holmes21-Nov-08 13:06
professionalMycroft Holmes21-Nov-08 13:06 
AnswerFunction for Status Pin
David Mujica21-Nov-08 13:39
David Mujica21-Nov-08 13:39 
GeneralRe: Function for Status Pin
Mycroft Holmes21-Nov-08 23:12
professionalMycroft Holmes21-Nov-08 23:12 
AnswerRe: Keeping track of Pin
Wendelius21-Nov-08 23:43
mentorWendelius21-Nov-08 23:43 
Questionconvert from MSAccess to Oracle Pin
sajid.salim.khan21-Nov-08 5:46
sajid.salim.khan21-Nov-08 5:46 
AnswerRe: convert from MSAccess to Oracle Pin
David Mujica21-Nov-08 6:01
David Mujica21-Nov-08 6:01 
GeneralRe: convert from MSAccess to Oracle Pin
sajid.salim.khan21-Nov-08 6:15
sajid.salim.khan21-Nov-08 6:15 
QuestionImport excel to Sql server Pin
Abhilash121-Nov-08 3:02
Abhilash121-Nov-08 3:02 
AnswerRe: Import excel to Sql server Pin
indian14321-Nov-08 3:20
indian14321-Nov-08 3:20 
AnswerRe: Import excel to Sql server Pin
indian14321-Nov-08 8:23
indian14321-Nov-08 8:23 
AnswerRe: Import excel to Sql server Pin
Jerry Hammond21-Nov-08 14:06
Jerry Hammond21-Nov-08 14:06 
AnswerRe: Import excel to Sql server Pin
Wendelius21-Nov-08 22:19
mentorWendelius21-Nov-08 22:19 
AnswerRe: Import excel to Sql server Pin
indian14324-Nov-08 0:43
indian14324-Nov-08 0:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.