sql query http://forums.asp.net/t/1796880.aspx/1?sql+query+Fri, 27 Apr 2012 10:14:26 -040017968804951107http://forums.asp.net/p/1796880/4951107.aspx/1?sql+query+sql query <p>I need one sql query for the following one.</p> <p>A table has columnA, ColumnB , etc...</p> <p>First I check if a value is present in the column A, then I return the row.</p> <p>Then I take the ColumnB value from the previous result and check if it is also present in A. If so, I return the row.</p> <p>&nbsp;</p> 2012-04-25T14:42:35-04:004951125http://forums.asp.net/p/1796880/4951125.aspx/1?Re+sql+query+Re: sql query <p>Select * from myTable where columnA is not null and ColumnB is not null</p> 2012-04-25T14:51:57-04:004951147http://forums.asp.net/p/1796880/4951147.aspx/1?Re+sql+query+Re: sql query <p>Hi</p> <p>As per my understanding if a table is like below</p> <p>ColA&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ColB</p> <p>------------------</p> <p>1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2</p> <p>2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3</p> <p>3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 4</p> <p>4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 5</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 6</p> <p>&nbsp;</p> <p>Then the result&nbsp;Should be like below</p> <p>ColA&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ColB</p> <p>------------------</p> <p>2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3</p> <p>3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 4</p> <p>4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 5</p> <p>If this is what is expected then the query should be lik</p> <pre class="prettyprint">select * from table where colB in (select colA from table where colA is not null)</pre> <p></p> <p>Hope this helps.</p> <p>Thanks.</p> 2012-04-25T15:01:06-04:004951157http://forums.asp.net/p/1796880/4951157.aspx/1?Re+sql+query+Re: sql query <p>SELECT * INTO #TempTable FROM Table1 where ColumnA = 'Value'</p> <p>SELECT * from Table1 WHERE ColumnB IN (Select ColumnA from #TempTable)</p> <p>Thanks,</p> 2012-04-25T15:04:56-04:004951178http://forums.asp.net/p/1796880/4951178.aspx/1?Re+sql+query+Re: sql query <p>Try something like that:</p> <pre class="prettyprint">select t1.* from MyTable as t1 where exists (select 1 from MyTable as t2 where (t2.ID &lt;&gt; t1.ID) and (t2.ColumnA = t1.ColumnA)) and exists (select 1 from Mytable as t3 where (t2.ColumnB = t1.ColumnA))</pre> <p></p> <p>Hope this helps.</p> <p>&nbsp;</p> 2012-04-25T15:17:22-04:004951259http://forums.asp.net/p/1796880/4951259.aspx/1?Re+sql+query+Re: sql query <p>Hi</p> <p>The table looks like:</p> <table border="0" cellspacing="0" cellpadding="0" width="128" style="width:96pt; border-collapse:collapse"> <colgroup><col span="2" width="64" style="width:48pt"></colgroup> <tbody> <tr height="20" style="height:15pt"> <td class="xl63" height="20" width="64" style="width:48pt; height:15pt">A</td> <td class="xl63" width="64" style="width:48pt; border-left-color:currentColor; border-left-width:medium; border-left-style:none"> B</td> </tr> <tr height="20" style="height:15pt"> <td class="xl63" height="20" align="right" style="height:15pt; border-top-color:currentColor; border-top-width:medium; border-top-style:none"> 1</td> <td class="xl63" align="right" style="border-top-color:currentColor; border-left-color:currentColor; border-top-width:medium; border-left-width:medium; border-top-style:none; border-left-style:none"> 2</td> </tr> <tr height="20" style="height:15pt"> <td class="xl63" height="20" align="right" style="height:15pt; border-top-color:currentColor; border-top-width:medium; border-top-style:none"> 1</td> <td class="xl63" align="right" style="border-top-color:currentColor; border-left-color:currentColor; border-top-width:medium; border-left-width:medium; border-top-style:none; border-left-style:none"> 5</td> </tr> <tr height="20" style="height:15pt"> <td class="xl63" height="20" align="right" style="height:15pt; border-top-color:currentColor; border-top-width:medium; border-top-style:none"> 3</td> <td class="xl63" align="right" style="border-top-color:currentColor; border-left-color:currentColor; border-top-width:medium; border-left-width:medium; border-top-style:none; border-left-style:none"> 4</td> </tr> <tr height="20" style="height:15pt"> <td class="xl63" height="20" align="right" style="height:15pt; border-top-color:currentColor; border-top-width:medium; border-top-style:none"> 4</td> <td class="xl63" align="right" style="border-top-color:currentColor; border-left-color:currentColor; border-top-width:medium; border-left-width:medium; border-top-style:none; border-left-style:none"> 1</td> </tr> <tr height="20" style="height:15pt"> <td class="xl63" height="20" align="right" style="height:15pt; border-top-color:currentColor; border-top-width:medium; border-top-style:none"> 5</td> <td class="xl63" align="right" style="border-top-color:currentColor; border-left-color:currentColor; border-top-width:medium; border-left-width:medium; border-top-style:none; border-left-style:none"> 2</td> </tr> </tbody> </table> <p>When I check for the value 3 in ColumnA, the result must be as follows:</p> <table border="0" cellspacing="0" cellpadding="0" width="128" style="width:96pt; border-collapse:collapse"> <colgroup><col span="2" width="64" style="width:48pt"></colgroup> <tbody> <tr height="20" style="height:15pt"> <td class="xl65" height="20" width="64" style="width:48pt; height:15pt">A</td> <td class="xl65" width="64" style="width:48pt; border-left-color:currentColor; border-left-width:medium; border-left-style:none"> B</td> </tr> <tr height="20" style="height:15pt"> <td class="xl65" height="20" align="right" style="height:15pt; border-top-color:currentColor; border-top-width:medium; border-top-style:none"> 3</td> <td class="xl65" align="right" style="border-top-color:currentColor; border-left-color:currentColor; border-top-width:medium; border-left-width:medium; border-top-style:none; border-left-style:none"> 4</td> </tr> <tr height="20" style="height:15pt"> <td class="xl65" height="20" align="right" style="height:15pt; border-top-color:currentColor; border-top-width:medium; border-top-style:none"> 4</td> <td class="xl65" align="right" style="border-top-color:currentColor; border-left-color:currentColor; border-top-width:medium; border-left-width:medium; border-top-style:none; border-left-style:none"> 1</td> </tr> <tr height="20" style="height:15pt"> <td class="xl65" height="20" align="right" style="height:15pt; border-top-color:currentColor; border-top-width:medium; border-top-style:none"> 1</td> <td class="xl65" align="right" style="border-top-color:currentColor; border-left-color:currentColor; border-top-width:medium; border-left-width:medium; border-top-style:none; border-left-style:none"> 2</td> </tr> <tr height="20" style="height:15pt"> <td class="xl65" height="20" align="right" style="height:15pt; border-top-color:currentColor; border-top-width:medium; border-top-style:none"> 2</td> <td class="xl65" align="right" style="border-top-color:currentColor; border-left-color:currentColor; border-top-width:medium; border-left-width:medium; border-top-style:none; border-left-style:none"> 3</td> </tr> <tr height="20" style="height:15pt"> <td class="xl65" height="20" align="right" style="height:15pt; border-top-color:currentColor; border-top-width:medium; border-top-style:none"> 3</td> <td class="xl65" align="right" style="border-top-color:currentColor; border-left-color:currentColor; border-top-width:medium; border-left-width:medium; border-top-style:none; border-left-style:none"> 4</td> </tr> <tr height="20" style="height:15pt"> <td class="xl65" height="20" align="right" style="height:15pt; border-top-color:currentColor; border-top-width:medium; border-top-style:none"> 1</td> <td class="xl65" align="right" style="border-top-color:currentColor; border-left-color:currentColor; border-top-width:medium; border-left-width:medium; border-top-style:none; border-left-style:none"> 5</td> </tr> <tr height="20" style="height:15pt"> <td class="xl65" height="20" align="right" style="height:15pt; border-top-color:currentColor; border-top-width:medium; border-top-style:none"> 5</td> <td class="xl65" align="right" style="border-top-color:currentColor; border-left-color:currentColor; border-top-width:medium; border-left-width:medium; border-top-style:none; border-left-style:none"> 2</td> </tr> </tbody> </table> <p>&nbsp;</p> <p>&nbsp;</p> 2012-04-25T16:22:57-04:004951268http://forums.asp.net/p/1796880/4951268.aspx/1?Re+sql+query+Re: sql query <p></p> <blockquote><span class="icon-blockquote"></span> <h4>mnmhemaj</h4> <p></p> <p>Hi</p> <p>The table looks like:</p> <table border="0" cellspacing="0" cellpadding="0" width="128" style="width:96pt; border-collapse:collapse"> <colgroup><col span="2" width="64" style="width:48pt"></colgroup> <tbody> <tr height="20" style="height:15pt"> <td class="xl63" height="20" width="64" style="width:48pt; height:15pt">A</td> <td class="xl63" width="64" style="width:48pt; border-left-color:currentColor; border-left-width:medium; border-left-style:none"> B</td> </tr> <tr height="20" style="height:15pt"> <td class="xl63" height="20" align="right" style="height:15pt; border-top-color:currentColor; border-top-width:medium; border-top-style:none"> 1</td> <td class="xl63" align="right" style="border-top-color:currentColor; border-left-color:currentColor; border-top-width:medium; border-left-width:medium; border-top-style:none; border-left-style:none"> 2</td> </tr> <tr height="20" style="height:15pt"> <td class="xl63" height="20" align="right" style="height:15pt; border-top-color:currentColor; border-top-width:medium; border-top-style:none"> 1</td> <td class="xl63" align="right" style="border-top-color:currentColor; border-left-color:currentColor; border-top-width:medium; border-left-width:medium; border-top-style:none; border-left-style:none"> 5</td> </tr> <tr height="20" style="height:15pt"> <td class="xl63" height="20" align="right" style="height:15pt; border-top-color:currentColor; border-top-width:medium; border-top-style:none"> 3</td> <td class="xl63" align="right" style="border-top-color:currentColor; border-left-color:currentColor; border-top-width:medium; border-left-width:medium; border-top-style:none; border-left-style:none"> 4</td> </tr> <tr height="20" style="height:15pt"> <td class="xl63" height="20" align="right" style="height:15pt; border-top-color:currentColor; border-top-width:medium; border-top-style:none"> 4</td> <td class="xl63" align="right" style="border-top-color:currentColor; border-left-color:currentColor; border-top-width:medium; border-left-width:medium; border-top-style:none; border-left-style:none"> 1</td> </tr> <tr height="20" style="height:15pt"> <td class="xl63" height="20" align="right" style="height:15pt; border-top-color:currentColor; border-top-width:medium; border-top-style:none"> 5</td> <td class="xl63" align="right" style="border-top-color:currentColor; border-left-color:currentColor; border-top-width:medium; border-left-width:medium; border-top-style:none; border-left-style:none"> 2</td> </tr> </tbody> </table> <p>When I check for the value 3 in ColumnA, the result must be as follows:</p> <table border="0" cellspacing="0" cellpadding="0" width="128" style="width:96pt; border-collapse:collapse"> <colgroup><col span="2" width="64" style="width:48pt"></colgroup> <tbody> <tr height="20" style="height:15pt"> <td class="xl65" height="20" width="64" style="width:48pt; height:15pt">A</td> <td class="xl65" width="64" style="width:48pt; border-left-color:currentColor; border-left-width:medium; border-left-style:none"> B</td> </tr> <tr height="20" style="height:15pt"> <td class="xl65" height="20" align="right" style="height:15pt; border-top-color:currentColor; border-top-width:medium; border-top-style:none"> 3</td> <td class="xl65" align="right" style="border-top-color:currentColor; border-left-color:currentColor; border-top-width:medium; border-left-width:medium; border-top-style:none; border-left-style:none"> 4</td> </tr> <tr height="20" style="height:15pt"> <td class="xl65" height="20" align="right" style="height:15pt; border-top-color:currentColor; border-top-width:medium; border-top-style:none"> 4</td> <td class="xl65" align="right" style="border-top-color:currentColor; border-left-color:currentColor; border-top-width:medium; border-left-width:medium; border-top-style:none; border-left-style:none"> 1</td> </tr> <tr height="20" style="height:15pt"> <td class="xl65" height="20" align="right" style="height:15pt; border-top-color:currentColor; border-top-width:medium; border-top-style:none"> 1</td> <td class="xl65" align="right" style="border-top-color:currentColor; border-left-color:currentColor; border-top-width:medium; border-left-width:medium; border-top-style:none; border-left-style:none"> 2</td> </tr> <tr height="20" style="height:15pt"> <td class="xl65" height="20" align="right" style="height:15pt; border-top-color:currentColor; border-top-width:medium; border-top-style:none"> 2</td> <td class="xl65" align="right" style="border-top-color:currentColor; border-left-color:currentColor; border-top-width:medium; border-left-width:medium; border-top-style:none; border-left-style:none"> 3</td> </tr> <tr height="20" style="height:15pt"> <td class="xl65" height="20" align="right" style="height:15pt; border-top-color:currentColor; border-top-width:medium; border-top-style:none"> 3</td> <td class="xl65" align="right" style="border-top-color:currentColor; border-left-color:currentColor; border-top-width:medium; border-left-width:medium; border-top-style:none; border-left-style:none"> 4</td> </tr> <tr height="20" style="height:15pt"> <td class="xl65" height="20" align="right" style="height:15pt; border-top-color:currentColor; border-top-width:medium; border-top-style:none"> 1</td> <td class="xl65" align="right" style="border-top-color:currentColor; border-left-color:currentColor; border-top-width:medium; border-left-width:medium; border-top-style:none; border-left-style:none"> 5</td> </tr> <tr height="20" style="height:15pt"> <td class="xl65" height="20" align="right" style="height:15pt; border-top-color:currentColor; border-top-width:medium; border-top-style:none"> 5</td> <td class="xl65" align="right" style="border-top-color:currentColor; border-left-color:currentColor; border-top-width:medium; border-left-width:medium; border-top-style:none; border-left-style:none"> 2</td> </tr> </tbody> </table> <p>&nbsp;</p> <p>&nbsp;</p> <p></p> </blockquote> <p></p> <p>You also said, &quot;Then I take the ColumnB value from the previous result and check if it is also present in A. If so, I return the row.&quot;</p> <p>Now, the third row of your desired output is the first row of tables A and B.&nbsp; That means there is no previous row.&nbsp; Something doesn't compute.</p> 2012-04-25T16:29:47-04:004951287http://forums.asp.net/p/1796880/4951287.aspx/1?Re+sql+query+Re: sql query <p>A and B are column names, not table names.</p> 2012-04-25T16:42:09-04:004951342http://forums.asp.net/p/1796880/4951342.aspx/1?Re+sql+query+Re: sql query <p>I do not understand your question.<br> The sample data you posted does not have any rows with the values A = 2, B = 3 (or A = 3, B = 2). And the line A = 1, B = 5 has no connection with the previous line.</p> <p>&nbsp;</p> 2012-04-25T17:23:46-04:004951391http://forums.asp.net/p/1796880/4951391.aspx/1?Re+sql+query+Re: sql query <p>Recursive CTE would do the trick for you</p> <pre class="prettyprint">DECLARE @VAL INT SET @VAL = 3 ;WITH CTE AS( SELECT A, B FROM YOURABLENAME WHERE A = @VAL UNION ALL SELECT T.A, T.B FROM YOURABLENAME T INNER JOIN CTE C ON T.A = C.B ) SELECT * FROM CTE</pre> <p><br> <br> </p> 2012-04-25T17:55:17-04:004951458http://forums.asp.net/p/1796880/4951458.aspx/1?Re+sql+query+Re: sql query <p>when I run this query, I get an error near 'WITH'.</p> <p>I tried also removing the ;, but still the same error.</p> 2012-04-25T18:48:47-04:004952027http://forums.asp.net/p/1796880/4952027.aspx/1?Re+sql+query+Re: sql query <p>What version of sql server you are using?&nbsp;Note, it would work on Sql server 2005 onwards.</p> 2012-04-26T06:06:56-04:004952099http://forums.asp.net/p/1796880/4952099.aspx/1?Re+sql+query+Re: sql query <p>2008</p> 2012-04-26T06:39:35-04:004952381http://forums.asp.net/p/1796880/4952381.aspx/1?Re+sql+query+Re: sql query <p>I have found out how to write the query.</p> <p>select a.t_mitm mitm, a.t_sitm sitm, a.t_cpha cpha from ttibom010 &nbsp;a&nbsp;where (a.t_mitm in (select t_sitm from ttibom010 &nbsp;where substring (a.t_mitm , 10, 10 ) = '2046000021' ) or substring (a.t_mitm , 10, 10 ) = '2046000021'</p> <p>But I get a different problem.</p> <p>this query runs in 30 seconds in SQL Management Studio.&nbsp;But it takes more than 5 min from the ASP.NET page.</p> <p>This query is running on more than 500000 records. I cannot change the index of the table as this is a baan table. How can I minimize the running time from the ASP.NET application?</p> 2012-04-26T09:01:32-04:004952791http://forums.asp.net/p/1796880/4952791.aspx/1?Re+sql+query+Re: sql query <p></p> <blockquote><span class="icon-blockquote"></span> <h4>mnmhemaj</h4> <p></p> <p>I have found out how to write the query.</p> <p>select a.t_mitm mitm, a.t_sitm sitm, a.t_cpha cpha from ttibom010 &nbsp;a&nbsp;where (a.t_mitm in (select t_sitm from ttibom010 &nbsp;where substring (a.t_mitm , 10, 10 ) = '2046000021' ) or substring (a.t_mitm , 10, 10 ) = '2046000021'</p> <p>But I get a different problem.</p> <p>this query runs in 30 seconds in SQL Management Studio.&nbsp;But it takes more than 5 min from the ASP.NET page.</p> <p>This query is running on more than 500000 records. I cannot change the index of the table as this is a baan table. How can I minimize the running time from the ASP.NET application?</p> <p></p> </blockquote> <p></p> <p>It's hard to read, but this:</p> <p>where (a.t_mitm in (select t_sitm from ttibom010&nbsp; where substring (a.t_mitm , 10, 10 ) = '2046000021' )</p> <p>or substring (a.t_mitm , 10, 10 ) = '2046000021'</p> <p>looks redundant.</p> <p>&nbsp;</p> 2012-04-26T11:52:51-04:004953032http://forums.asp.net/p/1796880/4953032.aspx/1?Re+sql+query+Re: sql query <p>It isn't redundant; the difference is t_mitm vs. t_sitm.</p> <p>Put your query in a stored procedure, call the stored procedure from asp.net.</p> <p>Follow the tips in this article to avoid parameter sniffing:</p> <p><a href="http://www.sommarskog.se/query-plan-mysteries.html">http://www.sommarskog.se/query-plan-mysteries.html</a></p> 2012-04-26T13:38:18-04:004954249http://forums.asp.net/p/1796880/4954249.aspx/1?Re+sql+query+Re: sql query <p>I solved it on my own.</p> 2012-04-27T06:45:11-04:004954445http://forums.asp.net/p/1796880/4954445.aspx/1?Re+sql+query+Re: sql query <p></p> <blockquote><span class="icon-blockquote"></span> <h4>mnmhemaj</h4> <p></p> <p>I solved it on my own.</p> <p></p> </blockquote> <p></p> <p>Hi,</p> <p>Would you please share your query? It will help others when they meet similar problems. Thanks a lot.</p> <p>Thanks.</p> 2012-04-27T08:37:46-04:004954631http://forums.asp.net/p/1796880/4954631.aspx/1?Re+sql+query+Re: sql query <p>First I had this query as as an&nbsp;ASP.NET SQL Datasource Select Command. So the page took more than 15 min to update a gridview.</p> <p>Then I just ran the query and filled a temporary&nbsp;table with the output records and then I wrote a Select Command from this table to the SQL Datasource .</p> <p>Now my asp.net page runs in just ½sec. I have not done anything to optimize the running time of ASP.NET page.</p> 2012-04-27T10:14:26-04:00