 




<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>IT Answers &#187; SQL query optimization</title>
	<atom:link href="http://itknowledgeexchange.techtarget.com/itanswers/tag/sql-server/sql-query-optimization/feed/" rel="self" type="application/rss+xml" />
	<link>http://itknowledgeexchange.techtarget.com/itanswers</link>
	<description></description>
	<lastBuildDate>Sat, 18 May 2013 21:33:01 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>Challenge improving query performance on large tables</title>
		<link>http://itknowledgeexchange.techtarget.com/itanswers/challenge-improving-query-performance-on-large-tables/</link>
		<comments>http://itknowledgeexchange.techtarget.com/itanswers/challenge-improving-query-performance-on-large-tables/#comments</comments>
		<pubDate>Tue, 27 Sep 2011 14:21:07 +0000</pubDate>
		<dc:creator>Sport2000</dc:creator>
				<category><![CDATA[Query optimization]]></category>
		<category><![CDATA[Query performance]]></category>
		<category><![CDATA[SQL query optimization]]></category>
		<category><![CDATA[SQL Server 2008]]></category>
		<category><![CDATA[SQL Server 2008 R2]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Hi, Here&#8217;s the structure of the db I&#8217;m using: * Table frs_ArticlesBDFournisseur which contains article main data.One row per article/color. Key is Refrefart (int). Size is over 220000 rows at the momemt. Only index is the PK (clusterred). * Table frs_ArticlesBDFournisseurTaille contains all the various sizes and barcode for the corresponding article/color from the previous [...]]]></description>
				<content:encoded><![CDATA[<p>Hi,<br/><br/> Here&#8217;s the structure of the db I&#8217;m using: * Table frs_ArticlesBDFournisseur which contains article main data.One row per article/color. Key is Refrefart (int). Size is over 220000 rows at the momemt. Only index is the PK (clusterred). * Table frs_ArticlesBDFournisseurTaille contains all the various sizes and barcode for the corresponding article/color from the previous table. Key is RefREFARTTAILLE (int). Size is over 1200000 rows at the moment. refrefart column also exists in this table to link with the previous table, altough there is no physical constraint linking the 2 tables. Indexes are PK (clustered), refrefart (non clustered) and refrefart,taille,codeean,codeeanfrs (non clustered).<br/><br/> Here&#8217;s what I want to do: I work at headquarters of franchise shops. I have to collect data from each selling point, match it with our own database (the one discribed above) to normalise the incoming data and complete it with more details. The best way to match an article is to use its barcode (when it exits in the shop data). We pack up to 2 barcodes with every article (callled CodeEan and CodeEanFrs from frs_ArticlesBDFournisseurTaille). I need to complete the information I have with the article size (called taille from frs_ArticlesBDFournisseurTaille), the article&#8217;s season (called Saison from frs_ArticlesBDFournisseur) and the number of different season the article has been existing in (to know whether it is a one season product or a permanent product).<br/><br/> Here&#8217;s the challenge for the query at hand: There is no constant data in frs_ArticlesBDFournisseurTaille with which I can do a GROUP BY. I need to get the latest product data (in case there is more than one season) and the total number of seasons.<br/><br/> Here&#8217;s the query I&#8217;ve designed: SELECT M.*, ART_REF, A.ART_PSEUDO, ART_NOM, ART_FEDAS, ART_ANNEECREATION, COU_CODE, COU_NOM, TGF_NOM, MRK_IDREF, MRK_NOM, MRK_NOM_ORI, CBI_EAN, CBI_EANFRS, B.RefRefArt, B.Taille AS sp2kTaille, CASE when B.CodeEan = &#8221; then null else B.CodeEan END AS sp2kEan, CASE when B.CodeEanFrs = &#8221; then null else B.CodeEanFrs END AS sp2kEanFrs, B.Saison, Coalesce(B.Pos, 0) as Permanent FROM gin_Mouvements M LEFT JOIN gin_Articles A ON M.MOV_ARTID=A.ART_ID LEFT JOIN gin_Couleurs C ON M.MOV_COUID=C.COU_ID LEFT JOIN gin_Tailles T ON M.MOV_TGFID=T.TGF_ID LEFT JOIN gin_Marques N ON A.ART_MRKID=N.MRK_ID LEFT JOIN gin_Ean E ON (M.MOV_ARTID=E.CBI_ARTID AND M.MOV_COUID=E.CBI_COUID AND M.MOV_TGFID=E.CBI_TGFID) OUTER APPLY (SELECT TOP 1 T.RefRefArt, CodeEan, CodeEANFrs, Taille, Art.Saison, Rank() over (order by T.RefREFART ASC) as Pos FROM frs_ArticlesBDFournisseurTaille T LEFT JOIN frs_ArticlesBDFournisseur Art ON T.RefREFART=Art.RefREFART WHERE (CodeEAN = E.CBI_EAN OR CodeEANFrs = CBI_EANFRS OR CodeEANFrs = CBI_EAN OR CodeEAN = CBI_EANFRS) ORDER BY RefREFART DESC) B<br/><br/> Query explained: All the first part, with all the left join, is to gather the data, relatively small, from the shops. There is no performance issue here. I&#8217;ve used an outer apply to avoid having to do a left join and the ON conditions which I couldn&#8217;t get to work to return at the same time the max(refrefart) and the total number of season; remember I can not make a group by.<br/><br/> Performances: This query runs at a 20row/s at best (nearly 20s to return 500 rows). The data in both tables described at the beginning is bound to increase twice a year by about 10000 new products and 6 times more for the barcodes. I&#8217;m afraid the performances will be degrading even more soon.<br/><br/> Advice ?: If anyone can see how to improve the query or rewrite the query differently or improve the db or else &#8230; please let me know.<br/><br/> Stats: Execution plan sum up: it is spending 57% on Nested Loops (Left Outer Join) and 40% on Index Scan (NonClustered)[sp2k_staging].[dbo].[frs_ArticlesBDFournisseurTaille].<art_ean] [T]. They are thick lines but they are mostly to the right of the diagram. The client statistics based on a TOP 500 run of the query: Client Execution Time 10:45:36   Query Profile Statistics      Number of INSERT, DELETE and UPDATE statements  0  0.0000   Rows affected by INSERT, DELETE, or UPDATE statements 0  0.0000   Number of SELECT statements        2  2.0000   Rows returned by SELECT statements     501  501.0000   Number of transactions         0  0.0000 Network Statistics      Number of server roundtrips       3  3.0000   TDS packets sent from client       3  3.0000   TDS packets received from server      119  119.0000   Bytes sent from client        2452 2452.0000   Bytes received from server       478675 478675.0000 Time Statistics      Client processing time        16895 16895.0000   Total execution time         18158 18158.0000   Wait time on server replies       1263 1263.0000<br/><br/> Thank you.<br/><br/></p>
<!-- wpms-network-global-inserts -->]]></content:encoded>
			<wfw:commentRss>http://itknowledgeexchange.techtarget.com/itanswers/challenge-improving-query-performance-on-large-tables/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Infinite recursion in SQL Server 2000</title>
		<link>http://itknowledgeexchange.techtarget.com/itanswers/infinite-recursion/</link>
		<comments>http://itknowledgeexchange.techtarget.com/itanswers/infinite-recursion/#comments</comments>
		<pubDate>Mon, 27 Sep 2010 06:52:18 +0000</pubDate>
		<dc:creator>Gomsbk</dc:creator>
				<category><![CDATA[Query optimization]]></category>
		<category><![CDATA[Recursion]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL query optimization]]></category>
		<category><![CDATA[SQL Server 2000]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[consider a recursive query in SQL.How to handle infinite recursion to make the query evaluation optimized?]]></description>
				<content:encoded><![CDATA[<p>consider a recursive query in SQL.How to handle infinite recursion to make the query evaluation optimized?<br/><br/></p>
<!-- wpms-network-global-inserts -->]]></content:encoded>
			<wfw:commentRss>http://itknowledgeexchange.techtarget.com/itanswers/infinite-recursion/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Joining 3 tables in SQL Server 2005</title>
		<link>http://itknowledgeexchange.techtarget.com/itanswers/join-3-tables-how/</link>
		<comments>http://itknowledgeexchange.techtarget.com/itanswers/join-3-tables-how/#comments</comments>
		<pubDate>Thu, 12 Aug 2010 12:37:01 +0000</pubDate>
		<dc:creator>Macha1973</dc:creator>
				<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL query optimization]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Server 2005]]></category>
		<category><![CDATA[SQL Server Query]]></category>
		<category><![CDATA[SQL tables]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Here are the 3 simple SELECT queries i have but i want to merge them all into 1 singl query.]]></description>
				<content:encoded><![CDATA[<p>Here are the 3 simple SELECT queries i have but i want to merge them all into 1 singl query.</p>
<!-- wpms-network-global-inserts -->]]></content:encoded>
			<wfw:commentRss>http://itknowledgeexchange.techtarget.com/itanswers/join-3-tables-how/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Improving the initial speed of a SQL query</title>
		<link>http://itknowledgeexchange.techtarget.com/itanswers/improving-the-initial-speed-of-a-sql-query/</link>
		<comments>http://itknowledgeexchange.techtarget.com/itanswers/improving-the-initial-speed-of-a-sql-query/#comments</comments>
		<pubDate>Tue, 21 Jul 2009 18:11:54 +0000</pubDate>
		<dc:creator>SQL Server Ask the Experts</dc:creator>
				<category><![CDATA[SQL queries]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL query optimization]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[I have optimized my SQL query to the maximum extent. The first time I ran the query, it took 7 seconds; the next time it took half a second. Why did this happen? How can I make the query run faster the first time around?]]></description>
				<content:encoded><![CDATA[<p>I have optimized my SQL query to the maximum extent. </p>
<p>The first time I ran the query, it took 7 seconds; the next time it took half a second. </p>
<p>Why did this happen? How can I make the query run faster the first time around?</p>
<!-- wpms-network-global-inserts -->]]></content:encoded>
			<wfw:commentRss>http://itknowledgeexchange.techtarget.com/itanswers/improving-the-initial-speed-of-a-sql-query/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sql Query in AS400</title>
		<link>http://itknowledgeexchange.techtarget.com/itanswers/sql-query-in-as400/</link>
		<comments>http://itknowledgeexchange.techtarget.com/itanswers/sql-query-in-as400/#comments</comments>
		<pubDate>Mon, 20 Jul 2009 07:21:04 +0000</pubDate>
		<dc:creator>Mr. SEA</dc:creator>
				<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL query optimization]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[I am trying to execute a Query String but I receive following error Query exceeded the specified time limit or storage limit however my query length is normal, nevertheless i have executed larger query too. How to solve this problem.]]></description>
				<content:encoded><![CDATA[<p>I am trying to execute a Query String but I receive following error<br />
<b>Query exceeded the specified time limit or storage limit</b><br />
however my query length is normal, nevertheless i have executed larger query too.</p>
<p>How to solve this problem.</p>
<!-- wpms-network-global-inserts -->]]></content:encoded>
			<wfw:commentRss>http://itknowledgeexchange.techtarget.com/itanswers/sql-query-in-as400/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SQL Server 2005 Query Very Slow in the first run</title>
		<link>http://itknowledgeexchange.techtarget.com/itanswers/sql-server-2005-query-very-slow-in-the-first-run/</link>
		<comments>http://itknowledgeexchange.techtarget.com/itanswers/sql-server-2005-query-very-slow-in-the-first-run/#comments</comments>
		<pubDate>Fri, 17 Jul 2009 10:15:20 +0000</pubDate>
		<dc:creator>Jsql</dc:creator>
				<category><![CDATA[SQL queries]]></category>
		<category><![CDATA[SQL query optimization]]></category>
		<category><![CDATA[SQL Server 2005]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[New Discussion Post by]]></description>
				<content:encoded><![CDATA[New Discussion Post by ]]></content:encoded>
			<wfw:commentRss>http://itknowledgeexchange.techtarget.com/itanswers/sql-server-2005-query-very-slow-in-the-first-run/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Slow SQL Query</title>
		<link>http://itknowledgeexchange.techtarget.com/itanswers/slow-sql-query/</link>
		<comments>http://itknowledgeexchange.techtarget.com/itanswers/slow-sql-query/#comments</comments>
		<pubDate>Mon, 13 Jul 2009 10:00:04 +0000</pubDate>
		<dc:creator>Mxaxyogesh2002</dc:creator>
				<category><![CDATA[Slow SQL Query]]></category>
		<category><![CDATA[SQL queries]]></category>
		<category><![CDATA[SQL query optimization]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[when i run below all SQL Query simulataneously against 1500000 Rows its take 10 minutes i put the non-clustered index on the columns TATCallType ,CallTo the data type of both columns is varchar select ID,AuditMaster_ID,CallTo,CallTypeTag into Auditdata_callto from auditdata where AuditData.TATCallType is null and substring(AuditData.CallTo,1,convert(int,0))=&#8221; and len(AuditData.CallTo)=10 select ID,AuditMaster_ID,CallTo,CallTypeTag into Auditdata_callto from auditdata where AuditData.TATCallType [...]]]></description>
				<content:encoded><![CDATA[<p>when i run below all SQL Query simulataneously against 1500000 Rows its take<br />
10 minutes<br />
i put the non-clustered index on the columns TATCallType ,CallTo<br />
the data type of both columns is varchar</p>
<p>select ID,AuditMaster_ID,CallTo,CallTypeTag into Auditdata_callto from auditdata where AuditData.TATCallType is null and substring(AuditData.CallTo,1,convert(int,0))=&#8221; and len(AuditData.CallTo)=10 </p>
<p>select ID,AuditMaster_ID,CallTo,CallTypeTag into Auditdata_callto from auditdata where AuditData.TATCallType is null and substring(AuditData.CallTo,1,convert(int,1))=&#8217;0&#8242; and len(AuditData.CallTo)=11 </p>
<p>select ID,AuditMaster_ID,CallTo,CallTypeTag into Auditdata_callto from auditdata where AuditData.TATCallType is null and substring(AuditData.CallTo,1,convert(int,2))=&#8217;00&#8242; and len(AuditData.CallTo)=12 </p>
<p>select ID,AuditMaster_ID,CallTo,CallTypeTag into Auditdata_callto from auditdata where AuditData.TATCallType is null and substring(AuditData.CallTo,1,convert(int,2))=&#8217;91&#8242; and len(AuditData.CallTo)=12 </p>
<p>select ID,AuditMaster_ID,CallTo,CallTypeTag into Auditdata_callto from auditdata where AuditData.TATCallType is null and substring(AuditData.CallTo,1,convert(int,4))=&#8217;0091&#8242; and len(AuditData.CallTo)=14 </p>
<p>select ID,AuditMaster_ID,CallTo,CallTypeTag into Auditdata_callto from auditdata where AuditData.TATCallType is null and substring(AuditData.CallTo,1,convert(int,3))=&#8217;910&#8242; and len(AuditData.CallTo)=13 </p>
<p>select ID,AuditMaster_ID,CallTo,CallTypeTag into Auditdata_callto from auditdata where AuditData.TATCallType is null and substring(AuditData.CallTo,1,convert(int,4))=&#8217;9191&#8242; and len(AuditData.CallTo)=14 </p>
<p>select ID,AuditMaster_ID,CallTo,CallTypeTag into Auditdata_callto from auditdata where AuditData.TATCallType is null and substring(AuditData.CallTo,1,convert(int,4))=&#8217;9100&#8242; and len(AuditData.CallTo)=14 </p>
<p>select ID,AuditMaster_ID,CallTo,CallTypeTag into Auditdata_callto from auditdata where AuditData.TATCallType is null and substring(AuditData.CallTo,1,convert(int,4))=&#8217;0091&#8242; and len(AuditData.CallTo)=15 </p>
<p>select ID,AuditMaster_ID,CallTo,CallTypeTag into Auditdata_callto from auditdata where AuditData.TATCallType is null and substring(AuditData.CallTo,1,convert(int,2))=&#8217;91&#8242; and len(AuditData.CallTo)=13 </p>
<p>select ID,AuditMaster_ID,CallTo,CallTypeTag into Auditdata_callto from auditdata where AuditData.TATCallType is null and substring(AuditData.CallTo,1,convert(int,6))=&#8217;009191&#8242; and len(AuditData.CallTo)=16 </p>
<p>select ID,AuditMaster_ID,CallTo,CallTypeTag into Auditdata_callto from auditdata where AuditData.TATCallType is null and substring(AuditData.CallTo,1,convert(int,8))=&#8217;00500003<br />
&#8216; and len(AuditData.CallTo)=18 </p>
<p>select ID,AuditMaster_ID,CallTo,CallTypeTag into Auditdata_callto from auditdata where AuditData.TATCallType is null and substring(AuditData.CallTo,1,convert(int,8))=&#8217;00500004<br />
&#8216; and len(AuditData.CallTo)=18 </p>
<p>please help me its urgent</p>
<p>thanks in advance</p>
<!-- wpms-network-global-inserts -->]]></content:encoded>
			<wfw:commentRss>http://itknowledgeexchange.techtarget.com/itanswers/slow-sql-query/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Finding nth most recent action with SQL</title>
		<link>http://itknowledgeexchange.techtarget.com/itanswers/finding-nth-most-recent-action/</link>
		<comments>http://itknowledgeexchange.techtarget.com/itanswers/finding-nth-most-recent-action/#comments</comments>
		<pubDate>Mon, 18 May 2009 22:11:41 +0000</pubDate>
		<dc:creator>Munona99</dc:creator>
				<category><![CDATA[COUNT statement]]></category>
		<category><![CDATA[Database programming]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL query optimization]]></category>
		<category><![CDATA[SQL statements]]></category>
		<category><![CDATA[SUM statement]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[I am trying to determine the date on which a certain number of things have happened cumulatively, eg I have a varying number of buyers each month. I want to find out on what date of that month, those buyers make their 3rd purchase. My SQL doesn&#8217;t seem to support TOP or ROWCOUNT so I [...]]]></description>
				<content:encoded><![CDATA[<p>I am trying to determine the date on which a certain number of things have happened cumulatively, eg I have a varying number of buyers each month. I want to find out on what date of that month, those buyers make their 3rd purchase. My SQL doesn&#8217;t seem to support TOP or ROWCOUNT so I would appreciate alternatives</p>
<!-- wpms-network-global-inserts -->]]></content:encoded>
			<wfw:commentRss>http://itknowledgeexchange.techtarget.com/itanswers/finding-nth-most-recent-action/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Different Results on Insert Select than Select</title>
		<link>http://itknowledgeexchange.techtarget.com/itanswers/different-results-on-insert-select-than-select/</link>
		<comments>http://itknowledgeexchange.techtarget.com/itanswers/different-results-on-insert-select-than-select/#comments</comments>
		<pubDate>Sun, 03 May 2009 23:36:10 +0000</pubDate>
		<dc:creator>Bem</dc:creator>
				<category><![CDATA[INSERT SELECT statement]]></category>
		<category><![CDATA[SELECT statement]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL query optimization]]></category>
		<category><![CDATA[SQL statements]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[I&#8217;m struggling, along with others, where I&#8217;m getting different results when doing an Insert select verses just the select. Lots of joins to get data from various tables. Only want the first record from the uniqfield table so created the tmp_uniqfieldid table with that item fielded, creation_ts thinking it would help matters&#8230;.. Here is the [...]]]></description>
				<content:encoded><![CDATA[<p>I&#8217;m struggling, along with others, where I&#8217;m getting different results when doing an Insert select verses just the select.  </p>
<p>Lots of joins to get data from various tables.  Only want the first record from the uniqfield table so created the tmp_uniqfieldid table with that item fielded, creation_ts  thinking it would help matters&#8230;..</p>
<p>Here is the query&#8230; The insert gets only 44 items where the same select gets the correct 166 items</p>
<p>insert into pack(select a.id, createdate,<br />
createtime, d.user, to_char(b.received_ts, &#8216;YYYYMMDD&#8217;),<br />
substr(to_char(b.received_ts, &#8216;HH24MISSFF&#8217;),1,8)</p>
<p>FROM uniqfield a, table b, table c, table d,<br />
(Select id, min(creation_ts) creation_ts from uniquefield   where fieldid in (select fieldid from pitest.tmp_uniqfieldid)<br />
group by id) uniqfiled_id</p>
<p>where id &gt;&#8217; &#8216; and ufield_id.id=a.id and<br />
a.creation_ts = ufield_id.creation_ts and<br />
createdate between 20040714 and 20040731 and<br />
a.fieldid=b.fieldid and b.wk_id =c.wk_id and<br />
 trim(c.user_id)=trim(d.user_id))</p>
<p>&#8212;&#8211;</p>
<p>1) id, createdate, createtime from table uniqfiled a<br />
2) user from table d where useid in table d=userid in table a<br />
3) recvdata, recvdtime from table c need to get wk_id from table b where fieldid in table b =fielded in table a then get wk_id in table b to get recvdate and recvdtime from table c</p>
<!-- wpms-network-global-inserts -->]]></content:encoded>
			<wfw:commentRss>http://itknowledgeexchange.techtarget.com/itanswers/different-results-on-insert-select-than-select/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>SQL order by Issue</title>
		<link>http://itknowledgeexchange.techtarget.com/itanswers/sql-order-by-issue/</link>
		<comments>http://itknowledgeexchange.techtarget.com/itanswers/sql-order-by-issue/#comments</comments>
		<pubDate>Sat, 02 May 2009 16:44:32 +0000</pubDate>
		<dc:creator>Rohit3312</dc:creator>
				<category><![CDATA[Dynamic SQL]]></category>
		<category><![CDATA[SELECT statement]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL query optimization]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Please help me write this. I need a SQL where the order by is done based on a variable passed to the query.SELECT * FROM (SELECT distinct gl.group_id, gl.group_name, gl.group_description, gl.status_code, gl.member_count, (SELECT grpp.group_name FROM test_group_relationship grel JOIN test_group grpp ON grel.parent_group_id = grpp.group_id WHERE grel.child_group_id = gl.group_id ) AS parent_group_name, gl.group_name_key, gl.group_description_key FROM test_group [...]]]></description>
				<content:encoded><![CDATA[<p>Please help me write this.<br />
I need a SQL where the order by is done based on a variable passed to the query.SELECT *<br />
    FROM (SELECT distinct gl.group_id,<br />
                 gl.group_name,<br />
                 gl.group_description,<br />
                 gl.status_code,<br />
                 gl.member_count,<br />
                 (SELECT grpp.group_name<br />
                      FROM test_group_relationship grel JOIN test_group grpp<br />
                               ON grel.parent_group_id = grpp.group_id<br />
                      WHERE grel.child_group_id = gl.group_id<br />
                 ) AS parent_group_name,<br />
                 gl.group_name_key,<br />
                 gl.group_description_key<br />
             FROM   test_group AS gl<br />
             WHERE  gl.group_org_id   = &#8217;3909&#8242;<br />
               AND (gl.group_name_key LIKE &#8216;%GROUP%&#8217;)<br />
         ) AS data_set<br />
    ORDER BY DECODE(:sort_key,<br />
                      &#8216;name&#8217;,            &#8216;constant&#8217;,<br />
                      &#8216;description&#8217;,     group_description_key,<br />
                      &#8216;memberCount&#8217;,     LPAD(member_count, 4),<br />
                      &#8216;status&#8217;,          LPAD(status_code, 4),<br />
                      &#8216;parentGroupName&#8217;, parent_group_name<br />
               )NULLS FIRST,<br />
         UPPER(SUBSTR(group_name, 1, 1)),<br />
         SUBSTR(group_name, 1, 1) DESC,<br />
         UPPER(group_name),<br />
         group_name DESC,<br />
         group_name_key,<br />
         ;</p>
<p>When,sort_key :=&#8217;name&#8217;.As we can see it evaluates to a constant and then the remaining columns in the ORDER BY clause.</p>
<p>Now,when variable sort_key:= &#8216;description&#8217; or &#8216;memberCount&#8217;&#8230;.each of the values, then the ORDER BY LOGIC is different for each of them.</p>
<p>n brief,each of the options that the sort_key variable gets-the ORDER BY logic is different and i have to implement it in the same SQL only now.</p>
<!-- wpms-network-global-inserts -->]]></content:encoded>
			<wfw:commentRss>http://itknowledgeexchange.techtarget.com/itanswers/sql-order-by-issue/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using memcached
Database Caching 3/32 queries in 0.036 seconds using memcached
Object Caching 919/1089 objects using memcached

Served from: itknowledgeexchange.techtarget.com @ 2013-05-19 09:10:46 -->