<?xml version="1.0" encoding="UTF-8" standalone="no"?><rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:blogger="http://schemas.google.com/blogger/2008" xmlns:gd="http://schemas.google.com/g/2005" xmlns:georss="http://www.georss.org/georss" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/" xmlns:thr="http://purl.org/syndication/thread/1.0" version="2.0"><channel><atom:id>tag:blogger.com,1999:blog-8397719027199589165</atom:id><lastBuildDate>Thu, 22 Aug 2024 00:42:59 +0000</lastBuildDate><category>Sql</category><category>Sql Server</category><category>Sqlite</category><category>Mysql</category><category>Sql Server 2008</category><category>Database</category><category>Python</category><category>Tsql</category><category>C#</category><category>Oracle</category><category>Postgresql</category><category>Sqlalchemy</category><category>.net</category><category>Android</category><category>Linq</category><category>Query Optimization</category><category>Ssis</category><category>Stored Procedures</category><category>Arrays</category><category>Ascii</category><category>Asp.net Core Mvc 2.0</category><category>Azure Blob Storage</category><category>Bulk</category><category>C</category><category>C++</category><category>Common Table Expression</category><category>Datastep</category><category>Diagnostics</category><category>Distinct</category><category>Django</category><category>Dll</category><category>Duration</category><category>Entity Framework</category><category>Entity Framework 4.1</category><category>Entity Framework Core</category><category>Flask</category><category>Foreign Keys</category><category>Greatest N Per Group</category><category>Hierarchical Data</category><category>Inner Join</category><category>Insert</category><category>Many To Many</category><category>Methods</category><category>Migration</category><category>Mongodb</category><category>Mysql5</category><category>Nosql</category><category>Oracle10g</category><category>Orm</category><category>Performance</category><category>Php</category><category>Poco</category><category>Proc Sql</category><category>Python 3.x</category><category>Rdbms</category><category>Rename</category><category>Replace</category><category>Sas</category><category>Scope Identity</category><category>Sql Order By</category><category>Sql Server 2005</category><category>Sql Server 2012</category><category>Sql Server 2014</category><category>Sql Server 2016</category><category>Sqldatatypes</category><category>Sqlparameter</category><category>Temp Tables</category><category>Teradata</category><category>Timedelta</category><category>Types</category><category>Variables</category><category>Vb.net</category><category>Visual Studio Code</category><category>Wordpress</category><title>rachmatmoradosugiarto</title><description></description><link>https://rachmatmoradosugiarto.blogspot.com/</link><managingEditor>noreply@blogger.com (Sandy Veum)</managingEditor><generator>Blogger</generator><openSearch:totalResults>52</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><language>en-us</language><itunes:explicit>no</itunes:explicit><itunes:subtitle/><itunes:owner><itunes:email>noreply@blogger.com</itunes:email></itunes:owner><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8397719027199589165.post-1445056329121892779</guid><pubDate>Thu, 22 Aug 2024 00:42:00 +0000</pubDate><atom:updated>2024-08-21T17:42:28.908-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">.net</category><category domain="http://www.blogger.com/atom/ns#">C#</category><category domain="http://www.blogger.com/atom/ns#">Dll</category><category domain="http://www.blogger.com/atom/ns#">Sqlite</category><title>How To Deploy Sqlite With .net</title><description>&lt;div class="question"&gt;I got a report from a user that when they installed my .net 2.0 app they got a message that a reference to system.data.sqlite.dll could not be found. I use Inno to install it in th&lt;/div&gt;&lt;h2 id="solution_1"&gt;Solution 1:

&lt;/h2&gt;&lt;div class="answer-desc"&gt;&lt;p&gt;Yes, if it is in the folder with the executable, it works. But you will have to deal with 32 bit an 64 bit architectures, there are seperate assemblies for that. 32 bit is in /System.Data.Sqlite.dll, 64 bit in /x64/System.Data.Sqlite.dll.&lt;/p&gt;&lt;/div&gt;&lt;h2 id="solution_2"&gt;Solution 2:

&lt;/h2&gt;&lt;div class="answer-desc"&gt;&lt;p&gt;As has been pointed out this is probably an architecture problem. We are in fact having the same issue with Oracle.&lt;/p&gt;&lt;p&gt;You have ? solutions:&lt;/p&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;ol&gt;&lt;li&gt;Create two installers one x86 and one x64 that use the appropriate sql lite dll.&lt;/li&gt;&lt;li&gt;Create an intelligent installer that copies the appropriate dll into the bin folder.&lt;/li&gt;&lt;li&gt;Install the relevant sql lite into the gac. You may need to create the installer for this and you would probably want to set it up as a prerequisite for your application.&lt;/li&gt;&lt;/ol&gt;&lt;p&gt;If you are not tied to SqlLite you might want to take a look at SqlCe. The installers are already there and available as prerequisite in visual studio and chances are they it is probably already installed on some client machines.&lt;/p&gt;&lt;/div&gt; 


</description><link>https://rachmatmoradosugiarto.blogspot.com/2024/08/how-to-deploy-sqlite-with-net.html</link><author>noreply@blogger.com (Sandy Veum)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8397719027199589165.post-4237353947631381521</guid><pubDate>Thu, 22 Aug 2024 00:22:00 +0000</pubDate><atom:updated>2024-08-21T17:22:58.005-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Sql</category><title>Calculate Totals Of Field Based On Current Fiscal Year Only - Sql</title><description>&lt;div class="question"&gt;I have seen many examples regarding calculating the sum of fields using the fiscal year, but I can not find one that fits my needs.  What I am trying to do is get just the current&lt;/div&gt;&lt;h2 id="solution_1"&gt;Solution 1:

&lt;/h2&gt;&lt;div class="answer-desc"&gt;&lt;p&gt;Add a where clause:&lt;/p&gt;&lt;pre&gt;&lt;code id="code_0" class="hljs language-vbnet"&gt;&lt;span class="hljs-keyword"&gt;FROM&lt;/span&gt; mytable
&lt;span class="hljs-keyword"&gt;WHERE&lt;/span&gt; startdate &amp;gt;= &lt;span class="hljs-comment"&gt;'2011-07-01'&lt;/span&gt;&lt;span class="hljs-keyword"&gt;GROUP&lt;/span&gt;&lt;span class="hljs-keyword"&gt;BY&lt;/span&gt; userid
&lt;/code&gt;&lt;button class="copy-btn btn btn-secondary btn-sm" onclick="copyCode(this,'code_0')"&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;Or with the start of the year dynamically:&lt;/p&gt;&lt;pre&gt;&lt;code id="code_1" class="hljs language-bash"&gt;&lt;span class="hljs-built_in"&gt;where&lt;/span&gt; startdate &amp;gt;= dateadd(yy, datepart(yy, getdate())-2001, &lt;span class="hljs-string"&gt;'2000-07-01'&lt;/span&gt;)
&lt;/code&gt;&lt;button class="copy-btn btn btn-secondary btn-sm" onclick="copyCode(this,'code_1')"&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id="solution_2"&gt;Solution 2:

&lt;/h2&gt;&lt;div class="answer-desc"&gt;&lt;p&gt;Maybe something like this:&lt;/p&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;pre&gt;&lt;code id="code_2" class="hljs language-scss"&gt;SELECT
    userid,
    &lt;span class="hljs-built_in"&gt;SUM&lt;/span&gt;(total_hours) - &lt;span class="hljs-built_in"&gt;SUM&lt;/span&gt;(missed_hours) AS hours
FROM 
    mytable
WHERE 
    &lt;span class="hljs-built_in"&gt;MONTH&lt;/span&gt;(startdate) BETWEEN &lt;span class="hljs-number"&gt;6&lt;/span&gt; AND &lt;span class="hljs-number"&gt;7&lt;/span&gt;
    AND &lt;span class="hljs-built_in"&gt;YEAR&lt;/span&gt;(startdate) IN (&lt;span class="hljs-number"&gt;2011&lt;/span&gt;,&lt;span class="hljs-number"&gt;2012&lt;/span&gt;)
GROUP BY userid
&lt;/code&gt;&lt;button class="copy-btn btn btn-secondary btn-sm" onclick="copyCode(this,'code_2')"&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id="solution_3"&gt;Solution 3:

&lt;/h2&gt;&lt;div class="answer-desc"&gt;&lt;p&gt;For the solution, two additional information is needed&lt;/p&gt;&lt;ul&gt;&lt;li&gt;the name of the date column &lt;/li&gt;&lt;li&gt;the vendor type of RDBMS you are using&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;I supposed your date column is &lt;code&gt;date_col&lt;/code&gt; and you are using MySQL&lt;/p&gt;&lt;pre&gt;&lt;code id="code_4" class="hljs language-sql"&gt;&lt;span class="hljs-keyword"&gt;SELECT&lt;/span&gt;    
userid,    
&lt;span class="hljs-built_in"&gt;SUM&lt;/span&gt;(total_hours) &lt;span class="hljs-operator"&gt;-&lt;/span&gt;&lt;span class="hljs-built_in"&gt;SUM&lt;/span&gt;(missed_hours) &lt;span class="hljs-keyword"&gt;AS&lt;/span&gt; hours    
&lt;span class="hljs-keyword"&gt;FROM&lt;/span&gt; mytable    
&lt;span class="hljs-keyword"&gt;WHERE&lt;/span&gt; date_col &lt;span class="hljs-keyword"&gt;between&lt;/span&gt; STR_TO_DATE(&lt;span class="hljs-string"&gt;'01,7,2011'&lt;/span&gt;,&lt;span class="hljs-string"&gt;'%d,%m,%Y'&lt;/span&gt;) &lt;span class="hljs-keyword"&gt;and&lt;/span&gt; STR_TO_DATE(&lt;span class="hljs-string"&gt;'01,7,2010'&lt;/span&gt;,&lt;span class="hljs-string"&gt;'%d,%m,%Y'&lt;/span&gt;)
&lt;span class="hljs-keyword"&gt;GROUP&lt;/span&gt;&lt;span class="hljs-keyword"&gt;BY&lt;/span&gt; userid
&lt;/code&gt;&lt;button class="copy-btn btn btn-secondary btn-sm" onclick="copyCode(this,'code_4')"&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;/div&gt; 

</description><link>https://rachmatmoradosugiarto.blogspot.com/2024/08/calculate-totals-of-field-based-on.html</link><author>noreply@blogger.com (Sandy Veum)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8397719027199589165.post-1288146311837585718</guid><pubDate>Thu, 22 Aug 2024 00:04:00 +0000</pubDate><atom:updated>2024-08-21T17:04:37.724-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Database</category><category domain="http://www.blogger.com/atom/ns#">Rename</category><category domain="http://www.blogger.com/atom/ns#">Sql</category><title>How Do I Rename A Column In A Database Table Using Sql?</title><description>&lt;div class="question"&gt;If I wish to simply rename a column (not change its type or constraints, just its name) in an SQL database using SQL, how do I do that?  Or is it not possible? This is for any data&lt;/div&gt;&lt;h2 id="solution_1"&gt;Solution 1:

&lt;/h2&gt;&lt;div class="answer-desc"&gt;&lt;p&gt;Specifically for SQL Server, use &lt;a rel="nofollow noreferrer noopener" href="https://msdn.microsoft.com/en-us/library/ms188351.aspx"&gt;&lt;code&gt;sp_rename&lt;/code&gt;&lt;/a&gt;&lt;/p&gt;&lt;pre&gt;&lt;code id="code_1" class="hljs language-php"&gt;&lt;span class="hljs-keyword"&gt;USE&lt;/span&gt;&lt;span class="hljs-title"&gt;AdventureWorks&lt;/span&gt;;
GO
EXEC sp_rename &lt;span class="hljs-string"&gt;'Sales.SalesTerritory.TerritoryID'&lt;/span&gt;, &lt;span class="hljs-string"&gt;'TerrID'&lt;/span&gt;, &lt;span class="hljs-string"&gt;'COLUMN'&lt;/span&gt;;
GO
&lt;/code&gt;&lt;button class="copy-btn btn btn-secondary btn-sm" onclick="copyCode(this,'code_1')"&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id="solution_2"&gt;Solution 2:

&lt;/h2&gt;&lt;div class="answer-desc"&gt;&lt;p&gt;On PostgreSQL (and many other RDBMS), you can do it with regular &lt;code&gt;ALTER TABLE&lt;/code&gt; statement:&lt;/p&gt;&lt;pre&gt;&lt;code id="code_3" class="hljs language-sql"&gt;&lt;span class="hljs-operator"&gt;=&lt;/span&gt;&lt;span class="hljs-operator"&gt;&amp;gt;&lt;/span&gt;&lt;span class="hljs-keyword"&gt;SELECT&lt;/span&gt;&lt;span class="hljs-operator"&gt;*&lt;/span&gt;&lt;span class="hljs-keyword"&gt;FROM&lt;/span&gt; Test1;
 id &lt;span class="hljs-operator"&gt;|&lt;/span&gt; foo &lt;span class="hljs-operator"&gt;|&lt;/span&gt; bar 
&lt;span class="hljs-comment"&gt;----+-----+-----&lt;/span&gt;&lt;span class="hljs-number"&gt;2&lt;/span&gt;&lt;span class="hljs-operator"&gt;|&lt;/span&gt;&lt;span class="hljs-number"&gt;1&lt;/span&gt;&lt;span class="hljs-operator"&gt;|&lt;/span&gt;&lt;span class="hljs-number"&gt;2&lt;/span&gt;&lt;span class="hljs-operator"&gt;=&lt;/span&gt;&lt;span class="hljs-operator"&gt;&amp;gt;&lt;/span&gt;&lt;span class="hljs-keyword"&gt;ALTER&lt;/span&gt;&lt;span class="hljs-keyword"&gt;TABLE&lt;/span&gt; Test1 RENAME &lt;span class="hljs-keyword"&gt;COLUMN&lt;/span&gt; foo &lt;span class="hljs-keyword"&gt;TO&lt;/span&gt; baz;
&lt;span class="hljs-keyword"&gt;ALTER&lt;/span&gt;&lt;span class="hljs-keyword"&gt;TABLE&lt;/span&gt;&lt;span class="hljs-operator"&gt;=&lt;/span&gt;&lt;span class="hljs-operator"&gt;&amp;gt;&lt;/span&gt;&lt;span class="hljs-keyword"&gt;SELECT&lt;/span&gt;&lt;span class="hljs-operator"&gt;*&lt;/span&gt;&lt;span class="hljs-keyword"&gt;FROM&lt;/span&gt; Test1;
 id &lt;span class="hljs-operator"&gt;|&lt;/span&gt; baz &lt;span class="hljs-operator"&gt;|&lt;/span&gt; bar 
&lt;span class="hljs-comment"&gt;----+-----+-----&lt;/span&gt;&lt;span class="hljs-number"&gt;2&lt;/span&gt;&lt;span class="hljs-operator"&gt;|&lt;/span&gt;&lt;span class="hljs-number"&gt;1&lt;/span&gt;&lt;span class="hljs-operator"&gt;|&lt;/span&gt;&lt;span class="hljs-number"&gt;2&lt;/span&gt;&lt;/code&gt;&lt;button class="copy-btn btn btn-secondary btn-sm" onclick="copyCode(this,'code_3')"&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id="solution_3"&gt;Solution 3:

&lt;/h2&gt;&lt;div class="answer-desc"&gt;&lt;p&gt;In MySQL, the syntax is &lt;a rel="nofollow noreferrer noopener" href="https://dev.mysql.com/doc/en/alter-table.html"&gt;&lt;code&gt;ALTER TABLE ... CHANGE&lt;/code&gt;&lt;/a&gt;:&lt;/p&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;pre&gt;&lt;code id="code_5" class="hljs language-xml"&gt;ALTER TABLE &lt;span class="hljs-tag"&gt;&amp;lt;&lt;span class="hljs-name"&gt;table_name&lt;/span&gt;&amp;gt;&lt;/span&gt; CHANGE &lt;span class="hljs-tag"&gt;&amp;lt;&lt;span class="hljs-name"&gt;column_name&lt;/span&gt;&amp;gt;&lt;/span&gt;&lt;span class="hljs-tag"&gt;&amp;lt;&lt;span class="hljs-name"&gt;new_column_name&lt;/span&gt;&amp;gt;&lt;/span&gt;&lt;span class="hljs-tag"&gt;&amp;lt;&lt;span class="hljs-name"&gt;data_type&lt;/span&gt;&amp;gt;&lt;/span&gt; ...
&lt;/code&gt;&lt;button class="copy-btn btn btn-secondary btn-sm" onclick="copyCode(this,'code_5')"&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;Note that you can't just rename and leave the type and constraints as is; you must retype the data type and constraints after the new name of the column.&lt;/p&gt;&lt;/div&gt;&lt;h2 id="solution_4"&gt;Solution 4:

&lt;/h2&gt;&lt;div class="answer-desc"&gt;&lt;p&gt;Unfortunately, for a database independent solution, you will need to know everything about the column. If it is used in other tables as a foreign key, they will need to be modified as well.&lt;/p&gt;&lt;pre&gt;&lt;code id="code_6" class="hljs language-sql"&gt;&lt;span class="hljs-keyword"&gt;ALTER&lt;/span&gt;&lt;span class="hljs-keyword"&gt;TABLE&lt;/span&gt; MyTable &lt;span class="hljs-keyword"&gt;ADD&lt;/span&gt; MyNewColumn OLD_COLUMN_TYPE;
&lt;span class="hljs-keyword"&gt;UPDATE&lt;/span&gt; MyTable &lt;span class="hljs-keyword"&gt;SET&lt;/span&gt; MyNewColumn &lt;span class="hljs-operator"&gt;=&lt;/span&gt; MyOldColumn;
&lt;span class="hljs-comment"&gt;-- add all necessary triggers and constraints to the new column...&lt;/span&gt;&lt;span class="hljs-comment"&gt;-- update all foreign key usages to point to the new column...&lt;/span&gt;&lt;span class="hljs-keyword"&gt;ALTER&lt;/span&gt;&lt;span class="hljs-keyword"&gt;TABLE&lt;/span&gt; MyTable &lt;span class="hljs-keyword"&gt;DROP&lt;/span&gt;&lt;span class="hljs-keyword"&gt;COLUMN&lt;/span&gt; MyOldColumn;
&lt;/code&gt;&lt;button class="copy-btn btn btn-secondary btn-sm" onclick="copyCode(this,'code_6')"&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;For the very simplest of cases (no constraints, triggers, indexes or keys), it will take the above 3 lines. For anything more complicated it can get very messy as you fill in the missing parts.&lt;/p&gt;&lt;p&gt;However, as mentioned above, there are simpler database specific methods if you know which database you need to modify ahead of time.&lt;/p&gt;&lt;/div&gt;&lt;h2 id="solution_5"&gt;Solution 5:

&lt;/h2&gt;&lt;div class="answer-desc"&gt;&lt;p&gt;I think this is the easiest way to change column name.&lt;/p&gt;&lt;pre&gt;&lt;code id="code_7" class="hljs language-bash"&gt;SP_RENAME &lt;span class="hljs-string"&gt;'TABLE_NAME.OLD_COLUMN_NAME'&lt;/span&gt;,&lt;span class="hljs-string"&gt;'NEW_COLUMN_NAME'&lt;/span&gt;&lt;/code&gt;&lt;button class="copy-btn btn btn-secondary btn-sm" onclick="copyCode(this,'code_7')"&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;/div&gt; 

</description><link>https://rachmatmoradosugiarto.blogspot.com/2024/08/how-do-i-rename-column-in-database.html</link><author>noreply@blogger.com (Sandy Veum)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8397719027199589165.post-2235552515547914612</guid><pubDate>Wed, 21 Aug 2024 23:47:00 +0000</pubDate><atom:updated>2024-08-21T16:47:22.846-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Python</category><category domain="http://www.blogger.com/atom/ns#">Python 3.x</category><category domain="http://www.blogger.com/atom/ns#">Sqlalchemy</category><category domain="http://www.blogger.com/atom/ns#">Visual Studio Code</category><title>"inheriting 'base', Which Is Not A Class" In Vs Code Using Sqlalchemy Declarative_base()</title><description>&lt;div class="question"&gt;VS Code shows 'Inheriting 'Base', which is not a class' as an error message given the below: from sqlalchemy.ext.declarative import declarative_base from sqlalchemy import Integer&lt;/div&gt;&lt;h2 id="solution_1"&gt;Solution 1:

&lt;/h2&gt;&lt;div class="answer-desc"&gt;&lt;h4&gt;&lt;code&gt;Inheriting 'Base', which is not a class&lt;/code&gt; is not actually an error.&lt;/h4&gt;&lt;p&gt;Rather, it's a static analysis result coming from Microsoft's Python language server (which in turn leans heavily on &lt;a rel="nofollow noreferrer noopener" href="https://www.pylint.org"&gt;&lt;code&gt;pylint&lt;/code&gt;&lt;/a&gt;) for this kind of analysis. It's not always accurate: If a class is dynamically generated and returned by a function (as is the case here), the static-checking tools may not properly understand its type.&lt;/p&gt;&lt;p&gt;As described in &lt;a rel="nofollow noreferrer noopener" href="https://github.com/microsoft/python-language-server/issues/1390"&gt;&lt;code&gt;microsoft/python-language-server#1390&lt;/code&gt;&lt;/a&gt;, this feature can be disabled with the following settings change:&lt;/p&gt;&lt;pre&gt;&lt;code id="code_3" class="hljs language-json"&gt;&lt;span class="hljs-attr"&gt;"python.analysis.disabled"&lt;/span&gt;&lt;span class="hljs-punctuation"&gt;:&lt;/span&gt;&lt;span class="hljs-punctuation"&gt;[&lt;/span&gt;&lt;span class="hljs-string"&gt;"inherit-non-class"&lt;/span&gt;&lt;span class="hljs-punctuation"&gt;]&lt;/span&gt;&lt;span class="hljs-punctuation"&gt;,&lt;/span&gt;&lt;/code&gt;&lt;button class="copy-btn btn btn-secondary btn-sm" onclick="copyCode(this,'code_3')"&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id="solution_2"&gt;Solution 2:

&lt;/h2&gt;&lt;div class="answer-desc"&gt;&lt;p&gt;As of VS Code 1.47, when using Marshmallow to serialize/deserialize SQLAlchemy objects and inheriting from &lt;a rel="nofollow noreferrer noopener" href="https://marshmallow-sqlalchemy.readthedocs.io/en/latest/api_reference.html#marshmallow_sqlalchemy.SQLAlchemyAutoSchema"&gt;&lt;code&gt;marshmallow_sqlalchemy.SQLAlchemyAutoSchema&lt;/code&gt;&lt;/a&gt;, using the solution from the &lt;a rel="nofollow noreferrer noopener" href="https://stackoverflow.com/a/57222704/2745495"&gt;other answer&lt;/a&gt;:&lt;/p&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;pre&gt;&lt;code id="code_5" class="hljs language-json"&gt;&lt;span class="hljs-attr"&gt;"python.analysis.disabled"&lt;/span&gt;&lt;span class="hljs-punctuation"&gt;:&lt;/span&gt;&lt;span class="hljs-punctuation"&gt;[&lt;/span&gt;&lt;span class="hljs-string"&gt;"inherit-non-class"&lt;/span&gt;&lt;span class="hljs-punctuation"&gt;]&lt;/span&gt;&lt;span class="hljs-punctuation"&gt;,&lt;/span&gt;&lt;/code&gt;&lt;button class="copy-btn btn btn-secondary btn-sm" onclick="copyCode(this,'code_5')"&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;does not seem to work anymore (i.e. you will still get a "&lt;em&gt;ma.SQLAlchemyAutoSchema', which is not a class.&lt;/em&gt;" warning). You can instead use the more generic &lt;a rel="nofollow noreferrer noopener" href="https://stackoverflow.com/q/45346575/2745495"&gt;&lt;code&gt;#noqa&lt;/code&gt;&lt;/a&gt; comment on specific lines:&lt;/p&gt;&lt;pre&gt;&lt;code id="code_7" class="hljs language-cpp"&gt;ma = &lt;span class="hljs-built_in"&gt;Marshmallow&lt;/span&gt;(app)

&lt;span class="hljs-keyword"&gt;class&lt;/span&gt;&lt;span class="hljs-built_in"&gt;UserSchema&lt;/span&gt;(ma.SQLAlchemyAutoSchema):  &lt;span class="hljs-meta"&gt;# noqa&lt;/span&gt;&lt;span class="hljs-keyword"&gt;class&lt;/span&gt; Meta:
        model = Person
        sqla_session = db.session
&lt;/code&gt;&lt;button class="copy-btn btn btn-secondary btn-sm" onclick="copyCode(this,'code_7')"&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;Note though, that VS Code treats &lt;code&gt;#noqa&lt;/code&gt; as a disable-all setting for that line.&lt;/p&gt;&lt;/div&gt; 


</description><link>https://rachmatmoradosugiarto.blogspot.com/2024/08/inheriting-base-which-is-not-class-in.html</link><author>noreply@blogger.com (Sandy Veum)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8397719027199589165.post-6697280389712804175</guid><pubDate>Wed, 21 Aug 2024 22:54:00 +0000</pubDate><atom:updated>2024-08-21T15:54:02.432-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Mysql</category><category domain="http://www.blogger.com/atom/ns#">Php</category><category domain="http://www.blogger.com/atom/ns#">Sql</category><title>Distinct On Only One Column - Only Display First Duplicate Row</title><description>&lt;div class="question"&gt;I'm using an SQL query in PHP to display 5 columns from my 'users' table. Here is my code for that:  SELECT DISTINCT account_id, full_name, email, login, phone, updated_at, last_re&lt;/div&gt;&lt;h2 id="solution_1"&gt;Solution 1:

&lt;/h2&gt;&lt;div class="answer-desc"&gt;&lt;p&gt;You should probably add another column. Now it is possible to get distinct record for every account_id using &lt;code&gt;GROUP BY&lt;/code&gt; clause but results of all nonagreggated columns can be ambigius. You have to have some order you approve or indicator inside group to determine which record for every accout_id is "first". With column marking which record in each group is first query is simple. Without it you have to accept some order telling query which record is "first". On example alphabetical order of full_name:&lt;/p&gt;&lt;pre&gt;&lt;code id="code_1" class="hljs language-sql"&gt;&lt;span class="hljs-keyword"&gt;SELECT&lt;/span&gt; account_id, 
       full_name, 
       email, 
       login, 
       phone, 
       updated_at, 
       last_request_at, 
       unconfirmed_email 
  &lt;span class="hljs-keyword"&gt;FROM&lt;/span&gt; table1 &lt;span class="hljs-keyword"&gt;WHERE&lt;/span&gt; full_name &lt;span class="hljs-keyword"&gt;IN&lt;/span&gt; (
    &lt;span class="hljs-keyword"&gt;SELECT&lt;/span&gt;&lt;span class="hljs-built_in"&gt;MIN&lt;/span&gt;(full_name) 
      &lt;span class="hljs-keyword"&gt;FROM&lt;/span&gt; table1 
      &lt;span class="hljs-keyword"&gt;GROUP&lt;/span&gt;&lt;span class="hljs-keyword"&gt;BY&lt;/span&gt; account_id 
      &lt;span class="hljs-keyword"&gt;WHERE&lt;/span&gt; id &lt;span class="hljs-operator"&gt;&amp;lt;&lt;/span&gt;&lt;span class="hljs-string"&gt;'300'&lt;/span&gt;
  )
&lt;/code&gt;&lt;button class="copy-btn btn btn-secondary btn-sm" onclick="copyCode(this,'code_1')"&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id="solution_2"&gt;Solution 2:

&lt;/h2&gt;&lt;div class="answer-desc"&gt;&lt;p&gt;Here is the solution to what you asked for,&lt;/p&gt;&lt;pre&gt;&lt;code id="code_2" class="hljs language-sql"&gt;&lt;span class="hljs-keyword"&gt;SELECT&lt;/span&gt; account_id,full_name,Superhero
&lt;span class="hljs-keyword"&gt;FROM&lt;/span&gt;(
&lt;span class="hljs-keyword"&gt;SELECT&lt;/span&gt; p.&lt;span class="hljs-operator"&gt;*&lt;/span&gt;,( 
            &lt;span class="hljs-keyword"&gt;CASE&lt;/span&gt; account_id 
            &lt;span class="hljs-keyword"&gt;WHEN&lt;/span&gt;&lt;span class="hljs-variable"&gt;@curType&lt;/span&gt;&lt;span class="hljs-keyword"&gt;THEN&lt;/span&gt;&lt;span class="hljs-variable"&gt;@curRow&lt;/span&gt; :&lt;span class="hljs-operator"&gt;=&lt;/span&gt;&lt;span class="hljs-variable"&gt;@curRow&lt;/span&gt;&lt;span class="hljs-operator"&gt;+&lt;/span&gt;&lt;span class="hljs-number"&gt;1&lt;/span&gt;&lt;span class="hljs-keyword"&gt;ELSE&lt;/span&gt;&lt;span class="hljs-variable"&gt;@curRow&lt;/span&gt; :&lt;span class="hljs-operator"&gt;=&lt;/span&gt;&lt;span class="hljs-number"&gt;1&lt;/span&gt;&lt;span class="hljs-keyword"&gt;AND&lt;/span&gt;&lt;span class="hljs-variable"&gt;@curType&lt;/span&gt; :&lt;span class="hljs-operator"&gt;=&lt;/span&gt; account_id &lt;span class="hljs-keyword"&gt;END&lt;/span&gt;
          ) &lt;span class="hljs-operator"&gt;+&lt;/span&gt;&lt;span class="hljs-number"&gt;1&lt;/span&gt;&lt;span class="hljs-keyword"&gt;AS&lt;/span&gt; rank
&lt;span class="hljs-keyword"&gt;FROM&lt;/span&gt;      test p, (&lt;span class="hljs-keyword"&gt;SELECT&lt;/span&gt;&lt;span class="hljs-variable"&gt;@curRow&lt;/span&gt; :&lt;span class="hljs-operator"&gt;=&lt;/span&gt;&lt;span class="hljs-number"&gt;0&lt;/span&gt;, &lt;span class="hljs-variable"&gt;@curType&lt;/span&gt; :&lt;span class="hljs-operator"&gt;=&lt;/span&gt;&lt;span class="hljs-string"&gt;''&lt;/span&gt;, &lt;span class="hljs-variable"&gt;@curRank&lt;/span&gt; :&lt;span class="hljs-operator"&gt;=&lt;/span&gt;&lt;span class="hljs-number"&gt;0&lt;/span&gt;) r) A
&lt;span class="hljs-keyword"&gt;WHERE&lt;/span&gt; rank&lt;span class="hljs-operator"&gt;=&lt;/span&gt;&lt;span class="hljs-number"&gt;2&lt;/span&gt;;
&lt;/code&gt;&lt;button class="copy-btn btn btn-secondary btn-sm" onclick="copyCode(this,'code_2')"&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;&lt;a rel="nofollow noreferrer noopener" href="http://sqlfiddle.com/#!2/0b9ab/1"&gt;&lt;strong&gt;Sql_Demo&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;h2&gt;Output:&lt;/h2&gt;&lt;pre&gt;&lt;code id="code_3" class="hljs language-undefined"&gt;ACCOUNT_ID  FULL_NAME   SUPERHERO
1             Batman    1
2            Spiderman  1
3           Professor X 1
4            Cyclops    1
&lt;/code&gt;&lt;button class="copy-btn btn btn-secondary btn-sm" onclick="copyCode(this,'code_3')"&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id="solution_3"&gt;Solution 3:

&lt;/h2&gt;&lt;div class="answer-desc"&gt;&lt;p&gt;try this&lt;/p&gt;&lt;pre&gt;&lt;code id="code_4" class="hljs language-csharp"&gt;&lt;span class="hljs-keyword"&gt;select&lt;/span&gt; * &lt;span class="hljs-keyword"&gt;from&lt;/span&gt; table1
&lt;span class="hljs-keyword"&gt;group&lt;/span&gt;&lt;span class="hljs-keyword"&gt;by&lt;/span&gt; account_id
&lt;/code&gt;&lt;button class="copy-btn btn btn-secondary btn-sm" onclick="copyCode(this,'code_4')"&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;&lt;a rel="nofollow noreferrer noopener" href="http://sqlfiddle.com/#!2/9bfc1/3"&gt;demo here&lt;/a&gt;&lt;/p&gt;&lt;p&gt;this will give you the first of every account_id &lt;/p&gt;&lt;/div&gt; 

</description><link>https://rachmatmoradosugiarto.blogspot.com/2024/08/distinct-on-only-one-column-only.html</link><author>noreply@blogger.com (Sandy Veum)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8397719027199589165.post-8488478937566750919</guid><pubDate>Wed, 21 Aug 2024 22:35:00 +0000</pubDate><atom:updated>2024-08-21T15:35:40.365-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Sql</category><category domain="http://www.blogger.com/atom/ns#">Temp Tables</category><title>What Are The Use Cases For Temporary Tables In Sql Database Systems?</title><description>&lt;div class="question"&gt;what are the main purposes for which temporary tables are used? I want to know the practical and commercial uses of temporary tables in actual softwares working in small and large&lt;/div&gt;&lt;h2 id="solution_1"&gt;Solution 1:

&lt;/h2&gt;&lt;div class="answer-desc"&gt;&lt;p&gt;In my experience, temporary tables are often used to store intermediate calculations in a complex series of CREATE or UPDATE queries that produce some sort of analysis result. An example might be the creation of summary tables for an OLAP database.&lt;/p&gt;&lt;p&gt;Temporary tables are also sometimes used to increase performance, in certain situations.&lt;/p&gt;&lt;/div&gt;&lt;h2 id="solution_2"&gt;Solution 2:

&lt;/h2&gt;&lt;div class="answer-desc"&gt;&lt;p&gt;I tend to prefer using &lt;a rel="nofollow noreferrer noopener" href="https://msdn.microsoft.com/en-us/library/ms175972.aspx"&gt;CTEs&lt;/a&gt; (common table expressions) to temporary tables. There is a good discussion in &lt;a rel="nofollow noreferrer noopener" href="https://stackoverflow.com/questions/690465/which-are-more-performant-cte-or-temporary-tables"&gt;answer&lt;/a&gt; to a similar question.&lt;/p&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;/div&gt; 


</description><link>https://rachmatmoradosugiarto.blogspot.com/2024/08/what-are-use-cases-for-temporary-tables.html</link><author>noreply@blogger.com (Sandy Veum)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8397719027199589165.post-892433390102911811</guid><pubDate>Wed, 21 Aug 2024 22:18:00 +0000</pubDate><atom:updated>2024-08-21T15:18:08.450-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Arrays</category><category domain="http://www.blogger.com/atom/ns#">Sqlparameter</category><category domain="http://www.blogger.com/atom/ns#">Vb.net</category><title>Sqlparameters Array In Vb.net</title><description>&lt;div class="question"&gt;I'm trying to create a typed-sized parameters array in VB.Net: Dim parameters() As SqlParameter = New SqlParameter() _         {           New SqlParameter('@first_name', SqlDbType&lt;/div&gt;

&lt;h2 id="solution_1"&gt;Solution 1:

&lt;/h2&gt;
&lt;div class="answer-desc"&gt;&lt;p&gt;You need to use the VB syntax for object initializers:&lt;/p&gt;
&lt;pre&gt;&lt;code id="code_0" class="hljs language-csharp"&gt;&lt;span class="hljs-function"&gt;Dim &lt;span class="hljs-title"&gt;parameters&lt;/span&gt;() As SqlParameter&lt;/span&gt; = &lt;span class="hljs-function"&gt;New &lt;span class="hljs-title"&gt;SqlParameter&lt;/span&gt;() _&lt;/span&gt;
        {
          &lt;span class="hljs-function"&gt;New &lt;span class="hljs-title"&gt;SqlParameter&lt;/span&gt;(&lt;span class="hljs-params"&gt;&lt;span class="hljs-string"&gt;"@first_name"&lt;/span&gt;, SqlDbType.VarChar, &lt;span class="hljs-number"&gt;50&lt;/span&gt;&lt;/span&gt;) With&lt;/span&gt; { .Value = &lt;span class="hljs-string"&gt;"john"&lt;/span&gt;},
          &lt;span class="hljs-function"&gt;New &lt;span class="hljs-title"&gt;SqlParameter&lt;/span&gt;(&lt;span class="hljs-params"&gt;&lt;span class="hljs-string"&gt;"@last_name"&lt;/span&gt;, SqlDbType.VarChar, &lt;span class="hljs-number"&gt;50&lt;/span&gt;&lt;/span&gt;) With&lt;/span&gt; { .Value = &lt;span class="hljs-string"&gt;"doe"&lt;/span&gt;},
          &lt;span class="hljs-function"&gt;New &lt;span class="hljs-title"&gt;SqlParameter&lt;/span&gt;(&lt;span class="hljs-params"&gt;&lt;span class="hljs-string"&gt;"@age"&lt;/span&gt;, SqlDbType.Int&lt;/span&gt;) With&lt;/span&gt; { .Value = &lt;span class="hljs-number"&gt;18&lt;/span&gt;},
          &lt;span class="hljs-function"&gt;New &lt;span class="hljs-title"&gt;SqlParameter&lt;/span&gt;(&lt;span class="hljs-params"&gt;&lt;span class="hljs-string"&gt;"@id"&lt;/span&gt;, SqlDbType.Int&lt;/span&gt;) With&lt;/span&gt; { .Value = &lt;span class="hljs-number"&gt;123&lt;/span&gt;}
        }
&lt;/code&gt;&lt;button class="copy-btn btn btn-secondary btn-sm" onclick="copyCode(this,'code_0')"&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;/div&gt; 
&lt;br&gt;
</description><link>https://rachmatmoradosugiarto.blogspot.com/2024/08/sqlparameters-array-in-vbnet.html</link><author>noreply@blogger.com (Sandy Veum)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8397719027199589165.post-4275131621304289652</guid><pubDate>Wed, 21 Aug 2024 21:29:00 +0000</pubDate><atom:updated>2024-08-21T14:29:32.215-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Sql Server</category><category domain="http://www.blogger.com/atom/ns#">Sql Server 2012</category><title>How To Write Dynamic Query Columns Into Rows In Sql Server?</title><description>&lt;div class="question"&gt;I have a question about in SQL Server: how to combine 4 columns into 1 column in a query. Table: emp  empid | addr | sal | doj ------+------+-----+------------   1   | hyd  | 10  |&lt;/div&gt;&lt;h2 id="solution_1"&gt;Solution 1:

&lt;/h2&gt;&lt;div class="answer-desc"&gt;&lt;p&gt;You could try using UNPIVOT, like this...&lt;/p&gt;&lt;pre&gt;&lt;code id="code_0" class="hljs language-sql"&gt;&lt;span class="hljs-keyword"&gt;SELECT&lt;/span&gt; empid, empvalues
&lt;span class="hljs-keyword"&gt;FROM&lt;/span&gt; (
    &lt;span class="hljs-keyword"&gt;select&lt;/span&gt; empid, addr, &lt;span class="hljs-keyword"&gt;convert&lt;/span&gt;(&lt;span class="hljs-type"&gt;varchar&lt;/span&gt;(&lt;span class="hljs-number"&gt;100&lt;/span&gt;), sal) &lt;span class="hljs-keyword"&gt;as&lt;/span&gt; sal,  &lt;span class="hljs-keyword"&gt;convert&lt;/span&gt;(&lt;span class="hljs-type"&gt;varchar&lt;/span&gt;(&lt;span class="hljs-number"&gt;100&lt;/span&gt;), doj, &lt;span class="hljs-number"&gt;103&lt;/span&gt;) &lt;span class="hljs-keyword"&gt;as&lt;/span&gt; doj
    &lt;span class="hljs-keyword"&gt;from&lt;/span&gt; emp
) pv
UNPIVOT
(
    empvalues
    &lt;span class="hljs-keyword"&gt;FOR&lt;/span&gt; ev &lt;span class="hljs-keyword"&gt;in&lt;/span&gt; (addr, sal, doj)
) &lt;span class="hljs-keyword"&gt;AS&lt;/span&gt; ev
&lt;/code&gt;&lt;button class="copy-btn btn btn-secondary btn-sm" onclick="copyCode(this,'code_0')"&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;produces this output...&lt;/p&gt;&lt;p&gt;&lt;a rel="nofollow noreferrer noopener" href="https://i.stack.imgur.com/4XFSB.png"&gt;&lt;img alt="sql results" src="https://i.stack.imgur.com/4XFSB.png"&gt;&lt;/a&gt;&lt;/p&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;p&gt;Resources for your reference...&lt;/p&gt;&lt;p&gt;&lt;a rel="nofollow noreferrer noopener" href="https://codingsight.com/understanding-pivot-unpivot-and-reverse-pivot-statements/"&gt;https://codingsight.com/understanding-pivot-unpivot-and-reverse-pivot-statements/&lt;/a&gt;&lt;a rel="nofollow noreferrer noopener" href="https://docs.microsoft.com/en-us/sql/t-sql/queries/from-using-pivot-and-unpivot?view=sql-server-ver15"&gt;https://docs.microsoft.com/en-us/sql/t-sql/queries/from-using-pivot-and-unpivot?view=sql-server-ver15&lt;/a&gt;&lt;/p&gt;&lt;/div&gt;&lt;h2 id="solution_2"&gt;Solution 2:

&lt;/h2&gt;&lt;div class="answer-desc"&gt;&lt;p&gt;Use &lt;strong&gt;Cross Apply&lt;/strong&gt;: &lt;/p&gt;&lt;pre&gt;&lt;code id="code_1" class="hljs language-sql"&gt;&lt;span class="hljs-keyword"&gt;create&lt;/span&gt;&lt;span class="hljs-keyword"&gt;table&lt;/span&gt; emp (empid &lt;span class="hljs-type"&gt;int&lt;/span&gt;, addr &lt;span class="hljs-type"&gt;varchar&lt;/span&gt;(&lt;span class="hljs-number"&gt;10&lt;/span&gt;), sal &lt;span class="hljs-type"&gt;int&lt;/span&gt;, doj datetime) 

&lt;span class="hljs-keyword"&gt;insert&lt;/span&gt;&lt;span class="hljs-keyword"&gt;into&lt;/span&gt; emp (empid, addr, sal, doj)
&lt;span class="hljs-keyword"&gt;values&lt;/span&gt; (&lt;span class="hljs-number"&gt;1&lt;/span&gt;, &lt;span class="hljs-string"&gt;'hyd'&lt;/span&gt;, &lt;span class="hljs-string"&gt;'10'&lt;/span&gt;, &lt;span class="hljs-string"&gt;'10-01-1990'&lt;/span&gt;),
  (&lt;span class="hljs-number"&gt;2&lt;/span&gt;, &lt;span class="hljs-string"&gt;'del'&lt;/span&gt;, &lt;span class="hljs-string"&gt;'20'&lt;/span&gt;, &lt;span class="hljs-string"&gt;'12-03-1999'&lt;/span&gt;),
  (&lt;span class="hljs-number"&gt;1&lt;/span&gt;, &lt;span class="hljs-string"&gt;'pune'&lt;/span&gt;, &lt;span class="hljs-string"&gt;'50'&lt;/span&gt;, &lt;span class="hljs-string"&gt;'12-03-2017'&lt;/span&gt;); 


&lt;span class="hljs-keyword"&gt;select&lt;/span&gt; empid,empvalues &lt;span class="hljs-keyword"&gt;from&lt;/span&gt; emp
&lt;span class="hljs-keyword"&gt;cross&lt;/span&gt; apply (&lt;span class="hljs-keyword"&gt;values&lt;/span&gt; (&lt;span class="hljs-built_in"&gt;cast&lt;/span&gt; (addr &lt;span class="hljs-keyword"&gt;as&lt;/span&gt;&lt;span class="hljs-type"&gt;varchar&lt;/span&gt;(&lt;span class="hljs-number"&gt;10&lt;/span&gt;)))
,(&lt;span class="hljs-built_in"&gt;cast&lt;/span&gt; (sal &lt;span class="hljs-keyword"&gt;as&lt;/span&gt;&lt;span class="hljs-type"&gt;varchar&lt;/span&gt;(&lt;span class="hljs-number"&gt;10&lt;/span&gt;)))  
,(Format(doj,&lt;span class="hljs-string"&gt;'MM-dd-yyyy'&lt;/span&gt;)))   d(empvalues) 
&lt;/code&gt;&lt;button class="copy-btn btn btn-secondary btn-sm" onclick="copyCode(this,'code_1')"&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;&lt;strong&gt;Results:&lt;/strong&gt;&lt;/p&gt;&lt;pre&gt;&lt;code id="code_2" class="hljs language-css"&gt;empid   empvalues
&lt;span class="hljs-number"&gt;1&lt;/span&gt;   hyd
&lt;span class="hljs-number"&gt;1&lt;/span&gt;&lt;span class="hljs-number"&gt;10&lt;/span&gt;&lt;span class="hljs-number"&gt;1&lt;/span&gt;&lt;span class="hljs-number"&gt;10&lt;/span&gt;-&lt;span class="hljs-number"&gt;01&lt;/span&gt;-&lt;span class="hljs-number"&gt;1990&lt;/span&gt;&lt;span class="hljs-number"&gt;2&lt;/span&gt;&lt;span class="hljs-selector-tag"&gt;del&lt;/span&gt;&lt;span class="hljs-number"&gt;2&lt;/span&gt;&lt;span class="hljs-number"&gt;20&lt;/span&gt;&lt;span class="hljs-number"&gt;2&lt;/span&gt;&lt;span class="hljs-number"&gt;12&lt;/span&gt;-&lt;span class="hljs-number"&gt;03&lt;/span&gt;-&lt;span class="hljs-number"&gt;1999&lt;/span&gt;&lt;span class="hljs-number"&gt;1&lt;/span&gt;   pune
&lt;span class="hljs-number"&gt;1&lt;/span&gt;&lt;span class="hljs-number"&gt;50&lt;/span&gt;&lt;span class="hljs-number"&gt;1&lt;/span&gt;&lt;span class="hljs-number"&gt;12&lt;/span&gt;-&lt;span class="hljs-number"&gt;03&lt;/span&gt;-&lt;span class="hljs-number"&gt;2017&lt;/span&gt;&lt;/code&gt;&lt;button class="copy-btn btn btn-secondary btn-sm" onclick="copyCode(this,'code_2')"&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;More info check below articles: &lt;/p&gt;&lt;p&gt;&lt;a rel="nofollow noreferrer noopener" href="https://docs.microsoft.com/en-us/sql/t-sql/queries/from-transact-sql?view=sql-server-ver15"&gt;docs.microsoft&lt;/a&gt;
and
&lt;a rel="nofollow noreferrer noopener" href="https://www.mssqltips.com/sqlservertip/1958/sql-server-cross-apply-and-outer-apply/"&gt;SQL Server CROSS APPLY and OUTER APPLY&lt;/a&gt;&lt;/p&gt;&lt;/div&gt; 


</description><link>https://rachmatmoradosugiarto.blogspot.com/2024/08/how-to-write-dynamic-query-columns-into.html</link><author>noreply@blogger.com (Sandy Veum)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8397719027199589165.post-5008169884231316791</guid><pubDate>Wed, 21 Aug 2024 20:53:00 +0000</pubDate><atom:updated>2024-08-21T13:53:43.547-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Hierarchical Data</category><category domain="http://www.blogger.com/atom/ns#">Sql Server</category><category domain="http://www.blogger.com/atom/ns#">Sql Server 2008</category><category domain="http://www.blogger.com/atom/ns#">Tsql</category><title>Create Tree Query From Numeric Mapping Table In Sql (specific Format)</title><description>&lt;div class="question"&gt;I have an exported table from accounting software like below.  AccountID     AccountName       ---------     -----------   11            Acc11   12            Acc12   13&lt;/div&gt;&lt;h2 id="solution_1"&gt;Solution 1:

&lt;/h2&gt;&lt;div class="answer-desc"&gt;&lt;p&gt;I modified my answer given in the first question...&lt;/p&gt;&lt;p&gt;It would be best, if your table would keep the relation data directly in indexed columns. Before you change your table's structure you might try this:&lt;/p&gt;&lt;p&gt;A table with test data&lt;/p&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;pre&gt;&lt;code id="code_0" class="hljs language-sql"&gt;&lt;span class="hljs-keyword"&gt;DECLARE&lt;/span&gt;&lt;span class="hljs-variable"&gt;@tbl&lt;/span&gt;&lt;span class="hljs-keyword"&gt;TABLE&lt;/span&gt; ( AccountID  &lt;span class="hljs-type"&gt;VARCHAR&lt;/span&gt;(&lt;span class="hljs-number"&gt;100&lt;/span&gt;), AccountName &lt;span class="hljs-type"&gt;VARCHAR&lt;/span&gt;(&lt;span class="hljs-number"&gt;100&lt;/span&gt;));
&lt;span class="hljs-keyword"&gt;INSERT&lt;/span&gt;&lt;span class="hljs-keyword"&gt;INTO&lt;/span&gt;&lt;span class="hljs-variable"&gt;@tbl&lt;/span&gt;&lt;span class="hljs-keyword"&gt;VALUES&lt;/span&gt; 
 (&lt;span class="hljs-string"&gt;'11'&lt;/span&gt;,&lt;span class="hljs-string"&gt;'Acc11'&lt;/span&gt;)
,(&lt;span class="hljs-string"&gt;'12'&lt;/span&gt;,&lt;span class="hljs-string"&gt;'Acc12'&lt;/span&gt;)
,(&lt;span class="hljs-string"&gt;'13'&lt;/span&gt;,&lt;span class="hljs-string"&gt;'Acc13'&lt;/span&gt;)
,(&lt;span class="hljs-string"&gt;'11/11'&lt;/span&gt;,&lt;span class="hljs-string"&gt;'Acc11/11'&lt;/span&gt;)
,(&lt;span class="hljs-string"&gt;'11/12'&lt;/span&gt;,&lt;span class="hljs-string"&gt;'Acc11/12'&lt;/span&gt;)
,(&lt;span class="hljs-string"&gt;'11/111'&lt;/span&gt;,&lt;span class="hljs-string"&gt;'Acc11/111'&lt;/span&gt;)
,(&lt;span class="hljs-string"&gt;'11/11/001'&lt;/span&gt;,&lt;span class="hljs-string"&gt;'Acc11/11/001'&lt;/span&gt;)
,(&lt;span class="hljs-string"&gt;'11/11/002'&lt;/span&gt;,&lt;span class="hljs-string"&gt;'Acc11/11/002'&lt;/span&gt;)
,(&lt;span class="hljs-string"&gt;'12/111'&lt;/span&gt;,&lt;span class="hljs-string"&gt;'Acc12/111'&lt;/span&gt;)
,(&lt;span class="hljs-string"&gt;'12/112'&lt;/span&gt;,&lt;span class="hljs-string"&gt;'Acc12/112'&lt;/span&gt;);
&lt;/code&gt;&lt;button class="copy-btn btn btn-secondary btn-sm" onclick="copyCode(this,'code_0')"&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;This will get the needed data into a newly created temp table called &lt;code&gt;#tempHierarchy&lt;/code&gt;&lt;/p&gt;&lt;pre&gt;&lt;code id="code_2" class="hljs language-sql"&gt;&lt;span class="hljs-keyword"&gt;SELECT&lt;/span&gt; AccountID
      ,AccountName
      ,&lt;span class="hljs-built_in"&gt;ROW_NUMBER&lt;/span&gt;() &lt;span class="hljs-keyword"&gt;OVER&lt;/span&gt;(&lt;span class="hljs-keyword"&gt;ORDER&lt;/span&gt;&lt;span class="hljs-keyword"&gt;BY&lt;/span&gt; LEN(AccountID)&lt;span class="hljs-operator"&gt;-&lt;/span&gt;LEN(REPLACE(AccountID,&lt;span class="hljs-string"&gt;'/'&lt;/span&gt;,&lt;span class="hljs-string"&gt;''&lt;/span&gt;)),AccountID) &lt;span class="hljs-keyword"&gt;AS&lt;/span&gt; ID
      ,Extended.HierarchyLevel
      ,STUFF(
       (
         &lt;span class="hljs-keyword"&gt;SELECT&lt;/span&gt;&lt;span class="hljs-string"&gt;'/'&lt;/span&gt;&lt;span class="hljs-operator"&gt;+&lt;/span&gt; A.B.value(&lt;span class="hljs-string"&gt;'.'&lt;/span&gt;,&lt;span class="hljs-string"&gt;'varchar(10)'&lt;/span&gt;)
         &lt;span class="hljs-keyword"&gt;FROM&lt;/span&gt; Extended.IDsXML.nodes(&lt;span class="hljs-string"&gt;'/x[position() &amp;lt;= sql:column("HierarchyLevel")]'&lt;/span&gt;) &lt;span class="hljs-keyword"&gt;AS&lt;/span&gt; A(B)
         &lt;span class="hljs-keyword"&gt;FOR&lt;/span&gt; XML PATH(&lt;span class="hljs-string"&gt;''&lt;/span&gt;)
       ),&lt;span class="hljs-number"&gt;1&lt;/span&gt;,&lt;span class="hljs-number"&gt;2&lt;/span&gt;,&lt;span class="hljs-string"&gt;''&lt;/span&gt;) &lt;span class="hljs-keyword"&gt;AS&lt;/span&gt; ParentPath
      ,Extended.IDsXML.value(&lt;span class="hljs-string"&gt;'/x[sql:column("HierarchyLevel")+1][1]'&lt;/span&gt;,&lt;span class="hljs-string"&gt;'varchar(10)'&lt;/span&gt;) &lt;span class="hljs-keyword"&gt;AS&lt;/span&gt; ownID
      ,Extended.IDsXML.value(&lt;span class="hljs-string"&gt;'/x[sql:column("HierarchyLevel")][1]'&lt;/span&gt;,&lt;span class="hljs-string"&gt;'varchar(10)'&lt;/span&gt;) &lt;span class="hljs-keyword"&gt;AS&lt;/span&gt; ancestorID
&lt;span class="hljs-keyword"&gt;INTO&lt;/span&gt; #tempHierarchy
&lt;span class="hljs-keyword"&gt;FROM&lt;/span&gt;&lt;span class="hljs-variable"&gt;@tbl&lt;/span&gt;&lt;span class="hljs-keyword"&gt;CROSS&lt;/span&gt; APPLY(&lt;span class="hljs-keyword"&gt;SELECT&lt;/span&gt; LEN(AccountID)&lt;span class="hljs-operator"&gt;-&lt;/span&gt;LEN(REPLACE(AccountID,&lt;span class="hljs-string"&gt;'/'&lt;/span&gt;,&lt;span class="hljs-string"&gt;''&lt;/span&gt;)) &lt;span class="hljs-operator"&gt;+&lt;/span&gt;&lt;span class="hljs-number"&gt;1&lt;/span&gt;&lt;span class="hljs-keyword"&gt;AS&lt;/span&gt; HierarchyLevel
                  ,&lt;span class="hljs-built_in"&gt;CAST&lt;/span&gt;(&lt;span class="hljs-string"&gt;'&amp;lt;x&amp;gt;&amp;lt;/x&amp;gt;&amp;lt;x&amp;gt;'&lt;/span&gt;&lt;span class="hljs-operator"&gt;+&lt;/span&gt; REPLACE(AccountID,&lt;span class="hljs-string"&gt;'/'&lt;/span&gt;,&lt;span class="hljs-string"&gt;'&amp;lt;/x&amp;gt;&amp;lt;x&amp;gt;'&lt;/span&gt;) &lt;span class="hljs-operator"&gt;+&lt;/span&gt;&lt;span class="hljs-string"&gt;'&amp;lt;/x&amp;gt;'&lt;/span&gt;&lt;span class="hljs-keyword"&gt;AS&lt;/span&gt; XML) &lt;span class="hljs-keyword"&gt;AS&lt;/span&gt; IDsXML) &lt;span class="hljs-keyword"&gt;AS&lt;/span&gt; Extended
;
&lt;/code&gt;&lt;button class="copy-btn btn btn-secondary btn-sm" onclick="copyCode(this,'code_2')"&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;The intermediate result&lt;/p&gt;&lt;pre&gt;&lt;code id="code_3" class="hljs language-diff"&gt;&lt;span class="hljs-addition"&gt;+-----------+--------------+----+----------------+------------+-------+------------+&lt;/span&gt;
| AccountID | AccountName  | ID | HierarchyLevel | ParentPath | ownID | ancestorID |
&lt;span class="hljs-addition"&gt;+-----------+--------------+----+----------------+------------+-------+------------+&lt;/span&gt;
| 11        | Acc11        | 1  | 1              |            | 11    |            |
&lt;span class="hljs-addition"&gt;+-----------+--------------+----+----------------+------------+-------+------------+&lt;/span&gt;
| 12        | Acc12        | 2  | 1              |            | 12    |            |
&lt;span class="hljs-addition"&gt;+-----------+--------------+----+----------------+------------+-------+------------+&lt;/span&gt;
| 13        | Acc13        | 3  | 1              |            | 13    |            |
&lt;span class="hljs-addition"&gt;+-----------+--------------+----+----------------+------------+-------+------------+&lt;/span&gt;
| 11/11     | Acc11/11     | 4  | 2              | 11         | 11    | 11         |
&lt;span class="hljs-addition"&gt;+-----------+--------------+----+----------------+------------+-------+------------+&lt;/span&gt;
| 11/111    | Acc11/111    | 5  | 2              | 11         | 111   | 11         |
&lt;span class="hljs-addition"&gt;+-----------+--------------+----+----------------+------------+-------+------------+&lt;/span&gt;
| 11/12     | Acc11/12     | 6  | 2              | 11         | 12    | 11         |
&lt;span class="hljs-addition"&gt;+-----------+--------------+----+----------------+------------+-------+------------+&lt;/span&gt;
| 12/111    | Acc12/111    | 7  | 2              | 12         | 111   | 12         |
&lt;span class="hljs-addition"&gt;+-----------+--------------+----+----------------+------------+-------+------------+&lt;/span&gt;
| 12/112    | Acc12/112    | 8  | 2              | 12         | 112   | 12         |
&lt;span class="hljs-addition"&gt;+-----------+--------------+----+----------------+------------+-------+------------+&lt;/span&gt;
| 11/11/001 | Acc11/11/001 | 9  | 3              | 11/11      | 001   | 11         |
&lt;span class="hljs-addition"&gt;+-----------+--------------+----+----------------+------------+-------+------------+&lt;/span&gt;
| 11/11/002 | Acc11/11/002 | 10 | 3              | 11/11      | 002   | 11         |
&lt;span class="hljs-addition"&gt;+-----------+--------------+----+----------------+------------+-------+------------+&lt;/span&gt;&lt;/code&gt;&lt;button class="copy-btn btn btn-secondary btn-sm" onclick="copyCode(this,'code_3')"&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;And now a similar recursive approach takes place as in my first answer. But - as it is using a real table now and all the string splitting has taken place already - it should be faster...&lt;/p&gt;&lt;pre&gt;&lt;code id="code_4" class="hljs language-vbnet"&gt;&lt;span class="hljs-keyword"&gt;WITH&lt;/span&gt; RecursiveCTE &lt;span class="hljs-keyword"&gt;AS&lt;/span&gt;
(
    &lt;span class="hljs-keyword"&gt;SELECT&lt;/span&gt; th.*
           ,CAST(NULL &lt;span class="hljs-keyword"&gt;AS&lt;/span&gt; BIGINT) &lt;span class="hljs-keyword"&gt;AS&lt;/span&gt; ParentID 
           ,&lt;span class="hljs-keyword"&gt;CASE&lt;/span&gt;&lt;span class="hljs-keyword"&gt;WHEN&lt;/span&gt; EXISTS(&lt;span class="hljs-keyword"&gt;SELECT&lt;/span&gt;&lt;span class="hljs-number"&gt;1&lt;/span&gt;&lt;span class="hljs-keyword"&gt;FROM&lt;/span&gt; #tempHierarchy &lt;span class="hljs-keyword"&gt;AS&lt;/span&gt; x &lt;span class="hljs-keyword"&gt;WHERE&lt;/span&gt; x.ParentPath=th.AccountID) &lt;span class="hljs-keyword"&gt;THEN&lt;/span&gt;&lt;span class="hljs-number"&gt;1&lt;/span&gt;&lt;span class="hljs-keyword"&gt;ELSE&lt;/span&gt;&lt;span class="hljs-number"&gt;0&lt;/span&gt;&lt;span class="hljs-keyword"&gt;END&lt;/span&gt;&lt;span class="hljs-keyword"&gt;AS&lt;/span&gt; HasChild
    &lt;span class="hljs-keyword"&gt;FROM&lt;/span&gt; #tempHierarchy &lt;span class="hljs-keyword"&gt;AS&lt;/span&gt; th &lt;span class="hljs-keyword"&gt;WHERE&lt;/span&gt; th.HierarchyLevel=&lt;span class="hljs-number"&gt;1&lt;/span&gt;
    UNION ALL
    &lt;span class="hljs-keyword"&gt;SELECT&lt;/span&gt; sa.AccountID
          ,sa.AccountName
          ,sa.ID
          ,sa.HierarchyLevel
          ,sa.ParentPath
          ,sa.ownID
          ,sa.ancestorID
          ,(&lt;span class="hljs-keyword"&gt;SELECT&lt;/span&gt; x.ID &lt;span class="hljs-keyword"&gt;FROM&lt;/span&gt; #tempHierarchy &lt;span class="hljs-keyword"&gt;AS&lt;/span&gt; x &lt;span class="hljs-keyword"&gt;WHERE&lt;/span&gt; x.AccountID=sa.ParentPath)
          ,&lt;span class="hljs-keyword"&gt;CASE&lt;/span&gt;&lt;span class="hljs-keyword"&gt;WHEN&lt;/span&gt; EXISTS(&lt;span class="hljs-keyword"&gt;SELECT&lt;/span&gt;&lt;span class="hljs-number"&gt;1&lt;/span&gt;&lt;span class="hljs-keyword"&gt;FROM&lt;/span&gt; #tempHierarchy &lt;span class="hljs-keyword"&gt;AS&lt;/span&gt; x &lt;span class="hljs-keyword"&gt;WHERE&lt;/span&gt; x.ParentPath=sa.AccountID) &lt;span class="hljs-keyword"&gt;THEN&lt;/span&gt;&lt;span class="hljs-number"&gt;1&lt;/span&gt;&lt;span class="hljs-keyword"&gt;ELSE&lt;/span&gt;&lt;span class="hljs-number"&gt;0&lt;/span&gt;&lt;span class="hljs-keyword"&gt;END&lt;/span&gt;&lt;span class="hljs-keyword"&gt;AS&lt;/span&gt; HasChild
    &lt;span class="hljs-keyword"&gt;FROM&lt;/span&gt; RecursiveCTE &lt;span class="hljs-keyword"&gt;AS&lt;/span&gt; r
    INNER &lt;span class="hljs-keyword"&gt;JOIN&lt;/span&gt; #tempHierarchy &lt;span class="hljs-keyword"&gt;AS&lt;/span&gt; sa &lt;span class="hljs-keyword"&gt;ON&lt;/span&gt; sa.HierarchyLevel=r.HierarchyLevel+&lt;span class="hljs-number"&gt;1&lt;/span&gt;&lt;span class="hljs-built_in"&gt;AND&lt;/span&gt; r.AccountID=sa.ParentPath
)
&lt;span class="hljs-keyword"&gt;SELECT&lt;/span&gt; r.AccountID
      ,r.AccountName
      ,r.ID
      ,r.ParentID
      ,r.HierarchyLevel
      ,r.HasChild
&lt;span class="hljs-keyword"&gt;FROM&lt;/span&gt; RecursiveCTE &lt;span class="hljs-keyword"&gt;AS&lt;/span&gt; r
&lt;span class="hljs-keyword"&gt;ORDER&lt;/span&gt;&lt;span class="hljs-keyword"&gt;BY&lt;/span&gt; HierarchyLevel,ParentID;
&lt;/code&gt;&lt;button class="copy-btn btn btn-secondary btn-sm" onclick="copyCode(this,'code_4')"&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;And finally I clean up&lt;/p&gt;&lt;pre&gt;&lt;code id="code_5" class="hljs language-less"&gt;&lt;span class="hljs-selector-tag"&gt;DROP&lt;/span&gt;&lt;span class="hljs-selector-tag"&gt;TABLE&lt;/span&gt;&lt;span class="hljs-selector-id"&gt;#tempHierarchy&lt;/span&gt;;
&lt;/code&gt;&lt;button class="copy-btn btn btn-secondary btn-sm" onclick="copyCode(this,'code_5')"&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;And here's the final result&lt;/p&gt;&lt;pre&gt;&lt;code id="code_6" class="hljs language-sql"&gt;&lt;span class="hljs-operator"&gt;+&lt;/span&gt;&lt;span class="hljs-comment"&gt;-----------+--------------+----+----------+----------------+----------+&lt;/span&gt;&lt;span class="hljs-operator"&gt;|&lt;/span&gt; AccountID &lt;span class="hljs-operator"&gt;|&lt;/span&gt; AccountName  &lt;span class="hljs-operator"&gt;|&lt;/span&gt; ID &lt;span class="hljs-operator"&gt;|&lt;/span&gt; ParentID &lt;span class="hljs-operator"&gt;|&lt;/span&gt; HierarchyLevel &lt;span class="hljs-operator"&gt;|&lt;/span&gt; HasChild &lt;span class="hljs-operator"&gt;|&lt;/span&gt;&lt;span class="hljs-operator"&gt;+&lt;/span&gt;&lt;span class="hljs-comment"&gt;-----------+--------------+----+----------+----------------+----------+&lt;/span&gt;&lt;span class="hljs-operator"&gt;|&lt;/span&gt;&lt;span class="hljs-number"&gt;11&lt;/span&gt;&lt;span class="hljs-operator"&gt;|&lt;/span&gt; Acc11        &lt;span class="hljs-operator"&gt;|&lt;/span&gt;&lt;span class="hljs-number"&gt;1&lt;/span&gt;&lt;span class="hljs-operator"&gt;|&lt;/span&gt;&lt;span class="hljs-keyword"&gt;NULL&lt;/span&gt;&lt;span class="hljs-operator"&gt;|&lt;/span&gt;&lt;span class="hljs-number"&gt;1&lt;/span&gt;&lt;span class="hljs-operator"&gt;|&lt;/span&gt;&lt;span class="hljs-number"&gt;1&lt;/span&gt;&lt;span class="hljs-operator"&gt;|&lt;/span&gt;&lt;span class="hljs-operator"&gt;+&lt;/span&gt;&lt;span class="hljs-comment"&gt;-----------+--------------+----+----------+----------------+----------+&lt;/span&gt;&lt;span class="hljs-operator"&gt;|&lt;/span&gt;&lt;span class="hljs-number"&gt;12&lt;/span&gt;&lt;span class="hljs-operator"&gt;|&lt;/span&gt; Acc12        &lt;span class="hljs-operator"&gt;|&lt;/span&gt;&lt;span class="hljs-number"&gt;2&lt;/span&gt;&lt;span class="hljs-operator"&gt;|&lt;/span&gt;&lt;span class="hljs-keyword"&gt;NULL&lt;/span&gt;&lt;span class="hljs-operator"&gt;|&lt;/span&gt;&lt;span class="hljs-number"&gt;1&lt;/span&gt;&lt;span class="hljs-operator"&gt;|&lt;/span&gt;&lt;span class="hljs-number"&gt;1&lt;/span&gt;&lt;span class="hljs-operator"&gt;|&lt;/span&gt;&lt;span class="hljs-operator"&gt;+&lt;/span&gt;&lt;span class="hljs-comment"&gt;-----------+--------------+----+----------+----------------+----------+&lt;/span&gt;&lt;span class="hljs-operator"&gt;|&lt;/span&gt;&lt;span class="hljs-number"&gt;13&lt;/span&gt;&lt;span class="hljs-operator"&gt;|&lt;/span&gt; Acc13        &lt;span class="hljs-operator"&gt;|&lt;/span&gt;&lt;span class="hljs-number"&gt;3&lt;/span&gt;&lt;span class="hljs-operator"&gt;|&lt;/span&gt;&lt;span class="hljs-keyword"&gt;NULL&lt;/span&gt;&lt;span class="hljs-operator"&gt;|&lt;/span&gt;&lt;span class="hljs-number"&gt;1&lt;/span&gt;&lt;span class="hljs-operator"&gt;|&lt;/span&gt;&lt;span class="hljs-number"&gt;0&lt;/span&gt;&lt;span class="hljs-operator"&gt;|&lt;/span&gt;&lt;span class="hljs-operator"&gt;+&lt;/span&gt;&lt;span class="hljs-comment"&gt;-----------+--------------+----+----------+----------------+----------+&lt;/span&gt;&lt;span class="hljs-operator"&gt;|&lt;/span&gt;&lt;span class="hljs-number"&gt;11&lt;/span&gt;&lt;span class="hljs-operator"&gt;/&lt;/span&gt;&lt;span class="hljs-number"&gt;11&lt;/span&gt;&lt;span class="hljs-operator"&gt;|&lt;/span&gt; Acc11&lt;span class="hljs-operator"&gt;/&lt;/span&gt;&lt;span class="hljs-number"&gt;11&lt;/span&gt;&lt;span class="hljs-operator"&gt;|&lt;/span&gt;&lt;span class="hljs-number"&gt;4&lt;/span&gt;&lt;span class="hljs-operator"&gt;|&lt;/span&gt;&lt;span class="hljs-number"&gt;1&lt;/span&gt;&lt;span class="hljs-operator"&gt;|&lt;/span&gt;&lt;span class="hljs-number"&gt;2&lt;/span&gt;&lt;span class="hljs-operator"&gt;|&lt;/span&gt;&lt;span class="hljs-number"&gt;1&lt;/span&gt;&lt;span class="hljs-operator"&gt;|&lt;/span&gt;&lt;span class="hljs-operator"&gt;+&lt;/span&gt;&lt;span class="hljs-comment"&gt;-----------+--------------+----+----------+----------------+----------+&lt;/span&gt;&lt;span class="hljs-operator"&gt;|&lt;/span&gt;&lt;span class="hljs-number"&gt;11&lt;/span&gt;&lt;span class="hljs-operator"&gt;/&lt;/span&gt;&lt;span class="hljs-number"&gt;111&lt;/span&gt;&lt;span class="hljs-operator"&gt;|&lt;/span&gt; Acc11&lt;span class="hljs-operator"&gt;/&lt;/span&gt;&lt;span class="hljs-number"&gt;111&lt;/span&gt;&lt;span class="hljs-operator"&gt;|&lt;/span&gt;&lt;span class="hljs-number"&gt;5&lt;/span&gt;&lt;span class="hljs-operator"&gt;|&lt;/span&gt;&lt;span class="hljs-number"&gt;1&lt;/span&gt;&lt;span class="hljs-operator"&gt;|&lt;/span&gt;&lt;span class="hljs-number"&gt;2&lt;/span&gt;&lt;span class="hljs-operator"&gt;|&lt;/span&gt;&lt;span class="hljs-number"&gt;0&lt;/span&gt;&lt;span class="hljs-operator"&gt;|&lt;/span&gt;&lt;span class="hljs-operator"&gt;+&lt;/span&gt;&lt;span class="hljs-comment"&gt;-----------+--------------+----+----------+----------------+----------+&lt;/span&gt;&lt;span class="hljs-operator"&gt;|&lt;/span&gt;&lt;span class="hljs-number"&gt;11&lt;/span&gt;&lt;span class="hljs-operator"&gt;/&lt;/span&gt;&lt;span class="hljs-number"&gt;12&lt;/span&gt;&lt;span class="hljs-operator"&gt;|&lt;/span&gt; Acc11&lt;span class="hljs-operator"&gt;/&lt;/span&gt;&lt;span class="hljs-number"&gt;12&lt;/span&gt;&lt;span class="hljs-operator"&gt;|&lt;/span&gt;&lt;span class="hljs-number"&gt;6&lt;/span&gt;&lt;span class="hljs-operator"&gt;|&lt;/span&gt;&lt;span class="hljs-number"&gt;1&lt;/span&gt;&lt;span class="hljs-operator"&gt;|&lt;/span&gt;&lt;span class="hljs-number"&gt;2&lt;/span&gt;&lt;span class="hljs-operator"&gt;|&lt;/span&gt;&lt;span class="hljs-number"&gt;0&lt;/span&gt;&lt;span class="hljs-operator"&gt;|&lt;/span&gt;&lt;span class="hljs-operator"&gt;+&lt;/span&gt;&lt;span class="hljs-comment"&gt;-----------+--------------+----+----------+----------------+----------+&lt;/span&gt;&lt;span class="hljs-operator"&gt;|&lt;/span&gt;&lt;span class="hljs-number"&gt;12&lt;/span&gt;&lt;span class="hljs-operator"&gt;/&lt;/span&gt;&lt;span class="hljs-number"&gt;111&lt;/span&gt;&lt;span class="hljs-operator"&gt;|&lt;/span&gt; Acc12&lt;span class="hljs-operator"&gt;/&lt;/span&gt;&lt;span class="hljs-number"&gt;111&lt;/span&gt;&lt;span class="hljs-operator"&gt;|&lt;/span&gt;&lt;span class="hljs-number"&gt;7&lt;/span&gt;&lt;span class="hljs-operator"&gt;|&lt;/span&gt;&lt;span class="hljs-number"&gt;2&lt;/span&gt;&lt;span class="hljs-operator"&gt;|&lt;/span&gt;&lt;span class="hljs-number"&gt;2&lt;/span&gt;&lt;span class="hljs-operator"&gt;|&lt;/span&gt;&lt;span class="hljs-number"&gt;0&lt;/span&gt;&lt;span class="hljs-operator"&gt;|&lt;/span&gt;&lt;span class="hljs-operator"&gt;+&lt;/span&gt;&lt;span class="hljs-comment"&gt;-----------+--------------+----+----------+----------------+----------+&lt;/span&gt;&lt;span class="hljs-operator"&gt;|&lt;/span&gt;&lt;span class="hljs-number"&gt;12&lt;/span&gt;&lt;span class="hljs-operator"&gt;/&lt;/span&gt;&lt;span class="hljs-number"&gt;112&lt;/span&gt;&lt;span class="hljs-operator"&gt;|&lt;/span&gt; Acc12&lt;span class="hljs-operator"&gt;/&lt;/span&gt;&lt;span class="hljs-number"&gt;112&lt;/span&gt;&lt;span class="hljs-operator"&gt;|&lt;/span&gt;&lt;span class="hljs-number"&gt;8&lt;/span&gt;&lt;span class="hljs-operator"&gt;|&lt;/span&gt;&lt;span class="hljs-number"&gt;2&lt;/span&gt;&lt;span class="hljs-operator"&gt;|&lt;/span&gt;&lt;span class="hljs-number"&gt;2&lt;/span&gt;&lt;span class="hljs-operator"&gt;|&lt;/span&gt;&lt;span class="hljs-number"&gt;0&lt;/span&gt;&lt;span class="hljs-operator"&gt;|&lt;/span&gt;&lt;span class="hljs-operator"&gt;+&lt;/span&gt;&lt;span class="hljs-comment"&gt;-----------+--------------+----+----------+----------------+----------+&lt;/span&gt;&lt;span class="hljs-operator"&gt;|&lt;/span&gt;&lt;span class="hljs-number"&gt;11&lt;/span&gt;&lt;span class="hljs-operator"&gt;/&lt;/span&gt;&lt;span class="hljs-number"&gt;11&lt;/span&gt;&lt;span class="hljs-operator"&gt;/&lt;/span&gt;&lt;span class="hljs-number"&gt;001&lt;/span&gt;&lt;span class="hljs-operator"&gt;|&lt;/span&gt; Acc11&lt;span class="hljs-operator"&gt;/&lt;/span&gt;&lt;span class="hljs-number"&gt;11&lt;/span&gt;&lt;span class="hljs-operator"&gt;/&lt;/span&gt;&lt;span class="hljs-number"&gt;001&lt;/span&gt;&lt;span class="hljs-operator"&gt;|&lt;/span&gt;&lt;span class="hljs-number"&gt;9&lt;/span&gt;&lt;span class="hljs-operator"&gt;|&lt;/span&gt;&lt;span class="hljs-number"&gt;4&lt;/span&gt;&lt;span class="hljs-operator"&gt;|&lt;/span&gt;&lt;span class="hljs-number"&gt;3&lt;/span&gt;&lt;span class="hljs-operator"&gt;|&lt;/span&gt;&lt;span class="hljs-number"&gt;0&lt;/span&gt;&lt;span class="hljs-operator"&gt;|&lt;/span&gt;&lt;span class="hljs-operator"&gt;+&lt;/span&gt;&lt;span class="hljs-comment"&gt;-----------+--------------+----+----------+----------------+----------+&lt;/span&gt;&lt;span class="hljs-operator"&gt;|&lt;/span&gt;&lt;span class="hljs-number"&gt;11&lt;/span&gt;&lt;span class="hljs-operator"&gt;/&lt;/span&gt;&lt;span class="hljs-number"&gt;11&lt;/span&gt;&lt;span class="hljs-operator"&gt;/&lt;/span&gt;&lt;span class="hljs-number"&gt;002&lt;/span&gt;&lt;span class="hljs-operator"&gt;|&lt;/span&gt; Acc11&lt;span class="hljs-operator"&gt;/&lt;/span&gt;&lt;span class="hljs-number"&gt;11&lt;/span&gt;&lt;span class="hljs-operator"&gt;/&lt;/span&gt;&lt;span class="hljs-number"&gt;002&lt;/span&gt;&lt;span class="hljs-operator"&gt;|&lt;/span&gt;&lt;span class="hljs-number"&gt;10&lt;/span&gt;&lt;span class="hljs-operator"&gt;|&lt;/span&gt;&lt;span class="hljs-number"&gt;4&lt;/span&gt;&lt;span class="hljs-operator"&gt;|&lt;/span&gt;&lt;span class="hljs-number"&gt;3&lt;/span&gt;&lt;span class="hljs-operator"&gt;|&lt;/span&gt;&lt;span class="hljs-number"&gt;0&lt;/span&gt;&lt;span class="hljs-operator"&gt;|&lt;/span&gt;&lt;span class="hljs-operator"&gt;+&lt;/span&gt;&lt;span class="hljs-comment"&gt;-----------+--------------+----+----------+----------------+----------+&lt;/span&gt;&lt;/code&gt;&lt;button class="copy-btn btn btn-secondary btn-sm" onclick="copyCode(this,'code_6')"&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;/div&gt; 

</description><link>https://rachmatmoradosugiarto.blogspot.com/2024/08/create-tree-query-from-numeric-mapping.html</link><author>noreply@blogger.com (Sandy Veum)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8397719027199589165.post-2299622117590252667</guid><pubDate>Wed, 21 Aug 2024 20:35:00 +0000</pubDate><atom:updated>2024-08-21T13:35:55.849-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Sql</category><category domain="http://www.blogger.com/atom/ns#">Sql Server</category><category domain="http://www.blogger.com/atom/ns#">Tsql</category><title>Joining Two Subqueries Or A Query With A Subquery Sql Server 2008</title><description>&lt;div class="question"&gt;monster edit: The query will now run but returns incorrect answers.  Added a rough schema.  PatientID is not the primary key in the tblPatientVisits table because the same patientI&lt;/div&gt;&lt;h2 id="solution_1"&gt;Solution 1:

&lt;/h2&gt;&lt;div class="answer-desc"&gt;&lt;p&gt;Hard to tell without sample data and desired results, but perhaps this is what you are after?&lt;/p&gt;&lt;pre&gt;&lt;code id="code_0" class="hljs language-vbnet"&gt;;&lt;span class="hljs-keyword"&gt;WITH&lt;/span&gt; x &lt;span class="hljs-keyword"&gt;AS&lt;/span&gt; 
(
  &lt;span class="hljs-keyword"&gt;SELECT&lt;/span&gt; c.CountyName, v.patientCounty, v.patientState, p.patientID
    &lt;span class="hljs-keyword"&gt;FROM&lt;/span&gt; dbo.tblPatient &lt;span class="hljs-keyword"&gt;AS&lt;/span&gt; p
    INNER &lt;span class="hljs-keyword"&gt;JOIN&lt;/span&gt; dbo.tblPatientVisits &lt;span class="hljs-keyword"&gt;AS&lt;/span&gt; v
    &lt;span class="hljs-keyword"&gt;ON&lt;/span&gt; p.patientID = v.patientID 
    INNER &lt;span class="hljs-keyword"&gt;JOIN&lt;/span&gt; dbo.tblStateCounties &lt;span class="hljs-keyword"&gt;AS&lt;/span&gt; c 
    &lt;span class="hljs-keyword"&gt;ON&lt;/span&gt; v.patientState = c.stateCode 
    &lt;span class="hljs-built_in"&gt;AND&lt;/span&gt; v.patientCounty = c.countyCode
  &lt;span class="hljs-keyword"&gt;WHERE&lt;/span&gt; c.stateCode = &lt;span class="hljs-comment"&gt;'21'&lt;/span&gt;
),
y &lt;span class="hljs-keyword"&gt;AS&lt;/span&gt; (&lt;span class="hljs-keyword"&gt;SELECT&lt;/span&gt; CountyName, c = COUNT(*) &lt;span class="hljs-keyword"&gt;FROM&lt;/span&gt; x &lt;span class="hljs-keyword"&gt;GROUP&lt;/span&gt;&lt;span class="hljs-keyword"&gt;BY&lt;/span&gt; CountyName),
z &lt;span class="hljs-keyword"&gt;AS&lt;/span&gt; (&lt;span class="hljs-keyword"&gt;SELECT&lt;/span&gt; CountyName, c = COUNT(PatientID) &lt;span class="hljs-keyword"&gt;FROM&lt;/span&gt; x 
  &lt;span class="hljs-keyword"&gt;GROUP&lt;/span&gt;&lt;span class="hljs-keyword"&gt;BY&lt;/span&gt; CountyName, patientState, PatientID HAVING COUNT(*)&amp;gt;&lt;span class="hljs-number"&gt;1&lt;/span&gt;)
&lt;span class="hljs-keyword"&gt;SELECT&lt;/span&gt; y.countyName, reAdmits = MAX(COALESCE(z.c, &lt;span class="hljs-number"&gt;0&lt;/span&gt;)), totalVisits = MAX(y.c)
&lt;span class="hljs-keyword"&gt;FROM&lt;/span&gt; y LEFT OUTER &lt;span class="hljs-keyword"&gt;JOIN&lt;/span&gt; z
&lt;span class="hljs-keyword"&gt;ON&lt;/span&gt; y.CountyName = z.CountyName
&lt;span class="hljs-keyword"&gt;GROUP&lt;/span&gt;&lt;span class="hljs-keyword"&gt;BY&lt;/span&gt; y.CountyName;
&lt;/code&gt;&lt;button class="copy-btn btn btn-secondary btn-sm" onclick="copyCode(this,'code_0')"&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id="solution_2"&gt;Solution 2:

&lt;/h2&gt;&lt;div class="answer-desc"&gt;&lt;p&gt;There appear to be several issues with this query. I assume that the subquery for "s" is intended to be substituted for "s" in the query before. Are you familiar with the "with" syntax? This would be close to how you are expressing it.&lt;/p&gt;&lt;p&gt;In any case, your first subquery is missing a ") " between the HAVING clause and the GROUP BY. In addition, the "s" subquery has a GROUP BY, but "SELECT *".&lt;/p&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;p&gt;The following may be what you are trying to express:&lt;/p&gt;&lt;pre&gt;&lt;code id="code_1" class="hljs language-sql"&gt;&lt;span class="hljs-keyword"&gt;select&lt;/span&gt; t.countyName, &lt;span class="hljs-built_in"&gt;count&lt;/span&gt;(t.countyName), s.countyName, &lt;span class="hljs-built_in"&gt;count&lt;/span&gt;(s.countyName)
&lt;span class="hljs-keyword"&gt;from&lt;/span&gt; (&lt;span class="hljs-keyword"&gt;select&lt;/span&gt; countyName, &lt;span class="hljs-built_in"&gt;count&lt;/span&gt;(countyName) &lt;span class="hljs-keyword"&gt;as&lt;/span&gt; readmitCounts
      &lt;span class="hljs-keyword"&gt;from&lt;/span&gt; (&lt;span class="hljs-keyword"&gt;SELECT&lt;/span&gt; tblPatient.patientID, tblStateCounties.countyName
            &lt;span class="hljs-keyword"&gt;FROM&lt;/span&gt; tblPatient &lt;span class="hljs-keyword"&gt;INNER&lt;/span&gt;&lt;span class="hljs-keyword"&gt;JOIN&lt;/span&gt;
                 tblPatientVisits
                 &lt;span class="hljs-keyword"&gt;ON&lt;/span&gt; tblPatient.patientID &lt;span class="hljs-operator"&gt;=&lt;/span&gt; tblPatientVisits.patientID &lt;span class="hljs-keyword"&gt;INNER&lt;/span&gt;&lt;span class="hljs-keyword"&gt;JOIN&lt;/span&gt;
                 tblStateCounties
                 &lt;span class="hljs-keyword"&gt;ON&lt;/span&gt; tblPatientVisits.patientState &lt;span class="hljs-operator"&gt;=&lt;/span&gt; tblStateCounties.stateCode &lt;span class="hljs-keyword"&gt;AND&lt;/span&gt;
                    tblPatientVisits.patientCounty &lt;span class="hljs-operator"&gt;=&lt;/span&gt; tblStateCounties.countyCode
            &lt;span class="hljs-keyword"&gt;GROUP&lt;/span&gt;&lt;span class="hljs-keyword"&gt;BY&lt;/span&gt; tblPatient.patientID, tblStateCounties.stateCode, tblStateCounties.countyName
            &lt;span class="hljs-keyword"&gt;HAVING&lt;/span&gt; (&lt;span class="hljs-built_in"&gt;COUNT&lt;/span&gt;(tblPatient.patientID) &lt;span class="hljs-operator"&gt;&amp;gt;&lt;/span&gt;&lt;span class="hljs-number"&gt;1&lt;/span&gt;) &lt;span class="hljs-keyword"&gt;AND&lt;/span&gt; (tblStateCounties.stateCode &lt;span class="hljs-operator"&gt;=&lt;/span&gt;&lt;span class="hljs-string"&gt;'21'&lt;/span&gt;)
           ) t
      &lt;span class="hljs-keyword"&gt;group&lt;/span&gt;&lt;span class="hljs-keyword"&gt;by&lt;/span&gt; countyname
     ) t &lt;span class="hljs-keyword"&gt;inner&lt;/span&gt;&lt;span class="hljs-keyword"&gt;join&lt;/span&gt;
     (&lt;span class="hljs-keyword"&gt;select&lt;/span&gt; tblStateCounties.countyName
      &lt;span class="hljs-keyword"&gt;from&lt;/span&gt; (&lt;span class="hljs-keyword"&gt;SELECT&lt;/span&gt; tblStateCounties.countyName, &lt;span class="hljs-built_in"&gt;COUNT&lt;/span&gt;(tblStateCounties.countyName) &lt;span class="hljs-keyword"&gt;AS&lt;/span&gt; counts
            &lt;span class="hljs-keyword"&gt;FROM&lt;/span&gt; tblPatient &lt;span class="hljs-keyword"&gt;INNER&lt;/span&gt;&lt;span class="hljs-keyword"&gt;JOIN&lt;/span&gt;
                 tblPatientVisits
                 &lt;span class="hljs-keyword"&gt;ON&lt;/span&gt; tblPatient.patientID &lt;span class="hljs-operator"&gt;=&lt;/span&gt; tblPatientVisits.patientID &lt;span class="hljs-keyword"&gt;INNER&lt;/span&gt;&lt;span class="hljs-keyword"&gt;JOIN&lt;/span&gt;
                 tblStateCounties
                 &lt;span class="hljs-keyword"&gt;ON&lt;/span&gt; tblPatientVisits.patientState &lt;span class="hljs-operator"&gt;=&lt;/span&gt; tblStateCounties.stateCode &lt;span class="hljs-keyword"&gt;AND&lt;/span&gt; 
                    tblPatientVisits.patientCounty &lt;span class="hljs-operator"&gt;=&lt;/span&gt; tblStateCounties.countyCode
            &lt;span class="hljs-keyword"&gt;WHERE&lt;/span&gt; (tblStateCounties.stateCode &lt;span class="hljs-operator"&gt;=&lt;/span&gt;&lt;span class="hljs-string"&gt;'21'&lt;/span&gt;)
       &lt;span class="hljs-keyword"&gt;GROUP&lt;/span&gt;&lt;span class="hljs-keyword"&gt;BY&lt;/span&gt; tblStateCounties.countyName
     )
     &lt;span class="hljs-keyword"&gt;on&lt;/span&gt; s.countyName &lt;span class="hljs-operator"&gt;=&lt;/span&gt; t.countyName
&lt;/code&gt;&lt;button class="copy-btn btn btn-secondary btn-sm" onclick="copyCode(this,'code_1')"&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id="solution_3"&gt;Solution 3:

&lt;/h2&gt;&lt;div class="answer-desc"&gt;&lt;p&gt;I think you are overcomplicating your issue, and that this can be done without mashing 2 queries together:&lt;/p&gt;&lt;pre&gt;&lt;code id="code_2" class="hljs language-vbnet"&gt;&lt;span class="hljs-keyword"&gt;SELECT&lt;/span&gt;  t.countyName, 
        SUM(Admissions) &lt;span class="hljs-keyword"&gt;AS&lt;/span&gt; TotalAdmissions,
        COUNT(*) &lt;span class="hljs-keyword"&gt;AS&lt;/span&gt; TotalPatients,
        COUNT(&lt;span class="hljs-keyword"&gt;CASE&lt;/span&gt;&lt;span class="hljs-keyword"&gt;WHEN&lt;/span&gt; Admissions &amp;gt; &lt;span class="hljs-number"&gt;1&lt;/span&gt;&lt;span class="hljs-keyword"&gt;THEN&lt;/span&gt; PatientID &lt;span class="hljs-keyword"&gt;END&lt;/span&gt;) &lt;span class="hljs-keyword"&gt;AS&lt;/span&gt; TotalPatientsReadmitted
        SUM(Admissions - &lt;span class="hljs-number"&gt;1&lt;/span&gt;) &lt;span class="hljs-keyword"&gt;AS&lt;/span&gt; TotalReadmissions
&lt;span class="hljs-keyword"&gt;FROM&lt;/span&gt;    (   &lt;span class="hljs-keyword"&gt;SELECT&lt;/span&gt;  tblPatient.PatientID,
                    tblStateCounties.countyName,
                    COUNT(*) &lt;span class="hljs-keyword"&gt;AS&lt;/span&gt; Admissions
            &lt;span class="hljs-keyword"&gt;FROM&lt;/span&gt;    tblPatient 
                    INNER &lt;span class="hljs-keyword"&gt;JOIN&lt;/span&gt; tblPatientVisits 
                        &lt;span class="hljs-keyword"&gt;ON&lt;/span&gt; tblPatient.patientID = tblPatientVisits.patientID 
                    INNER &lt;span class="hljs-keyword"&gt;JOIN&lt;/span&gt; tblStateCounties 
                        &lt;span class="hljs-keyword"&gt;ON&lt;/span&gt; tblPatientVisits.patientState = tblStateCounties.stateCode 
                        &lt;span class="hljs-built_in"&gt;AND&lt;/span&gt; tblPatientVisits.patientCounty = tblStateCounties.countyCode
            &lt;span class="hljs-keyword"&gt;WHERE&lt;/span&gt;   tblStateCounties.stateCode = &lt;span class="hljs-comment"&gt;'21'&lt;/span&gt;&lt;span class="hljs-keyword"&gt;GROUP&lt;/span&gt;&lt;span class="hljs-keyword"&gt;BY&lt;/span&gt; tblPatient.PatientID, tblStateCounties.countyName
        ) &lt;span class="hljs-keyword"&gt;AS&lt;/span&gt; t
&lt;span class="hljs-keyword"&gt;GROUP&lt;/span&gt;&lt;span class="hljs-keyword"&gt;BY&lt;/span&gt; CountyName
&lt;/code&gt;&lt;button class="copy-btn btn btn-secondary btn-sm" onclick="copyCode(this,'code_2')"&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;I have just taken your &lt;code&gt;FROM&lt;/code&gt; and &lt;code&gt;JOINS&lt;/code&gt; to get what I think is all the data you need (and a little more). I have hopefully given the columns sensible enough names for it to be apparent what each one should be showing.&lt;/p&gt;&lt;/div&gt; 

</description><link>https://rachmatmoradosugiarto.blogspot.com/2024/08/joining-two-subqueries-or-query-with.html</link><author>noreply@blogger.com (Sandy Veum)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8397719027199589165.post-6793930397595287626</guid><pubDate>Wed, 21 Aug 2024 20:15:00 +0000</pubDate><atom:updated>2024-08-21T13:15:43.931-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Performance</category><category domain="http://www.blogger.com/atom/ns#">Sql</category><category domain="http://www.blogger.com/atom/ns#">Sql Server</category><category domain="http://www.blogger.com/atom/ns#">Tsql</category><title>Sql Performance Max()</title><description>&lt;div class="question"&gt;Just got a small question. When trying to get a single max-Value of a table. Which one is better? SELECT MAX(id) FROM myTable WHERE (whatever)  or SELECT TOP 1 id FROM myTable WHER&lt;/div&gt;&lt;h2 id="solution_1"&gt;Solution 1:

&lt;/h2&gt;&lt;div class="answer-desc"&gt;&lt;p&gt;There will be no difference as you can test yourself by inspecting the execution plans. If &lt;code&gt;id&lt;/code&gt; is the clustered index, you should see an ordered clustered index scan; if it is not indexed, you'll still see either a table scan or a clustered index scan, but it won't be ordered in either case.&lt;/p&gt;&lt;p&gt;The &lt;code&gt;TOP 1&lt;/code&gt; approach can be useful if you want to pull along other values from the row, which is easier than pulling the max in a subquery and then joining. If you want other values from the row, you need to dictate how to deal with ties in both cases.&lt;/p&gt;&lt;p&gt;Having said that, there are some scenarios where the plan can be different, so it is important to test depending on whether the column is indexed and whether or not it is monotonically increasing. I created a simple table and inserted 50000 rows:&lt;/p&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;pre&gt;&lt;code id="code_2" class="hljs language-sql"&gt;&lt;span class="hljs-keyword"&gt;CREATE&lt;/span&gt;&lt;span class="hljs-keyword"&gt;TABLE&lt;/span&gt; dbo.x
(
  a &lt;span class="hljs-type"&gt;INT&lt;/span&gt;, b &lt;span class="hljs-type"&gt;INT&lt;/span&gt;, c &lt;span class="hljs-type"&gt;INT&lt;/span&gt;, d &lt;span class="hljs-type"&gt;INT&lt;/span&gt;, 
  e DATETIME, f DATETIME, g DATETIME, h DATETIME
);
&lt;span class="hljs-keyword"&gt;CREATE&lt;/span&gt;&lt;span class="hljs-keyword"&gt;UNIQUE&lt;/span&gt; CLUSTERED INDEX a &lt;span class="hljs-keyword"&gt;ON&lt;/span&gt; dbo.x(a);
&lt;span class="hljs-keyword"&gt;CREATE&lt;/span&gt; INDEX b &lt;span class="hljs-keyword"&gt;ON&lt;/span&gt; dbo.x(b)
&lt;span class="hljs-keyword"&gt;CREATE&lt;/span&gt; INDEX e &lt;span class="hljs-keyword"&gt;ON&lt;/span&gt; dbo.x(e);
&lt;span class="hljs-keyword"&gt;CREATE&lt;/span&gt; INDEX f &lt;span class="hljs-keyword"&gt;ON&lt;/span&gt; dbo.x(f);

&lt;span class="hljs-keyword"&gt;INSERT&lt;/span&gt; dbo.x(a, b, c, d, e, f, g, h)
&lt;span class="hljs-keyword"&gt;SELECT&lt;/span&gt; 
  n.rn, &lt;span class="hljs-comment"&gt;-- ints monotonically increasing&lt;/span&gt;
  n.a,  &lt;span class="hljs-comment"&gt;-- ints in random order&lt;/span&gt;
  n.rn, 
  n.a, 
  DATEADD(&lt;span class="hljs-keyword"&gt;DAY&lt;/span&gt;, n.rn&lt;span class="hljs-operator"&gt;/&lt;/span&gt;&lt;span class="hljs-number"&gt;100&lt;/span&gt;, &lt;span class="hljs-string"&gt;'20100101'&lt;/span&gt;), &lt;span class="hljs-comment"&gt;-- dates monotonically increasing&lt;/span&gt;
  DATEADD(&lt;span class="hljs-keyword"&gt;DAY&lt;/span&gt;, &lt;span class="hljs-operator"&gt;-&lt;/span&gt;n.a &lt;span class="hljs-operator"&gt;%&lt;/span&gt;&lt;span class="hljs-number"&gt;1000&lt;/span&gt;, &lt;span class="hljs-string"&gt;'20120101'&lt;/span&gt;),     &lt;span class="hljs-comment"&gt;-- dates in random order&lt;/span&gt;
  DATEADD(&lt;span class="hljs-keyword"&gt;DAY&lt;/span&gt;, n.rn&lt;span class="hljs-operator"&gt;/&lt;/span&gt;&lt;span class="hljs-number"&gt;100&lt;/span&gt;, &lt;span class="hljs-string"&gt;'20100101'&lt;/span&gt;),
  DATEADD(&lt;span class="hljs-keyword"&gt;DAY&lt;/span&gt;, &lt;span class="hljs-operator"&gt;-&lt;/span&gt;n.a &lt;span class="hljs-operator"&gt;%&lt;/span&gt;&lt;span class="hljs-number"&gt;1000&lt;/span&gt;, &lt;span class="hljs-string"&gt;'20120101'&lt;/span&gt;)
&lt;span class="hljs-keyword"&gt;FROM&lt;/span&gt;
(
  &lt;span class="hljs-keyword"&gt;SELECT&lt;/span&gt; TOP (&lt;span class="hljs-number"&gt;50000&lt;/span&gt;) 
     (&lt;span class="hljs-built_in"&gt;ABS&lt;/span&gt;(s1.[object_id]) &lt;span class="hljs-operator"&gt;%&lt;/span&gt;&lt;span class="hljs-number"&gt;10000&lt;/span&gt;) &lt;span class="hljs-operator"&gt;+&lt;/span&gt;&lt;span class="hljs-number"&gt;1&lt;/span&gt;, 
     rn &lt;span class="hljs-operator"&gt;=&lt;/span&gt;&lt;span class="hljs-built_in"&gt;ROW_NUMBER&lt;/span&gt;() &lt;span class="hljs-keyword"&gt;OVER&lt;/span&gt; (&lt;span class="hljs-keyword"&gt;ORDER&lt;/span&gt;&lt;span class="hljs-keyword"&gt;BY&lt;/span&gt; s2.[object_id])
  &lt;span class="hljs-keyword"&gt;FROM&lt;/span&gt; sys.all_objects &lt;span class="hljs-keyword"&gt;AS&lt;/span&gt; s1 
  &lt;span class="hljs-keyword"&gt;CROSS&lt;/span&gt;&lt;span class="hljs-keyword"&gt;JOIN&lt;/span&gt; sys.all_objects &lt;span class="hljs-keyword"&gt;AS&lt;/span&gt; s2
) &lt;span class="hljs-keyword"&gt;AS&lt;/span&gt; n(a,rn);
GO
&lt;/code&gt;&lt;button class="copy-btn btn btn-secondary btn-sm" onclick="copyCode(this,'code_2')"&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;On my system this created values in a/c from 1 to 50000, b/d between 3 and 9994, e/g from 2010-01-01 through 2011-05-16, and f/h from 2009-04-28 through 2012-01-01.&lt;/p&gt;&lt;p&gt;First, let's compare the indexed monotonically increasing integer columns, a and c. a has a clustered index, c does not:&lt;/p&gt;&lt;pre&gt;&lt;code id="code_3" class="hljs language-sql"&gt;&lt;span class="hljs-keyword"&gt;SELECT&lt;/span&gt;&lt;span class="hljs-built_in"&gt;MAX&lt;/span&gt;(a) &lt;span class="hljs-keyword"&gt;FROM&lt;/span&gt; dbo.x;
&lt;span class="hljs-keyword"&gt;SELECT&lt;/span&gt; TOP (&lt;span class="hljs-number"&gt;1&lt;/span&gt;) a &lt;span class="hljs-keyword"&gt;FROM&lt;/span&gt; dbo.x &lt;span class="hljs-keyword"&gt;ORDER&lt;/span&gt;&lt;span class="hljs-keyword"&gt;BY&lt;/span&gt; a &lt;span class="hljs-keyword"&gt;DESC&lt;/span&gt;;

&lt;span class="hljs-keyword"&gt;SELECT&lt;/span&gt;&lt;span class="hljs-built_in"&gt;MAX&lt;/span&gt;(c) &lt;span class="hljs-keyword"&gt;FROM&lt;/span&gt; dbo.x;
&lt;span class="hljs-keyword"&gt;SELECT&lt;/span&gt; TOP (&lt;span class="hljs-number"&gt;1&lt;/span&gt;) c &lt;span class="hljs-keyword"&gt;FROM&lt;/span&gt; dbo.x &lt;span class="hljs-keyword"&gt;ORDER&lt;/span&gt;&lt;span class="hljs-keyword"&gt;BY&lt;/span&gt; c &lt;span class="hljs-keyword"&gt;DESC&lt;/span&gt;;
&lt;/code&gt;&lt;button class="copy-btn btn btn-secondary btn-sm" onclick="copyCode(this,'code_3')"&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;Results:&lt;/p&gt;&lt;p&gt;&lt;img alt="enter image description here" src="https://i.stack.imgur.com/QsQSD.png"&gt;&lt;/p&gt;&lt;p&gt;The big problem with the 4th query is that, unlike &lt;code&gt;MAX&lt;/code&gt;, it requires a sort. Here is 3 compared to 4:&lt;/p&gt;&lt;p&gt;&lt;img alt="enter image description here" src="https://i.stack.imgur.com/nUL9Y.png"&gt;&lt;/p&gt;&lt;p&gt;&lt;img alt="enter image description here" src="https://i.stack.imgur.com/4tKdF.png"&gt;&lt;/p&gt;&lt;p&gt;This will be a common problem across all of these query variations: a &lt;code&gt;MAX&lt;/code&gt; against an unindexed column will be able to piggy-back on the clustered index scan and perform a stream aggregate, while &lt;code&gt;TOP 1&lt;/code&gt; needs to perform a sort which is going to be more expensive.&lt;/p&gt;&lt;p&gt;I did test and saw the exact same results across testing b+d, e+g, and f+h.&lt;/p&gt;&lt;p&gt;So it seems to me that, in addition to producing more standards-compliance code, there is a potential performance benefit to using &lt;code&gt;MAX&lt;/code&gt; in favor of &lt;code&gt;TOP 1&lt;/code&gt; depending on the underlying table and indexes (which can change after you've put your code in production). So I would say that, without further information, &lt;code&gt;MAX&lt;/code&gt; is preferable.&lt;/p&gt;&lt;p&gt;(And as I said before, &lt;code&gt;TOP 1&lt;/code&gt; might really be the behavior you're after, if you're pulling additional columns. You'll want to test &lt;code&gt;MAX&lt;/code&gt; + &lt;code&gt;JOIN&lt;/code&gt; methods as well if that's what you're after.)&lt;/p&gt;&lt;/div&gt;&lt;h2 id="solution_2"&gt;Solution 2:

&lt;/h2&gt;&lt;div class="answer-desc"&gt;&lt;p&gt;The first one is certainly clearer in the intention.&lt;/p&gt;&lt;p&gt;There shouldn't be a significant performance difference thought for this specific query (they should actually be pretty much identical, even though the result is different if there are no rows in &lt;code&gt;myTable&lt;/code&gt;). Unless you have a valid reason to tune the query (e.g. a proven performance issue), always pick the one which shows the intent of the code.&lt;/p&gt;&lt;/div&gt;&lt;h2 id="solution_3"&gt;Solution 3:

&lt;/h2&gt;&lt;div class="answer-desc"&gt;&lt;p&gt;All query optimizers worth their salt should produce query plans with identical performance for both queries: if there is an index on the column being optimized, both queries should use it; if there is no index, both would produce a full table scan.&lt;/p&gt;&lt;/div&gt;&lt;h2 id="solution_4"&gt;Solution 4:

&lt;/h2&gt;&lt;div class="answer-desc"&gt;&lt;blockquote&gt;&lt;p&gt;Though I suspect the TOP 1 sort operator is over costed in the plan. I tried with TOP 1, TOP 100, &amp;gt;and TOP 101 and all gave me the same estimated subtree cost despite the fact that the last one &amp;gt;would need to sort all the rows. – Martin Smith Jul 2 at 6:53&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;Whether you need 1 row or 100 rows the optimizer has to do same amount of work in this example i.e. read all the rows from the table(clustered index scan).Then sort all those rows(sort opertaion) as there is no index on the column C..Finally just display which one are needed.&lt;/p&gt;&lt;pre&gt;&lt;code id="code_14" class="hljs language-css"&gt;SELECT &lt;span class="hljs-attribute"&gt;TOP&lt;/span&gt; (&lt;span class="hljs-number"&gt;1&lt;/span&gt;) &lt;span class="hljs-selector-tag"&gt;b&lt;/span&gt;&lt;span class="hljs-selector-tag"&gt;FROM&lt;/span&gt; dbo&lt;span class="hljs-selector-class"&gt;.x&lt;/span&gt;&lt;span class="hljs-attribute"&gt;ORDER&lt;/span&gt; BY &lt;span class="hljs-selector-tag"&gt;b&lt;/span&gt; DESC
option(recompile); 
SELECT &lt;span class="hljs-attribute"&gt;TOP&lt;/span&gt; (&lt;span class="hljs-number"&gt;100&lt;/span&gt;) &lt;span class="hljs-selector-tag"&gt;b&lt;/span&gt;&lt;span class="hljs-selector-tag"&gt;FROM&lt;/span&gt; dbo&lt;span class="hljs-selector-class"&gt;.x&lt;/span&gt;&lt;span class="hljs-attribute"&gt;ORDER&lt;/span&gt; BY &lt;span class="hljs-selector-tag"&gt;b&lt;/span&gt; DESC
option(recompile); 
&lt;/code&gt;&lt;button class="copy-btn btn btn-secondary btn-sm" onclick="copyCode(this,'code_14')"&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;Try above code and here top 1 and top 100 shows diff cost because there is an index on column b. Thus in this case you do not need to read all rows and sort them but the work is to go to last page pointer.For one row read the last row on last leaf page of index. TFor 100 row find the last row on last page and then start the backward scan till you get the 100 rows.&lt;/p&gt;&lt;/div&gt; 

</description><link>https://rachmatmoradosugiarto.blogspot.com/2024/08/sql-performance-max.html</link><author>noreply@blogger.com (Sandy Veum)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8397719027199589165.post-1525316589004874785</guid><pubDate>Wed, 21 Aug 2024 19:56:00 +0000</pubDate><atom:updated>2024-08-21T12:56:29.606-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Mysql</category><category domain="http://www.blogger.com/atom/ns#">Sql</category><title>How To Store Multiple Values In Same Sql Column</title><description>&lt;div class="question"&gt;I want to create a Questions and answers application like stack overflow. I do not know how to store multiple answers in MySQL table column. I want to know how to store questions a&lt;/div&gt;&lt;h2 id="solution_1"&gt;Solution 1:

&lt;/h2&gt;&lt;div class="answer-desc"&gt;&lt;p&gt;No...don't do it.
Very bad idea IMHO. It'll be hard to do any searching and stuff.
There's a reason to why we still have R'ship database models.&lt;/p&gt;&lt;p&gt;But if you must go down that path, you can always store it as JSON or XML, and maybe somewhere in future, Oracle will provide functionality to search JSON or XML within columns :)&lt;/p&gt;&lt;/div&gt;&lt;h2 id="solution_2"&gt;Solution 2:

&lt;/h2&gt;&lt;div class="answer-desc"&gt;&lt;p&gt;Creating two tables and connecting them using foreign key relationship is one of the best solutions.
Because keeping questions and answers in a same table may not allow you to have multiple answers for the same questions. If you want a single question to have multiple answers then the redundancy of your DB will surely increase. So, try to use DB-Relationship concepts properly.&lt;/p&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;/div&gt;&lt;h2 id="solution_3"&gt;Solution 3:

&lt;/h2&gt;&lt;div class="answer-desc"&gt;&lt;p&gt;You can create an object (suppose you want to save firstname and lastname in the same column, just for example), something like this (VB example):&lt;/p&gt;&lt;pre&gt;&lt;code id="code_0" class="hljs language-vbnet"&gt;&lt;span class="hljs-keyword"&gt;public&lt;/span&gt;&lt;span class="hljs-keyword"&gt;class&lt;/span&gt;&lt;span class="hljs-keyword"&gt;myClass&lt;/span&gt;&lt;span class="hljs-keyword"&gt;public&lt;/span&gt;&lt;span class="hljs-keyword"&gt;property&lt;/span&gt; firstname
&lt;span class="hljs-keyword"&gt;public&lt;/span&gt;&lt;span class="hljs-keyword"&gt;property&lt;/span&gt; lastname
&lt;span class="hljs-keyword"&gt;end&lt;/span&gt;&lt;span class="hljs-keyword"&gt;class&lt;/span&gt;&lt;/code&gt;&lt;button class="copy-btn btn btn-secondary btn-sm" onclick="copyCode(this,'code_0')"&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;then create an instance and assign properties&lt;/p&gt;&lt;pre&gt;&lt;code id="code_1" class="hljs language-vbnet"&gt;&lt;span class="hljs-keyword"&gt;dim&lt;/span&gt; cls &lt;span class="hljs-keyword"&gt;as&lt;/span&gt;&lt;span class="hljs-built_in"&gt;new&lt;/span&gt;&lt;span class="hljs-keyword"&gt;myClass&lt;/span&gt;
cls.firstname= ...
cls.lastname = ...
&lt;/code&gt;&lt;button class="copy-btn btn btn-secondary btn-sm" onclick="copyCode(this,'code_1')"&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;Then use JavaScriptSerializer to convert this object into json string and store that string in the column. On the way back, desialize the value from the column using JavaScriptSerializer to get back an instance of myClass. Search on this site if you need information about how to use JavaScriptSerializer (it has been answered here several times, hence, not repeating).&lt;/p&gt;&lt;p&gt;If using SQL Server, you may also have a column of XML data type and store XML (instead of json) in the column. You can search in XML in SQL Server. See &lt;a rel="nofollow noreferrer noopener" href="https://stackoverflow.com/questions/10344553/how-can-i-query-a-value-in-sql-server-xml-column"&gt;How can I query a value in SQL Server XML column&lt;/a&gt;. MySQL newer versions also support searching in data stored in json format.&lt;/p&gt;&lt;/div&gt;&lt;h2 id="solution_4"&gt;Solution 4:

&lt;/h2&gt;&lt;div class="answer-desc"&gt;&lt;p&gt;If you need StackOverflow Structure you should to create two tables, &lt;code&gt;Questions&lt;/code&gt; and &lt;code&gt;Answers&lt;/code&gt; and relate them together with a foreign key from &lt;code&gt;Answers' to 'Questions&lt;/code&gt;&lt;/p&gt;&lt;/div&gt;&lt;h2 id="solution_5"&gt;Solution 5:

&lt;/h2&gt;&lt;div class="answer-desc"&gt;&lt;p&gt;By reading your Question, I realized, from my point of view, that your skills may not be good enough to do and to understand what you want to do. &lt;/p&gt;&lt;p&gt;So I´d like to suggest you to watch &lt;a rel="nofollow noreferrer noopener" href="https://www.youtube.com/watch?v=KgiCxe-ZW8o&amp;amp;list=PL32BC9C878BA72085"&gt;this&lt;/a&gt; Tutorial by Bucky Roberts on YouTube
and to read &lt;a rel="nofollow noreferrer noopener" href="https://en.wikipedia.org/wiki/Data_redundancy"&gt;this&lt;/a&gt; Wikipedia Article about Data Redundancy.&lt;/p&gt;&lt;p&gt;Anyways to answer your Question.&lt;/p&gt;&lt;p&gt;I would not store answers and questions all in a single table column.
Think about creating a second table called 'answers', give an ID to the answers and connect them with the table'questions'.&lt;/p&gt;&lt;p&gt;This is called the Foreign Key Concept.
You can read about it &lt;a rel="nofollow noreferrer noopener" href="https://www.w3schools.com/sql/sql_foreignkey.asp"&gt;here&lt;/a&gt;. &lt;/p&gt;&lt;/div&gt; 

</description><link>https://rachmatmoradosugiarto.blogspot.com/2024/08/how-to-store-multiple-values-in-same.html</link><author>noreply@blogger.com (Sandy Veum)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8397719027199589165.post-4043797370276022410</guid><pubDate>Wed, 21 Aug 2024 19:37:00 +0000</pubDate><atom:updated>2024-08-21T12:37:04.250-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Sql</category><category domain="http://www.blogger.com/atom/ns#">Sql Server</category><category domain="http://www.blogger.com/atom/ns#">Stored Procedures</category><title>Sql Case With 2 Conditions</title><description>&lt;div class="question"&gt;I have a stored procedure that will accept 2 different parameters. The first parameter will determine which column I want to sort on, the second parameter will determine whether it&lt;/div&gt;&lt;h2 id="solution_1"&gt;Solution 1:

&lt;/h2&gt;&lt;div class="answer-desc"&gt;&lt;p&gt;The following will work:&lt;/p&gt;&lt;pre&gt;&lt;code id="code_0" class="hljs language-less"&gt;&lt;span class="hljs-selector-tag"&gt;Select&lt;/span&gt; * &lt;span class="hljs-selector-tag"&gt;from&lt;/span&gt;&lt;span class="hljs-selector-tag"&gt;empTable&lt;/span&gt;&lt;span class="hljs-selector-tag"&gt;Order&lt;/span&gt;&lt;span class="hljs-selector-tag"&gt;by&lt;/span&gt;&lt;span class="hljs-selector-tag"&gt;CASE&lt;/span&gt;&lt;span class="hljs-keyword"&gt;WHEN&lt;/span&gt;&lt;span class="hljs-variable"&gt;@sortcolumn&lt;/span&gt; = &lt;span class="hljs-string"&gt;'First_Name'&lt;/span&gt;&lt;span class="hljs-keyword"&gt;AND&lt;/span&gt;&lt;span class="hljs-variable"&gt;@SortOrder&lt;/span&gt; = &lt;span class="hljs-string"&gt;'ascending'&lt;/span&gt; THEN fname END ASC,
CASE &lt;span class="hljs-keyword"&gt;WHEN&lt;/span&gt;&lt;span class="hljs-variable"&gt;@sortcolumn&lt;/span&gt; = &lt;span class="hljs-string"&gt;'First_Name'&lt;/span&gt;&lt;span class="hljs-keyword"&gt;AND&lt;/span&gt;&lt;span class="hljs-variable"&gt;@SortOrder&lt;/span&gt; = &lt;span class="hljs-string"&gt;'descending'&lt;/span&gt; THEN fname END DESC
&lt;/code&gt;&lt;button class="copy-btn btn btn-secondary btn-sm" onclick="copyCode(this,'code_0')"&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;etc...&lt;/p&gt;&lt;p&gt;In order to avoid typing each of these case statements by hand, you could write a "generator" script that you use to create this (especially good if the table definition would change):&lt;/p&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;pre&gt;&lt;code id="code_1" class="hljs language-sql"&gt;&lt;span class="hljs-keyword"&gt;SELECT&lt;/span&gt;&lt;span class="hljs-string"&gt;'CASE WHEN @SortColumn = '''&lt;/span&gt;&lt;span class="hljs-operator"&gt;+&lt;/span&gt; C.name &lt;span class="hljs-operator"&gt;+&lt;/span&gt;&lt;span class="hljs-string"&gt;''' AND @SortOrder = ''ascending'' THEN '&lt;/span&gt;&lt;span class="hljs-operator"&gt;+&lt;/span&gt; C.name &lt;span class="hljs-operator"&gt;+&lt;/span&gt;&lt;span class="hljs-string"&gt;' END ASC,'&lt;/span&gt;&lt;span class="hljs-operator"&gt;+&lt;/span&gt;&lt;span class="hljs-type"&gt;CHAR&lt;/span&gt;(&lt;span class="hljs-number"&gt;13&lt;/span&gt;) &lt;span class="hljs-operator"&gt;+&lt;/span&gt;&lt;span class="hljs-type"&gt;CHAR&lt;/span&gt;(&lt;span class="hljs-number"&gt;10&lt;/span&gt;) &lt;span class="hljs-operator"&gt;+&lt;/span&gt;&lt;span class="hljs-string"&gt;'CASE WHEN @SortColumn = '''&lt;/span&gt;&lt;span class="hljs-operator"&gt;+&lt;/span&gt; C.name &lt;span class="hljs-operator"&gt;+&lt;/span&gt;&lt;span class="hljs-string"&gt;''' AND @SortOrder = ''descending'' THEN '&lt;/span&gt;&lt;span class="hljs-operator"&gt;+&lt;/span&gt; C.name &lt;span class="hljs-operator"&gt;+&lt;/span&gt;&lt;span class="hljs-string"&gt;' END DESC,'&lt;/span&gt;&lt;span class="hljs-keyword"&gt;FROM&lt;/span&gt; sys.columns C 
&lt;span class="hljs-keyword"&gt;WHERE&lt;/span&gt; C.object_id &lt;span class="hljs-operator"&gt;=&lt;/span&gt; object_id(&lt;span class="hljs-string"&gt;'[Schema].[Table]'&lt;/span&gt;)
&lt;/code&gt;&lt;button class="copy-btn btn btn-secondary btn-sm" onclick="copyCode(this,'code_1')"&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id="solution_2"&gt;Solution 2:

&lt;/h2&gt;&lt;div class="answer-desc"&gt;&lt;p&gt;If you want to avoid dynamic SQL and using 2x your conditions, you can use &lt;code&gt;row_number&lt;/code&gt;&lt;/p&gt;&lt;p&gt;eg:&lt;/p&gt;&lt;pre&gt;&lt;code id="code_3" class="hljs language-sql"&gt;&lt;span class="hljs-keyword"&gt;declare&lt;/span&gt;&lt;span class="hljs-variable"&gt;@t&lt;/span&gt;&lt;span class="hljs-keyword"&gt;table&lt;/span&gt; (string &lt;span class="hljs-type"&gt;varchar&lt;/span&gt;(&lt;span class="hljs-number"&gt;50&lt;/span&gt;), number &lt;span class="hljs-type"&gt;int&lt;/span&gt;)

&lt;span class="hljs-keyword"&gt;insert&lt;/span&gt;&lt;span class="hljs-variable"&gt;@t&lt;/span&gt;&lt;span class="hljs-keyword"&gt;values&lt;/span&gt; (&lt;span class="hljs-string"&gt;'a'&lt;/span&gt;,&lt;span class="hljs-number"&gt;9&lt;/span&gt;),(&lt;span class="hljs-string"&gt;'f'&lt;/span&gt;,&lt;span class="hljs-number"&gt;2&lt;/span&gt;),(&lt;span class="hljs-string"&gt;'c'&lt;/span&gt;,&lt;span class="hljs-number"&gt;1&lt;/span&gt;)

&lt;span class="hljs-keyword"&gt;declare&lt;/span&gt;&lt;span class="hljs-variable"&gt;@sc&lt;/span&gt;&lt;span class="hljs-type"&gt;varchar&lt;/span&gt;(&lt;span class="hljs-number"&gt;10&lt;/span&gt;) &lt;span class="hljs-operator"&gt;=&lt;/span&gt;&lt;span class="hljs-string"&gt;'number'&lt;/span&gt;, &lt;span class="hljs-comment"&gt;-- or 'string', etc&lt;/span&gt;&lt;span class="hljs-variable"&gt;@so&lt;/span&gt;&lt;span class="hljs-type"&gt;varchar&lt;/span&gt;(&lt;span class="hljs-number"&gt;10&lt;/span&gt;) &lt;span class="hljs-operator"&gt;=&lt;/span&gt;&lt;span class="hljs-string"&gt;'desc'&lt;/span&gt;&lt;span class="hljs-comment"&gt;-- or 'asc'&lt;/span&gt;&lt;span class="hljs-keyword"&gt;select&lt;/span&gt;&lt;span class="hljs-operator"&gt;*&lt;/span&gt;&lt;span class="hljs-keyword"&gt;from&lt;/span&gt; 
(
    &lt;span class="hljs-keyword"&gt;select&lt;/span&gt;&lt;span class="hljs-operator"&gt;*&lt;/span&gt;,
        &lt;span class="hljs-keyword"&gt;case&lt;/span&gt;&lt;span class="hljs-variable"&gt;@sc&lt;/span&gt;&lt;span class="hljs-keyword"&gt;when&lt;/span&gt;&lt;span class="hljs-string"&gt;'string'&lt;/span&gt;&lt;span class="hljs-keyword"&gt;then&lt;/span&gt;&lt;span class="hljs-built_in"&gt;ROW_NUMBER&lt;/span&gt;() &lt;span class="hljs-keyword"&gt;over&lt;/span&gt; (&lt;span class="hljs-keyword"&gt;order&lt;/span&gt;&lt;span class="hljs-keyword"&gt;by&lt;/span&gt; string)
                 &lt;span class="hljs-keyword"&gt;when&lt;/span&gt;&lt;span class="hljs-string"&gt;'number'&lt;/span&gt;&lt;span class="hljs-keyword"&gt;then&lt;/span&gt;&lt;span class="hljs-built_in"&gt;ROW_NUMBER&lt;/span&gt;() &lt;span class="hljs-keyword"&gt;over&lt;/span&gt; (&lt;span class="hljs-keyword"&gt;order&lt;/span&gt;&lt;span class="hljs-keyword"&gt;by&lt;/span&gt; number)
        &lt;span class="hljs-keyword"&gt;end&lt;/span&gt; rn
    &lt;span class="hljs-keyword"&gt;from&lt;/span&gt;&lt;span class="hljs-variable"&gt;@t&lt;/span&gt;
) v
&lt;span class="hljs-keyword"&gt;order&lt;/span&gt;&lt;span class="hljs-keyword"&gt;by&lt;/span&gt;&lt;span class="hljs-keyword"&gt;case&lt;/span&gt;&lt;span class="hljs-variable"&gt;@so&lt;/span&gt;&lt;span class="hljs-keyword"&gt;when&lt;/span&gt;&lt;span class="hljs-string"&gt;'desc'&lt;/span&gt;&lt;span class="hljs-keyword"&gt;then&lt;/span&gt;&lt;span class="hljs-operator"&gt;-&lt;/span&gt;rn &lt;span class="hljs-keyword"&gt;else&lt;/span&gt; rn &lt;span class="hljs-keyword"&gt;end&lt;/span&gt;&lt;/code&gt;&lt;button class="copy-btn btn btn-secondary btn-sm" onclick="copyCode(this,'code_3')"&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id="solution_3"&gt;Solution 3:

&lt;/h2&gt;&lt;div class="answer-desc"&gt;&lt;p&gt;You can just copy and paste and run this. I hate dynamic SQL, don't do it. &lt;/p&gt;&lt;p&gt;Unfortunately you'll have to duplicate the query....but it solves your specific problem.&lt;/p&gt;&lt;pre&gt;&lt;code id="code_4" class="hljs language-sql"&gt;&lt;span class="hljs-keyword"&gt;DECLARE&lt;/span&gt;&lt;span class="hljs-variable"&gt;@sortcolumn&lt;/span&gt;&lt;span class="hljs-type"&gt;varchar&lt;/span&gt;(&lt;span class="hljs-number"&gt;10&lt;/span&gt;),
    &lt;span class="hljs-variable"&gt;@sortorder&lt;/span&gt;&lt;span class="hljs-type"&gt;varchar&lt;/span&gt;(&lt;span class="hljs-number"&gt;10&lt;/span&gt;)

&lt;span class="hljs-keyword"&gt;SET&lt;/span&gt;&lt;span class="hljs-variable"&gt;@sortcolumn&lt;/span&gt;&lt;span class="hljs-operator"&gt;=&lt;/span&gt;&lt;span class="hljs-string"&gt;'fname'&lt;/span&gt;&lt;span class="hljs-keyword"&gt;SET&lt;/span&gt;&lt;span class="hljs-variable"&gt;@sortorder&lt;/span&gt;&lt;span class="hljs-operator"&gt;=&lt;/span&gt;&lt;span class="hljs-string"&gt;'DESC'&lt;/span&gt;&lt;span class="hljs-keyword"&gt;DECLARE&lt;/span&gt;&lt;span class="hljs-variable"&gt;@Data&lt;/span&gt;&lt;span class="hljs-keyword"&gt;TABLE&lt;/span&gt;
        (
        fname nvarchar(&lt;span class="hljs-number"&gt;10&lt;/span&gt;),
        lname nvarchar(&lt;span class="hljs-number"&gt;10&lt;/span&gt;),
        empID &lt;span class="hljs-type"&gt;int&lt;/span&gt;
        )
&lt;span class="hljs-keyword"&gt;INSERT&lt;/span&gt;&lt;span class="hljs-keyword"&gt;INTO&lt;/span&gt;&lt;span class="hljs-variable"&gt;@Data&lt;/span&gt;&lt;span class="hljs-keyword"&gt;VALUES&lt;/span&gt; (&lt;span class="hljs-string"&gt;'BBB'&lt;/span&gt;, &lt;span class="hljs-string"&gt;'BBB'&lt;/span&gt;, &lt;span class="hljs-number"&gt;2&lt;/span&gt;)
&lt;span class="hljs-keyword"&gt;INSERT&lt;/span&gt;&lt;span class="hljs-keyword"&gt;INTO&lt;/span&gt;&lt;span class="hljs-variable"&gt;@Data&lt;/span&gt;&lt;span class="hljs-keyword"&gt;VALUES&lt;/span&gt; (&lt;span class="hljs-string"&gt;'AAA'&lt;/span&gt;, &lt;span class="hljs-string"&gt;'AAA'&lt;/span&gt;, &lt;span class="hljs-number"&gt;1&lt;/span&gt;)


IF &lt;span class="hljs-variable"&gt;@sortorder&lt;/span&gt;&lt;span class="hljs-operator"&gt;=&lt;/span&gt;&lt;span class="hljs-string"&gt;'DESC'&lt;/span&gt;&lt;span class="hljs-keyword"&gt;BEGIN&lt;/span&gt;&lt;span class="hljs-keyword"&gt;SELECT&lt;/span&gt;&lt;span class="hljs-operator"&gt;*&lt;/span&gt;&lt;span class="hljs-keyword"&gt;FROM&lt;/span&gt;&lt;span class="hljs-variable"&gt;@Data&lt;/span&gt;&lt;span class="hljs-keyword"&gt;ORDER&lt;/span&gt;&lt;span class="hljs-keyword"&gt;BY&lt;/span&gt;&lt;span class="hljs-keyword"&gt;CASE&lt;/span&gt;&lt;span class="hljs-keyword"&gt;WHEN&lt;/span&gt;&lt;span class="hljs-variable"&gt;@sortcolumn&lt;/span&gt;&lt;span class="hljs-operator"&gt;=&lt;/span&gt;&lt;span class="hljs-string"&gt;'fname'&lt;/span&gt;&lt;span class="hljs-keyword"&gt;THEN&lt;/span&gt; fname
            &lt;span class="hljs-keyword"&gt;WHEN&lt;/span&gt;&lt;span class="hljs-variable"&gt;@sortcolumn&lt;/span&gt;&lt;span class="hljs-operator"&gt;=&lt;/span&gt;&lt;span class="hljs-string"&gt;'lname'&lt;/span&gt;&lt;span class="hljs-keyword"&gt;THEN&lt;/span&gt; lname
        &lt;span class="hljs-keyword"&gt;END&lt;/span&gt;&lt;span class="hljs-keyword"&gt;DESC&lt;/span&gt;&lt;span class="hljs-keyword"&gt;END&lt;/span&gt;&lt;span class="hljs-keyword"&gt;ELSE&lt;/span&gt;&lt;span class="hljs-keyword"&gt;BEGIN&lt;/span&gt;&lt;span class="hljs-keyword"&gt;SELECT&lt;/span&gt;&lt;span class="hljs-operator"&gt;*&lt;/span&gt;&lt;span class="hljs-keyword"&gt;FROM&lt;/span&gt;&lt;span class="hljs-variable"&gt;@Data&lt;/span&gt;&lt;span class="hljs-keyword"&gt;ORDER&lt;/span&gt;&lt;span class="hljs-keyword"&gt;BY&lt;/span&gt;&lt;span class="hljs-keyword"&gt;CASE&lt;/span&gt;&lt;span class="hljs-keyword"&gt;WHEN&lt;/span&gt;&lt;span class="hljs-variable"&gt;@sortcolumn&lt;/span&gt;&lt;span class="hljs-operator"&gt;=&lt;/span&gt;&lt;span class="hljs-string"&gt;'fname'&lt;/span&gt;&lt;span class="hljs-keyword"&gt;THEN&lt;/span&gt; fname
            &lt;span class="hljs-keyword"&gt;WHEN&lt;/span&gt;&lt;span class="hljs-variable"&gt;@sortcolumn&lt;/span&gt;&lt;span class="hljs-operator"&gt;=&lt;/span&gt;&lt;span class="hljs-string"&gt;'lname'&lt;/span&gt;&lt;span class="hljs-keyword"&gt;THEN&lt;/span&gt; lname
        &lt;span class="hljs-keyword"&gt;END&lt;/span&gt;&lt;span class="hljs-keyword"&gt;END&lt;/span&gt;&lt;/code&gt;&lt;button class="copy-btn btn btn-secondary btn-sm" onclick="copyCode(this,'code_4')"&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id="solution_4"&gt;Solution 4:

&lt;/h2&gt;&lt;div class="answer-desc"&gt;&lt;p&gt;Modifying Jon's answer to cap the ORDER BY list at just 2 instead of 2 * #columns&lt;/p&gt;&lt;pre&gt;&lt;code id="code_5" class="hljs language-sql"&gt;&lt;span class="hljs-keyword"&gt;SELECT&lt;/span&gt;&lt;span class="hljs-operator"&gt;*&lt;/span&gt;&lt;span class="hljs-keyword"&gt;FROM&lt;/span&gt; MyTable
&lt;span class="hljs-keyword"&gt;CROSS&lt;/span&gt; APPLY (&lt;span class="hljs-keyword"&gt;VALUES&lt;/span&gt;
  (&lt;span class="hljs-string"&gt;'First_Name'&lt;/span&gt;,fname),
  (&lt;span class="hljs-string"&gt;'Last_Name'&lt;/span&gt; ,lname),
  (&lt;span class="hljs-string"&gt;'Id'&lt;/span&gt;        ,ID   )
) sort(SortColumn, SortValue)
&lt;span class="hljs-keyword"&gt;WHERE&lt;/span&gt; SortColumn &lt;span class="hljs-operator"&gt;=&lt;/span&gt;&lt;span class="hljs-variable"&gt;@SortColumn&lt;/span&gt;&lt;span class="hljs-keyword"&gt;ORDER&lt;/span&gt;&lt;span class="hljs-keyword"&gt;BY&lt;/span&gt;&lt;span class="hljs-keyword"&gt;CASE&lt;/span&gt;&lt;span class="hljs-variable"&gt;@SortOrder&lt;/span&gt;&lt;span class="hljs-keyword"&gt;WHEN&lt;/span&gt;&lt;span class="hljs-string"&gt;'ascending'&lt;/span&gt;&lt;span class="hljs-keyword"&gt;THEN&lt;/span&gt; SortValue &lt;span class="hljs-keyword"&gt;END&lt;/span&gt;&lt;span class="hljs-keyword"&gt;ASC&lt;/span&gt;,
  &lt;span class="hljs-keyword"&gt;CASE&lt;/span&gt;&lt;span class="hljs-variable"&gt;@SortOrder&lt;/span&gt;&lt;span class="hljs-keyword"&gt;WHEN&lt;/span&gt;&lt;span class="hljs-string"&gt;'descending'&lt;/span&gt;&lt;span class="hljs-keyword"&gt;THEN&lt;/span&gt; SortValue &lt;span class="hljs-keyword"&gt;END&lt;/span&gt;&lt;span class="hljs-keyword"&gt;DESC&lt;/span&gt;&lt;/code&gt;&lt;button class="copy-btn btn btn-secondary btn-sm" onclick="copyCode(this,'code_5')"&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;/div&gt; 

</description><link>https://rachmatmoradosugiarto.blogspot.com/2024/08/sql-case-with-2-conditions.html</link><author>noreply@blogger.com (Sandy Veum)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8397719027199589165.post-1125257901302495641</guid><pubDate>Wed, 21 Aug 2024 19:17:00 +0000</pubDate><atom:updated>2024-08-21T12:17:19.522-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Azure Blob Storage</category><category domain="http://www.blogger.com/atom/ns#">Sql</category><category domain="http://www.blogger.com/atom/ns#">Sql Server 2016</category><category domain="http://www.blogger.com/atom/ns#">Ssis</category><title>Cannot Create A Task From Xml For Task Azure Blob Download Task. Error 0x80070057 The Parameter Is In Correct</title><description>&lt;div class="question"&gt;We have implemented an SSIS package which downloads the file from Azure blob storage. The package works when we execute from local development box. But it's failing on TEST environ&lt;/div&gt;&lt;h2 id="solution_1"&gt;Solution 1:

&lt;/h2&gt;&lt;div class="answer-desc"&gt;&lt;p&gt;For the first error, please refer to Maintenance Plan Error
&lt;a rel="nofollow noreferrer noopener" href="https://blog.sqlauthority.com/2018/02/16/sql-server-maintenance-plan-error-cannot-create-task-xml-task-eue-error-0x80070057-parameter-incorrect-onpreexecute/"&gt;https://blog.sqlauthority.com/2018/02/16/sql-server-maintenance-plan-error-cannot-create-task-xml-task-eue-error-0x80070057-parameter-incorrect-onpreexecute/&lt;/a&gt;
Error: The task with the name "Data Flow Task"
&lt;a rel="nofollow noreferrer noopener" href="https://social.msdn.microsoft.com/Forums/ie/en-US/0b57adfa-0872-4cd4-85d8-40bfaa520285/error-when-executing-ssis-package?forum=sqlintegrationservices"&gt;https://social.msdn.microsoft.com/Forums/ie/en-US/0b57adfa-0872-4cd4-85d8-40bfaa520285/error-when-executing-ssis-package?forum=sqlintegrationservices&lt;/a&gt;&lt;/p&gt;&lt;p&gt;Just for clarification : Have you could try following options:
1. Try using DTexce tool to execute the package to check if this issue persist.
2. Register the Azure Blob Task into GAC manually.
3. Check if the account has permission to upload file to Azure.&lt;/p&gt;&lt;p&gt;Azure Blob Download Task
&lt;a rel="nofollow noreferrer noopener" href="https://docs.microsoft.com/en-us/sql/integration-services/control-flow/azure-blob-download-task?view=sql-server-2017"&gt;https://docs.microsoft.com/en-us/sql/integration-services/control-flow/azure-blob-download-task?view=sql-server-2017&lt;/a&gt;&lt;/p&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;p&gt;Kindly let us know if the above helps or you need further assistance on this issue.&lt;/p&gt;&lt;/div&gt; 

</description><link>https://rachmatmoradosugiarto.blogspot.com/2024/08/cannot-create-task-from-xml-for-task.html</link><author>noreply@blogger.com (Sandy Veum)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8397719027199589165.post-2952226516193219654</guid><pubDate>Wed, 21 Aug 2024 18:57:00 +0000</pubDate><atom:updated>2024-08-21T11:57:58.931-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Sql</category><title>Sql Select Rows With Unique Values</title><description>&lt;div class="question"&gt;I'm having a problem getting the rows I need from my database. The question is simple: I have a table with customer data in it. Now I want to do a mailing (via post) to all my cust&lt;/div&gt;&lt;h2 id="solution_1"&gt;Solution 1:

&lt;/h2&gt;&lt;div class="answer-desc"&gt;&lt;pre&gt;&lt;code id="code_0" class="hljs language-sql"&gt;&lt;span class="hljs-keyword"&gt;SELECT&lt;/span&gt; title
     , last_name
     , first_name
     , address_line_1
     , address_line_2
     , etc
&lt;span class="hljs-keyword"&gt;FROM&lt;/span&gt;   (
        &lt;span class="hljs-keyword"&gt;SELECT&lt;/span&gt; title
             , last_name
             , first_name
             , address_line_1
             , address_line_2
             , etc
             , &lt;span class="hljs-built_in"&gt;Row_Number&lt;/span&gt;() &lt;span class="hljs-keyword"&gt;OVER&lt;/span&gt; (&lt;span class="hljs-keyword"&gt;PARTITION&lt;/span&gt;&lt;span class="hljs-keyword"&gt;BY&lt;/span&gt; address_line_1, address_line_2, etc &lt;span class="hljs-keyword"&gt;ORDER&lt;/span&gt;&lt;span class="hljs-keyword"&gt;BY&lt;/span&gt; date_of_birth &lt;span class="hljs-keyword"&gt;ASC&lt;/span&gt;) &lt;span class="hljs-keyword"&gt;As&lt;/span&gt; row_number
        &lt;span class="hljs-keyword"&gt;FROM&lt;/span&gt;   your_table
       ) &lt;span class="hljs-keyword"&gt;As&lt;/span&gt; all_duplicatified
&lt;span class="hljs-keyword"&gt;WHERE&lt;/span&gt;  row_num &lt;span class="hljs-operator"&gt;=&lt;/span&gt;&lt;span class="hljs-number"&gt;1&lt;/span&gt;&lt;/code&gt;&lt;button class="copy-btn btn btn-secondary btn-sm" onclick="copyCode(this,'code_0')"&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;This gives every row a row number. The row number is "reset" on every partition (which in this case is our address fields) and the ordering of the numbers is determined by age (dob).&lt;/p&gt;&lt;p&gt;Therefore if we only show the ones where &lt;code&gt;row_num = 1&lt;/code&gt; we get just that eldest persons entry first.&lt;/p&gt;&lt;/div&gt;&lt;h2 id="solution_2"&gt;Solution 2:

&lt;/h2&gt;&lt;div class="answer-desc"&gt;&lt;p&gt;You can you the following SQL construct:&lt;/p&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;pre&gt;&lt;code id="code_2" class="hljs language-sql"&gt;&lt;span class="hljs-keyword"&gt;select&lt;/span&gt;&lt;span class="hljs-keyword"&gt;distinct&lt;/span&gt; ...
&lt;/code&gt;&lt;button class="copy-btn btn btn-secondary btn-sm" onclick="copyCode(this,'code_2')"&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;Select distinct will avoid duplicates in your result set. You can use select distinct to gather all unique addresses and then cross reference with the email address&lt;/p&gt;&lt;/div&gt;&lt;h2 id="solution_3"&gt;Solution 3:

&lt;/h2&gt;&lt;div class="answer-desc"&gt;&lt;p&gt;Use &lt;a rel="nofollow noreferrer noopener" href="https://technet.microsoft.com/en-us/library/ms187831%28v=sql.105%29.aspx"&gt;SELECT DISTINCT&lt;/a&gt; to only select unique adresses&lt;/p&gt;&lt;/div&gt;&lt;h2 id="solution_4"&gt;Solution 4:

&lt;/h2&gt;&lt;div class="answer-desc"&gt;&lt;p&gt;Here's how I would do something like this in Oracle:&lt;/p&gt;&lt;pre&gt;&lt;code id="code_3" class="hljs language-sql"&gt;&lt;span class="hljs-comment"&gt;--Create testing table&lt;/span&gt;&lt;span class="hljs-keyword"&gt;CREATE&lt;/span&gt;&lt;span class="hljs-keyword"&gt;TABLE&lt;/span&gt; UniqueValTest (
  fname NVARCHAR2(&lt;span class="hljs-number"&gt;100&lt;/span&gt;),
  lname NVARCHAR2(&lt;span class="hljs-number"&gt;100&lt;/span&gt;),
  address NVARCHAR2(&lt;span class="hljs-number"&gt;100&lt;/span&gt;),
  city NVARCHAR2(&lt;span class="hljs-number"&gt;50&lt;/span&gt;),
  state NVARCHAR2(&lt;span class="hljs-number"&gt;2&lt;/span&gt;),
  zip NVARCHAR2(&lt;span class="hljs-number"&gt;5&lt;/span&gt;),
  age NUMBER,
  recid NUMBER
  );

&lt;span class="hljs-comment"&gt;--Create sample data&lt;/span&gt;&lt;span class="hljs-keyword"&gt;INSERT&lt;/span&gt;&lt;span class="hljs-keyword"&gt;INTO&lt;/span&gt; UniqueValTest (fname, lname, address, city, state, zip, age, recid)
&lt;span class="hljs-keyword"&gt;VALUES&lt;/span&gt; (&lt;span class="hljs-string"&gt;'JOHN'&lt;/span&gt;, &lt;span class="hljs-string"&gt;'SMITH'&lt;/span&gt;, &lt;span class="hljs-string"&gt;'123 MAIN ST'&lt;/span&gt;, &lt;span class="hljs-string"&gt;'JAMESTOWN'&lt;/span&gt;, &lt;span class="hljs-string"&gt;'LA'&lt;/span&gt;, &lt;span class="hljs-string"&gt;'12345'&lt;/span&gt;, &lt;span class="hljs-number"&gt;28&lt;/span&gt;, &lt;span class="hljs-number"&gt;1&lt;/span&gt;);
&lt;span class="hljs-keyword"&gt;INSERT&lt;/span&gt;&lt;span class="hljs-keyword"&gt;INTO&lt;/span&gt; UniqueValTest (fname, lname, address, city, state, zip, age, recid)
&lt;span class="hljs-keyword"&gt;VALUES&lt;/span&gt; (&lt;span class="hljs-string"&gt;'JENNIFER'&lt;/span&gt;, &lt;span class="hljs-string"&gt;'SMITH'&lt;/span&gt;, &lt;span class="hljs-string"&gt;'123 MAIN ST'&lt;/span&gt;, &lt;span class="hljs-string"&gt;'JAMESTOWN'&lt;/span&gt;, &lt;span class="hljs-string"&gt;'LA'&lt;/span&gt;, &lt;span class="hljs-string"&gt;'12345'&lt;/span&gt;, &lt;span class="hljs-number"&gt;30&lt;/span&gt;, &lt;span class="hljs-number"&gt;2&lt;/span&gt;);
&lt;span class="hljs-keyword"&gt;INSERT&lt;/span&gt;&lt;span class="hljs-keyword"&gt;INTO&lt;/span&gt; UniqueValTest (fname, lname, address, city, state, zip, age, recid)
&lt;span class="hljs-keyword"&gt;VALUES&lt;/span&gt; (&lt;span class="hljs-string"&gt;'RACHEL'&lt;/span&gt;, &lt;span class="hljs-string"&gt;'ALLEN'&lt;/span&gt;, &lt;span class="hljs-string"&gt;'225 MAIN ST'&lt;/span&gt;, &lt;span class="hljs-string"&gt;'JAMESTOWN'&lt;/span&gt;, &lt;span class="hljs-string"&gt;'LA'&lt;/span&gt;, &lt;span class="hljs-string"&gt;'12345'&lt;/span&gt;, &lt;span class="hljs-number"&gt;25&lt;/span&gt;, &lt;span class="hljs-number"&gt;3&lt;/span&gt;);
&lt;span class="hljs-keyword"&gt;INSERT&lt;/span&gt;&lt;span class="hljs-keyword"&gt;INTO&lt;/span&gt; UniqueValTest (fname, lname, address, city, state, zip, age, recid)
&lt;span class="hljs-keyword"&gt;VALUES&lt;/span&gt; (&lt;span class="hljs-string"&gt;'JOSEPH'&lt;/span&gt;, &lt;span class="hljs-string"&gt;'ALLEN'&lt;/span&gt;, &lt;span class="hljs-string"&gt;'225 MAIN ST'&lt;/span&gt;, &lt;span class="hljs-string"&gt;'JAMESTOWN'&lt;/span&gt;, &lt;span class="hljs-string"&gt;'LA'&lt;/span&gt;, &lt;span class="hljs-string"&gt;'12345'&lt;/span&gt;, &lt;span class="hljs-number"&gt;25&lt;/span&gt;, &lt;span class="hljs-number"&gt;4&lt;/span&gt;);
&lt;span class="hljs-keyword"&gt;INSERT&lt;/span&gt;&lt;span class="hljs-keyword"&gt;INTO&lt;/span&gt; UniqueValTest (fname, lname, address, city, state, zip, age, recid)
&lt;span class="hljs-keyword"&gt;VALUES&lt;/span&gt; (&lt;span class="hljs-string"&gt;'MARK'&lt;/span&gt;, &lt;span class="hljs-string"&gt;'MCBRIDE'&lt;/span&gt;, &lt;span class="hljs-string"&gt;'228 MAIN ST'&lt;/span&gt;, &lt;span class="hljs-string"&gt;'JAMESTOWN'&lt;/span&gt;, &lt;span class="hljs-string"&gt;'LA'&lt;/span&gt;, &lt;span class="hljs-string"&gt;'12345'&lt;/span&gt;, &lt;span class="hljs-number"&gt;55&lt;/span&gt;, &lt;span class="hljs-number"&gt;5&lt;/span&gt;);

&lt;span class="hljs-comment"&gt;--Here's the real part, pulling the data with the dedupe and priority&lt;/span&gt;&lt;span class="hljs-keyword"&gt;CREATE&lt;/span&gt;&lt;span class="hljs-keyword"&gt;TABLE&lt;/span&gt; TestDataPull &lt;span class="hljs-keyword"&gt;AS&lt;/span&gt;&lt;span class="hljs-keyword"&gt;SELECT&lt;/span&gt; T.&lt;span class="hljs-operator"&gt;*&lt;/span&gt;, &lt;span class="hljs-built_in"&gt;ROW_NUMBER&lt;/span&gt;() &lt;span class="hljs-keyword"&gt;OVER&lt;/span&gt; (&lt;span class="hljs-keyword"&gt;PARTITION&lt;/span&gt;&lt;span class="hljs-keyword"&gt;BY&lt;/span&gt; lname, address, zip &lt;span class="hljs-keyword"&gt;ORDER&lt;/span&gt;&lt;span class="hljs-keyword"&gt;BY&lt;/span&gt; lname, address, zip, age &lt;span class="hljs-keyword"&gt;DESC&lt;/span&gt; NULLS &lt;span class="hljs-keyword"&gt;LAST&lt;/span&gt;) &lt;span class="hljs-keyword"&gt;AS&lt;/span&gt; dupeid
  &lt;span class="hljs-keyword"&gt;FROM&lt;/span&gt; UNIQUEVALTEST T;

&lt;span class="hljs-comment"&gt;--Now you can easily select your data&lt;/span&gt;&lt;span class="hljs-keyword"&gt;SELECT&lt;/span&gt; fname, lname, address, city, state, zip &lt;span class="hljs-keyword"&gt;FROM&lt;/span&gt; TestDataPull &lt;span class="hljs-keyword"&gt;WHERE&lt;/span&gt; dupeid &lt;span class="hljs-operator"&gt;=&lt;/span&gt;&lt;span class="hljs-number"&gt;1&lt;/span&gt;;
&lt;/code&gt;&lt;button class="copy-btn btn btn-secondary btn-sm" onclick="copyCode(this,'code_3')"&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;/div&gt; 

</description><link>https://rachmatmoradosugiarto.blogspot.com/2024/08/sql-select-rows-with-unique-values.html</link><author>noreply@blogger.com (Sandy Veum)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8397719027199589165.post-9163668703841501353</guid><pubDate>Wed, 21 Aug 2024 18:37:00 +0000</pubDate><atom:updated>2024-08-21T11:37:59.111-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Database</category><category domain="http://www.blogger.com/atom/ns#">Oracle</category><category domain="http://www.blogger.com/atom/ns#">Oracle10g</category><category domain="http://www.blogger.com/atom/ns#">Sql</category><title>Comparing List Of Values Against Table</title><description>&lt;div class="question"&gt;I tried to find solution for this problem for some time but without success so any help would be much appreciated. List of IDs needs to be compared against a table and find out whi&lt;/div&gt;&lt;h2 id="solution_1"&gt;Solution 1:

&lt;/h2&gt;&lt;div class="answer-desc"&gt;&lt;p&gt;there are many built in public collection types. you can leverage one of them like this:&lt;/p&gt;&lt;pre&gt;&lt;code id="code_0" class="hljs language-sql"&gt;&lt;span class="hljs-keyword"&gt;with&lt;/span&gt; ids &lt;span class="hljs-keyword"&gt;as&lt;/span&gt; (&lt;span class="hljs-keyword"&gt;select&lt;/span&gt;&lt;span class="hljs-comment"&gt;/*+ cardinality(a, 1) */&lt;/span&gt; column_value id
               &lt;span class="hljs-keyword"&gt;from&lt;/span&gt;&lt;span class="hljs-keyword"&gt;table&lt;/span&gt;(UTL_NLA_ARRAY_INT(&lt;span class="hljs-number"&gt;100&lt;/span&gt;, &lt;span class="hljs-number"&gt;200&lt;/span&gt;, &lt;span class="hljs-number"&gt;300&lt;/span&gt;)) a
            )
&lt;span class="hljs-keyword"&gt;select&lt;/span&gt; ids.id, &lt;span class="hljs-keyword"&gt;case&lt;/span&gt;&lt;span class="hljs-keyword"&gt;when&lt;/span&gt; m.id &lt;span class="hljs-keyword"&gt;is&lt;/span&gt;&lt;span class="hljs-keyword"&gt;null&lt;/span&gt;&lt;span class="hljs-keyword"&gt;then&lt;/span&gt;&lt;span class="hljs-string"&gt;'**NO MATCH**'&lt;/span&gt;&lt;span class="hljs-keyword"&gt;else&lt;/span&gt; m.value &lt;span class="hljs-keyword"&gt;end&lt;/span&gt;&lt;span class="hljs-keyword"&gt;value&lt;/span&gt;&lt;span class="hljs-keyword"&gt;from&lt;/span&gt; ids
         &lt;span class="hljs-keyword"&gt;left&lt;/span&gt;&lt;span class="hljs-keyword"&gt;outer&lt;/span&gt;&lt;span class="hljs-keyword"&gt;join&lt;/span&gt; my_table m
                     &lt;span class="hljs-keyword"&gt;on&lt;/span&gt; m.id &lt;span class="hljs-operator"&gt;=&lt;/span&gt; ids.id;
&lt;/code&gt;&lt;button class="copy-btn btn btn-secondary btn-sm" onclick="copyCode(this,'code_0')"&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;to see a list of public types on your DB, run :&lt;/p&gt;&lt;pre&gt;&lt;code id="code_1" class="hljs language-sql"&gt;&lt;span class="hljs-keyword"&gt;select&lt;/span&gt; owner, type_name, coll_type, elem_type_name, upper_bound, &lt;span class="hljs-keyword"&gt;precision&lt;/span&gt;, scale &lt;span class="hljs-keyword"&gt;from&lt;/span&gt; all_coll_types
 &lt;span class="hljs-keyword"&gt;where&lt;/span&gt; elem_type_name &lt;span class="hljs-keyword"&gt;in&lt;/span&gt; (&lt;span class="hljs-string"&gt;'FLOAT'&lt;/span&gt;, &lt;span class="hljs-string"&gt;'INTEGER'&lt;/span&gt;, &lt;span class="hljs-string"&gt;'NUMBER'&lt;/span&gt;, &lt;span class="hljs-string"&gt;'DOUBLE PRECISION'&lt;/span&gt;)
&lt;/code&gt;&lt;button class="copy-btn btn btn-secondary btn-sm" onclick="copyCode(this,'code_1')"&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;the hint&lt;/p&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;pre&gt;&lt;code id="code_2" class="hljs language-cpp"&gt;&lt;span class="hljs-comment"&gt;/*+ cardinality(a, 1) */&lt;/span&gt;&lt;/code&gt;&lt;button class="copy-btn btn btn-secondary btn-sm" onclick="copyCode(this,'code_2')"&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;is just used to tell oracle how many elements are in our array (if not specified, the default will be an assumption of 8k elements). just set to a reasonably accurate number.&lt;/p&gt;&lt;/div&gt;&lt;h2 id="solution_2"&gt;Solution 2:

&lt;/h2&gt;&lt;div class="answer-desc"&gt;&lt;p&gt;You can transform a variable into a query using &lt;code&gt;CONNECT BY&lt;/code&gt; (tested on 11g, should work on 10g+):&lt;/p&gt;&lt;pre&gt;&lt;code id="code_4" class="hljs language-sql"&gt;&lt;span class="hljs-keyword"&gt;SQL&lt;/span&gt;&lt;span class="hljs-operator"&gt;&amp;gt;&lt;/span&gt;&lt;span class="hljs-keyword"&gt;WITH&lt;/span&gt; DATA &lt;span class="hljs-keyword"&gt;AS&lt;/span&gt; (&lt;span class="hljs-keyword"&gt;SELECT&lt;/span&gt;&lt;span class="hljs-string"&gt;'100,200,300'&lt;/span&gt; txt &lt;span class="hljs-keyword"&gt;FROM&lt;/span&gt; dual)
  &lt;span class="hljs-number"&gt;2&lt;/span&gt;&lt;span class="hljs-keyword"&gt;SELECT&lt;/span&gt; regexp_substr(txt, &lt;span class="hljs-string"&gt;'[^,]+'&lt;/span&gt;, &lt;span class="hljs-number"&gt;1&lt;/span&gt;, LEVEL) item &lt;span class="hljs-keyword"&gt;FROM&lt;/span&gt; DATA
  &lt;span class="hljs-number"&gt;3&lt;/span&gt;&lt;span class="hljs-keyword"&gt;CONNECT&lt;/span&gt;&lt;span class="hljs-keyword"&gt;BY&lt;/span&gt; LEVEL &lt;span class="hljs-operator"&gt;&amp;lt;=&lt;/span&gt; length(txt) &lt;span class="hljs-operator"&gt;-&lt;/span&gt; length(REPLACE(txt, &lt;span class="hljs-string"&gt;','&lt;/span&gt;, &lt;span class="hljs-string"&gt;''&lt;/span&gt;)) &lt;span class="hljs-operator"&gt;+&lt;/span&gt;&lt;span class="hljs-number"&gt;1&lt;/span&gt;;

ITEM
&lt;span class="hljs-comment"&gt;--------------------------------------------&lt;/span&gt;&lt;span class="hljs-number"&gt;100&lt;/span&gt;&lt;span class="hljs-number"&gt;200&lt;/span&gt;&lt;span class="hljs-number"&gt;300&lt;/span&gt;&lt;/code&gt;&lt;button class="copy-btn btn btn-secondary btn-sm" onclick="copyCode(this,'code_4')"&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;You can then join this result to the table as if it were a standard view:&lt;/p&gt;&lt;pre&gt;&lt;code id="code_5" class="hljs language-sql"&gt;&lt;span class="hljs-keyword"&gt;SQL&lt;/span&gt;&lt;span class="hljs-operator"&gt;&amp;gt;&lt;/span&gt;&lt;span class="hljs-keyword"&gt;WITH&lt;/span&gt; DATA &lt;span class="hljs-keyword"&gt;AS&lt;/span&gt; (&lt;span class="hljs-keyword"&gt;SELECT&lt;/span&gt;&lt;span class="hljs-string"&gt;'100,200,300'&lt;/span&gt; txt &lt;span class="hljs-keyword"&gt;FROM&lt;/span&gt; dual)
  &lt;span class="hljs-number"&gt;2&lt;/span&gt;&lt;span class="hljs-keyword"&gt;SELECT&lt;/span&gt; v.id, dbt.value01
  &lt;span class="hljs-number"&gt;3&lt;/span&gt;&lt;span class="hljs-keyword"&gt;FROM&lt;/span&gt; dbt
  &lt;span class="hljs-number"&gt;4&lt;/span&gt;&lt;span class="hljs-keyword"&gt;RIGHT&lt;/span&gt;&lt;span class="hljs-keyword"&gt;JOIN&lt;/span&gt;&lt;span class="hljs-number"&gt;5&lt;/span&gt;      (&lt;span class="hljs-keyword"&gt;SELECT&lt;/span&gt; to_number(regexp_substr(txt, &lt;span class="hljs-string"&gt;'[^,]+'&lt;/span&gt;, &lt;span class="hljs-number"&gt;1&lt;/span&gt;, LEVEL)) ID
  &lt;span class="hljs-number"&gt;6&lt;/span&gt;&lt;span class="hljs-keyword"&gt;FROM&lt;/span&gt; DATA
  &lt;span class="hljs-number"&gt;7&lt;/span&gt;&lt;span class="hljs-keyword"&gt;CONNECT&lt;/span&gt;&lt;span class="hljs-keyword"&gt;BY&lt;/span&gt; LEVEL &lt;span class="hljs-operator"&gt;&amp;lt;=&lt;/span&gt; length(txt) &lt;span class="hljs-operator"&gt;-&lt;/span&gt; length(REPLACE(txt, &lt;span class="hljs-string"&gt;','&lt;/span&gt;, &lt;span class="hljs-string"&gt;''&lt;/span&gt;)) &lt;span class="hljs-operator"&gt;+&lt;/span&gt;&lt;span class="hljs-number"&gt;1&lt;/span&gt;) v
  &lt;span class="hljs-number"&gt;8&lt;/span&gt;&lt;span class="hljs-keyword"&gt;ON&lt;/span&gt; dbt.id &lt;span class="hljs-operator"&gt;=&lt;/span&gt; v.id;

        ID VALUE01
&lt;span class="hljs-comment"&gt;---------- ----------&lt;/span&gt;&lt;span class="hljs-number"&gt;100&lt;/span&gt; Ann
       &lt;span class="hljs-number"&gt;300&lt;/span&gt; John
       &lt;span class="hljs-number"&gt;200&lt;/span&gt;&lt;/code&gt;&lt;button class="copy-btn btn btn-secondary btn-sm" onclick="copyCode(this,'code_5')"&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id="solution_3"&gt;Solution 3:

&lt;/h2&gt;&lt;div class="answer-desc"&gt;&lt;p&gt;One way of tackling this is to dynamically create a common table expression that can then be included in the query. The final synatx you'd be aiming for is:&lt;/p&gt;&lt;pre&gt;&lt;code id="code_6" class="hljs language-sql"&gt;&lt;span class="hljs-keyword"&gt;with&lt;/span&gt; list_of_values &lt;span class="hljs-keyword"&gt;as&lt;/span&gt; (
  &lt;span class="hljs-keyword"&gt;select&lt;/span&gt;&lt;span class="hljs-number"&gt;100&lt;/span&gt; val &lt;span class="hljs-keyword"&gt;from&lt;/span&gt; dual &lt;span class="hljs-keyword"&gt;union&lt;/span&gt;&lt;span class="hljs-keyword"&gt;all&lt;/span&gt;&lt;span class="hljs-keyword"&gt;select&lt;/span&gt;&lt;span class="hljs-number"&gt;200&lt;/span&gt; val &lt;span class="hljs-keyword"&gt;from&lt;/span&gt; dual &lt;span class="hljs-keyword"&gt;union&lt;/span&gt;&lt;span class="hljs-keyword"&gt;all&lt;/span&gt;&lt;span class="hljs-keyword"&gt;select&lt;/span&gt;&lt;span class="hljs-number"&gt;300&lt;/span&gt; val &lt;span class="hljs-keyword"&gt;from&lt;/span&gt; dual &lt;span class="hljs-keyword"&gt;union&lt;/span&gt;&lt;span class="hljs-keyword"&gt;all&lt;/span&gt;
  ...)
&lt;span class="hljs-keyword"&gt;select&lt;/span&gt;
  lov.val,
  ...
&lt;span class="hljs-keyword"&gt;from&lt;/span&gt;
  list_of_values lov &lt;span class="hljs-keyword"&gt;left&lt;/span&gt;&lt;span class="hljs-keyword"&gt;outer&lt;/span&gt;&lt;span class="hljs-keyword"&gt;join&lt;/span&gt; 
  other_data     t   &lt;span class="hljs-keyword"&gt;on&lt;/span&gt; (lov.val &lt;span class="hljs-operator"&gt;=&lt;/span&gt; t.val)
&lt;/code&gt;&lt;button class="copy-btn btn btn-secondary btn-sm" onclick="copyCode(this,'code_6')"&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;It's not very elegant, particularly for large sets of values, but compatibility with a database on which you might have few privileges is very good.&lt;/p&gt;&lt;/div&gt; 

</description><link>https://rachmatmoradosugiarto.blogspot.com/2024/08/comparing-list-of-values-against-table.html</link><author>noreply@blogger.com (Sandy Veum)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8397719027199589165.post-8858577935389957372</guid><pubDate>Wed, 21 Aug 2024 18:17:00 +0000</pubDate><atom:updated>2024-08-21T11:17:49.273-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Flask</category><category domain="http://www.blogger.com/atom/ns#">Many To Many</category><category domain="http://www.blogger.com/atom/ns#">Postgresql</category><category domain="http://www.blogger.com/atom/ns#">Python</category><category domain="http://www.blogger.com/atom/ns#">Sqlalchemy</category><title>Flask-sqlalchemy, Primary Key For Secondary Table In Many-to-many Relationship</title><description>&lt;div class="question"&gt;I am trying to build simple many-to-many relationship using Flask-Sqlalchemy for Postgresql Database: from app import db from sqlalchemy.dialects.postgresql import UUID  authors_bo&lt;/div&gt;

&lt;h2 id="solution_1"&gt;Solution 1:

&lt;/h2&gt;
&lt;div class="answer-desc"&gt;&lt;p&gt;&lt;a rel="nofollow noreferrer noopener" href="https://groups.google.com/forum/#!topic/sqlalchemy/WAHaPIuzqZQ"&gt;https://groups.google.com/forum/#!topic/sqlalchemy/WAHaPIuzqZQ&lt;/a&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code id="code_0" class="hljs language-python"&gt;authors_books = db.Table(
    &lt;span class="hljs-string"&gt;'authors_books'&lt;/span&gt;,
    db.Column(&lt;span class="hljs-string"&gt;'id'&lt;/span&gt;, UUID(as_uuid=&lt;span class="hljs-literal"&gt;True&lt;/span&gt;), primary_key=&lt;span class="hljs-literal"&gt;True&lt;/span&gt;, default=&lt;span class="hljs-keyword"&gt;lambda&lt;/span&gt;: uuid.uuid4().&lt;span class="hljs-built_in"&gt;hex&lt;/span&gt;),
    db.Column(&lt;span class="hljs-string"&gt;'author_id'&lt;/span&gt;, UUID(as_uuid=&lt;span class="hljs-literal"&gt;True&lt;/span&gt;), db.ForeignKey(&lt;span class="hljs-string"&gt;'authors.id'&lt;/span&gt;)),
    db.Column(&lt;span class="hljs-string"&gt;'book_id'&lt;/span&gt;, UUID(as_uuid=&lt;span class="hljs-literal"&gt;True&lt;/span&gt;), db.ForeignKey(&lt;span class="hljs-string"&gt;'books.id'&lt;/span&gt;)),
)
&lt;/code&gt;&lt;button class="copy-btn btn btn-secondary btn-sm" onclick="copyCode(this,'code_0')"&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;/div&gt; 
&lt;br&gt;
</description><link>https://rachmatmoradosugiarto.blogspot.com/2024/08/flask-sqlalchemy-primary-key-for.html</link><author>noreply@blogger.com (Sandy Veum)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8397719027199589165.post-576511149017433765</guid><pubDate>Wed, 21 Aug 2024 17:05:00 +0000</pubDate><atom:updated>2024-08-21T10:05:51.602-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Sql</category><category domain="http://www.blogger.com/atom/ns#">Sql Server</category><category domain="http://www.blogger.com/atom/ns#">Sql Server 2008</category><title>How To Obtain Failed Jobs From Sql Server Agent Through Script?</title><description>&lt;div class="question"&gt;I want to get the failed jobs which sql server agent failed to start or run through sql script...&lt;/div&gt;&lt;h2 id="solution_1"&gt;Solution 1:

&lt;/h2&gt;&lt;div class="answer-desc"&gt;&lt;p&gt;I created a view to obtain the failed jobs in sql server&lt;/p&gt;&lt;pre&gt;&lt;code id="code_0" class="hljs language-sql"&gt;&lt;span class="hljs-keyword"&gt;CREATE&lt;/span&gt;&lt;span class="hljs-keyword"&gt;VIEW&lt;/span&gt; dbo.View_Failed_Jobs
    &lt;span class="hljs-keyword"&gt;AS&lt;/span&gt;&lt;span class="hljs-keyword"&gt;SELECT&lt;/span&gt;   Job.instance_id
        ,SysJobs.job_id
        ,SysJobs.name &lt;span class="hljs-keyword"&gt;as&lt;/span&gt;&lt;span class="hljs-string"&gt;'JOB_NAME'&lt;/span&gt;
        ,SysJobSteps.step_name &lt;span class="hljs-keyword"&gt;as&lt;/span&gt;&lt;span class="hljs-string"&gt;'STEP_NAME'&lt;/span&gt;
        ,Job.run_status
        ,Job.sql_message_id
        ,Job.sql_severity
        ,Job.message
        ,Job.exec_date
        ,Job.run_duration
        ,Job.server
        ,SysJobSteps.output_file_name
    &lt;span class="hljs-keyword"&gt;FROM&lt;/span&gt;    (&lt;span class="hljs-keyword"&gt;SELECT&lt;/span&gt; Instance.instance_id
        ,DBSysJobHistory.job_id
        ,DBSysJobHistory.step_id
        ,DBSysJobHistory.sql_message_id
        ,DBSysJobHistory.sql_severity
        ,DBSysJobHistory.message
        ,(&lt;span class="hljs-keyword"&gt;CASE&lt;/span&gt; DBSysJobHistory.run_status
            &lt;span class="hljs-keyword"&gt;WHEN&lt;/span&gt;&lt;span class="hljs-number"&gt;0&lt;/span&gt;&lt;span class="hljs-keyword"&gt;THEN&lt;/span&gt;&lt;span class="hljs-string"&gt;'Failed'&lt;/span&gt;&lt;span class="hljs-keyword"&gt;WHEN&lt;/span&gt;&lt;span class="hljs-number"&gt;1&lt;/span&gt;&lt;span class="hljs-keyword"&gt;THEN&lt;/span&gt;&lt;span class="hljs-string"&gt;'Succeeded'&lt;/span&gt;&lt;span class="hljs-keyword"&gt;WHEN&lt;/span&gt;&lt;span class="hljs-number"&gt;2&lt;/span&gt;&lt;span class="hljs-keyword"&gt;THEN&lt;/span&gt;&lt;span class="hljs-string"&gt;'Retry'&lt;/span&gt;&lt;span class="hljs-keyword"&gt;WHEN&lt;/span&gt;&lt;span class="hljs-number"&gt;3&lt;/span&gt;&lt;span class="hljs-keyword"&gt;THEN&lt;/span&gt;&lt;span class="hljs-string"&gt;'Canceled'&lt;/span&gt;&lt;span class="hljs-keyword"&gt;WHEN&lt;/span&gt;&lt;span class="hljs-number"&gt;4&lt;/span&gt;&lt;span class="hljs-keyword"&gt;THEN&lt;/span&gt;&lt;span class="hljs-string"&gt;'In progress'&lt;/span&gt;&lt;span class="hljs-keyword"&gt;END&lt;/span&gt;) &lt;span class="hljs-keyword"&gt;as&lt;/span&gt; run_status
        ,((&lt;span class="hljs-built_in"&gt;SUBSTRING&lt;/span&gt;(&lt;span class="hljs-built_in"&gt;CAST&lt;/span&gt;(DBSysJobHistory.run_date &lt;span class="hljs-keyword"&gt;AS&lt;/span&gt;&lt;span class="hljs-type"&gt;VARCHAR&lt;/span&gt;(&lt;span class="hljs-number"&gt;8&lt;/span&gt;)), &lt;span class="hljs-number"&gt;5&lt;/span&gt;, &lt;span class="hljs-number"&gt;2&lt;/span&gt;) &lt;span class="hljs-operator"&gt;+&lt;/span&gt;&lt;span class="hljs-string"&gt;'/'&lt;/span&gt;&lt;span class="hljs-operator"&gt;+&lt;/span&gt;&lt;span class="hljs-built_in"&gt;SUBSTRING&lt;/span&gt;(&lt;span class="hljs-built_in"&gt;CAST&lt;/span&gt;(DBSysJobHistory.run_date &lt;span class="hljs-keyword"&gt;AS&lt;/span&gt;&lt;span class="hljs-type"&gt;VARCHAR&lt;/span&gt;(&lt;span class="hljs-number"&gt;8&lt;/span&gt;)), &lt;span class="hljs-number"&gt;7&lt;/span&gt;, &lt;span class="hljs-number"&gt;2&lt;/span&gt;) &lt;span class="hljs-operator"&gt;+&lt;/span&gt;&lt;span class="hljs-string"&gt;'/'&lt;/span&gt;&lt;span class="hljs-operator"&gt;+&lt;/span&gt;&lt;span class="hljs-built_in"&gt;SUBSTRING&lt;/span&gt;(&lt;span class="hljs-built_in"&gt;CAST&lt;/span&gt;(DBSysJobHistory.run_date &lt;span class="hljs-keyword"&gt;AS&lt;/span&gt;&lt;span class="hljs-type"&gt;VARCHAR&lt;/span&gt;(&lt;span class="hljs-number"&gt;8&lt;/span&gt;)), &lt;span class="hljs-number"&gt;1&lt;/span&gt;, &lt;span class="hljs-number"&gt;4&lt;/span&gt;) &lt;span class="hljs-operator"&gt;+&lt;/span&gt;&lt;span class="hljs-string"&gt;' '&lt;/span&gt;&lt;span class="hljs-operator"&gt;+&lt;/span&gt;&lt;span class="hljs-built_in"&gt;SUBSTRING&lt;/span&gt;((REPLICATE(&lt;span class="hljs-string"&gt;'0'&lt;/span&gt;,&lt;span class="hljs-number"&gt;6&lt;/span&gt;&lt;span class="hljs-operator"&gt;-&lt;/span&gt;LEN(&lt;span class="hljs-built_in"&gt;CAST&lt;/span&gt;(DBSysJobHistory.run_time &lt;span class="hljs-keyword"&gt;AS&lt;/span&gt;&lt;span class="hljs-type"&gt;varchar&lt;/span&gt;)))
        &lt;span class="hljs-operator"&gt;+&lt;/span&gt;&lt;span class="hljs-built_in"&gt;CAST&lt;/span&gt;(DBSysJobHistory.run_time &lt;span class="hljs-keyword"&gt;AS&lt;/span&gt;&lt;span class="hljs-type"&gt;VARCHAR&lt;/span&gt;)), &lt;span class="hljs-number"&gt;1&lt;/span&gt;, &lt;span class="hljs-number"&gt;2&lt;/span&gt;) &lt;span class="hljs-operator"&gt;+&lt;/span&gt;&lt;span class="hljs-string"&gt;':'&lt;/span&gt;&lt;span class="hljs-operator"&gt;+&lt;/span&gt;&lt;span class="hljs-built_in"&gt;SUBSTRING&lt;/span&gt;((REPLICATE(&lt;span class="hljs-string"&gt;'0'&lt;/span&gt;,&lt;span class="hljs-number"&gt;6&lt;/span&gt;&lt;span class="hljs-operator"&gt;-&lt;/span&gt;LEN(&lt;span class="hljs-built_in"&gt;CAST&lt;/span&gt;(DBSysJobHistory.run_time &lt;span class="hljs-keyword"&gt;AS&lt;/span&gt;&lt;span class="hljs-type"&gt;VARCHAR&lt;/span&gt;)))
        &lt;span class="hljs-operator"&gt;+&lt;/span&gt;&lt;span class="hljs-built_in"&gt;CAST&lt;/span&gt;(DBSysJobHistory.run_time &lt;span class="hljs-keyword"&gt;AS&lt;/span&gt;&lt;span class="hljs-type"&gt;VARCHAR&lt;/span&gt;)), &lt;span class="hljs-number"&gt;3&lt;/span&gt;, &lt;span class="hljs-number"&gt;2&lt;/span&gt;) &lt;span class="hljs-operator"&gt;+&lt;/span&gt;&lt;span class="hljs-string"&gt;':'&lt;/span&gt;&lt;span class="hljs-operator"&gt;+&lt;/span&gt;&lt;span class="hljs-built_in"&gt;SUBSTRING&lt;/span&gt;((REPLICATE(&lt;span class="hljs-string"&gt;'0'&lt;/span&gt;,&lt;span class="hljs-number"&gt;6&lt;/span&gt;&lt;span class="hljs-operator"&gt;-&lt;/span&gt;LEN(&lt;span class="hljs-built_in"&gt;CAST&lt;/span&gt;(DBSysJobHistory.run_time &lt;span class="hljs-keyword"&gt;as&lt;/span&gt;&lt;span class="hljs-type"&gt;varchar&lt;/span&gt;)))
        &lt;span class="hljs-operator"&gt;+&lt;/span&gt;&lt;span class="hljs-built_in"&gt;CAST&lt;/span&gt;(DBSysJobHistory.run_time &lt;span class="hljs-keyword"&gt;AS&lt;/span&gt;&lt;span class="hljs-type"&gt;VARCHAR&lt;/span&gt;)), &lt;span class="hljs-number"&gt;5&lt;/span&gt;, &lt;span class="hljs-number"&gt;2&lt;/span&gt;))) &lt;span class="hljs-keyword"&gt;AS&lt;/span&gt;&lt;span class="hljs-string"&gt;'exec_date'&lt;/span&gt;
        ,DBSysJobHistory.run_duration
        ,DBSysJobHistory.retries_attempted
        ,DBSysJobHistory.server
        &lt;span class="hljs-keyword"&gt;FROM&lt;/span&gt; msdb.dbo.sysjobhistory DBSysJobHistory
        &lt;span class="hljs-keyword"&gt;JOIN&lt;/span&gt; (&lt;span class="hljs-keyword"&gt;SELECT&lt;/span&gt; DBSysJobHistory.job_id
            ,DBSysJobHistory.step_id
            ,&lt;span class="hljs-built_in"&gt;MAX&lt;/span&gt;(DBSysJobHistory.instance_id) &lt;span class="hljs-keyword"&gt;as&lt;/span&gt; instance_id
            &lt;span class="hljs-keyword"&gt;FROM&lt;/span&gt; msdb.dbo.sysjobhistory DBSysJobHistory
            &lt;span class="hljs-keyword"&gt;GROUP&lt;/span&gt;&lt;span class="hljs-keyword"&gt;BY&lt;/span&gt; DBSysJobHistory.job_id
            ,DBSysJobHistory.step_id
            ) &lt;span class="hljs-keyword"&gt;AS&lt;/span&gt; Instance &lt;span class="hljs-keyword"&gt;ON&lt;/span&gt; DBSysJobHistory.instance_id &lt;span class="hljs-operator"&gt;=&lt;/span&gt; Instance.instance_id
        &lt;span class="hljs-keyword"&gt;WHERE&lt;/span&gt; DBSysJobHistory.run_status &lt;span class="hljs-operator"&gt;&amp;lt;&amp;gt;&lt;/span&gt;&lt;span class="hljs-number"&gt;1&lt;/span&gt;
        ) &lt;span class="hljs-keyword"&gt;AS&lt;/span&gt; Job
    &lt;span class="hljs-keyword"&gt;JOIN&lt;/span&gt; msdb.dbo.sysjobs SysJobs
       &lt;span class="hljs-keyword"&gt;ON&lt;/span&gt; (Job.job_id &lt;span class="hljs-operator"&gt;=&lt;/span&gt; SysJobs.job_id)
    &lt;span class="hljs-keyword"&gt;JOIN&lt;/span&gt; msdb.dbo.sysjobsteps SysJobSteps
       &lt;span class="hljs-keyword"&gt;ON&lt;/span&gt; (Job.job_id &lt;span class="hljs-operator"&gt;=&lt;/span&gt; SysJobSteps.job_id &lt;span class="hljs-keyword"&gt;AND&lt;/span&gt; Job.step_id &lt;span class="hljs-operator"&gt;=&lt;/span&gt; SysJobSteps.step_id)
    GO
&lt;/code&gt;&lt;button class="copy-btn btn btn-secondary btn-sm" onclick="copyCode(this,'code_0')"&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;-- Now execute the select statement to query the failed jobs&lt;/p&gt;&lt;pre&gt;&lt;code id="code_1" class="hljs language-sql"&gt;&lt;span class="hljs-keyword"&gt;Select&lt;/span&gt;&lt;span class="hljs-operator"&gt;*&lt;/span&gt;&lt;span class="hljs-keyword"&gt;from&lt;/span&gt; View_Failed_Jobs
&lt;/code&gt;&lt;button class="copy-btn btn btn-secondary btn-sm" onclick="copyCode(this,'code_1')"&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id="solution_2"&gt;Solution 2:

&lt;/h2&gt;&lt;div class="answer-desc"&gt;&lt;p&gt;Here is an example for SQL Server 2008:
&lt;a rel="nofollow noreferrer noopener" href="http://www.toadworld.com/platforms/sql-server/w/wiki/10351.sql-server-agent-job-query-samples"&gt;SQL Server Agent Job Query Samples&lt;/a&gt;&lt;/p&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;p&gt;You'll find a lot more if you google for "sql server failed jobs", but unfortunately most of the examples don't say if they work for SQL Server 2008, for example &lt;a rel="nofollow noreferrer noopener" href="https://sqlschoolhouse.wordpress.com/2010/07/27/get-failed-jobs-from-sql-server-agent/"&gt;this one&lt;/a&gt;.&lt;/p&gt;&lt;hr&gt;&lt;p&gt;&lt;strong&gt;EDIT:&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;Hint: when you copy the code from the first link, use the "view source" button:
&lt;img alt="view source" src="https://i.stack.imgur.com/yIqJc.png"&gt;&lt;/p&gt;&lt;p&gt;There's a "copy to clipboard" button as well which I tried first, but it generated some HTML tags in the SQL code which SQL Server complained about (and didn't let me execute the query).&lt;/p&gt;&lt;/div&gt;&lt;h2 id="solution_3"&gt;Solution 3:

&lt;/h2&gt;&lt;div class="answer-desc"&gt;&lt;p&gt;you can use shortcut using sql agent--&amp;gt;job activity monitor.&lt;a rel="nofollow noreferrer noopener" href="https://i.stack.imgur.com/4EmcA.jpg"&gt;&lt;img alt="enter image description here" src="https://i.stack.imgur.com/4EmcA.jpg"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/div&gt; 

</description><link>https://rachmatmoradosugiarto.blogspot.com/2024/08/how-to-obtain-failed-jobs-from-sql.html</link><author>noreply@blogger.com (Sandy Veum)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8397719027199589165.post-7501180871316673812</guid><pubDate>Wed, 21 Aug 2024 16:49:00 +0000</pubDate><atom:updated>2024-08-21T09:49:04.763-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Query Optimization</category><category domain="http://www.blogger.com/atom/ns#">Sql</category><category domain="http://www.blogger.com/atom/ns#">Wordpress</category><title>Why Does Wordpress Have Separate 'usersmeta' And 'users' Sql Tables. Why Not Combine Them?</title><description>&lt;div class="question"&gt;Alongside the users table, Wordpress has a usersmeta table with the following columns  meta_id user_id meta_key (e.g. first_name) meta_value (e.g. Tom)  Each user has 20 rows in th&lt;/div&gt;&lt;h2 id="solution_1"&gt;Solution 1:

&lt;/h2&gt;&lt;div class="answer-desc" style="height: auto !important;"&gt;&lt;h3&gt;Entity Attribute Value&lt;/h3&gt;&lt;p&gt;It's known as the &lt;a rel="nofollow noreferrer noopener" href="https://en.wikipedia.org/wiki/Entity%E2%80%93attribute%E2%80%93value_model"&gt;Entity Attribute Value&lt;/a&gt; (EAV) data model, and allows an arbitrary number of attributes to be assigned to a given entity. That means any number of meta-data entries per user.&lt;/p&gt;&lt;h3&gt;Why use it&lt;/h3&gt;&lt;p&gt;By default there are a few keys that wordpress sets (20 stated in the question) but there can be any number. If all users have one thousand meta data entries - there are simply one thousand entries in the usermeta table for each user - it doesn't have (in terms of the database structure) a limit to the number of meta data entries a user can have. It also permits one user to have one thousand meta data entires, whilst all others have 20 and still store the data efficiently - or any permutation thereof.&lt;/p&gt;&lt;p&gt;In addition to flexibility, using this kind of structure permits the main users table to remain small - which means more efficient queries.&lt;/p&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;h3&gt;Alternatives&lt;/h3&gt;&lt;p&gt;The alternatives to using EAV include:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Modify the schema whenever the number of attributes changes&lt;/li&gt;&lt;li&gt;Store all attributes in a serialized string (on the user object)&lt;/li&gt;&lt;li&gt;Use a schemaless db&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Permissions is the biggest problem with the first point, it is not a good idea to grant blanket access to alter the schema of your database tables, and is a (sane) roadblock for many if not most wordpress installs (hosted on wordpress.com or on a shared host where the db user has no alter permissions). Mysql also has a hard-limit of &lt;a rel="nofollow noreferrer noopener" href="https://dev.mysql.com/doc/refman/5.6/en/column-count-limit.html"&gt;4096 columns and 65,535 bytes per row&lt;/a&gt;. Attempting to store a large number of columns in a single table will eventually fail, along the way creating a table that is inefficient to query.&lt;/p&gt;&lt;p&gt;Storing all attribute in a serialized string would make it difficult and slow to query by a meta-data value.&lt;/p&gt;&lt;p&gt;Wordpress is quite tied to mysql, and therefore changing datastore isn't a realistic option.&lt;/p&gt;&lt;h3&gt;Further WP info&lt;/h3&gt;&lt;p&gt;If you aren't using any/many plugins it's possible you will have a constant number of rows in the usermeta table for each user, but typically each plugin you add may need to add meta-data for users; the number added may not be trivial and this data is stored in the usermeta table.&lt;/p&gt;&lt;p&gt;The docs for &lt;a rel="nofollow noreferrer noopener" href="https://codex.wordpress.org/Function_Reference/add_user_meta"&gt;add_meta_user&lt;/a&gt; may add some clarity as to why the database is structured that way. If you put code like this somewhere:&lt;/p&gt;&lt;pre&gt;&lt;code id="code_0" class="hljs language-bash"&gt;add_user_meta(&lt;span class="hljs-variable"&gt;$user_id&lt;/span&gt;, &lt;span class="hljs-string"&gt;"favorite_color"&lt;/span&gt;, &lt;span class="hljs-string"&gt;"blue"&lt;/span&gt;);
&lt;/code&gt;&lt;button class="copy-btn btn btn-secondary btn-sm" onclick="copyCode(this,'code_0')"&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;It will create a row in the usermeta table for the given user_id, without the need to add a column (favorite_color) to the main users table. That makes it easy-ish to find users by favorite color without the need to modify the schema of the users table.&lt;/p&gt;&lt;/div&gt;&lt;h2 id="solution_2"&gt;Solution 2:

&lt;/h2&gt;&lt;div class="answer-desc" style="height: auto !important;"&gt;&lt;p&gt;This is really a question about database normalization. You can look for information on that topic in many places.&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Basic answer&lt;/strong&gt; Since there is a huge literature about this, and there are a lot of differences, I will just give some examples of why this might happen - it boild down to trade-offs; Speed versus storage requirements, or ease of use versus data duplication. Efficiency is multidimensional, and since wordpress does a lot of different things, it may have various reasons to keep them separate - space could be an issue, speed of queries may depend on this, it may be easier to look at just the meta table instead of the full table for some purposes, or vice versa.&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Further reading&lt;/strong&gt; This is a deep topic, you may want to learn more - there are hundreds of books and thousands of scholarly papers on these issues. For instance, look at this previous SO question about designing a database:
&lt;a rel="nofollow noreferrer noopener" href="https://stackoverflow.com/questions/2766334/database-design-one-huge-table-or-separate-tables"&gt;Database design: one huge table or separate tables?&lt;/a&gt;, or this one: &lt;a rel="nofollow noreferrer noopener" href="https://stackoverflow.com/questions/2320633/first-time-database-design-am-i-overengineering"&gt;First-time database design: am I overengineering?&lt;/a&gt;
or &lt;a rel="nofollow noreferrer noopener" href="http://databases.about.com/od/specificproducts/a/normalization.htm"&gt;Database Normalization Basics
&lt;/a&gt; on About.com.&lt;/p&gt;&lt;/div&gt; 


</description><link>https://rachmatmoradosugiarto.blogspot.com/2024/08/why-does-wordpress-have-separate.html</link><author>noreply@blogger.com (Sandy Veum)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8397719027199589165.post-4901450722365427241</guid><pubDate>Wed, 21 Aug 2024 16:31:00 +0000</pubDate><atom:updated>2024-08-21T09:31:56.986-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Common Table Expression</category><category domain="http://www.blogger.com/atom/ns#">Inner Join</category><category domain="http://www.blogger.com/atom/ns#">Sql</category><category domain="http://www.blogger.com/atom/ns#">Sql Order By</category><category domain="http://www.blogger.com/atom/ns#">Sql Server 2008</category><title>Ordering Parent Rows By Date Descending With Child Rows Ordered Independently Beneath Each</title><description>&lt;div class="question"&gt;This is a contrived version of my table schema to illustrate my problem: QuoteID, Details, DateCreated, ModelQuoteID Where QuoteID is the primary key and ModelQuoteID is a nullable&lt;/div&gt;&lt;h2 id="solution_1"&gt;Solution 1:

&lt;/h2&gt;&lt;div class="answer-desc"&gt;&lt;p&gt;Watching the Olympics -- just skimmed your post -- looks like you want to control the sort at each level (root and one level in), and make sure the data is returned with the children directly beneath its parent (so you can page the data...). We do this all the time. You can add an &lt;code&gt;order by&lt;/code&gt; to each inner query and create a &lt;code&gt;sort&lt;/code&gt; column. I contrived a slightly different example that should be easy for you to apply to your circumstance. I sorted the root ascending and level one descending just to illustrate how you can control each part.&lt;/p&gt;&lt;pre&gt;&lt;code id="code_2" class="hljs language-sql"&gt;&lt;span class="hljs-keyword"&gt;declare&lt;/span&gt;&lt;span class="hljs-variable"&gt;@tbl&lt;/span&gt;&lt;span class="hljs-keyword"&gt;table&lt;/span&gt; (id &lt;span class="hljs-type"&gt;int&lt;/span&gt;, parent &lt;span class="hljs-type"&gt;int&lt;/span&gt;, name &lt;span class="hljs-type"&gt;varchar&lt;/span&gt;(&lt;span class="hljs-number"&gt;10&lt;/span&gt;))

&lt;span class="hljs-keyword"&gt;insert&lt;/span&gt;&lt;span class="hljs-keyword"&gt;into&lt;/span&gt;&lt;span class="hljs-variable"&gt;@tbl&lt;/span&gt; (id, parent, name)
&lt;span class="hljs-keyword"&gt;values&lt;/span&gt; (&lt;span class="hljs-number"&gt;1&lt;/span&gt;, &lt;span class="hljs-keyword"&gt;null&lt;/span&gt;, &lt;span class="hljs-string"&gt;'def'&lt;/span&gt;), (&lt;span class="hljs-number"&gt;2&lt;/span&gt;, &lt;span class="hljs-number"&gt;1&lt;/span&gt;, &lt;span class="hljs-string"&gt;'this'&lt;/span&gt;), (&lt;span class="hljs-number"&gt;3&lt;/span&gt;, &lt;span class="hljs-number"&gt;1&lt;/span&gt;, &lt;span class="hljs-string"&gt;'is'&lt;/span&gt;), (&lt;span class="hljs-number"&gt;4&lt;/span&gt;, &lt;span class="hljs-number"&gt;1&lt;/span&gt;, &lt;span class="hljs-string"&gt;'a'&lt;/span&gt;), (&lt;span class="hljs-number"&gt;5&lt;/span&gt;, &lt;span class="hljs-number"&gt;1&lt;/span&gt;, &lt;span class="hljs-string"&gt;'test'&lt;/span&gt;),
       (&lt;span class="hljs-number"&gt;6&lt;/span&gt;, &lt;span class="hljs-keyword"&gt;null&lt;/span&gt;, &lt;span class="hljs-string"&gt;'abc'&lt;/span&gt;), (&lt;span class="hljs-number"&gt;7&lt;/span&gt;, &lt;span class="hljs-number"&gt;6&lt;/span&gt;, &lt;span class="hljs-string"&gt;'this'&lt;/span&gt;), (&lt;span class="hljs-number"&gt;8&lt;/span&gt;, &lt;span class="hljs-number"&gt;6&lt;/span&gt;, &lt;span class="hljs-string"&gt;'is'&lt;/span&gt;), (&lt;span class="hljs-number"&gt;9&lt;/span&gt;, &lt;span class="hljs-number"&gt;6&lt;/span&gt;, &lt;span class="hljs-string"&gt;'another'&lt;/span&gt;), (&lt;span class="hljs-number"&gt;10&lt;/span&gt;, &lt;span class="hljs-number"&gt;6&lt;/span&gt;, &lt;span class="hljs-string"&gt;'test'&lt;/span&gt;)

;&lt;span class="hljs-keyword"&gt;with&lt;/span&gt; cte (id, parent, name, sort) &lt;span class="hljs-keyword"&gt;as&lt;/span&gt; (
  &lt;span class="hljs-keyword"&gt;select&lt;/span&gt; id, parent, name, &lt;span class="hljs-built_in"&gt;cast&lt;/span&gt;(&lt;span class="hljs-keyword"&gt;right&lt;/span&gt;(&lt;span class="hljs-string"&gt;'0000'&lt;/span&gt;&lt;span class="hljs-operator"&gt;+&lt;/span&gt;&lt;span class="hljs-built_in"&gt;cast&lt;/span&gt;(&lt;span class="hljs-built_in"&gt;row_number&lt;/span&gt;() &lt;span class="hljs-keyword"&gt;over&lt;/span&gt; (&lt;span class="hljs-keyword"&gt;order&lt;/span&gt;&lt;span class="hljs-keyword"&gt;by&lt;/span&gt; name) &lt;span class="hljs-keyword"&gt;as&lt;/span&gt;&lt;span class="hljs-type"&gt;varchar&lt;/span&gt;(&lt;span class="hljs-number"&gt;4&lt;/span&gt;)), &lt;span class="hljs-number"&gt;4&lt;/span&gt;) &lt;span class="hljs-keyword"&gt;as&lt;/span&gt;&lt;span class="hljs-type"&gt;varchar&lt;/span&gt;(&lt;span class="hljs-number"&gt;1024&lt;/span&gt;))
  &lt;span class="hljs-keyword"&gt;from&lt;/span&gt;&lt;span class="hljs-variable"&gt;@tbl&lt;/span&gt;&lt;span class="hljs-keyword"&gt;where&lt;/span&gt;  parent &lt;span class="hljs-keyword"&gt;is&lt;/span&gt;&lt;span class="hljs-keyword"&gt;null&lt;/span&gt;&lt;span class="hljs-keyword"&gt;union&lt;/span&gt;&lt;span class="hljs-keyword"&gt;all&lt;/span&gt;&lt;span class="hljs-keyword"&gt;select&lt;/span&gt; t.id, t.parent, t.name, &lt;span class="hljs-built_in"&gt;cast&lt;/span&gt;(cte.sort &lt;span class="hljs-operator"&gt;+&lt;/span&gt;&lt;span class="hljs-keyword"&gt;right&lt;/span&gt;(&lt;span class="hljs-string"&gt;'0000'&lt;/span&gt;&lt;span class="hljs-operator"&gt;+&lt;/span&gt;&lt;span class="hljs-built_in"&gt;cast&lt;/span&gt;(&lt;span class="hljs-built_in"&gt;row_number&lt;/span&gt;() &lt;span class="hljs-keyword"&gt;over&lt;/span&gt; (&lt;span class="hljs-keyword"&gt;order&lt;/span&gt;&lt;span class="hljs-keyword"&gt;by&lt;/span&gt; t.name &lt;span class="hljs-keyword"&gt;desc&lt;/span&gt;) &lt;span class="hljs-keyword"&gt;as&lt;/span&gt;&lt;span class="hljs-type"&gt;varchar&lt;/span&gt;(&lt;span class="hljs-number"&gt;4&lt;/span&gt;)), &lt;span class="hljs-number"&gt;4&lt;/span&gt;) &lt;span class="hljs-keyword"&gt;as&lt;/span&gt;&lt;span class="hljs-type"&gt;varchar&lt;/span&gt;(&lt;span class="hljs-number"&gt;1024&lt;/span&gt;))
  &lt;span class="hljs-keyword"&gt;from&lt;/span&gt;&lt;span class="hljs-variable"&gt;@tbl&lt;/span&gt; t &lt;span class="hljs-keyword"&gt;inner&lt;/span&gt;&lt;span class="hljs-keyword"&gt;join&lt;/span&gt; cte &lt;span class="hljs-keyword"&gt;on&lt;/span&gt; t.parent &lt;span class="hljs-operator"&gt;=&lt;/span&gt; cte.id
)
&lt;span class="hljs-keyword"&gt;select&lt;/span&gt;&lt;span class="hljs-operator"&gt;*&lt;/span&gt;&lt;span class="hljs-keyword"&gt;from&lt;/span&gt; cte
&lt;span class="hljs-keyword"&gt;order&lt;/span&gt;&lt;span class="hljs-keyword"&gt;by&lt;/span&gt; sort
&lt;/code&gt;&lt;button class="copy-btn btn btn-secondary btn-sm" onclick="copyCode(this,'code_2')"&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;This produces these results:&lt;/p&gt;&lt;pre&gt;&lt;code id="code_3" class="hljs language-kotlin"&gt;id    parent    name     sort
----  --------  -------  ----------
&lt;span class="hljs-number"&gt;6&lt;/span&gt;     NULL      abc      &lt;span class="hljs-number"&gt;0001&lt;/span&gt;&lt;span class="hljs-number"&gt;7&lt;/span&gt;&lt;span class="hljs-number"&gt;6&lt;/span&gt;&lt;span class="hljs-keyword"&gt;this&lt;/span&gt;&lt;span class="hljs-number"&gt;00010001&lt;/span&gt;&lt;span class="hljs-number"&gt;10&lt;/span&gt;&lt;span class="hljs-number"&gt;6&lt;/span&gt;         test     &lt;span class="hljs-number"&gt;00010002&lt;/span&gt;&lt;span class="hljs-number"&gt;8&lt;/span&gt;&lt;span class="hljs-number"&gt;6&lt;/span&gt;&lt;span class="hljs-keyword"&gt;is&lt;/span&gt;&lt;span class="hljs-number"&gt;00010003&lt;/span&gt;&lt;span class="hljs-number"&gt;9&lt;/span&gt;&lt;span class="hljs-number"&gt;6&lt;/span&gt;         another  &lt;span class="hljs-number"&gt;00010004&lt;/span&gt;&lt;span class="hljs-number"&gt;1&lt;/span&gt;     NULL      def      &lt;span class="hljs-number"&gt;0002&lt;/span&gt;&lt;span class="hljs-number"&gt;2&lt;/span&gt;&lt;span class="hljs-number"&gt;1&lt;/span&gt;&lt;span class="hljs-keyword"&gt;this&lt;/span&gt;&lt;span class="hljs-number"&gt;00020001&lt;/span&gt;&lt;span class="hljs-number"&gt;5&lt;/span&gt;&lt;span class="hljs-number"&gt;1&lt;/span&gt;         test     &lt;span class="hljs-number"&gt;00020002&lt;/span&gt;&lt;span class="hljs-number"&gt;3&lt;/span&gt;&lt;span class="hljs-number"&gt;1&lt;/span&gt;&lt;span class="hljs-keyword"&gt;is&lt;/span&gt;&lt;span class="hljs-number"&gt;00020003&lt;/span&gt;&lt;span class="hljs-number"&gt;4&lt;/span&gt;&lt;span class="hljs-number"&gt;1&lt;/span&gt;         a        &lt;span class="hljs-number"&gt;00020004&lt;/span&gt;&lt;/code&gt;&lt;button class="copy-btn btn btn-secondary btn-sm" onclick="copyCode(this,'code_3')"&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;You can see that the root nodes are sorted ascending and the inner nodes are sorted descending.&lt;/p&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;/div&gt; 

</description><link>https://rachmatmoradosugiarto.blogspot.com/2024/08/ordering-parent-rows-by-date-descending.html</link><author>noreply@blogger.com (Sandy Veum)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8397719027199589165.post-7123897148645162636</guid><pubDate>Wed, 21 Aug 2024 16:15:00 +0000</pubDate><atom:updated>2024-08-21T09:15:52.887-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Sql</category><category domain="http://www.blogger.com/atom/ns#">Sql Server</category><category domain="http://www.blogger.com/atom/ns#">Sql Server 2014</category><title>Insert And Update In A Cursor In Sql Server</title><description>&lt;div class="question"&gt;I have a Table  shown below DateRange 10/1 11/1 12/1  I am looping through this table using cursor and trying to populate a temp table Create table #BidStatusCalculation1 (       C&lt;/div&gt;&lt;h2 id="solution_1"&gt;Solution 1:

&lt;/h2&gt;&lt;div class="answer-desc"&gt;&lt;p&gt;Use &lt;strong&gt;&lt;a rel="nofollow noreferrer noopener" href="https://docs.microsoft.com/en-us/sql/t-sql/queries/from-using-pivot-and-unpivot?view=sql-server-2017"&gt;&lt;code&gt;PIVOT&lt;/code&gt;&lt;/a&gt;&lt;/strong&gt; as&lt;/p&gt;&lt;pre&gt;&lt;code id="code_1" class="hljs language-css"&gt;SELECT &lt;span class="hljs-selector-tag"&gt;Code&lt;/span&gt;,
       &lt;span class="hljs-selector-attr"&gt;[22]&lt;/span&gt; Data1,
       &lt;span class="hljs-selector-attr"&gt;[33]&lt;/span&gt; Data2,
       &lt;span class="hljs-selector-attr"&gt;[43]&lt;/span&gt; Data3
&lt;span class="hljs-selector-tag"&gt;FROM&lt;/span&gt;
    (
      SELECT *
      &lt;span class="hljs-selector-tag"&gt;FROM&lt;/span&gt; T
    ) TBL
    PIVOT
    (
      MAX(Data) FOR Data IN(&lt;span class="hljs-selector-attr"&gt;[22]&lt;/span&gt;,&lt;span class="hljs-selector-attr"&gt;[33]&lt;/span&gt;,&lt;span class="hljs-selector-attr"&gt;[43]&lt;/span&gt;)
    ) PVT
&lt;/code&gt;&lt;button class="copy-btn btn btn-secondary btn-sm" onclick="copyCode(this,'code_1')"&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;&lt;strong&gt;&lt;a rel="nofollow noreferrer noopener" href="https://dbfiddle.uk/?rdbms=sqlserver_2017&amp;amp;fiddle=60fcd65b82ee60e45fc5d4e6d852cc1d"&gt;Demo&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;For dynamic pivot&lt;/p&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;pre&gt;&lt;code id="code_2" class="hljs language-sql"&gt;&lt;span class="hljs-keyword"&gt;CREATE&lt;/span&gt;&lt;span class="hljs-keyword"&gt;TABLE&lt;/span&gt; T
(
  Code &lt;span class="hljs-type"&gt;VARCHAR&lt;/span&gt;(&lt;span class="hljs-number"&gt;45&lt;/span&gt;),
  Data &lt;span class="hljs-type"&gt;INT&lt;/span&gt;
);

&lt;span class="hljs-keyword"&gt;INSERT&lt;/span&gt;&lt;span class="hljs-keyword"&gt;INTO&lt;/span&gt; T &lt;span class="hljs-keyword"&gt;VALUES&lt;/span&gt;
(&lt;span class="hljs-string"&gt;'SL Payroll'&lt;/span&gt;,    &lt;span class="hljs-number"&gt;22&lt;/span&gt;),
(&lt;span class="hljs-string"&gt;'SL Payroll'&lt;/span&gt;,    &lt;span class="hljs-number"&gt;33&lt;/span&gt;),
(&lt;span class="hljs-string"&gt;'SL Payroll'&lt;/span&gt;,    &lt;span class="hljs-number"&gt;43&lt;/span&gt;);

&lt;span class="hljs-keyword"&gt;DECLARE&lt;/span&gt;&lt;span class="hljs-variable"&gt;@IN&lt;/span&gt;&lt;span class="hljs-type"&gt;VARCHAR&lt;/span&gt;(MAX) &lt;span class="hljs-operator"&gt;=&lt;/span&gt;&lt;span class="hljs-string"&gt;''&lt;/span&gt;,
        &lt;span class="hljs-variable"&gt;@Cols&lt;/span&gt;&lt;span class="hljs-type"&gt;VARCHAR&lt;/span&gt;(MAX) &lt;span class="hljs-operator"&gt;=&lt;/span&gt;&lt;span class="hljs-string"&gt;''&lt;/span&gt;;

&lt;span class="hljs-keyword"&gt;SELECT&lt;/span&gt;&lt;span class="hljs-variable"&gt;@IN&lt;/span&gt;&lt;span class="hljs-operator"&gt;=&lt;/span&gt;&lt;span class="hljs-variable"&gt;@IN&lt;/span&gt;&lt;span class="hljs-operator"&gt;+&lt;/span&gt;&lt;span class="hljs-string"&gt;','&lt;/span&gt;&lt;span class="hljs-operator"&gt;+&lt;/span&gt; QUOTENAME(Data),
       &lt;span class="hljs-variable"&gt;@Cols&lt;/span&gt;&lt;span class="hljs-operator"&gt;=&lt;/span&gt;&lt;span class="hljs-variable"&gt;@Cols&lt;/span&gt;&lt;span class="hljs-operator"&gt;+&lt;/span&gt;&lt;span class="hljs-string"&gt;','&lt;/span&gt;&lt;span class="hljs-operator"&gt;+&lt;/span&gt; QUOTENAME(Data) &lt;span class="hljs-operator"&gt;+&lt;/span&gt;&lt;span class="hljs-string"&gt;' AS Data'&lt;/span&gt;&lt;span class="hljs-operator"&gt;+&lt;/span&gt;&lt;span class="hljs-built_in"&gt;CAST&lt;/span&gt;(&lt;span class="hljs-built_in"&gt;ROW_NUMBER&lt;/span&gt;()&lt;span class="hljs-keyword"&gt;OVER&lt;/span&gt;(&lt;span class="hljs-keyword"&gt;ORDER&lt;/span&gt;&lt;span class="hljs-keyword"&gt;BY&lt;/span&gt; (&lt;span class="hljs-keyword"&gt;SELECT&lt;/span&gt;&lt;span class="hljs-keyword"&gt;NULL&lt;/span&gt;)) &lt;span class="hljs-keyword"&gt;AS&lt;/span&gt;&lt;span class="hljs-type"&gt;VARCHAR&lt;/span&gt;)
&lt;span class="hljs-keyword"&gt;FROM&lt;/span&gt; T;

&lt;span class="hljs-keyword"&gt;SELECT&lt;/span&gt;&lt;span class="hljs-variable"&gt;@IN&lt;/span&gt;&lt;span class="hljs-operator"&gt;=&lt;/span&gt; STUFF(&lt;span class="hljs-variable"&gt;@IN&lt;/span&gt;, &lt;span class="hljs-number"&gt;1&lt;/span&gt;, &lt;span class="hljs-number"&gt;1&lt;/span&gt;, &lt;span class="hljs-string"&gt;''&lt;/span&gt;), &lt;span class="hljs-variable"&gt;@Cols&lt;/span&gt;&lt;span class="hljs-operator"&gt;=&lt;/span&gt; STUFF(&lt;span class="hljs-variable"&gt;@Cols&lt;/span&gt;, &lt;span class="hljs-number"&gt;1&lt;/span&gt;, &lt;span class="hljs-number"&gt;1&lt;/span&gt;, &lt;span class="hljs-string"&gt;''&lt;/span&gt;);

&lt;span class="hljs-keyword"&gt;DECLARE&lt;/span&gt;&lt;span class="hljs-variable"&gt;@SQL&lt;/span&gt; NVARCHAR(MAX) &lt;span class="hljs-operator"&gt;=&lt;/span&gt; N&lt;span class="hljs-string"&gt;'SELECT Code, '&lt;/span&gt;&lt;span class="hljs-operator"&gt;+&lt;/span&gt;&lt;span class="hljs-variable"&gt;@Cols&lt;/span&gt;&lt;span class="hljs-operator"&gt;+&lt;/span&gt;&lt;span class="hljs-string"&gt;' FROM (SELECT * FROM T) TBL PIVOT (MAX(Data) FOR Data IN('&lt;/span&gt;&lt;span class="hljs-operator"&gt;+&lt;/span&gt;&lt;span class="hljs-variable"&gt;@IN&lt;/span&gt;&lt;span class="hljs-operator"&gt;+&lt;/span&gt;&lt;span class="hljs-string"&gt;')) PVT'&lt;/span&gt;;
     &lt;span class="hljs-keyword"&gt;EXECUTE&lt;/span&gt; sp_executesql &lt;span class="hljs-variable"&gt;@SQL&lt;/span&gt;;
&lt;/code&gt;&lt;button class="copy-btn btn btn-secondary btn-sm" onclick="copyCode(this,'code_2')"&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;&lt;strong&gt;&lt;a rel="nofollow noreferrer noopener" href="https://dbfiddle.uk/?rdbms=sqlserver_2017&amp;amp;fiddle=d8c751dfe61b18f32814a7afec5edf4e"&gt;Demo&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;&lt;/div&gt; 

</description><link>https://rachmatmoradosugiarto.blogspot.com/2024/08/insert-and-update-in-cursor-in-sql.html</link><author>noreply@blogger.com (Sandy Veum)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8397719027199589165.post-8201586617127978722</guid><pubDate>Wed, 21 Aug 2024 14:45:00 +0000</pubDate><atom:updated>2024-08-21T07:45:21.226-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Sql Server 2008</category><title>If Loop In 'then' Statement Of Case Statement In Sql Server 2008</title><description>&lt;div class="question"&gt;If I wanted to use if condition in Then statement of Case statement in SQL Server 2008. How to use it?&lt;/div&gt;&lt;h2 id="solution_1"&gt;Solution 1:

&lt;/h2&gt;&lt;div class="answer-desc"&gt;&lt;p&gt;Nest them&lt;/p&gt;&lt;pre&gt;&lt;code id="code_0" class="hljs language-sql"&gt;&lt;span class="hljs-keyword"&gt;CASE&lt;/span&gt;&lt;span class="hljs-keyword"&gt;WHEN&lt;/span&gt; foo &lt;span class="hljs-operator"&gt;=&lt;/span&gt;&lt;span class="hljs-string"&gt;'bar'&lt;/span&gt;&lt;span class="hljs-keyword"&gt;THEN&lt;/span&gt;&lt;span class="hljs-keyword"&gt;CASE&lt;/span&gt;&lt;span class="hljs-keyword"&gt;WHEN&lt;/span&gt; black &lt;span class="hljs-operator"&gt;=&lt;/span&gt;&lt;span class="hljs-string"&gt;'white'&lt;/span&gt;&lt;span class="hljs-keyword"&gt;THEN&lt;/span&gt; .. 
                 &lt;span class="hljs-keyword"&gt;WHEN&lt;/span&gt; W &lt;span class="hljs-operator"&gt;=&lt;/span&gt;&lt;span class="hljs-string"&gt;'TF'&lt;/span&gt;&lt;span class="hljs-keyword"&gt;THEN&lt;/span&gt; ... 
                 &lt;span class="hljs-keyword"&gt;ELSE&lt;/span&gt;&lt;span class="hljs-keyword"&gt;END&lt;/span&gt;&lt;span class="hljs-keyword"&gt;WHEN&lt;/span&gt; fish &lt;span class="hljs-operator"&gt;=&lt;/span&gt;&lt;span class="hljs-string"&gt;'bicycle'&lt;/span&gt;&lt;span class="hljs-keyword"&gt;THEN&lt;/span&gt; ... 
   &lt;span class="hljs-keyword"&gt;ELSE&lt;/span&gt; ... 
&lt;span class="hljs-keyword"&gt;END&lt;/span&gt;&lt;/code&gt;&lt;button class="copy-btn btn btn-secondary btn-sm" onclick="copyCode(this,'code_0')"&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id="solution_2"&gt;Solution 2:

&lt;/h2&gt;&lt;div class="answer-desc"&gt;&lt;p&gt;You can use &lt;code&gt;Then&lt;/code&gt; keyword &lt;a rel="nofollow noreferrer noopener" href="https://msdn.microsoft.com/en-us/library/ms181765.aspx"&gt;Case&lt;/a&gt; statement like follows in Sql server&lt;/p&gt;&lt;pre&gt;&lt;code id="code_2" class="hljs language-sql"&gt;&lt;span class="hljs-keyword"&gt;SELECT&lt;/span&gt;   ProductNumber, Category &lt;span class="hljs-operator"&gt;=&lt;/span&gt;&lt;span class="hljs-keyword"&gt;CASE&lt;/span&gt; ProductLine
         &lt;span class="hljs-keyword"&gt;WHEN&lt;/span&gt; TestRowValue &lt;span class="hljs-operator"&gt;=&lt;/span&gt;&lt;span class="hljs-string"&gt;'R'&lt;/span&gt;&lt;span class="hljs-keyword"&gt;THEN&lt;/span&gt;&lt;span class="hljs-keyword"&gt;Case&lt;/span&gt;&lt;span class="hljs-keyword"&gt;When&lt;/span&gt; TestColumn &lt;span class="hljs-operator"&gt;=&lt;/span&gt;&lt;span class="hljs-string"&gt;'P'&lt;/span&gt;&lt;span class="hljs-keyword"&gt;THEN&lt;/span&gt;&lt;span class="hljs-string"&gt;'Road'&lt;/span&gt;&lt;span class="hljs-keyword"&gt;ELSE&lt;/span&gt;&lt;span class="hljs-string"&gt;'Board'&lt;/span&gt;&lt;span class="hljs-keyword"&gt;WHEN&lt;/span&gt;&lt;span class="hljs-string"&gt;'M'&lt;/span&gt;&lt;span class="hljs-keyword"&gt;THEN&lt;/span&gt;&lt;span class="hljs-string"&gt;'Mountain'&lt;/span&gt;&lt;span class="hljs-keyword"&gt;WHEN&lt;/span&gt;&lt;span class="hljs-string"&gt;'T'&lt;/span&gt;&lt;span class="hljs-keyword"&gt;THEN&lt;/span&gt;&lt;span class="hljs-string"&gt;'Touring'&lt;/span&gt;&lt;span class="hljs-keyword"&gt;WHEN&lt;/span&gt;&lt;span class="hljs-string"&gt;'S'&lt;/span&gt;&lt;span class="hljs-keyword"&gt;THEN&lt;/span&gt;&lt;span class="hljs-string"&gt;'Other sale items'&lt;/span&gt;&lt;span class="hljs-keyword"&gt;ELSE&lt;/span&gt;&lt;span class="hljs-string"&gt;'Not for sale'&lt;/span&gt;&lt;span class="hljs-keyword"&gt;END&lt;/span&gt;,
   Name
&lt;span class="hljs-keyword"&gt;FROM&lt;/span&gt; Production.Product
&lt;/code&gt;&lt;button class="copy-btn btn btn-secondary btn-sm" onclick="copyCode(this,'code_2')"&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;&lt;a name='more'&gt;&lt;/a&gt;</description><link>https://rachmatmoradosugiarto.blogspot.com/2024/08/if-loop-in-then-statement-of-case.html</link><author>noreply@blogger.com (Sandy Veum)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8397719027199589165.post-629973271793382732</guid><pubDate>Wed, 21 Aug 2024 14:09:00 +0000</pubDate><atom:updated>2024-08-21T07:09:07.757-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Mysql</category><category domain="http://www.blogger.com/atom/ns#">Query Optimization</category><category domain="http://www.blogger.com/atom/ns#">Sql</category><title>How Can I Speed Up This Select Concat/group By Query?</title><description>&lt;div class="question"&gt;I'm working on selecting locations (city, state) out of a database.  The problem is that the query is running a tad slow and I'm not sure how to speed it up.  For example: SELECT C&lt;/div&gt;&lt;h2 id="solution_1"&gt;Solution 1:

&lt;/h2&gt;&lt;div class="answer-desc"&gt;&lt;p&gt;Create a composite index on &lt;code&gt;(state, city)&lt;/code&gt; and rewrite your query as this:&lt;/p&gt;&lt;pre&gt;&lt;code id="code_1" class="hljs language-sql"&gt;&lt;span class="hljs-keyword"&gt;SELECT&lt;/span&gt;  CONCAT_WS(&lt;span class="hljs-string"&gt;', '&lt;/span&gt;, city, state) &lt;span class="hljs-keyword"&gt;AS&lt;/span&gt; location, &lt;span class="hljs-built_in"&gt;AVG&lt;/span&gt;(latitude), &lt;span class="hljs-built_in"&gt;AVG&lt;/span&gt;(longitude) 
&lt;span class="hljs-keyword"&gt;FROM&lt;/span&gt;    places
&lt;span class="hljs-keyword"&gt;WHERE&lt;/span&gt;   state&lt;span class="hljs-operator"&gt;=&lt;/span&gt;&lt;span class="hljs-string"&gt;'NY'&lt;/span&gt;&lt;span class="hljs-keyword"&gt;AND&lt;/span&gt; city&lt;span class="hljs-operator"&gt;=&lt;/span&gt;&lt;span class="hljs-string"&gt;'New York'&lt;/span&gt;&lt;span class="hljs-keyword"&gt;GROUP&lt;/span&gt;&lt;span class="hljs-keyword"&gt;BY&lt;/span&gt;
        state, city
&lt;/code&gt;&lt;button class="copy-btn btn btn-secondary btn-sm" onclick="copyCode(this,'code_1')"&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;Note that for this very query you may omit &lt;code&gt;GROUP BY&lt;/code&gt; clause:&lt;/p&gt;&lt;pre&gt;&lt;code id="code_3" class="hljs language-sql"&gt;&lt;span class="hljs-keyword"&gt;SELECT&lt;/span&gt;&lt;span class="hljs-string"&gt;'New York, NY'&lt;/span&gt;&lt;span class="hljs-keyword"&gt;AS&lt;/span&gt; location, &lt;span class="hljs-built_in"&gt;AVG&lt;/span&gt;(latitude), &lt;span class="hljs-built_in"&gt;AVG&lt;/span&gt;(longitude) 
&lt;span class="hljs-keyword"&gt;FROM&lt;/span&gt;    places
&lt;span class="hljs-keyword"&gt;WHERE&lt;/span&gt;   state&lt;span class="hljs-operator"&gt;=&lt;/span&gt;&lt;span class="hljs-string"&gt;'NY'&lt;/span&gt;&lt;span class="hljs-keyword"&gt;AND&lt;/span&gt; city&lt;span class="hljs-operator"&gt;=&lt;/span&gt;&lt;span class="hljs-string"&gt;'New York'&lt;/span&gt;&lt;/code&gt;&lt;button class="copy-btn btn btn-secondary btn-sm" onclick="copyCode(this,'code_3')"&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;However, this query will still need it:&lt;/p&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;pre&gt;&lt;code id="code_4" class="hljs language-sql"&gt;&lt;span class="hljs-keyword"&gt;SELECT&lt;/span&gt;  CONCAT_WS(&lt;span class="hljs-string"&gt;', '&lt;/span&gt;, city, state) &lt;span class="hljs-keyword"&gt;AS&lt;/span&gt; location, &lt;span class="hljs-built_in"&gt;AVG&lt;/span&gt;(latitude), &lt;span class="hljs-built_in"&gt;AVG&lt;/span&gt;(longitude) 
&lt;span class="hljs-keyword"&gt;FROM&lt;/span&gt;    places
&lt;span class="hljs-keyword"&gt;WHERE&lt;/span&gt;   state&lt;span class="hljs-operator"&gt;=&lt;/span&gt;&lt;span class="hljs-string"&gt;'NY'&lt;/span&gt;&lt;span class="hljs-keyword"&gt;GROUP&lt;/span&gt;&lt;span class="hljs-keyword"&gt;BY&lt;/span&gt;
        state, city
&lt;/code&gt;&lt;button class="copy-btn btn btn-secondary btn-sm" onclick="copyCode(this,'code_4')"&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id="solution_2"&gt;Solution 2:

&lt;/h2&gt;&lt;div class="answer-desc" style="height: auto !important;"&gt;&lt;p&gt;It's funny, but almost every problem people have with databases is speed, not storage requirements. That should tell you something :-)&lt;/p&gt;&lt;p&gt;We've had problems like this before and I've said this many times: per-row functions generally do not scale well. The best method we've found to fix them is with insert/update triggers (I'm assuming MySQL has these).&lt;/p&gt;&lt;p&gt;Create another column call pretty_city_state (or whatever) and have the triggers populate it from city and state whenever you insert or update a row. Then create an index on it.&lt;/p&gt;&lt;p&gt;This takes advantage of the fact that database rows are generally read &lt;em&gt;far&lt;/em&gt; more often than they're written (especially in this case). By evaluating that column on write, you bear the cost across the writes (thousands) rather than the reads (millions probably). And it's the writing when it &lt;em&gt;should&lt;/em&gt; be borne simply because pretty_city_state will only change when either city or state changes. If you do the concat on every select, you're wasting effort.&lt;/p&gt;&lt;p&gt;Try that and measure the difference - I'm sure you'll find that your selects will scream along at a minimal cost for the triggers (and that cost disappears totally once you have all cities and states within your database.&lt;/p&gt;&lt;p&gt;And yes, I know this breaks 3NF. It's perfectly acceptable to do so for performance reasons &lt;em&gt;if you know what you're doing&lt;/em&gt;.&lt;/p&gt;&lt;p&gt;Your query could be done as:&lt;/p&gt;&lt;pre&gt;&lt;code id="code_5" class="hljs language-sql"&gt;&lt;span class="hljs-keyword"&gt;SELECT&lt;/span&gt; pretty_city_state &lt;span class="hljs-keyword"&gt;as&lt;/span&gt; location, &lt;span class="hljs-built_in"&gt;AVG&lt;/span&gt;(latitude), &lt;span class="hljs-built_in"&gt;AVG&lt;/span&gt;(longitude) 
&lt;span class="hljs-keyword"&gt;FROM&lt;/span&gt; places
&lt;span class="hljs-keyword"&gt;WHERE&lt;/span&gt; city&lt;span class="hljs-operator"&gt;=&lt;/span&gt;&lt;span class="hljs-string"&gt;'New York'&lt;/span&gt;&lt;span class="hljs-keyword"&gt;AND&lt;/span&gt; state&lt;span class="hljs-operator"&gt;=&lt;/span&gt;&lt;span class="hljs-string"&gt;'NY'&lt;/span&gt;&lt;span class="hljs-keyword"&gt;GROUP&lt;/span&gt;&lt;span class="hljs-keyword"&gt;BY&lt;/span&gt; pretty_city_state
&lt;/code&gt;&lt;button class="copy-btn btn btn-secondary btn-sm" onclick="copyCode(this,'code_5')"&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;or, maybe even faster (measure, don't guess) if you can concatenate the city and state before starting the query:&lt;/p&gt;&lt;pre&gt;&lt;code id="code_6" class="hljs language-sql"&gt;&lt;span class="hljs-keyword"&gt;SELECT&lt;/span&gt; pretty_city_state &lt;span class="hljs-keyword"&gt;as&lt;/span&gt; location, &lt;span class="hljs-built_in"&gt;AVG&lt;/span&gt;(latitude), &lt;span class="hljs-built_in"&gt;AVG&lt;/span&gt;(longitude) 
&lt;span class="hljs-keyword"&gt;FROM&lt;/span&gt; places
&lt;span class="hljs-keyword"&gt;WHERE&lt;/span&gt; pretty_city_state &lt;span class="hljs-operator"&gt;=&lt;/span&gt;&lt;span class="hljs-string"&gt;'New York, NY'&lt;/span&gt;&lt;span class="hljs-keyword"&gt;GROUP&lt;/span&gt;&lt;span class="hljs-keyword"&gt;BY&lt;/span&gt; pretty_city_state
&lt;/code&gt;&lt;button class="copy-btn btn btn-secondary btn-sm" onclick="copyCode(this,'code_6')"&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id="solution_3"&gt;Solution 3:

&lt;/h2&gt;&lt;div class="answer-desc"&gt;&lt;p&gt;One way to best optimize a query like this is to have those columns set as Index columns. That way it can easily sort/group based off of a tree or hash. Also, concatenation of strings might have some implications too. &lt;/p&gt;&lt;/div&gt;&lt;h2 id="solution_4"&gt;Solution 4:

&lt;/h2&gt;&lt;div class="answer-desc"&gt;&lt;p&gt;Adding an index on fields "city" and "state" will help.&lt;/p&gt;&lt;p&gt;Also, depending on the cardinality of each field (number of distinct values), on the version of MySQL, the table engine and other parameters, inverting the WHERE clauses may have an impact on the execution time of your query. I'd try:&lt;/p&gt;&lt;pre&gt;&lt;code id="code_7" class="hljs language-perl"&gt;WHERE &lt;span class="hljs-keyword"&gt;state&lt;/span&gt;=&lt;span class="hljs-string"&gt;'NY'&lt;/span&gt; AND city=&lt;span class="hljs-string"&gt;'New York'&lt;/span&gt;&lt;/code&gt;&lt;button class="copy-btn btn btn-secondary btn-sm" onclick="copyCode(this,'code_7')"&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;/div&gt; 

</description><link>https://rachmatmoradosugiarto.blogspot.com/2024/08/how-can-i-speed-up-this-select.html</link><author>noreply@blogger.com (Sandy Veum)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8397719027199589165.post-8162452744621396938</guid><pubDate>Wed, 21 Aug 2024 13:32:00 +0000</pubDate><atom:updated>2024-08-21T06:32:51.057-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Asp.net Core Mvc 2.0</category><category domain="http://www.blogger.com/atom/ns#">Entity Framework Core</category><category domain="http://www.blogger.com/atom/ns#">Linq</category><category domain="http://www.blogger.com/atom/ns#">Sql Server</category><title>How Do I Write Ef.functions Extension Method?</title><description>&lt;div class="question"&gt;I see that EF Core 2 has EF.Functions property EF Core 2.0 Announcement which can be used by EF Core or providers to define methods that map to database functions or operators so t&lt;/div&gt;&lt;h2 id="solution_1"&gt;Solution 1:

&lt;/h2&gt;&lt;div class="answer-desc"&gt;&lt;p&gt;Adding new scalar method to &lt;code&gt;EF.Functions&lt;/code&gt; is easy - you simply define extension method on &lt;code&gt;DbFunctions&lt;/code&gt; class. However providing SQL translation is hard and requires digging into EFC internals.&lt;/p&gt;&lt;p&gt;However EFC 2.0 also introduces a much simpler approach, explained in &lt;a rel="nofollow noreferrer noopener" href="https://docs.microsoft.com/en-us/ef/core/what-is-new/ef-core-2.0#database-scalar-function-mapping"&gt;Database scalar function mapping&lt;/a&gt; section of the &lt;em&gt;New features in EF Core 2.0&lt;/em&gt; documentation topic.&lt;/p&gt;&lt;p&gt;According to that, the easiest would be to add a static method to your &lt;code&gt;DbContext&lt;/code&gt; derived class and mark it with &lt;code&gt;DbFunction&lt;/code&gt; attribute. E.g.&lt;/p&gt;&lt;a name='more'&gt;&lt;/a&gt;&lt;pre&gt;&lt;code id="code_4" class="hljs language-csharp"&gt;&lt;span class="hljs-keyword"&gt;public&lt;/span&gt;&lt;span class="hljs-keyword"&gt;class&lt;/span&gt;&lt;span class="hljs-title"&gt;MyDbContext&lt;/span&gt; : &lt;span class="hljs-title"&gt;DbContext&lt;/span&gt;
{
    &lt;span class="hljs-comment"&gt;// ...&lt;/span&gt;

    [&lt;span class="hljs-meta"&gt;DbFunction(&lt;span class="hljs-string"&gt;"SOUNDEX"&lt;/span&gt;)&lt;/span&gt;]
    &lt;span class="hljs-function"&gt;&lt;span class="hljs-keyword"&gt;public&lt;/span&gt;&lt;span class="hljs-keyword"&gt;static&lt;/span&gt;&lt;span class="hljs-built_in"&gt;string&lt;/span&gt;&lt;span class="hljs-title"&gt;Soundex&lt;/span&gt;(&lt;span class="hljs-params"&gt;&lt;span class="hljs-built_in"&gt;string&lt;/span&gt; s&lt;/span&gt;)&lt;/span&gt; =&amp;gt; &lt;span class="hljs-keyword"&gt;throw&lt;/span&gt;&lt;span class="hljs-keyword"&gt;new&lt;/span&gt; Exception();
}
&lt;/code&gt;&lt;button class="copy-btn btn btn-secondary btn-sm" onclick="copyCode(this,'code_4')"&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;and use something like this:&lt;/p&gt;&lt;pre&gt;&lt;code id="code_5" class="hljs language-java"&gt;&lt;span class="hljs-type"&gt;string&lt;/span&gt;&lt;span class="hljs-variable"&gt;param&lt;/span&gt;&lt;span class="hljs-operator"&gt;=&lt;/span&gt; ...;
&lt;span class="hljs-type"&gt;MyDbContext&lt;/span&gt;&lt;span class="hljs-variable"&gt;db&lt;/span&gt;&lt;span class="hljs-operator"&gt;=&lt;/span&gt; ...;
&lt;span class="hljs-type"&gt;var&lt;/span&gt;&lt;span class="hljs-variable"&gt;query&lt;/span&gt;&lt;span class="hljs-operator"&gt;=&lt;/span&gt; db.Customers
    .Where(e =&amp;gt; MyDbContext.Soundex(e.LastName) == MyDbContext.Soundex(param));
&lt;/code&gt;&lt;button class="copy-btn btn btn-secondary btn-sm" onclick="copyCode(this,'code_5')"&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;You can declare such static methods in a different class, but then you need to manually register them using &lt;code&gt;HasDbFunction&lt;/code&gt; fluent API.&lt;/p&gt;&lt;/div&gt;&lt;h2 id="solution_2"&gt;Solution 2:

&lt;/h2&gt;&lt;div class="answer-desc"&gt;&lt;p&gt;EFC 3.0 has changed this process a little, as per &lt;a rel="nofollow noreferrer noopener" href="https://docs.microsoft.com/en-us/ef/core/what-is-new/ef-core-3.0/breaking-changes#udf-empty-string"&gt;https://docs.microsoft.com/en-us/ef/core/what-is-new/ef-core-3.0/breaking-changes#udf-empty-string&lt;/a&gt;&lt;/p&gt;&lt;p&gt;Example of adding CHARINDEX in a partial context class:&lt;/p&gt;&lt;pre&gt;&lt;code id="code_7" class="hljs language-csharp"&gt;&lt;span class="hljs-keyword"&gt;public&lt;/span&gt;&lt;span class="hljs-keyword"&gt;partial&lt;/span&gt;&lt;span class="hljs-keyword"&gt;class&lt;/span&gt;&lt;span class="hljs-title"&gt;MyDbContext&lt;/span&gt;
{
    [&lt;span class="hljs-meta"&gt;DbFunction(&lt;span class="hljs-string"&gt;"CHARINDEX"&lt;/span&gt;)&lt;/span&gt;]
    &lt;span class="hljs-keyword"&gt;public&lt;/span&gt;&lt;span class="hljs-keyword"&gt;static&lt;/span&gt;&lt;span class="hljs-built_in"&gt;int&lt;/span&gt;? CharIndex(&lt;span class="hljs-built_in"&gt;string&lt;/span&gt; toSearch, &lt;span class="hljs-built_in"&gt;string&lt;/span&gt; target) =&amp;gt; &lt;span class="hljs-keyword"&gt;throw&lt;/span&gt;&lt;span class="hljs-keyword"&gt;new&lt;/span&gt; Exception();

    &lt;span class="hljs-function"&gt;&lt;span class="hljs-keyword"&gt;partial&lt;/span&gt;&lt;span class="hljs-keyword"&gt;void&lt;/span&gt;&lt;span class="hljs-title"&gt;OnModelCreatingPartial&lt;/span&gt;(&lt;span class="hljs-params"&gt;
        ModelBuilder modelBuilder&lt;/span&gt;)&lt;/span&gt;
    {
        modelBuilder
            .HasDbFunction(&lt;span class="hljs-keyword"&gt;typeof&lt;/span&gt;(MyDbContext).GetMethod(&lt;span class="hljs-keyword"&gt;nameof&lt;/span&gt;(CharIndex)))
            .HasTranslation(
                args =&amp;gt;
                    SqlFunctionExpression.Create(&lt;span class="hljs-string"&gt;"CHARINDEX"&lt;/span&gt;, args, &lt;span class="hljs-keyword"&gt;typeof&lt;/span&gt;(&lt;span class="hljs-built_in"&gt;int&lt;/span&gt;?), &lt;span class="hljs-literal"&gt;null&lt;/span&gt;));
    }
}
&lt;/code&gt;&lt;button class="copy-btn btn btn-secondary btn-sm" onclick="copyCode(this,'code_7')"&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;/div&gt; 


</description><link>https://rachmatmoradosugiarto.blogspot.com/2024/08/how-do-i-write-effunctions-extension.html</link><author>noreply@blogger.com (Sandy Veum)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8397719027199589165.post-8190932080780326206</guid><pubDate>Wed, 21 Aug 2024 12:35:00 +0000</pubDate><atom:updated>2024-08-21T05:35:40.560-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Django</category><category domain="http://www.blogger.com/atom/ns#">Duration</category><category domain="http://www.blogger.com/atom/ns#">Python</category><category domain="http://www.blogger.com/atom/ns#">Sql</category><category domain="http://www.blogger.com/atom/ns#">Timedelta</category><title>Aggregate In Django With Duration Field</title><description>&lt;div class="question"&gt;My model is: class Mo(Model):     dur = DurationField(default=timedelta(0))      price_per_minute = FloatField(default=0.0)      minutes_int = IntegerField(default=0)  # dublicates&lt;/div&gt;
&lt;h2 id="solution_1"&gt;Solution 1:

&lt;/h2&gt;
&lt;div class="answer-desc"&gt;&lt;p&gt;You can use &lt;code&gt;ExpressionWrapper(F('dur'), output_field=BigIntegerField())&lt;/code&gt; to make Django treat &lt;code&gt;dur&lt;/code&gt; as an integer value.&lt;/p&gt;&lt;/div&gt; 
&lt;br&gt;
</description><link>https://rachmatmoradosugiarto.blogspot.com/2024/08/aggregate-in-django-with-duration-field.html</link><author>noreply@blogger.com (Sandy Veum)</author><thr:total>0</thr:total></item></channel></rss>