<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-5301110832091710857</id><updated>2024-08-21T17:07:22.601-07:00</updated><category term="Sql"/><category term="Sql Server"/><category term="C#"/><category term="Sql Server 2008"/><category term="Postgresql"/><category term="Mysql"/><category term="Database"/><category term="Sqlite"/><category term="Stored Procedures"/><category term=".net"/><category term="Asp.net"/><category term="Oracle"/><category term="Php"/><category term="Pivot"/><category term="Python"/><category term="Ruby On Rails"/><category term="Tsql"/><category term="Android"/><category term="Asp.net Mvc 3"/><category term="Base64"/><category term="Common Table Expression"/><category term="Database Design"/><category term="Date"/><category term="Datetime"/><category term="Ddl"/><category term="Dynamic"/><category term="Ef Code First"/><category term="Entity Framework"/><category term="Error Handling"/><category term="Group By"/><category term="Hana"/><category term="Hana Sql Script"/><category term="Heidisql"/><category term="Heroku"/><category term="Laravel"/><category term="List"/><category term="Login"/><category term="Membership Provider"/><category term="Ms Access"/><category term="Nosql"/><category term="Null"/><category term="Outer Join"/><category term="Permissions"/><category term="Plpgsql"/><category term="Postgresql 8.4"/><category term="Postgresql 9.1"/><category term="Pylons"/><category term="Pyramid"/><category term="Rounding"/><category term="Ruby"/><category term="Sap"/><category term="Scanning"/><category term="Select"/><category term="Sequential"/><category term="Split"/><category term="Sql Insert"/><category term="Sql Server 2005"/><category term="Sql Server 2008 R2"/><category term="Sqlalchemy"/><category term="Sqldatareader"/><category term="String"/><category term="Tobase64string"/><category term="Vb.net"/><category term="Vba"/><category term="Visual Studio 2010"/><category term="Web"/><category term="Web Services"/><category term="Xml"/><category term="Xpath"/><title type='text'>sumitmotwani</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='https://sumitmotwani.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/5301110832091710857/posts/default'/><link rel='alternate' type='text/html' href='https://sumitmotwani.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><link rel='next' type='application/atom+xml' href='https://www.blogger.com/feeds/5301110832091710857/posts/default?start-index=26&amp;max-results=25'/><author><name>Jackie Leffler</name><uri>http://www.blogger.com/profile/07006275599358224644</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>40</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-5301110832091710857.post-2422202250754843283</id><published>2024-08-21T17:06:00.001-07:00</published><updated>2024-08-21T17:06:51.142-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="C#"/><category scheme="http://www.blogger.com/atom/ns#" term="Null"/><category scheme="http://www.blogger.com/atom/ns#" term="Sqldatareader"/><title type='text'>How To Check If A Datareader Is Null Or Empty</title><content type='html'>&lt;div class=&quot;question&quot;&gt;I have a datareader that return a lsit of records from a sql server database. I have a field in the database called &#39;Additional&#39;. This field is 50% of the time empty or null. I am&lt;/div&gt;&lt;h2 id=&quot;solution_1&quot;&gt;Solution 1:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;pre&gt;&lt;code id=&quot;code_0&quot; class=&quot;hljs language-bash&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;/span&gt; (myReader[&lt;span class=&quot;hljs-string&quot;&gt;&quot;Additional&quot;&lt;/span&gt;] != DBNull.Value)
{
    ltlAdditional.Text = &lt;span class=&quot;hljs-string&quot;&gt;&quot;contains data&quot;&lt;/span&gt;;
}
&lt;span class=&quot;hljs-keyword&quot;&gt;else&lt;/span&gt;
{
     ltlAdditional.Text = &lt;span class=&quot;hljs-string&quot;&gt;&quot;is null&quot;&lt;/span&gt;;
}
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_0&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;solution_2&quot;&gt;Solution 2:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;pre&gt;&lt;code id=&quot;code_1&quot; class=&quot;hljs language-cpp&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;/span&gt; (myReader.HasRows) &lt;span class=&quot;hljs-comment&quot;&gt;//The key Word is **.HasRows**&lt;/span&gt;

{

    ltlAdditional.Text = &lt;span class=&quot;hljs-string&quot;&gt;&quot;Contains data&quot;&lt;/span&gt;;

}

&lt;span class=&quot;hljs-keyword&quot;&gt;else&lt;/span&gt;

{   

    ltlAdditional.Text = &lt;span class=&quot;hljs-string&quot;&gt;&quot;Is null Or Empty&quot;&lt;/span&gt;;

}
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_1&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;solution_3&quot;&gt;Solution 3:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;I haven&#39;t used DataReaders for 3+ years, so I wanted to confirm my memory and found this. Anyway, for anyone who happens upon this post like I did and wants a method to test IsDBNull using the column name instead of ordinal number, and you are using VS 2008+ (&amp;amp; .NET 3.5 I think), you can write an extension method so that you can pass the column name in:&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_2&quot; class=&quot;hljs language-csharp&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;public&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;static&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;hljs-title&quot;&gt;DataReaderExtensions&lt;/span&gt;
{
    &lt;span class=&quot;hljs-function&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;public&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;static&lt;/span&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;bool&lt;/span&gt;&lt;span class=&quot;hljs-title&quot;&gt;IsDBNull&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;this&lt;/span&gt; IDataReader dataReader, &lt;span class=&quot;hljs-built_in&quot;&gt;string&lt;/span&gt; columnName &lt;/span&gt;)&lt;/span&gt;
    {
        &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;/span&gt; dataReader[columnName] == DBNull.Value;
    }
}
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_2&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;Kevin&lt;/p&gt;&lt;/div&gt;&lt;h2 id=&quot;solution_4&quot;&gt;Solution 4:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;This is the correct and tested solution&lt;/p&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;pre&gt;&lt;code id=&quot;code_3&quot; class=&quot;hljs language-bash&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;/span&gt; (myReader.Read())
{

    ltlAdditional.Text = &lt;span class=&quot;hljs-string&quot;&gt;&quot;Contains data&quot;&lt;/span&gt;;
}
&lt;span class=&quot;hljs-keyword&quot;&gt;else&lt;/span&gt;
{   
    ltlAdditional.Text = &lt;span class=&quot;hljs-string&quot;&gt;&quot;Is null&quot;&lt;/span&gt;;
}
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_3&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;solution_5&quot;&gt;Solution 5:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;I also use OleDbDataReader.IsDBNull()&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_4&quot; class=&quot;hljs language-bash&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;/span&gt; ( myReader.IsDBNull(colNum) ) { retrievedValue = &lt;span class=&quot;hljs-string&quot;&gt;&quot;&quot;&lt;/span&gt;; }
&lt;span class=&quot;hljs-keyword&quot;&gt;else&lt;/span&gt; { retrievedValue = myReader.GetString(colNum); }
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_4&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;/div&gt; 

</content><link rel='replies' type='application/atom+xml' href='https://sumitmotwani.blogspot.com/feeds/2422202250754843283/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://sumitmotwani.blogspot.com/2024/08/how-to-check-if-datareader-is-null-or.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/5301110832091710857/posts/default/2422202250754843283'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/5301110832091710857/posts/default/2422202250754843283'/><link rel='alternate' type='text/html' href='https://sumitmotwani.blogspot.com/2024/08/how-to-check-if-datareader-is-null-or.html' title='How To Check If A Datareader Is Null Or Empty'/><author><name>Jackie Leffler</name><uri>http://www.blogger.com/profile/07006275599358224644</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5301110832091710857.post-9206911126130200545</id><published>2024-08-21T16:13:00.001-07:00</published><updated>2024-08-21T16:13:29.354-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Sql"/><category scheme="http://www.blogger.com/atom/ns#" term="Sql Server"/><category scheme="http://www.blogger.com/atom/ns#" term="Sql Server 2005"/><category scheme="http://www.blogger.com/atom/ns#" term="Tsql"/><title type='text'>Select Top N Dynamically With N From The Same Select Statement</title><content type='html'>&lt;div class=&quot;question&quot;&gt;I have a procedure that tries to insert samples in a table for each Agent, the # of samples for each agent varies based on some calculation Declare @samplesize int Declare @Top int&lt;/div&gt;&lt;h2 id=&quot;solution_1&quot;&gt;Solution 1:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;You can use &lt;code&gt;row_number()&lt;/code&gt; to do essentially the same thing:&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_1&quot; class=&quot;hljs language-sql&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;WITH&lt;/span&gt; DataToInsert &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt;
(
    &lt;span class=&quot;hljs-keyword&quot;&gt;Select&lt;/span&gt; AgentID, Surveys, LOB,(&lt;span class=&quot;hljs-keyword&quot;&gt;case&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;when&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;day&lt;/span&gt;(getdate())&lt;span class=&quot;hljs-operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;then&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;else&lt;/span&gt; (&lt;span class=&quot;hljs-keyword&quot;&gt;day&lt;/span&gt;(getdate())) &lt;span class=&quot;hljs-operator&quot;&gt;-&lt;/span&gt; (Surveys&lt;span class=&quot;hljs-operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;3&lt;/span&gt;) &lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;) SampleSize &lt;span class=&quot;hljs-keyword&quot;&gt;from&lt;/span&gt; Current_Agent_SurveyCount_HSI &lt;span class=&quot;hljs-keyword&quot;&gt;Where&lt;/span&gt; surveys&lt;span class=&quot;hljs-operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;8&lt;/span&gt; 
)
&lt;span class=&quot;hljs-keyword&quot;&gt;Insert&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;Into&lt;/span&gt; Survey_Source_New (LOB, CenterName, CallDate, AgentZid, TN, Ticket, RecordingID, &lt;span class=&quot;hljs-keyword&quot;&gt;Cycle&lt;/span&gt;, [&lt;span class=&quot;hljs-type&quot;&gt;TimeStamp&lt;/span&gt;])  
    &lt;span class=&quot;hljs-keyword&quot;&gt;select&lt;/span&gt; LOB, CenterName, CallDate, AgentZid, TN, Ticket, RecordingID, &lt;span class=&quot;hljs-keyword&quot;&gt;Cycle&lt;/span&gt;, [&lt;span class=&quot;hljs-type&quot;&gt;TimeStamp&lt;/span&gt;]
    &lt;span class=&quot;hljs-keyword&quot;&gt;from&lt;/span&gt; (&lt;span class=&quot;hljs-keyword&quot;&gt;Select&lt;/span&gt; ss.LOB, CenterName, CallDate, AgentZid, TN, Ticket, RecordingID, &lt;span class=&quot;hljs-keyword&quot;&gt;Cycle&lt;/span&gt;, [&lt;span class=&quot;hljs-type&quot;&gt;TimeStamp&lt;/span&gt;],
                 &lt;span class=&quot;hljs-built_in&quot;&gt;row_number&lt;/span&gt;() &lt;span class=&quot;hljs-keyword&quot;&gt;over&lt;/span&gt; (&lt;span class=&quot;hljs-keyword&quot;&gt;order&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;by&lt;/span&gt; newid()) &lt;span class=&quot;hljs-keyword&quot;&gt;as&lt;/span&gt; seqnum
          &lt;span class=&quot;hljs-keyword&quot;&gt;From&lt;/span&gt; Survey_source_Level1 ss &lt;span class=&quot;hljs-keyword&quot;&gt;inner&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;join&lt;/span&gt;
               DataToInsert du &lt;span class=&quot;hljs-keyword&quot;&gt;on&lt;/span&gt; ss.AgentZID&lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;du.agentID
          &lt;span class=&quot;hljs-keyword&quot;&gt;where&lt;/span&gt; flag &lt;span class=&quot;hljs-keyword&quot;&gt;is&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;and&lt;/span&gt; du.samplesize&lt;span class=&quot;hljs-operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;7&lt;/span&gt;
         ) t
    &lt;span class=&quot;hljs-keyword&quot;&gt;where&lt;/span&gt; seqnum &lt;span class=&quot;hljs-operator&quot;&gt;&amp;lt;=&lt;/span&gt; du.sample_size
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_1&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;You might be able to simplify this a bit, but I don&#39;t know if &lt;code&gt;flag&lt;/code&gt; is coming from du or ss.&lt;/p&gt;&lt;/div&gt;&lt;h2 id=&quot;solution_2&quot;&gt;Solution 2:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;I have an added scenario over the same question.
I made some changes to the existing solution, the scenario is :
I get a list of Agents everyday and have to send each Agent a set of random Surveys, the count depends on some calculation for each agent.&lt;/p&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;pre&gt;&lt;code id=&quot;code_3&quot; class=&quot;hljs language-sql&quot;&gt;&lt;span class=&quot;hljs-comment&quot;&gt;--Get Agent List&lt;/span&gt;
;&lt;span class=&quot;hljs-keyword&quot;&gt;WITH&lt;/span&gt; AgentsList &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt;    
(    
    &lt;span class=&quot;hljs-keyword&quot;&gt;Select&lt;/span&gt; AgentID, Surveys, LOB,(&lt;span class=&quot;hljs-keyword&quot;&gt;case&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;when&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;day&lt;/span&gt;(getdate())&lt;span class=&quot;hljs-operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;then&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;else&lt;/span&gt; (&lt;span class=&quot;hljs-keyword&quot;&gt;day&lt;/span&gt;(getdate())) &lt;span class=&quot;hljs-operator&quot;&gt;-&lt;/span&gt; (Surveys&lt;span class=&quot;hljs-operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;3&lt;/span&gt;) &lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;) SampleSize     
    &lt;span class=&quot;hljs-keyword&quot;&gt;from&lt;/span&gt; Current_Agent_SurveyCount_FIOS &lt;span class=&quot;hljs-keyword&quot;&gt;Where&lt;/span&gt; surveys&lt;span class=&quot;hljs-operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;8&lt;/span&gt;     
)
&lt;span class=&quot;hljs-comment&quot;&gt;--Get All the Surveys for each Agent&lt;/span&gt;
, AgentSurveys &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt;
(
    &lt;span class=&quot;hljs-keyword&quot;&gt;select&lt;/span&gt; ss.LOB, ss.CenterName, ss.CallDate, ss.AgentZid, ss.TN, ss.Ticket, ss.RecordingID, ss.Cycle, ss.[&lt;span class=&quot;hljs-type&quot;&gt;TimeStamp&lt;/span&gt;],ss.Flag,AL.samplesize 
    &lt;span class=&quot;hljs-keyword&quot;&gt;from&lt;/span&gt; Survey_Source_Level1_Sri ss
    &lt;span class=&quot;hljs-keyword&quot;&gt;inner&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;join&lt;/span&gt; AgentsList AL &lt;span class=&quot;hljs-keyword&quot;&gt;on&lt;/span&gt; ss.AgentZID&lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;AL.agentID
    &lt;span class=&quot;hljs-keyword&quot;&gt;where&lt;/span&gt; flag &lt;span class=&quot;hljs-keyword&quot;&gt;is&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;GROUP&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;BY&lt;/span&gt; ss.LOB, ss.CenterName, ss.CallDate, ss.AgentZid, ss.TN, ss.Ticket, ss.RecordingID, ss.Cycle, ss.[&lt;span class=&quot;hljs-type&quot;&gt;TimeStamp&lt;/span&gt;],ss.Flag,AL.samplesize
)
&lt;span class=&quot;hljs-comment&quot;&gt;--Mark random ranking for each survey&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;Select&lt;/span&gt; LOB, CenterName, CallDate, AgentZid, TN, Ticket, RecordingID, &lt;span class=&quot;hljs-keyword&quot;&gt;Cycle&lt;/span&gt;, [&lt;span class=&quot;hljs-type&quot;&gt;TimeStamp&lt;/span&gt;],samplesize,    
             &lt;span class=&quot;hljs-built_in&quot;&gt;rank&lt;/span&gt;() &lt;span class=&quot;hljs-keyword&quot;&gt;over&lt;/span&gt; (&lt;span class=&quot;hljs-keyword&quot;&gt;partition&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;by&lt;/span&gt; agentzid &lt;span class=&quot;hljs-keyword&quot;&gt;order&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;by&lt;/span&gt; newid()) &lt;span class=&quot;hljs-keyword&quot;&gt;as&lt;/span&gt; seqnum    
      &lt;span class=&quot;hljs-keyword&quot;&gt;From&lt;/span&gt; AgentSurveys
      &lt;span class=&quot;hljs-keyword&quot;&gt;group&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;by&lt;/span&gt; LOB, CenterName, CallDate, AgentZid, TN, Ticket, RecordingID, &lt;span class=&quot;hljs-keyword&quot;&gt;Cycle&lt;/span&gt;, [&lt;span class=&quot;hljs-type&quot;&gt;TimeStamp&lt;/span&gt;],samplesize      
      &lt;span class=&quot;hljs-keyword&quot;&gt;order&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;by&lt;/span&gt; agentzid,seqnum,tn
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_3&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;Now from the last query i get a table like&lt;/p&gt;&lt;p&gt;&lt;img alt=&quot;enter image description here&quot; src=&quot;https://i.stack.imgur.com/pe2Se.jpg&quot;&gt;&lt;/p&gt;&lt;p&gt;I require the N number of rows from each group where N comes from SampleSize column.&lt;/p&gt;&lt;/div&gt; 


</content><link rel='replies' type='application/atom+xml' href='https://sumitmotwani.blogspot.com/feeds/9206911126130200545/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://sumitmotwani.blogspot.com/2024/08/select-top-n-dynamically-with-n-from.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/5301110832091710857/posts/default/9206911126130200545'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/5301110832091710857/posts/default/9206911126130200545'/><link rel='alternate' type='text/html' href='https://sumitmotwani.blogspot.com/2024/08/select-top-n-dynamically-with-n-from.html' title='Select Top N Dynamically With N From The Same Select Statement'/><author><name>Jackie Leffler</name><uri>http://www.blogger.com/profile/07006275599358224644</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5301110832091710857.post-3584439709904860492</id><published>2024-08-21T15:03:00.001-07:00</published><updated>2024-08-21T15:03:04.810-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Postgresql"/><category scheme="http://www.blogger.com/atom/ns#" term="Postgresql 9.1"/><category scheme="http://www.blogger.com/atom/ns#" term="Sql"/><title type='text'>Creating A Character Sequence On Postgresql</title><content type='html'>&lt;div class=&quot;question&quot;&gt;So, I have this sequence referenced on a table column where everytime I do an insert its value is defined by nextval(&#39;ptable_pr_codigo_seq&#39;::regclass) CREATE SEQUENCE public.ptable&lt;/div&gt;&lt;h2 id=&quot;solution_1&quot;&gt;Solution 1:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;Challenge accepted ;) &lt;/p&gt;&lt;p&gt;I do not think there is any way to do that with only PostgreSQL sequence mechanism (&lt;a rel=&quot;nofollow noreferrer noopener&quot; href=&quot;https://www.postgresql.org/docs/current/sql-createsequence.html&quot;&gt;1&lt;/a&gt;)
But If you really need something like that (and I am quite interested on why you need something like this), you can do a function that return you the next value that you want and put it into a trigger.&lt;/p&gt;&lt;p&gt;For example, create a table first :&lt;/p&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;pre&gt;&lt;code id=&quot;code_0&quot; class=&quot;hljs language-sql&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;create&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;table&lt;/span&gt; test (test_id &lt;span class=&quot;hljs-type&quot;&gt;varchar&lt;/span&gt;);
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_0&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;Use a function like this one below&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_1&quot; class=&quot;hljs language-sql&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;create&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;or&lt;/span&gt; replace &lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;/span&gt; next_id_test()
 &lt;span class=&quot;hljs-keyword&quot;&gt;returns&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;trigger&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;language&lt;/span&gt; plpgsql &lt;span class=&quot;hljs-keyword&quot;&gt;as&lt;/span&gt; $&lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;/span&gt;$
&lt;span class=&quot;hljs-keyword&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;with&lt;/span&gt; cte_conform_char_list &lt;span class=&quot;hljs-keyword&quot;&gt;as&lt;/span&gt;
    (
        &lt;span class=&quot;hljs-keyword&quot;&gt;select&lt;/span&gt; val, &lt;span class=&quot;hljs-built_in&quot;&gt;row_number&lt;/span&gt;() &lt;span class=&quot;hljs-keyword&quot;&gt;over&lt;/span&gt; (&lt;span class=&quot;hljs-keyword&quot;&gt;order&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;by&lt;/span&gt; val), &lt;span class=&quot;hljs-built_in&quot;&gt;lead&lt;/span&gt;(val) &lt;span class=&quot;hljs-keyword&quot;&gt;over&lt;/span&gt; (&lt;span class=&quot;hljs-keyword&quot;&gt;order&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;by&lt;/span&gt; val)
        &lt;span class=&quot;hljs-keyword&quot;&gt;from&lt;/span&gt; (&lt;span class=&quot;hljs-keyword&quot;&gt;values&lt;/span&gt; (&lt;span class=&quot;hljs-string&quot;&gt;&#39;A&#39;&lt;/span&gt;), (&lt;span class=&quot;hljs-string&quot;&gt;&#39;B&#39;&lt;/span&gt;), (&lt;span class=&quot;hljs-string&quot;&gt;&#39;C&#39;&lt;/span&gt;), (&lt;span class=&quot;hljs-string&quot;&gt;&#39;D&#39;&lt;/span&gt;), (&lt;span class=&quot;hljs-string&quot;&gt;&#39;E&#39;&lt;/span&gt;), (&lt;span class=&quot;hljs-string&quot;&gt;&#39;F&#39;&lt;/span&gt;)) &lt;span class=&quot;hljs-keyword&quot;&gt;as&lt;/span&gt; t(val) &lt;span class=&quot;hljs-comment&quot;&gt;-- you can continue this list as much as you want it ;)&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;order&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;by&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;
    )
    , cte_built_char_list &lt;span class=&quot;hljs-keyword&quot;&gt;as&lt;/span&gt;
    (
        &lt;span class=&quot;hljs-keyword&quot;&gt;select&lt;/span&gt; 
            cte.val
            , cte.row_number
            , &lt;span class=&quot;hljs-built_in&quot;&gt;coalesce&lt;/span&gt;(cte.lead, cte_2.val) &lt;span class=&quot;hljs-keyword&quot;&gt;as&lt;/span&gt; next_char
        &lt;span class=&quot;hljs-keyword&quot;&gt;from&lt;/span&gt; cte_conform_char_list cte
            &lt;span class=&quot;hljs-keyword&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;outer&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;join&lt;/span&gt; cte_conform_char_list cte_2
                &lt;span class=&quot;hljs-keyword&quot;&gt;on&lt;/span&gt; cte_2.row_number &lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt; cte.row_number &lt;span class=&quot;hljs-operator&quot;&gt;-&lt;/span&gt; (&lt;span class=&quot;hljs-keyword&quot;&gt;select&lt;/span&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;max&lt;/span&gt;(row_number) &lt;span class=&quot;hljs-keyword&quot;&gt;from&lt;/span&gt; cte_conform_char_list) &lt;span class=&quot;hljs-operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;
    )
    &lt;span class=&quot;hljs-keyword&quot;&gt;select&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;case&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;when&lt;/span&gt; row_number &lt;span class=&quot;hljs-operator&quot;&gt;&amp;lt;&lt;/span&gt; (&lt;span class=&quot;hljs-keyword&quot;&gt;select&lt;/span&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;max&lt;/span&gt;(row_number) &lt;span class=&quot;hljs-keyword&quot;&gt;from&lt;/span&gt; cte_built_char_list)
                &lt;span class=&quot;hljs-keyword&quot;&gt;then&lt;/span&gt; repeat(next_char, &lt;span class=&quot;hljs-built_in&quot;&gt;cast&lt;/span&gt;(&lt;span class=&quot;hljs-built_in&quot;&gt;rank&lt;/span&gt;() &lt;span class=&quot;hljs-keyword&quot;&gt;over&lt;/span&gt; (&lt;span class=&quot;hljs-keyword&quot;&gt;partition&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;by&lt;/span&gt; row_number &lt;span class=&quot;hljs-keyword&quot;&gt;order&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;by&lt;/span&gt; test_id) &lt;span class=&quot;hljs-keyword&quot;&gt;as&lt;/span&gt;&lt;span class=&quot;hljs-type&quot;&gt;int&lt;/span&gt;)) 
                &lt;span class=&quot;hljs-keyword&quot;&gt;else&lt;/span&gt; repeat(next_char, &lt;span class=&quot;hljs-built_in&quot;&gt;cast&lt;/span&gt;(&lt;span class=&quot;hljs-built_in&quot;&gt;rank&lt;/span&gt;() &lt;span class=&quot;hljs-keyword&quot;&gt;over&lt;/span&gt; (&lt;span class=&quot;hljs-keyword&quot;&gt;partition&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;by&lt;/span&gt; row_number &lt;span class=&quot;hljs-keyword&quot;&gt;order&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;by&lt;/span&gt; test_id) &lt;span class=&quot;hljs-operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;as&lt;/span&gt;&lt;span class=&quot;hljs-type&quot;&gt;int&lt;/span&gt;))
        &lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;as&lt;/span&gt; next_test_id &lt;span class=&quot;hljs-keyword&quot;&gt;into&lt;/span&gt; new.test_id
    &lt;span class=&quot;hljs-keyword&quot;&gt;from&lt;/span&gt; test T
        &lt;span class=&quot;hljs-keyword&quot;&gt;inner&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;join&lt;/span&gt; cte_built_char_list cte &lt;span class=&quot;hljs-keyword&quot;&gt;on&lt;/span&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;substring&lt;/span&gt;(T.test_id &lt;span class=&quot;hljs-keyword&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;) &lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt; cte.val
    &lt;span class=&quot;hljs-keyword&quot;&gt;order&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;by&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;char_length&lt;/span&gt;(test_id), test_id;

    &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;new&lt;/span&gt;;
&lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;;
$&lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;/span&gt;$;
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_1&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;Attach the function to a before trigger&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_2&quot; class=&quot;hljs language-sql&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;create&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;trigger&lt;/span&gt; tg_test before &lt;span class=&quot;hljs-keyword&quot;&gt;insert&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;on&lt;/span&gt; test &lt;span class=&quot;hljs-keyword&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;each&lt;/span&gt;&lt;span class=&quot;hljs-type&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;execute&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;procedure&lt;/span&gt; next_id_test();
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_2&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;Insert a value that doesn&#39;t really matter (it will be changed anyway)&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_3&quot; class=&quot;hljs language-sql&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;insert&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;into&lt;/span&gt; test &lt;span class=&quot;hljs-keyword&quot;&gt;values&lt;/span&gt; (&lt;span class=&quot;hljs-string&quot;&gt;&#39;ttt&#39;&lt;/span&gt;);
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_3&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;Then you can observe you have the right character.&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_4&quot; class=&quot;hljs language-csharp&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;select&lt;/span&gt; *
&lt;span class=&quot;hljs-keyword&quot;&gt;from&lt;/span&gt; test;
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_4&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;I know that&#39;s a little heavy way but I do not see any other one.
The function is probably not perfect but I do not have a lot of time :)&lt;/p&gt;&lt;p&gt;Hope it&#39;s going to help you ;)&lt;/p&gt;&lt;/div&gt; 

</content><link rel='replies' type='application/atom+xml' href='https://sumitmotwani.blogspot.com/feeds/3584439709904860492/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://sumitmotwani.blogspot.com/2024/08/creating-character-sequence-on.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/5301110832091710857/posts/default/3584439709904860492'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/5301110832091710857/posts/default/3584439709904860492'/><link rel='alternate' type='text/html' href='https://sumitmotwani.blogspot.com/2024/08/creating-character-sequence-on.html' title='Creating A Character Sequence On Postgresql'/><author><name>Jackie Leffler</name><uri>http://www.blogger.com/profile/07006275599358224644</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5301110832091710857.post-4043252984991431194</id><published>2024-08-21T14:47:00.001-07:00</published><updated>2024-08-21T14:47:52.236-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Sql"/><category scheme="http://www.blogger.com/atom/ns#" term="Sql Server 2008"/><title type='text'>Violation Of Primary Key Constraint Error Sql</title><content type='html'>&lt;div class=&quot;question&quot;&gt;Here is the code BEGIN TRANSACTION  INSERT INTO [cresql].[dbo].[AR_Transactions]         (DateTime , Dirty, Store_ID, Trans_Type,  Cashier_ID, CustNum, Trans_Amount, Prev_Cust_Bala&lt;/div&gt;&lt;h2 id=&quot;solution_1&quot;&gt;Solution 1:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;By design, your table &lt;code&gt;[cresql].[dbo].[AR_Transactions]&lt;/code&gt; is supposed to hold only one value for the primary key - in your case, this seems to be a combination of columns that make up this PK.&lt;/p&gt;&lt;p&gt;This is not a SQL issue, but seems to be a design concept.&lt;/p&gt;&lt;p&gt;You should find out what combination of keys is making up your primary key - it may be that the design states that you can insert once and update later on for that combination (although I would not consider this to be good design).&lt;/p&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;p&gt;&lt;strong&gt;EDIT&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;Since Trans_Id is the PK, and you are not using that column in your insert, it is either being inserted as IDENTITY, as a DEFAULT (brrrrr), or using a TRIGGER.&lt;/p&gt;&lt;p&gt;&lt;strong&gt;IDENTITY:&lt;/strong&gt; Check to see if the IDENTITY specification is screwed up. If so, just reset it. Here is how to &lt;a rel=&quot;nofollow noreferrer noopener&quot; href=&quot;https://stackoverflow.com/questions/147210/how-can-i-reseed-an-identity-column-in-a-t-sql-table-variable&quot;&gt;How can I reseed an identity column in a T-SQL table variable?&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;DEFAULT:&lt;/strong&gt; This is an odd choice, and you may have to ask the one who designed the database, read documentation, or just simply figure it out on your own. In any case, this is a very uncommon choice.&lt;/p&gt;&lt;p&gt;&lt;strong&gt;TRIGGER:&lt;/strong&gt; Find the trigger and read through the code to see what it is doing. Here is how to find a list of triggers &lt;a rel=&quot;nofollow noreferrer noopener&quot; href=&quot;https://stackoverflow.com/questions/636452/what-is-the-best-way-to-check-whether-a-trigger-exists-in-sql-server&quot;&gt;What is the most portable way to check whether a trigger exists in SQL Server?&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Alternate scenario:&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;There could be a trigger that inserts into an AUDIT table that may have the problem. Check if the table has any other triggers and see what they do.&lt;/p&gt;&lt;/div&gt; 

</content><link rel='replies' type='application/atom+xml' href='https://sumitmotwani.blogspot.com/feeds/4043252984991431194/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://sumitmotwani.blogspot.com/2024/08/violation-of-primary-key-constraint.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/5301110832091710857/posts/default/4043252984991431194'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/5301110832091710857/posts/default/4043252984991431194'/><link rel='alternate' type='text/html' href='https://sumitmotwani.blogspot.com/2024/08/violation-of-primary-key-constraint.html' title='Violation Of Primary Key Constraint Error Sql'/><author><name>Jackie Leffler</name><uri>http://www.blogger.com/profile/07006275599358224644</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5301110832091710857.post-7336663060973359848</id><published>2024-08-21T14:12:00.001-07:00</published><updated>2024-08-21T14:12:30.398-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Oracle"/><category scheme="http://www.blogger.com/atom/ns#" term="Outer Join"/><category scheme="http://www.blogger.com/atom/ns#" term="Sql"/><title type='text'>Oracle Outer Join Syntax</title><content type='html'>&lt;div class=&quot;question&quot;&gt;I have a query that looks like this: select * from find fsc,         let lf,         cust cus,         STRIKE ist WHERE   fsc.id = lf.id AND     ist.ID_old = fsc.ID_old AND     lf.&lt;/div&gt;&lt;h2 id=&quot;solution_1&quot;&gt;Solution 1:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;I believe you want this:&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_0&quot; class=&quot;hljs language-csharp&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;select&lt;/span&gt; *
&lt;span class=&quot;hljs-keyword&quot;&gt;from&lt;/span&gt; find fsc &lt;span class=&quot;hljs-keyword&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;let&lt;/span&gt; lf
     &lt;span class=&quot;hljs-keyword&quot;&gt;on&lt;/span&gt; fsc.id = lf.id &lt;span class=&quot;hljs-keyword&quot;&gt;join&lt;/span&gt;
     STRIKE ist
     &lt;span class=&quot;hljs-keyword&quot;&gt;on&lt;/span&gt; ist.ID_old = fsc.ID_old left &lt;span class=&quot;hljs-keyword&quot;&gt;join&lt;/span&gt;
     cust cus
     &lt;span class=&quot;hljs-keyword&quot;&gt;on&lt;/span&gt; lf.cust_id = cus.cust_id;
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_0&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;To be honest, the outer join is probably not necessary. Why would &lt;code&gt;lf&lt;/code&gt; have a &lt;code&gt;cust_id&lt;/code&gt; that is not valid? The only reasonable possibility is if the value is &lt;code&gt;NULL&lt;/code&gt;.&lt;/p&gt;&lt;/div&gt;&lt;/p&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;</content><link rel='replies' type='application/atom+xml' href='https://sumitmotwani.blogspot.com/feeds/7336663060973359848/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://sumitmotwani.blogspot.com/2024/08/oracle-outer-join-syntax.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/5301110832091710857/posts/default/7336663060973359848'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/5301110832091710857/posts/default/7336663060973359848'/><link rel='alternate' type='text/html' href='https://sumitmotwani.blogspot.com/2024/08/oracle-outer-join-syntax.html' title='Oracle Outer Join Syntax'/><author><name>Jackie Leffler</name><uri>http://www.blogger.com/profile/07006275599358224644</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5301110832091710857.post-6415495412522123490</id><published>2024-08-21T12:58:00.001-07:00</published><updated>2024-08-21T12:58:46.817-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Common Table Expression"/><category scheme="http://www.blogger.com/atom/ns#" term="Hana"/><category scheme="http://www.blogger.com/atom/ns#" term="Hana Sql Script"/><category scheme="http://www.blogger.com/atom/ns#" term="Sap"/><category scheme="http://www.blogger.com/atom/ns#" term="Sql Insert"/><title type='text'>Does Sqlscript For Sap Hana Support The Use Of Insert With Ctes (common Table Expressions)?</title><content type='html'>&lt;div class=&quot;question&quot;&gt;I know this isn&#39;t a specific bit of code or problem, but I am having trouble with a very similar issue to the person asking this (except theirs is for SQL Server): Combining INSERT&lt;/div&gt;&lt;h2 id=&quot;solution_1&quot;&gt;Solution 1:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot; style=&quot;height: auto !important;&quot;&gt;&lt;p&gt;&lt;strong&gt;Short answer:&lt;/strong&gt;
No, CTEs are not supported for INSERT/UPDATE statements.&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Longer answer:&lt;/strong&gt;
SQLScript&#39;s INSERT/UPDATE commands are actually &quot;borrowed&quot; SQL commands as the &lt;a rel=&quot;nofollow noreferrer noopener&quot; href=&quot;https://help.sap.com/viewer/de2486ee947e43e684d39702027f8a94/2.0.02/en-US/7a2da744ce544db1814a5fff250e99f6.html&quot;&gt;documentation explains&lt;/a&gt;. &lt;/p&gt;&lt;p&gt;Checking the documentation for SQL INSERT we find that it &lt;a rel=&quot;nofollow noreferrer noopener&quot; href=&quot;https://help.sap.com/viewer/4fe29514fd584807ac9f2a04f6754767/2.0.02/en-US/20f7f70975191014a76da70c9181720e.html&quot;&gt;supports a subquery&lt;/a&gt; as a source of values. &lt;/p&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;p&gt;The subquery term is defined as part of the SQL SELECT statement. Checking the &lt;a rel=&quot;nofollow noreferrer noopener&quot; href=&quot;https://help.sap.com/viewer/4fe29514fd584807ac9f2a04f6754767/2.0.02/en-US/20fcf24075191014a89e9dc7b8408b26.html&quot;&gt;documentation for SELECT&lt;/a&gt; shows that &lt;code&gt;&amp;lt;subquery&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;with_clause&amp;gt;&lt;/code&gt; are different, non-overlapping terms. &lt;/p&gt;&lt;p&gt;This means, that CTEs cannot be used in subqueries and therefore not be part of the subqueries used in INSERT/UPDATE commands.&lt;/p&gt;&lt;p&gt;You can, however, use SQLScript &lt;strong&gt;table variables&lt;/strong&gt; in INSERT statements in your SQLScript blocks, which is very similar to CTEs:&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_2&quot; class=&quot;hljs language-sql&quot;&gt;DO &lt;span class=&quot;hljs-keyword&quot;&gt;BEGIN&lt;/span&gt;
  te_a :&lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;SELECT&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;10&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;xyz&#39;&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;as&lt;/span&gt; VAL &lt;span class=&quot;hljs-keyword&quot;&gt;from&lt;/span&gt; dummy;
  te_b :&lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;SELECT&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;20&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;abc&#39;&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;as&lt;/span&gt; VAL &lt;span class=&quot;hljs-keyword&quot;&gt;from&lt;/span&gt; dummy;

  te_all :&lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;SELECT&lt;/span&gt;&lt;span class=&quot;hljs-operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;from&lt;/span&gt; :te_a
      &lt;span class=&quot;hljs-keyword&quot;&gt;UNION&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;ALL&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;SELECT&lt;/span&gt;&lt;span class=&quot;hljs-operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;from&lt;/span&gt; :te_b;

  &lt;span class=&quot;hljs-keyword&quot;&gt;INSERT&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;INTO&lt;/span&gt; VALS 
             (&lt;span class=&quot;hljs-keyword&quot;&gt;SELECT&lt;/span&gt;&lt;span class=&quot;hljs-operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;from&lt;/span&gt; :te_all);

&lt;span class=&quot;hljs-keyword&quot;&gt;END&lt;/span&gt;;
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_2&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;solution_2&quot;&gt;Solution 2:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;You can convert the CTE into a Sub-Select statement in many cases&lt;/p&gt;&lt;p&gt;You can use following&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_3&quot; class=&quot;hljs language-sql&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;insert&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;into&lt;/span&gt; city (city, countryid, citycode)
&lt;span class=&quot;hljs-keyword&quot;&gt;select&lt;/span&gt;
    city, countryid, citycode
&lt;span class=&quot;hljs-keyword&quot;&gt;from&lt;/span&gt; (
    &lt;span class=&quot;hljs-comment&quot;&gt;-- CTE Expression as subselect&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;select&lt;/span&gt;&lt;span class=&quot;hljs-operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;from&lt;/span&gt; city
    &lt;span class=&quot;hljs-comment&quot;&gt;-- end (CTE)&lt;/span&gt;
) cte
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_3&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;Instead of using following valid CTE command combined with INSERT (on SQL Server)&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_4&quot; class=&quot;hljs language-sql&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;with&lt;/span&gt; cte &lt;span class=&quot;hljs-keyword&quot;&gt;as&lt;/span&gt; (
    &lt;span class=&quot;hljs-keyword&quot;&gt;select&lt;/span&gt;&lt;span class=&quot;hljs-operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;from&lt;/span&gt; city
)
&lt;span class=&quot;hljs-keyword&quot;&gt;insert&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;into&lt;/span&gt; city (city, countryid, citycode)
&lt;span class=&quot;hljs-keyword&quot;&gt;select&lt;/span&gt;
    city, countryid, citycode
&lt;span class=&quot;hljs-keyword&quot;&gt;from&lt;/span&gt; cte
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_4&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;solution_3&quot;&gt;Solution 3:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;SAP HANA includes this posibility, the order of the code is different than SQL Server:&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_5&quot; class=&quot;hljs language-vbnet&quot;&gt;INSERT &lt;span class=&quot;hljs-keyword&quot;&gt;INTO&lt;/span&gt; EXAMPLE (ID)
 &lt;span class=&quot;hljs-keyword&quot;&gt;WITH&lt;/span&gt; cte1 &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; (&lt;span class=&quot;hljs-keyword&quot;&gt;SELECT&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; ID &lt;span class=&quot;hljs-keyword&quot;&gt;FROM&lt;/span&gt; DUMMY)
 &lt;span class=&quot;hljs-keyword&quot;&gt;SELECT&lt;/span&gt; ID &lt;span class=&quot;hljs-keyword&quot;&gt;FROM&lt;/span&gt; cte1;
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_5&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;/div&gt; 

</content><link rel='replies' type='application/atom+xml' href='https://sumitmotwani.blogspot.com/feeds/6415495412522123490/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://sumitmotwani.blogspot.com/2024/08/does-sqlscript-for-sap-hana-support-use.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/5301110832091710857/posts/default/6415495412522123490'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/5301110832091710857/posts/default/6415495412522123490'/><link rel='alternate' type='text/html' href='https://sumitmotwani.blogspot.com/2024/08/does-sqlscript-for-sap-hana-support-use.html' title='Does Sqlscript For Sap Hana Support The Use Of Insert With Ctes (common Table Expressions)?'/><author><name>Jackie Leffler</name><uri>http://www.blogger.com/profile/07006275599358224644</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5301110832091710857.post-2543133340320145192</id><published>2024-08-21T12:19:00.001-07:00</published><updated>2024-08-21T12:19:43.457-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="C#"/><category scheme="http://www.blogger.com/atom/ns#" term="Sql"/><category scheme="http://www.blogger.com/atom/ns#" term="Sql Server 2008"/><category scheme="http://www.blogger.com/atom/ns#" term="Web"/><category scheme="http://www.blogger.com/atom/ns#" term="Web Services"/><title type='text'>Sql Select With Stored Procedure And Parameters?</title><content type='html'>&lt;div class=&quot;question&quot;&gt;I&#39;ve been writing a lot of web services with SQL inserts based on a stored procedure, and I haven&#39;t really worked with any SELECTS.   The one SELECT I have in mind is very simple.&lt;/div&gt;&lt;h2 id=&quot;solution_1&quot;&gt;Solution 1:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;Here is where you are going wrong:&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_0&quot; class=&quot;hljs language-scss&quot;&gt;cmd&lt;span class=&quot;hljs-selector-class&quot;&gt;.ExecuteNonQuery&lt;/span&gt;();
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_0&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;You &lt;em&gt;are&lt;/em&gt; executing a query.&lt;/p&gt;&lt;p&gt;You need to &lt;a rel=&quot;nofollow noreferrer noopener&quot; href=&quot;https://msdn.microsoft.com/en-us/library/8acxyzc3.aspx&quot;&gt;&lt;code&gt;ExecuteReader&lt;/code&gt;&lt;/a&gt; or &lt;a rel=&quot;nofollow noreferrer noopener&quot; href=&quot;https://msdn.microsoft.com/en-us/library/system.data.oledb.oledbcommand.executescalar.aspx&quot;&gt;&lt;code&gt;ExecuteScalar&lt;/code&gt;&lt;/a&gt; instead. &lt;code&gt;ExecuteReader&lt;/code&gt; is used for a result set (several rows/columns), &lt;code&gt;ExecuteScalar&lt;/code&gt; when the query returns a single result (it returns &lt;code&gt;object&lt;/code&gt;, so the result needs to be cast to the correct type). &lt;/p&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;pre&gt;&lt;code id=&quot;code_6&quot; class=&quot;hljs language-csharp&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;var&lt;/span&gt; result = (&lt;span class=&quot;hljs-built_in&quot;&gt;int&lt;/span&gt;)cmd.ExecuteScalar();
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_6&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;The &lt;code&gt;results&lt;/code&gt; variable will now hold a &lt;a rel=&quot;nofollow noreferrer noopener&quot; href=&quot;https://msdn.microsoft.com/en-us/library/system.data.oledb.oledbdatareader.aspx&quot;&gt;&lt;code&gt;OledbDataReader&lt;/code&gt;&lt;/a&gt; or a value with the results of the &lt;code&gt;SELECT&lt;/code&gt;. You can iterate over the results (for a reader), or the scalar value (for a scalar).&lt;/p&gt;&lt;/div&gt;&lt;h2 id=&quot;solution_2&quot;&gt;Solution 2:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;Since you are only after a single value, you can use cmd.ExecuteScalar();&lt;/p&gt;&lt;p&gt;A complete example is as follows:&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_10&quot; class=&quot;hljs language-csharp&quot;&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;string&lt;/span&gt; ConnString = &lt;span class=&quot;hljs-string&quot;&gt;&quot;Data Source=Removed&quot;&lt;/span&gt;; 
    &lt;span class=&quot;hljs-built_in&quot;&gt;string&lt;/span&gt; userSid = &lt;span class=&quot;hljs-string&quot;&gt;&quot;SomeSid&quot;&lt;/span&gt;;
    &lt;span class=&quot;hljs-built_in&quot;&gt;string&lt;/span&gt; SqlString = &lt;span class=&quot;hljs-string&quot;&gt;&quot;SELECT COUNT(AD_SID) As ReturnCount FROM AD_Authorization WHERE AD_SID = @userSID;&quot;&lt;/span&gt;; 
    &lt;span class=&quot;hljs-built_in&quot;&gt;int&lt;/span&gt; returnCount = &lt;span class=&quot;hljs-number&quot;&gt;0&lt;/span&gt;;
    &lt;span class=&quot;hljs-keyword&quot;&gt;using&lt;/span&gt; (OleDbConnection conn = &lt;span class=&quot;hljs-keyword&quot;&gt;new&lt;/span&gt; OleDbConnection(ConnString)) 
    { 
        &lt;span class=&quot;hljs-keyword&quot;&gt;using&lt;/span&gt; (OleDbCommand cmd = &lt;span class=&quot;hljs-keyword&quot;&gt;new&lt;/span&gt; OleDbCommand(SqlString, conn)) 
        { 
            cmd.CommandType = CommandType.Text; 
            cmd.Parameters.AddWithValue(&lt;span class=&quot;hljs-string&quot;&gt;&quot;@userSID&quot;&lt;/span&gt;, userSid); 
            conn.Open(); 
            returnCount = Convert.ToInt32(cmd.ExecuteScalar()); 
        } 
    } 
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_10&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;If you wanted to return MULTIPLE rows, you can use the ExecuteReader() method. This returns an IDataReader via which you can enumerate the result set row by row.&lt;/p&gt;&lt;/div&gt;&lt;h2 id=&quot;solution_3&quot;&gt;Solution 3:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;You need to use &lt;a rel=&quot;nofollow noreferrer noopener&quot; href=&quot;https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executescalar.aspx&quot;&gt;&lt;code&gt;ExecuteScalar&lt;/code&gt;&lt;/a&gt; instead of &lt;code&gt;ExecuteNonQuery&lt;/code&gt;:&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_13&quot; class=&quot;hljs language-java&quot;&gt;&lt;span class=&quot;hljs-type&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;hljs-variable&quot;&gt;query&lt;/span&gt;&lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;&quot;SELECT COUNT(AD_SID) As ReturnCount FROM AD_Authorization WHERE AD_SID = @userSID &quot;&lt;/span&gt;;
using (&lt;span class=&quot;hljs-type&quot;&gt;OleDbConnection&lt;/span&gt;&lt;span class=&quot;hljs-variable&quot;&gt;conn&lt;/span&gt;&lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;hljs-title class_&quot;&gt;OleDbConnection&lt;/span&gt;(ConnString)) {
    using (&lt;span class=&quot;hljs-type&quot;&gt;OleDbCommand&lt;/span&gt;&lt;span class=&quot;hljs-variable&quot;&gt;cmd&lt;/span&gt;&lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;hljs-title class_&quot;&gt;OleDbCommand&lt;/span&gt;(query, conn))
    {
        cmd.Parameters.AddWithValue(&lt;span class=&quot;hljs-string&quot;&gt;&quot;userSID&quot;&lt;/span&gt;, userSID.ToString());
        conn.Open();
        &lt;span class=&quot;hljs-type&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;hljs-variable&quot;&gt;returnCount&lt;/span&gt;&lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt; (Int32) cmd.ExecuteScalar();
        conn.Close();
    }
}
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_13&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;solution_4&quot;&gt;Solution 4:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;cmd.executescalar will return a single value, such as your count.&lt;/p&gt;&lt;p&gt;You would use cmd.executereader when you are returning a list of records&lt;/p&gt;&lt;/div&gt; 

</content><link rel='replies' type='application/atom+xml' href='https://sumitmotwani.blogspot.com/feeds/2543133340320145192/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://sumitmotwani.blogspot.com/2024/08/sql-select-with-stored-procedure-and.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/5301110832091710857/posts/default/2543133340320145192'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/5301110832091710857/posts/default/2543133340320145192'/><link rel='alternate' type='text/html' href='https://sumitmotwani.blogspot.com/2024/08/sql-select-with-stored-procedure-and.html' title='Sql Select With Stored Procedure And Parameters?'/><author><name>Jackie Leffler</name><uri>http://www.blogger.com/profile/07006275599358224644</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5301110832091710857.post-6232853971029518595</id><published>2024-08-21T11:40:00.001-07:00</published><updated>2024-08-21T11:40:24.055-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Sql Server"/><category scheme="http://www.blogger.com/atom/ns#" term="Stored Procedures"/><title type='text'>Stored Procedure To Find Free Slots With Different Work Times Per Day Of The Week</title><content type='html'>&lt;div class=&quot;question&quot;&gt;I am using the awesome stored procedure created by Aaron Bertrand here as a start since it almost does what I need. Sadly my SQL skills are not as good as his so I need some help h&lt;/div&gt;&lt;h2 id=&quot;solution_1&quot;&gt;Solution 1:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;Ok, this answer builds on the last one, we&#39;re going to add the lunch intervals and the events and going to look at a date range for events.&lt;/p&gt;&lt;p&gt;First lets make some test data:&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_0&quot; class=&quot;hljs language-csharp&quot;&gt;DECLARE @event TABLE
(
    [&lt;span class=&quot;hljs-meta&quot;&gt;event_id&lt;/span&gt;] [&lt;span class=&quot;hljs-built_in&quot;&gt;int&lt;/span&gt;] IDENTITY(&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;,&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;),
    [&lt;span class=&quot;hljs-meta&quot;&gt;event_start&lt;/span&gt;] [datetime],
    [&lt;span class=&quot;hljs-meta&quot;&gt;event_end&lt;/span&gt;] [datetime],
    [&lt;span class=&quot;hljs-meta&quot;&gt;UserId&lt;/span&gt;] [&lt;span class=&quot;hljs-built_in&quot;&gt;int&lt;/span&gt;]
)

INSERT INTO @event
( event_start, event_end, UserId )
VALUES
(&lt;span class=&quot;hljs-string&quot;&gt;&#39;12/01/2014 09:00:00&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;12/01/2014 09:30:00&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;),
(&lt;span class=&quot;hljs-string&quot;&gt;&#39;12/01/2014 09:30:00&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;12/01/2014 10:30:00&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;),
(&lt;span class=&quot;hljs-string&quot;&gt;&#39;12/01/2014 11:00:00&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;12/01/2014 12:00:00&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;),
(&lt;span class=&quot;hljs-string&quot;&gt;&#39;12/01/2014 13:00:00&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;12/01/2014 16:30:00&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;);

DECLARE @Users TABLE
(
    [&lt;span class=&quot;hljs-meta&quot;&gt;UserID&lt;/span&gt;] [&lt;span class=&quot;hljs-built_in&quot;&gt;int&lt;/span&gt;],
    [&lt;span class=&quot;hljs-meta&quot;&gt;Username&lt;/span&gt;] [varchar](&lt;span class=&quot;hljs-number&quot;&gt;32&lt;/span&gt;) NULL,
    [&lt;span class=&quot;hljs-meta&quot;&gt;Sunday&lt;/span&gt;] [bit] NULL,
    [&lt;span class=&quot;hljs-meta&quot;&gt;Monday&lt;/span&gt;] [bit] NULL,
    [&lt;span class=&quot;hljs-meta&quot;&gt;Tuesday&lt;/span&gt;] [bit] NULL,
    [&lt;span class=&quot;hljs-meta&quot;&gt;Wednesday&lt;/span&gt;] [bit] NULL,
    [&lt;span class=&quot;hljs-meta&quot;&gt;Thursday&lt;/span&gt;] [bit] NULL,
    [&lt;span class=&quot;hljs-meta&quot;&gt;Friday&lt;/span&gt;] [bit] NULL,
    [&lt;span class=&quot;hljs-meta&quot;&gt;Saturday&lt;/span&gt;] [bit] NULL,
    [&lt;span class=&quot;hljs-meta&quot;&gt;WorkStartSunday&lt;/span&gt;] [time](&lt;span class=&quot;hljs-number&quot;&gt;7&lt;/span&gt;) NULL,
    [&lt;span class=&quot;hljs-meta&quot;&gt;WorkEndSunday&lt;/span&gt;] [time](&lt;span class=&quot;hljs-number&quot;&gt;7&lt;/span&gt;) NULL,
    [&lt;span class=&quot;hljs-meta&quot;&gt;IntervalStartSunday&lt;/span&gt;] [time](&lt;span class=&quot;hljs-number&quot;&gt;7&lt;/span&gt;) NULL,
    [&lt;span class=&quot;hljs-meta&quot;&gt;IntervalEndSunday&lt;/span&gt;] [time](&lt;span class=&quot;hljs-number&quot;&gt;7&lt;/span&gt;) NULL,
    [&lt;span class=&quot;hljs-meta&quot;&gt;WorkStartMonday&lt;/span&gt;] [time](&lt;span class=&quot;hljs-number&quot;&gt;7&lt;/span&gt;) NULL,
    [&lt;span class=&quot;hljs-meta&quot;&gt;WorkEndMonday&lt;/span&gt;] [time](&lt;span class=&quot;hljs-number&quot;&gt;7&lt;/span&gt;) NULL,
    [&lt;span class=&quot;hljs-meta&quot;&gt;IntervalStartMonday&lt;/span&gt;] [time](&lt;span class=&quot;hljs-number&quot;&gt;7&lt;/span&gt;) NULL,
    [&lt;span class=&quot;hljs-meta&quot;&gt;IntervalEndMonday&lt;/span&gt;] [time](&lt;span class=&quot;hljs-number&quot;&gt;7&lt;/span&gt;) NULL,
    [&lt;span class=&quot;hljs-meta&quot;&gt;WorkStartTuesday&lt;/span&gt;] [time](&lt;span class=&quot;hljs-number&quot;&gt;7&lt;/span&gt;) NULL,
    [&lt;span class=&quot;hljs-meta&quot;&gt;WorkEndTuesday&lt;/span&gt;] [time](&lt;span class=&quot;hljs-number&quot;&gt;7&lt;/span&gt;) NULL,
    [&lt;span class=&quot;hljs-meta&quot;&gt;IntervalStartTuesday&lt;/span&gt;] [time](&lt;span class=&quot;hljs-number&quot;&gt;7&lt;/span&gt;) NULL,
    [&lt;span class=&quot;hljs-meta&quot;&gt;IntervalEndTuesday&lt;/span&gt;] [time](&lt;span class=&quot;hljs-number&quot;&gt;7&lt;/span&gt;) NULL,
    [&lt;span class=&quot;hljs-meta&quot;&gt;WorkStartWednesday&lt;/span&gt;] [time](&lt;span class=&quot;hljs-number&quot;&gt;7&lt;/span&gt;) NULL,
    [&lt;span class=&quot;hljs-meta&quot;&gt;WorkEndWednesday&lt;/span&gt;] [time](&lt;span class=&quot;hljs-number&quot;&gt;7&lt;/span&gt;) NULL,
    [&lt;span class=&quot;hljs-meta&quot;&gt;IntervalStartWednesday&lt;/span&gt;] [time](&lt;span class=&quot;hljs-number&quot;&gt;7&lt;/span&gt;) NULL,
    [&lt;span class=&quot;hljs-meta&quot;&gt;IntervalEndWednesday&lt;/span&gt;] [time](&lt;span class=&quot;hljs-number&quot;&gt;7&lt;/span&gt;) NULL,
    [&lt;span class=&quot;hljs-meta&quot;&gt;WorkStartThursday&lt;/span&gt;] [time](&lt;span class=&quot;hljs-number&quot;&gt;7&lt;/span&gt;) NULL,
    [&lt;span class=&quot;hljs-meta&quot;&gt;WorkEndThursday&lt;/span&gt;] [time](&lt;span class=&quot;hljs-number&quot;&gt;7&lt;/span&gt;) NULL,
    [&lt;span class=&quot;hljs-meta&quot;&gt;IntervalStartThursday&lt;/span&gt;] [time](&lt;span class=&quot;hljs-number&quot;&gt;7&lt;/span&gt;) NULL,
    [&lt;span class=&quot;hljs-meta&quot;&gt;IntervalEndThursday&lt;/span&gt;] [time](&lt;span class=&quot;hljs-number&quot;&gt;7&lt;/span&gt;) NULL,
    [&lt;span class=&quot;hljs-meta&quot;&gt;WorkStartFriday&lt;/span&gt;] [time](&lt;span class=&quot;hljs-number&quot;&gt;7&lt;/span&gt;) NULL,
    [&lt;span class=&quot;hljs-meta&quot;&gt;WorkEndFriday&lt;/span&gt;] [time](&lt;span class=&quot;hljs-number&quot;&gt;7&lt;/span&gt;) NULL,
    [&lt;span class=&quot;hljs-meta&quot;&gt;IntervalStartFriday&lt;/span&gt;] [time](&lt;span class=&quot;hljs-number&quot;&gt;7&lt;/span&gt;) NULL,
    [&lt;span class=&quot;hljs-meta&quot;&gt;IntervalEndFriday&lt;/span&gt;] [time](&lt;span class=&quot;hljs-number&quot;&gt;7&lt;/span&gt;) NULL,
    [&lt;span class=&quot;hljs-meta&quot;&gt;WorkStartSaturday&lt;/span&gt;] [time](&lt;span class=&quot;hljs-number&quot;&gt;7&lt;/span&gt;) NULL,
    [&lt;span class=&quot;hljs-meta&quot;&gt;WorkEndSaturday&lt;/span&gt;] [time](&lt;span class=&quot;hljs-number&quot;&gt;7&lt;/span&gt;) NULL,
    [&lt;span class=&quot;hljs-meta&quot;&gt;IntervalStartSaturday&lt;/span&gt;] [time](&lt;span class=&quot;hljs-number&quot;&gt;7&lt;/span&gt;) NULL,
    [&lt;span class=&quot;hljs-meta&quot;&gt;IntervalEndSaturday&lt;/span&gt;] [time](&lt;span class=&quot;hljs-number&quot;&gt;7&lt;/span&gt;) NULL
);

INSERT INTO @Users 
([UserID], [Username], [Sunday], [Monday], [Tuesday], [Wednesday], [Thursday], [Friday], [Saturday], [WorkStartSunday], [WorkEndSunday], [IntervalStartSunday], [IntervalEndSunday], [WorkStartMonday], [WorkEndMonday], [IntervalStartMonday], [IntervalEndMonday], [WorkStartTuesday], [WorkEndTuesday], [IntervalStartTuesday], [IntervalEndTuesday], [WorkStartWednesday], [WorkEndWednesday], [IntervalStartWednesday], [IntervalEndWednesday], [WorkStartThursday], [WorkEndThursday], [IntervalStartThursday], [IntervalEndThursday], [WorkStartFriday], [WorkEndFriday], [IntervalStartFriday], [IntervalEndFriday], [WorkStartSaturday], [WorkEndSaturday], [IntervalStartSaturday], [IntervalEndSaturday]) 
VALUES 
(&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;User A&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-number&quot;&gt;0&lt;/span&gt;, &lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;, &lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;, &lt;span class=&quot;hljs-number&quot;&gt;0&lt;/span&gt;, &lt;span class=&quot;hljs-number&quot;&gt;0&lt;/span&gt;, &lt;span class=&quot;hljs-number&quot;&gt;0&lt;/span&gt;, &lt;span class=&quot;hljs-number&quot;&gt;0&lt;/span&gt;, NULL, NULL, NULL, NULL, CAST(&lt;span class=&quot;hljs-string&quot;&gt;&#39;08:00:00&#39;&lt;/span&gt; AS Time), CAST(&lt;span class=&quot;hljs-string&quot;&gt;&#39;18:00:00&#39;&lt;/span&gt; AS Time), CAST(&lt;span class=&quot;hljs-string&quot;&gt;&#39;12:00:00&#39;&lt;/span&gt; AS Time), CAST(&lt;span class=&quot;hljs-string&quot;&gt;&#39;13:00:00&#39;&lt;/span&gt; AS Time), CAST(&lt;span class=&quot;hljs-string&quot;&gt;&#39;10:00:00&#39;&lt;/span&gt; AS Time), CAST(&lt;span class=&quot;hljs-string&quot;&gt;&#39;15:00:00&#39;&lt;/span&gt; AS Time), NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL)
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_0&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;Once again, I am going to create a numbers table, now lets set our data range, and I am also going to generate time slots based on a 30 minute duration.&lt;/p&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;pre&gt;&lt;code id=&quot;code_1&quot; class=&quot;hljs language-sql&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;DECLARE&lt;/span&gt;&lt;span class=&quot;hljs-variable&quot;&gt;@yearstart&lt;/span&gt; DATETIME &lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;&#39;01/01/2014&#39;&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;DECLARE&lt;/span&gt;&lt;span class=&quot;hljs-variable&quot;&gt;@duration&lt;/span&gt;_minutes &lt;span class=&quot;hljs-type&quot;&gt;INT&lt;/span&gt;&lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;DECLARE&lt;/span&gt;&lt;span class=&quot;hljs-variable&quot;&gt;@number&lt;/span&gt;_of_numbers &lt;span class=&quot;hljs-type&quot;&gt;INT&lt;/span&gt;&lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;100000&lt;/span&gt;;
;&lt;span class=&quot;hljs-keyword&quot;&gt;WITH&lt;/span&gt;
    a &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; (&lt;span class=&quot;hljs-keyword&quot;&gt;SELECT&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; i &lt;span class=&quot;hljs-keyword&quot;&gt;UNION&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;ALL&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;SELECT&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;),
    b &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; (&lt;span class=&quot;hljs-keyword&quot;&gt;SELECT&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; i &lt;span class=&quot;hljs-keyword&quot;&gt;FROM&lt;/span&gt; a &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; x, a &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; y),
    c &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; (&lt;span class=&quot;hljs-keyword&quot;&gt;SELECT&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; i &lt;span class=&quot;hljs-keyword&quot;&gt;FROM&lt;/span&gt; b &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; x, b &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; y),
    d &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; (&lt;span class=&quot;hljs-keyword&quot;&gt;SELECT&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; i &lt;span class=&quot;hljs-keyword&quot;&gt;FROM&lt;/span&gt; c &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; x, c &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; y),
    e &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; (&lt;span class=&quot;hljs-keyword&quot;&gt;SELECT&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; i &lt;span class=&quot;hljs-keyword&quot;&gt;FROM&lt;/span&gt; d &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; x, d &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; y),
    f &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; (&lt;span class=&quot;hljs-keyword&quot;&gt;SELECT&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; i &lt;span class=&quot;hljs-keyword&quot;&gt;FROM&lt;/span&gt; e &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; x, e &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; y),
    numbers &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; 
(
    &lt;span class=&quot;hljs-keyword&quot;&gt;SELECT&lt;/span&gt; TOP(&lt;span class=&quot;hljs-variable&quot;&gt;@number&lt;/span&gt;_of_numbers)
    (&lt;span class=&quot;hljs-built_in&quot;&gt;ROW_NUMBER&lt;/span&gt;() &lt;span class=&quot;hljs-keyword&quot;&gt;OVER&lt;/span&gt; (&lt;span class=&quot;hljs-keyword&quot;&gt;ORDER&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;BY&lt;/span&gt; (&lt;span class=&quot;hljs-keyword&quot;&gt;SELECT&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;NULL&lt;/span&gt;))) &lt;span class=&quot;hljs-operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; number
    &lt;span class=&quot;hljs-keyword&quot;&gt;FROM&lt;/span&gt; f
)
, mydays &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt;
(
    &lt;span class=&quot;hljs-keyword&quot;&gt;SELECT&lt;/span&gt; DATEADD(&lt;span class=&quot;hljs-keyword&quot;&gt;DAY&lt;/span&gt;, number, &lt;span class=&quot;hljs-variable&quot;&gt;@yearstart&lt;/span&gt;) CheckDate
    &lt;span class=&quot;hljs-keyword&quot;&gt;FROM&lt;/span&gt; numbers
)
, myduration &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt;
(
    &lt;span class=&quot;hljs-keyword&quot;&gt;SELECT&lt;/span&gt; DATEADD(&lt;span class=&quot;hljs-keyword&quot;&gt;MINUTE&lt;/span&gt;, number &lt;span class=&quot;hljs-operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;hljs-variable&quot;&gt;@duration&lt;/span&gt;_minutes, &lt;span class=&quot;hljs-variable&quot;&gt;@yearstart&lt;/span&gt;) CheckMinute
    &lt;span class=&quot;hljs-keyword&quot;&gt;FROM&lt;/span&gt; numbers
)
, workdays &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt;
(
    &lt;span class=&quot;hljs-keyword&quot;&gt;SELECT&lt;/span&gt; 
        m.CheckDate &lt;span class=&quot;hljs-operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;CAST&lt;/span&gt;(u.WorkStartSunday &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; datetime) &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; WorkStart,
        m.CheckDate &lt;span class=&quot;hljs-operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;CAST&lt;/span&gt;(u.WorkEndSunday &lt;span class=&quot;hljs-keyword&quot;&gt;as&lt;/span&gt; datetime) &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; WorkEnd,
        u.UserID,
        u.Username
    &lt;span class=&quot;hljs-keyword&quot;&gt;FROM&lt;/span&gt; mydays m
    &lt;span class=&quot;hljs-keyword&quot;&gt;INNER&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;JOIN&lt;/span&gt;&lt;span class=&quot;hljs-variable&quot;&gt;@Users&lt;/span&gt; u
        &lt;span class=&quot;hljs-keyword&quot;&gt;ON&lt;/span&gt; (DATEPART(WEEKDAY, m.CheckDate ) &lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;AND&lt;/span&gt; u.Sunday &lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;)
    &lt;span class=&quot;hljs-keyword&quot;&gt;UNION&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;SELECT&lt;/span&gt; 
        m.CheckDate &lt;span class=&quot;hljs-operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;CAST&lt;/span&gt;(u.WorkStartMonday &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; datetime) &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; WorkStart,
        m.CheckDate &lt;span class=&quot;hljs-operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;CAST&lt;/span&gt;(u.WorkEndMonday &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; datetime) &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; WorkEnd,
        u.UserID,
        u.Username
    &lt;span class=&quot;hljs-keyword&quot;&gt;FROM&lt;/span&gt; mydays m
    &lt;span class=&quot;hljs-keyword&quot;&gt;INNER&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;JOIN&lt;/span&gt;&lt;span class=&quot;hljs-variable&quot;&gt;@Users&lt;/span&gt; u
        &lt;span class=&quot;hljs-keyword&quot;&gt;ON&lt;/span&gt; (DATEPART(WEEKDAY, m.CheckDate ) &lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;AND&lt;/span&gt; u.Monday &lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;)
        &lt;span class=&quot;hljs-keyword&quot;&gt;UNION&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;SELECT&lt;/span&gt; 
        m.CheckDate &lt;span class=&quot;hljs-operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;CAST&lt;/span&gt;(u.WorkStartTuesday &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; datetime) &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; WorkStart,
        m.CheckDate &lt;span class=&quot;hljs-operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;CAST&lt;/span&gt;(u.WorkEndTuesday &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; datetime) &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; WorkEnd,
        u.UserID,
        u.Username
    &lt;span class=&quot;hljs-keyword&quot;&gt;FROM&lt;/span&gt; mydays m
    &lt;span class=&quot;hljs-keyword&quot;&gt;INNER&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;JOIN&lt;/span&gt;&lt;span class=&quot;hljs-variable&quot;&gt;@Users&lt;/span&gt; u
        &lt;span class=&quot;hljs-keyword&quot;&gt;ON&lt;/span&gt; (DATEPART(WEEKDAY, m.CheckDate ) &lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;AND&lt;/span&gt; u.Tuesday &lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;)
    &lt;span class=&quot;hljs-keyword&quot;&gt;UNION&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;SELECT&lt;/span&gt; 
        m.CheckDate &lt;span class=&quot;hljs-operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;CAST&lt;/span&gt;(u.WorkStartWednesday &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; datetime) &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; WorkStart,
        m.CheckDate &lt;span class=&quot;hljs-operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;CAST&lt;/span&gt;(u.WorkEndWednesday &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; datetime) &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; WorkEnd,
        u.UserID,
        u.Username
    &lt;span class=&quot;hljs-keyword&quot;&gt;FROM&lt;/span&gt; mydays m
    &lt;span class=&quot;hljs-keyword&quot;&gt;INNER&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;JOIN&lt;/span&gt;&lt;span class=&quot;hljs-variable&quot;&gt;@Users&lt;/span&gt; u
        &lt;span class=&quot;hljs-keyword&quot;&gt;ON&lt;/span&gt; (DATEPART(WEEKDAY, m.CheckDate ) &lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;AND&lt;/span&gt; u.Wednesday &lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;)
    &lt;span class=&quot;hljs-keyword&quot;&gt;UNION&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;SELECT&lt;/span&gt; 
        m.CheckDate &lt;span class=&quot;hljs-operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;CAST&lt;/span&gt;(u.WorkStartThursday &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; datetime) &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; WorkStart,
        m.CheckDate &lt;span class=&quot;hljs-operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;CAST&lt;/span&gt;(u.WorkEndThursday &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; datetime) &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; WorkEnd,
        u.UserID,
        u.Username
    &lt;span class=&quot;hljs-keyword&quot;&gt;FROM&lt;/span&gt; mydays m
    &lt;span class=&quot;hljs-keyword&quot;&gt;INNER&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;JOIN&lt;/span&gt;&lt;span class=&quot;hljs-variable&quot;&gt;@Users&lt;/span&gt; u
        &lt;span class=&quot;hljs-keyword&quot;&gt;ON&lt;/span&gt; (DATEPART(WEEKDAY, m.CheckDate ) &lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;AND&lt;/span&gt; u.Thursday &lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;)
    &lt;span class=&quot;hljs-keyword&quot;&gt;UNION&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;SELECT&lt;/span&gt; 
        m.CheckDate &lt;span class=&quot;hljs-operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;CAST&lt;/span&gt;(u.WorkStartFriday &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; datetime) &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; WorkStart,
        m.CheckDate &lt;span class=&quot;hljs-operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;CAST&lt;/span&gt;(u.WorkEndFriday &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; datetime) &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; WorkEnd,
        u.UserID,
        u.Username
    &lt;span class=&quot;hljs-keyword&quot;&gt;FROM&lt;/span&gt; mydays m
    &lt;span class=&quot;hljs-keyword&quot;&gt;INNER&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;JOIN&lt;/span&gt;&lt;span class=&quot;hljs-variable&quot;&gt;@Users&lt;/span&gt; u
        &lt;span class=&quot;hljs-keyword&quot;&gt;ON&lt;/span&gt; (DATEPART(WEEKDAY, m.CheckDate ) &lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;AND&lt;/span&gt; u.Friday &lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;)
    &lt;span class=&quot;hljs-keyword&quot;&gt;UNION&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;SELECT&lt;/span&gt; 
        m.CheckDate &lt;span class=&quot;hljs-operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;CAST&lt;/span&gt;(u.WorkStartSaturday &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; datetime) &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; WorkStart,
        m.CheckDate &lt;span class=&quot;hljs-operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;CAST&lt;/span&gt;(u.WorkEndSaturday &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; datetime) &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; WorkEnd,
        u.UserID,
        u.Username
    &lt;span class=&quot;hljs-keyword&quot;&gt;FROM&lt;/span&gt; mydays m
    &lt;span class=&quot;hljs-keyword&quot;&gt;INNER&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;JOIN&lt;/span&gt;&lt;span class=&quot;hljs-variable&quot;&gt;@Users&lt;/span&gt; u
        &lt;span class=&quot;hljs-keyword&quot;&gt;ON&lt;/span&gt; (DATEPART(WEEKDAY, m.CheckDate ) &lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;AND&lt;/span&gt; u.Saturday &lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;)
),
intervals &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt;
(
    &lt;span class=&quot;hljs-keyword&quot;&gt;SELECT&lt;/span&gt; 
        m.CheckDate &lt;span class=&quot;hljs-operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;CAST&lt;/span&gt;(u.IntervalStartSunday &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; datetime) &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; WorkStart,
        m.CheckDate &lt;span class=&quot;hljs-operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;CAST&lt;/span&gt;(u.IntervalEndSunday &lt;span class=&quot;hljs-keyword&quot;&gt;as&lt;/span&gt; datetime) &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; WorkEnd,
        u.UserID,
        u.Username
    &lt;span class=&quot;hljs-keyword&quot;&gt;FROM&lt;/span&gt; mydays m
    &lt;span class=&quot;hljs-keyword&quot;&gt;INNER&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;JOIN&lt;/span&gt;&lt;span class=&quot;hljs-variable&quot;&gt;@Users&lt;/span&gt; u
        &lt;span class=&quot;hljs-keyword&quot;&gt;ON&lt;/span&gt; (DATEPART(WEEKDAY, m.CheckDate ) &lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;AND&lt;/span&gt; u.Sunday &lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;)
        &lt;span class=&quot;hljs-keyword&quot;&gt;AND&lt;/span&gt; u.IntervalStartSunday &lt;span class=&quot;hljs-keyword&quot;&gt;IS&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;NOT&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;UNION&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;SELECT&lt;/span&gt; 
        m.CheckDate &lt;span class=&quot;hljs-operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;CAST&lt;/span&gt;(u.IntervalStartMonday &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; datetime) &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; WorkStart,
        m.CheckDate &lt;span class=&quot;hljs-operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;CAST&lt;/span&gt;(u.IntervalEndMonday &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; datetime) &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; WorkEnd,
        u.UserID,
        u.Username
    &lt;span class=&quot;hljs-keyword&quot;&gt;FROM&lt;/span&gt; mydays m
    &lt;span class=&quot;hljs-keyword&quot;&gt;INNER&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;JOIN&lt;/span&gt;&lt;span class=&quot;hljs-variable&quot;&gt;@Users&lt;/span&gt; u
        &lt;span class=&quot;hljs-keyword&quot;&gt;ON&lt;/span&gt; (DATEPART(WEEKDAY, m.CheckDate ) &lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;AND&lt;/span&gt; u.Monday &lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;)
        &lt;span class=&quot;hljs-keyword&quot;&gt;AND&lt;/span&gt; u.IntervalStartMonday &lt;span class=&quot;hljs-keyword&quot;&gt;IS&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;NOT&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;UNION&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;SELECT&lt;/span&gt; 
        m.CheckDate &lt;span class=&quot;hljs-operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;CAST&lt;/span&gt;(u.IntervalStartTuesday &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; datetime) &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; WorkStart,
        m.CheckDate &lt;span class=&quot;hljs-operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;CAST&lt;/span&gt;(u.IntervalEndTuesday &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; datetime) &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; WorkEnd,
        u.UserID,
        u.Username
    &lt;span class=&quot;hljs-keyword&quot;&gt;FROM&lt;/span&gt; mydays m
    &lt;span class=&quot;hljs-keyword&quot;&gt;INNER&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;JOIN&lt;/span&gt;&lt;span class=&quot;hljs-variable&quot;&gt;@Users&lt;/span&gt; u
        &lt;span class=&quot;hljs-keyword&quot;&gt;ON&lt;/span&gt; (DATEPART(WEEKDAY, m.CheckDate ) &lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;AND&lt;/span&gt; u.Tuesday &lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;)
        &lt;span class=&quot;hljs-keyword&quot;&gt;AND&lt;/span&gt; u.IntervalStartTuesday &lt;span class=&quot;hljs-keyword&quot;&gt;IS&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;NOT&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;UNION&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;SELECT&lt;/span&gt; 
        m.CheckDate &lt;span class=&quot;hljs-operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;CAST&lt;/span&gt;(u.IntervalStartWednesday &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; datetime) &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; WorkStart,
        m.CheckDate &lt;span class=&quot;hljs-operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;CAST&lt;/span&gt;(u.IntervalEndWednesday &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; datetime) &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; WorkEnd,
        u.UserID,
        u.Username
    &lt;span class=&quot;hljs-keyword&quot;&gt;FROM&lt;/span&gt; mydays m
    &lt;span class=&quot;hljs-keyword&quot;&gt;INNER&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;JOIN&lt;/span&gt;&lt;span class=&quot;hljs-variable&quot;&gt;@Users&lt;/span&gt; u
        &lt;span class=&quot;hljs-keyword&quot;&gt;ON&lt;/span&gt; (DATEPART(WEEKDAY, m.CheckDate ) &lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;AND&lt;/span&gt; u.Wednesday &lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;)
        &lt;span class=&quot;hljs-keyword&quot;&gt;AND&lt;/span&gt; u.IntervalStartWednesday &lt;span class=&quot;hljs-keyword&quot;&gt;IS&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;NOT&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;UNION&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;SELECT&lt;/span&gt; 
        m.CheckDate &lt;span class=&quot;hljs-operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;CAST&lt;/span&gt;(u.IntervalStartThursday &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; datetime) &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; WorkStart,
        m.CheckDate &lt;span class=&quot;hljs-operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;CAST&lt;/span&gt;(u.IntervalEndThursday &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; datetime) &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; WorkEnd,
        u.UserID,
        u.Username
    &lt;span class=&quot;hljs-keyword&quot;&gt;FROM&lt;/span&gt; mydays m
    &lt;span class=&quot;hljs-keyword&quot;&gt;INNER&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;JOIN&lt;/span&gt;&lt;span class=&quot;hljs-variable&quot;&gt;@Users&lt;/span&gt; u
        &lt;span class=&quot;hljs-keyword&quot;&gt;ON&lt;/span&gt; (DATEPART(WEEKDAY, m.CheckDate ) &lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;AND&lt;/span&gt; u.Thursday &lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;)
        &lt;span class=&quot;hljs-keyword&quot;&gt;AND&lt;/span&gt; u.IntervalStartThursday &lt;span class=&quot;hljs-keyword&quot;&gt;IS&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;NOT&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;UNION&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;SELECT&lt;/span&gt; 
        m.CheckDate &lt;span class=&quot;hljs-operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;CAST&lt;/span&gt;(u.IntervalStartFriday &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; datetime) &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; WorkStart,
        m.CheckDate &lt;span class=&quot;hljs-operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;CAST&lt;/span&gt;(u.IntervalEndFriday &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; datetime) &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; WorkEnd,
        u.UserID,
        u.Username
    &lt;span class=&quot;hljs-keyword&quot;&gt;FROM&lt;/span&gt; mydays m
    &lt;span class=&quot;hljs-keyword&quot;&gt;INNER&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;JOIN&lt;/span&gt;&lt;span class=&quot;hljs-variable&quot;&gt;@Users&lt;/span&gt; u
        &lt;span class=&quot;hljs-keyword&quot;&gt;ON&lt;/span&gt; (DATEPART(WEEKDAY, m.CheckDate ) &lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;AND&lt;/span&gt; u.Friday &lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;)
        &lt;span class=&quot;hljs-keyword&quot;&gt;AND&lt;/span&gt; u.IntervalStartFriday &lt;span class=&quot;hljs-keyword&quot;&gt;IS&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;NOT&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;UNION&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;SELECT&lt;/span&gt; 
        m.CheckDate &lt;span class=&quot;hljs-operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;CAST&lt;/span&gt;(u.IntervalStartSaturday &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; datetime) &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; WorkStart,
        m.CheckDate &lt;span class=&quot;hljs-operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;CAST&lt;/span&gt;(u.IntervalEndSaturday &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; datetime) &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; WorkEnd,
        u.UserID,
        u.Username
    &lt;span class=&quot;hljs-keyword&quot;&gt;FROM&lt;/span&gt; mydays m
    &lt;span class=&quot;hljs-keyword&quot;&gt;INNER&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;JOIN&lt;/span&gt;&lt;span class=&quot;hljs-variable&quot;&gt;@Users&lt;/span&gt; u
        &lt;span class=&quot;hljs-keyword&quot;&gt;ON&lt;/span&gt; (DATEPART(WEEKDAY, m.CheckDate ) &lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;AND&lt;/span&gt; u.Saturday &lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;)
        &lt;span class=&quot;hljs-keyword&quot;&gt;AND&lt;/span&gt; u.IntervalStartSaturday &lt;span class=&quot;hljs-keyword&quot;&gt;IS&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;NOT&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;NULL&lt;/span&gt;
)
, timeslots &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt;
(
    &lt;span class=&quot;hljs-keyword&quot;&gt;SELECT&lt;/span&gt; m.CheckMinute &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; StartSlot,
        &lt;span class=&quot;hljs-built_in&quot;&gt;LEAD&lt;/span&gt;(m.CheckMinute) &lt;span class=&quot;hljs-keyword&quot;&gt;OVER&lt;/span&gt; (&lt;span class=&quot;hljs-keyword&quot;&gt;ORDER&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;BY&lt;/span&gt; m.CheckMinute) &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; EndSlot
    &lt;span class=&quot;hljs-keyword&quot;&gt;FROM&lt;/span&gt; myduration m
)
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_1&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;Now I also created the intervals (lunch hour data) and our final output will be a list of time slots and whether it is open, event, or lunch for a date range: &lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_2&quot; class=&quot;hljs language-vbnet&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;select&lt;/span&gt; 
    t.StartSlot,
    t.EndSlot,
    &lt;span class=&quot;hljs-keyword&quot;&gt;CASE&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;WHEN&lt;/span&gt; i.WorkStart &lt;span class=&quot;hljs-built_in&quot;&gt;IS&lt;/span&gt; NULL &lt;span class=&quot;hljs-built_in&quot;&gt;AND&lt;/span&gt; e.event_start &lt;span class=&quot;hljs-built_in&quot;&gt;IS&lt;/span&gt; NULL &lt;span class=&quot;hljs-keyword&quot;&gt;THEN&lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;&#39;Open&#39; &lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;WHEN&lt;/span&gt; i.WorkStart &lt;span class=&quot;hljs-built_in&quot;&gt;IS&lt;/span&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;NOT&lt;/span&gt; NULL &lt;span class=&quot;hljs-keyword&quot;&gt;THEN&lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;&#39;Lunch&#39;&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;ELSE&lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;&#39;Event&#39;&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;END&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; IType
&lt;span class=&quot;hljs-keyword&quot;&gt;from&lt;/span&gt; timeslots t
INNER &lt;span class=&quot;hljs-keyword&quot;&gt;JOIN&lt;/span&gt; workdays w
    &lt;span class=&quot;hljs-keyword&quot;&gt;ON&lt;/span&gt; t.StartSlot &amp;gt;= w.WorkStart 
    &lt;span class=&quot;hljs-built_in&quot;&gt;AND&lt;/span&gt; t.StartSlot &amp;lt; w.WorkEnd
LEFT &lt;span class=&quot;hljs-keyword&quot;&gt;JOIN&lt;/span&gt; intervals i
    &lt;span class=&quot;hljs-keyword&quot;&gt;ON&lt;/span&gt; t.StartSlot &amp;gt;= i.WorkStart
    &lt;span class=&quot;hljs-built_in&quot;&gt;AND&lt;/span&gt; t.StartSlot &amp;lt; i.WorkEnd
LEFT &lt;span class=&quot;hljs-keyword&quot;&gt;JOIN&lt;/span&gt; @&lt;span class=&quot;hljs-keyword&quot;&gt;event&lt;/span&gt; e
    &lt;span class=&quot;hljs-keyword&quot;&gt;ON&lt;/span&gt; t.StartSlot &amp;gt;= e.event_start
    &lt;span class=&quot;hljs-built_in&quot;&gt;AND&lt;/span&gt; t.StartSlot &amp;lt; e.event_end
&lt;span class=&quot;hljs-keyword&quot;&gt;WHERE&lt;/span&gt; w.WorkStart &amp;gt;= &lt;span class=&quot;hljs-comment&quot;&gt;&#39;12/01/2014&#39;&lt;/span&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;AND&lt;/span&gt; w.WorkEnd &amp;lt; &lt;span class=&quot;hljs-comment&quot;&gt;&#39;12/04/2014&#39;&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;ORDER&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;BY&lt;/span&gt; t.StartSlot
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_2&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;Here is the output:&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_3&quot; class=&quot;hljs language-yaml&quot;&gt;&lt;span class=&quot;hljs-string&quot;&gt;StartSlot&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;EndSlot&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;IType&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2014-12-01 08:00:00.000 &lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2014-12-01 08:30:00.000 &lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;Open&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2014-12-01 08:30:00.000 &lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2014-12-01 09:00:00.000 &lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;Open&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2014-12-01 09:00:00.000 &lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2014-12-01 09:30:00.000 &lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;Event&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2014-12-01 09:30:00.000 &lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2014-12-01 10:00:00.000 &lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;Event&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2014-12-01 10:00:00.000 &lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2014-12-01 10:30:00.000 &lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;Event&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2014-12-01 10:30:00.000 &lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2014-12-01 11:00:00.000 &lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;Open&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2014-12-01 11:00:00.000 &lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2014-12-01 11:30:00.000 &lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;Event&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2014-12-01 11:30:00.000 &lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2014-12-01 12:00:00.000 &lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;Event&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2014-12-01 12:00:00.000 &lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2014-12-01 12:30:00.000 &lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;Lunch&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2014-12-01 12:30:00.000 &lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2014-12-01 13:00:00.000 &lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;Lunch&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2014-12-01 13:00:00.000 &lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2014-12-01 13:30:00.000 &lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;Event&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2014-12-01 13:30:00.000 &lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2014-12-01 14:00:00.000 &lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;Event&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2014-12-01 14:00:00.000 &lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2014-12-01 14:30:00.000 &lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;Event&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2014-12-01 14:30:00.000 &lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2014-12-01 15:00:00.000 &lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;Event&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2014-12-01 15:00:00.000 &lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2014-12-01 15:30:00.000 &lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;Event&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2014-12-01 15:30:00.000 &lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2014-12-01 16:00:00.000 &lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;Event&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2014-12-01 16:00:00.000 &lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2014-12-01 16:30:00.000 &lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;Event&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2014-12-01 16:30:00.000 &lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2014-12-01 17:00:00.000 &lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;Open&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2014-12-01 17:00:00.000 &lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2014-12-01 17:30:00.000 &lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;Open&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2014-12-01 17:30:00.000 &lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2014-12-01 18:00:00.000 &lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;Open&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2014-12-02 10:00:00.000 &lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2014-12-02 10:30:00.000 &lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;Open&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2014-12-02 10:30:00.000 &lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2014-12-02 11:00:00.000 &lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;Open&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2014-12-02 11:00:00.000 &lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2014-12-02 11:30:00.000 &lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;Open&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2014-12-02 11:30:00.000 &lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2014-12-02 12:00:00.000 &lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;Open&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2014-12-02 12:00:00.000 &lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2014-12-02 12:30:00.000 &lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;Open&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2014-12-02 12:30:00.000 &lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2014-12-02 13:00:00.000 &lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;Open&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2014-12-02 13:00:00.000 &lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2014-12-02 13:30:00.000 &lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;Open&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2014-12-02 13:30:00.000 &lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2014-12-02 14:00:00.000 &lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;Open&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2014-12-02 14:00:00.000 &lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2014-12-02 14:30:00.000 &lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;Open&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2014-12-02 14:30:00.000 &lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2014-12-02 15:00:00.000 &lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;Open&lt;/span&gt;&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_3&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;solution_2&quot;&gt;Solution 2:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;Ok lets make the test data:&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_4&quot; class=&quot;hljs language-csharp&quot;&gt;DECLARE @Users TABLE
(
    [&lt;span class=&quot;hljs-meta&quot;&gt;UserID&lt;/span&gt;] [&lt;span class=&quot;hljs-built_in&quot;&gt;int&lt;/span&gt;],
    [&lt;span class=&quot;hljs-meta&quot;&gt;Username&lt;/span&gt;] [varchar](&lt;span class=&quot;hljs-number&quot;&gt;32&lt;/span&gt;) NULL,
    [&lt;span class=&quot;hljs-meta&quot;&gt;Sunday&lt;/span&gt;] [bit] NULL,
    [&lt;span class=&quot;hljs-meta&quot;&gt;Monday&lt;/span&gt;] [bit] NULL,
    [&lt;span class=&quot;hljs-meta&quot;&gt;Tuesday&lt;/span&gt;] [bit] NULL,
    [&lt;span class=&quot;hljs-meta&quot;&gt;Wednesday&lt;/span&gt;] [bit] NULL,
    [&lt;span class=&quot;hljs-meta&quot;&gt;Thursday&lt;/span&gt;] [bit] NULL,
    [&lt;span class=&quot;hljs-meta&quot;&gt;Friday&lt;/span&gt;] [bit] NULL,
    [&lt;span class=&quot;hljs-meta&quot;&gt;Saturday&lt;/span&gt;] [bit] NULL,
    [&lt;span class=&quot;hljs-meta&quot;&gt;WorkStartSunday&lt;/span&gt;] [time](&lt;span class=&quot;hljs-number&quot;&gt;7&lt;/span&gt;) NULL,
    [&lt;span class=&quot;hljs-meta&quot;&gt;WorkEndSunday&lt;/span&gt;] [time](&lt;span class=&quot;hljs-number&quot;&gt;7&lt;/span&gt;) NULL,
    [&lt;span class=&quot;hljs-meta&quot;&gt;IntervalStartSunday&lt;/span&gt;] [time](&lt;span class=&quot;hljs-number&quot;&gt;7&lt;/span&gt;) NULL,
    [&lt;span class=&quot;hljs-meta&quot;&gt;IntervalEndSunday&lt;/span&gt;] [time](&lt;span class=&quot;hljs-number&quot;&gt;7&lt;/span&gt;) NULL,
    [&lt;span class=&quot;hljs-meta&quot;&gt;WorkStartMonday&lt;/span&gt;] [time](&lt;span class=&quot;hljs-number&quot;&gt;7&lt;/span&gt;) NULL,
    [&lt;span class=&quot;hljs-meta&quot;&gt;WorkEndMonday&lt;/span&gt;] [time](&lt;span class=&quot;hljs-number&quot;&gt;7&lt;/span&gt;) NULL,
    [&lt;span class=&quot;hljs-meta&quot;&gt;IntervalStartMonday&lt;/span&gt;] [time](&lt;span class=&quot;hljs-number&quot;&gt;7&lt;/span&gt;) NULL,
    [&lt;span class=&quot;hljs-meta&quot;&gt;IntervalEndMonday&lt;/span&gt;] [time](&lt;span class=&quot;hljs-number&quot;&gt;7&lt;/span&gt;) NULL,
    [&lt;span class=&quot;hljs-meta&quot;&gt;WorkStartTuesday&lt;/span&gt;] [time](&lt;span class=&quot;hljs-number&quot;&gt;7&lt;/span&gt;) NULL,
    [&lt;span class=&quot;hljs-meta&quot;&gt;WorkEndTuesday&lt;/span&gt;] [time](&lt;span class=&quot;hljs-number&quot;&gt;7&lt;/span&gt;) NULL,
    [&lt;span class=&quot;hljs-meta&quot;&gt;IntervalStartTuesday&lt;/span&gt;] [time](&lt;span class=&quot;hljs-number&quot;&gt;7&lt;/span&gt;) NULL,
    [&lt;span class=&quot;hljs-meta&quot;&gt;IntervalEndTuesday&lt;/span&gt;] [time](&lt;span class=&quot;hljs-number&quot;&gt;7&lt;/span&gt;) NULL,
    [&lt;span class=&quot;hljs-meta&quot;&gt;WorkStartWednesday&lt;/span&gt;] [time](&lt;span class=&quot;hljs-number&quot;&gt;7&lt;/span&gt;) NULL,
    [&lt;span class=&quot;hljs-meta&quot;&gt;WorkEndWednesday&lt;/span&gt;] [time](&lt;span class=&quot;hljs-number&quot;&gt;7&lt;/span&gt;) NULL,
    [&lt;span class=&quot;hljs-meta&quot;&gt;IntervalStartWednesday&lt;/span&gt;] [time](&lt;span class=&quot;hljs-number&quot;&gt;7&lt;/span&gt;) NULL,
    [&lt;span class=&quot;hljs-meta&quot;&gt;IntervalEndWednesday&lt;/span&gt;] [time](&lt;span class=&quot;hljs-number&quot;&gt;7&lt;/span&gt;) NULL,
    [&lt;span class=&quot;hljs-meta&quot;&gt;WorkStartThursday&lt;/span&gt;] [time](&lt;span class=&quot;hljs-number&quot;&gt;7&lt;/span&gt;) NULL,
    [&lt;span class=&quot;hljs-meta&quot;&gt;WorkEndThursday&lt;/span&gt;] [time](&lt;span class=&quot;hljs-number&quot;&gt;7&lt;/span&gt;) NULL,
    [&lt;span class=&quot;hljs-meta&quot;&gt;IntervalStartThursday&lt;/span&gt;] [time](&lt;span class=&quot;hljs-number&quot;&gt;7&lt;/span&gt;) NULL,
    [&lt;span class=&quot;hljs-meta&quot;&gt;IntervalEndThursday&lt;/span&gt;] [time](&lt;span class=&quot;hljs-number&quot;&gt;7&lt;/span&gt;) NULL,
    [&lt;span class=&quot;hljs-meta&quot;&gt;WorkStartFriday&lt;/span&gt;] [time](&lt;span class=&quot;hljs-number&quot;&gt;7&lt;/span&gt;) NULL,
    [&lt;span class=&quot;hljs-meta&quot;&gt;WorkEndFriday&lt;/span&gt;] [time](&lt;span class=&quot;hljs-number&quot;&gt;7&lt;/span&gt;) NULL,
    [&lt;span class=&quot;hljs-meta&quot;&gt;IntervalStartFriday&lt;/span&gt;] [time](&lt;span class=&quot;hljs-number&quot;&gt;7&lt;/span&gt;) NULL,
    [&lt;span class=&quot;hljs-meta&quot;&gt;IntervalEndFriday&lt;/span&gt;] [time](&lt;span class=&quot;hljs-number&quot;&gt;7&lt;/span&gt;) NULL,
    [&lt;span class=&quot;hljs-meta&quot;&gt;WorkStartSaturday&lt;/span&gt;] [time](&lt;span class=&quot;hljs-number&quot;&gt;7&lt;/span&gt;) NULL,
    [&lt;span class=&quot;hljs-meta&quot;&gt;WorkEndSaturday&lt;/span&gt;] [time](&lt;span class=&quot;hljs-number&quot;&gt;7&lt;/span&gt;) NULL,
    [&lt;span class=&quot;hljs-meta&quot;&gt;IntervalStartSaturday&lt;/span&gt;] [time](&lt;span class=&quot;hljs-number&quot;&gt;7&lt;/span&gt;) NULL,
    [&lt;span class=&quot;hljs-meta&quot;&gt;IntervalEndSaturday&lt;/span&gt;] [time](&lt;span class=&quot;hljs-number&quot;&gt;7&lt;/span&gt;) NULL
);

INSERT INTO @Users 
([UserID], [Username], [Sunday], [Monday], [Tuesday], [Wednesday], [Thursday], [Friday], [Saturday], [WorkStartSunday], [WorkEndSunday], [IntervalStartSunday], [IntervalEndSunday], [WorkStartMonday], [WorkEndMonday], [IntervalStartMonday], [IntervalEndMonday], [WorkStartTuesday], [WorkEndTuesday], [IntervalStartTuesday], [IntervalEndTuesday], [WorkStartWednesday], [WorkEndWednesday], [IntervalStartWednesday], [IntervalEndWednesday], [WorkStartThursday], [WorkEndThursday], [IntervalStartThursday], [IntervalEndThursday], [WorkStartFriday], [WorkEndFriday], [IntervalStartFriday], [IntervalEndFriday], [WorkStartSaturday], [WorkEndSaturday], [IntervalStartSaturday], [IntervalEndSaturday]) 
VALUES 
(&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;User A&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-number&quot;&gt;0&lt;/span&gt;, &lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;, &lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;, &lt;span class=&quot;hljs-number&quot;&gt;0&lt;/span&gt;, &lt;span class=&quot;hljs-number&quot;&gt;0&lt;/span&gt;, &lt;span class=&quot;hljs-number&quot;&gt;0&lt;/span&gt;, &lt;span class=&quot;hljs-number&quot;&gt;0&lt;/span&gt;, NULL, NULL, NULL, NULL, CAST(&lt;span class=&quot;hljs-string&quot;&gt;&#39;08:00:00&#39;&lt;/span&gt; AS Time), CAST(&lt;span class=&quot;hljs-string&quot;&gt;&#39;18:00:00&#39;&lt;/span&gt; AS Time), CAST(&lt;span class=&quot;hljs-string&quot;&gt;&#39;12:00:00&#39;&lt;/span&gt; AS Time), CAST(&lt;span class=&quot;hljs-string&quot;&gt;&#39;13:00:00&#39;&lt;/span&gt; AS Time), CAST(&lt;span class=&quot;hljs-string&quot;&gt;&#39;10:00:00&#39;&lt;/span&gt; AS Time), CAST(&lt;span class=&quot;hljs-string&quot;&gt;&#39;15:00:00&#39;&lt;/span&gt; AS Time), NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL)
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_4&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;Now that we have the user record created, lets make a numbers table and set the beginning of the date range we are creating:&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_5&quot; class=&quot;hljs language-sql&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;DECLARE&lt;/span&gt;&lt;span class=&quot;hljs-variable&quot;&gt;@yearstart&lt;/span&gt; DATETIME &lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;&#39;01/01/2014&#39;&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;DECLARE&lt;/span&gt;&lt;span class=&quot;hljs-variable&quot;&gt;@number&lt;/span&gt;_of_numbers &lt;span class=&quot;hljs-type&quot;&gt;INT&lt;/span&gt;&lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;100000&lt;/span&gt;;
;&lt;span class=&quot;hljs-keyword&quot;&gt;WITH&lt;/span&gt;
    a &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; (&lt;span class=&quot;hljs-keyword&quot;&gt;SELECT&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; i &lt;span class=&quot;hljs-keyword&quot;&gt;UNION&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;ALL&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;SELECT&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;),
    b &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; (&lt;span class=&quot;hljs-keyword&quot;&gt;SELECT&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; i &lt;span class=&quot;hljs-keyword&quot;&gt;FROM&lt;/span&gt; a &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; x, a &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; y),
    c &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; (&lt;span class=&quot;hljs-keyword&quot;&gt;SELECT&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; i &lt;span class=&quot;hljs-keyword&quot;&gt;FROM&lt;/span&gt; b &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; x, b &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; y),
    d &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; (&lt;span class=&quot;hljs-keyword&quot;&gt;SELECT&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; i &lt;span class=&quot;hljs-keyword&quot;&gt;FROM&lt;/span&gt; c &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; x, c &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; y),
    e &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; (&lt;span class=&quot;hljs-keyword&quot;&gt;SELECT&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; i &lt;span class=&quot;hljs-keyword&quot;&gt;FROM&lt;/span&gt; d &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; x, d &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; y),
    f &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; (&lt;span class=&quot;hljs-keyword&quot;&gt;SELECT&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; i &lt;span class=&quot;hljs-keyword&quot;&gt;FROM&lt;/span&gt; e &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; x, e &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; y),
    numbers &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; 
(
    &lt;span class=&quot;hljs-keyword&quot;&gt;SELECT&lt;/span&gt; TOP(&lt;span class=&quot;hljs-variable&quot;&gt;@number&lt;/span&gt;_of_numbers)
    &lt;span class=&quot;hljs-built_in&quot;&gt;ROW_NUMBER&lt;/span&gt;() &lt;span class=&quot;hljs-keyword&quot;&gt;OVER&lt;/span&gt; (&lt;span class=&quot;hljs-keyword&quot;&gt;ORDER&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;BY&lt;/span&gt; (&lt;span class=&quot;hljs-keyword&quot;&gt;SELECT&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;NULL&lt;/span&gt;)) &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; number
    &lt;span class=&quot;hljs-keyword&quot;&gt;FROM&lt;/span&gt; f
)
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_5&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;Using our numbers table, lets make some days to check against:&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_6&quot; class=&quot;hljs language-less&quot;&gt;, &lt;span class=&quot;hljs-selector-tag&quot;&gt;mydays&lt;/span&gt;&lt;span class=&quot;hljs-selector-tag&quot;&gt;AS&lt;/span&gt;
(
    SELECT DATEADD(DAY, number, &lt;span class=&quot;hljs-variable&quot;&gt;@yearstart&lt;/span&gt;) CheckDate
    FROM numbers
)
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_6&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;Now using the user table, we are going to make a all of the working days of the week for the user, based on whether the flag is set, and the starting date / time and ending date / time&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_7&quot; class=&quot;hljs language-less&quot;&gt;, &lt;span class=&quot;hljs-selector-tag&quot;&gt;workdays&lt;/span&gt;&lt;span class=&quot;hljs-selector-tag&quot;&gt;AS&lt;/span&gt;
(
    SELECT 
        m.CheckDate + CAST(u.WorkStartSunday AS datetime) AS WorkStart,
        m.CheckDate + CAST(u.WorkEndSunday as datetime) AS WorkEnd,
        u.UserID,
        u.Username
    FROM mydays m
    INNER JOIN &lt;span class=&quot;hljs-variable&quot;&gt;@Users&lt;/span&gt; u
        ON (DATEPART(WEEKDAY, m.CheckDate ) = &lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;AND&lt;/span&gt; u.Sunday = &lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;)
    UNION
    SELECT 
        m.CheckDate + CAST(u.WorkStartMonday AS datetime) AS WorkStart,
        m.CheckDate + CAST(u.WorkEndMonday AS datetime) AS WorkEnd,
        u.UserID,
        u.Username
    FROM mydays m
    INNER JOIN &lt;span class=&quot;hljs-variable&quot;&gt;@Users&lt;/span&gt; u
        ON (DATEPART(WEEKDAY, m.CheckDate ) = &lt;span class=&quot;hljs-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;AND&lt;/span&gt; u.Monday = &lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;)
        UNION
    SELECT 
        m.CheckDate + CAST(u.WorkStartTuesday AS datetime) AS WorkStart,
        m.CheckDate + CAST(u.WorkEndTuesday AS datetime) AS WorkEnd,
        u.UserID,
        u.Username
    FROM mydays m
    INNER JOIN &lt;span class=&quot;hljs-variable&quot;&gt;@Users&lt;/span&gt; u
        ON (DATEPART(WEEKDAY, m.CheckDate ) = &lt;span class=&quot;hljs-number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;AND&lt;/span&gt; u.Tuesday = &lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;)
    UNION
    SELECT 
        m.CheckDate + CAST(u.WorkStartWednesday AS datetime) AS WorkStart,
        m.CheckDate + CAST(u.WorkEndWednesday AS datetime) AS WorkEnd,
        u.UserID,
        u.Username
    FROM mydays m
    INNER JOIN &lt;span class=&quot;hljs-variable&quot;&gt;@Users&lt;/span&gt; u
        ON (DATEPART(WEEKDAY, m.CheckDate ) = &lt;span class=&quot;hljs-number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;AND&lt;/span&gt; u.Wednesday = &lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;)
    UNION
    SELECT 
        m.CheckDate + CAST(u.WorkStartThursday AS datetime) AS WorkStart,
        m.CheckDate + CAST(u.WorkEndThursday AS datetime) AS WorkEnd,
        u.UserID,
        u.Username
    FROM mydays m
    INNER JOIN &lt;span class=&quot;hljs-variable&quot;&gt;@Users&lt;/span&gt; u
        ON (DATEPART(WEEKDAY, m.CheckDate ) = &lt;span class=&quot;hljs-number&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;AND&lt;/span&gt; u.Thursday = &lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;)
    UNION
    SELECT 
        m.CheckDate + CAST(u.WorkStartFriday AS datetime) AS WorkStart,
        m.CheckDate + CAST(u.WorkEndFriday AS datetime) AS WorkEnd,
        u.UserID,
        u.Username
    FROM mydays m
    INNER JOIN &lt;span class=&quot;hljs-variable&quot;&gt;@Users&lt;/span&gt; u
        ON (DATEPART(WEEKDAY, m.CheckDate ) = &lt;span class=&quot;hljs-number&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;AND&lt;/span&gt; u.Friday = &lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;)
    UNION
    SELECT 
        m.CheckDate + CAST(u.WorkStartSaturday AS datetime) AS WorkStart,
        m.CheckDate + CAST(u.WorkEndSaturday AS datetime) AS WorkEnd,
        u.UserID,
        u.Username
    FROM mydays m
    INNER JOIN &lt;span class=&quot;hljs-variable&quot;&gt;@Users&lt;/span&gt; u
        ON (DATEPART(WEEKDAY, m.CheckDate ) = &lt;span class=&quot;hljs-number&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;AND&lt;/span&gt; u.Saturday = &lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;)
)
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_7&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;Now lets look at the data we generated:&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_8&quot; class=&quot;hljs language-sql&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;SELECT&lt;/span&gt; w.WorkStart, w.WorkEnd, DATENAME(WEEKDAY, w.WorkStart) DayOfWeek, w.UserID, w.Username
&lt;span class=&quot;hljs-keyword&quot;&gt;FROM&lt;/span&gt; workdays w
&lt;span class=&quot;hljs-keyword&quot;&gt;where&lt;/span&gt; w.WorkStart &lt;span class=&quot;hljs-operator&quot;&gt;&amp;gt;=&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;&#39;12/01/2014&#39;&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;AND&lt;/span&gt; w.WorkEnd &lt;span class=&quot;hljs-operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;&#39;01/01/2015&#39;&lt;/span&gt;&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_8&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;Here is the output:&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_9&quot; class=&quot;hljs language-yaml&quot;&gt;&lt;span class=&quot;hljs-string&quot;&gt;WorkStart&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;WorkEnd&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;DayOfWeek&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;UserID&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;Username&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2014-12-01 08:00:00.000 &lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2014-12-01 18:00:00.000 &lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;Monday&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;User&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2014-12-02 10:00:00.000 &lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2014-12-02 15:00:00.000 &lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;Tuesday&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;User&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2014-12-08 08:00:00.000 &lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2014-12-08 18:00:00.000 &lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;Monday&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;User&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2014-12-09 10:00:00.000 &lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2014-12-09 15:00:00.000 &lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;Tuesday&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;User&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2014-12-15 08:00:00.000 &lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2014-12-15 18:00:00.000 &lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;Monday&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;User&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2014-12-16 10:00:00.000 &lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2014-12-16 15:00:00.000 &lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;Tuesday&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;User&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2014-12-22 08:00:00.000 &lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2014-12-22 18:00:00.000 &lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;Monday&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;User&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2014-12-23 10:00:00.000 &lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2014-12-23 15:00:00.000 &lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;Tuesday&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;User&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2014-12-29 08:00:00.000 &lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2014-12-29 18:00:00.000 &lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;Monday&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;User&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2014-12-30 10:00:00.000 &lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2014-12-30 15:00:00.000 &lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;Tuesday&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;User&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;A&lt;/span&gt;&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_9&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;You can do the same thing for the intervals (I guess the interval is the lunch break?), and then join against them to find the work times available, and then join against the events to see which work times you can create an event against.&lt;/p&gt;&lt;/div&gt; 


</content><link rel='replies' type='application/atom+xml' href='https://sumitmotwani.blogspot.com/feeds/6232853971029518595/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://sumitmotwani.blogspot.com/2024/08/stored-procedure-to-find-free-slots.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/5301110832091710857/posts/default/6232853971029518595'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/5301110832091710857/posts/default/6232853971029518595'/><link rel='alternate' type='text/html' href='https://sumitmotwani.blogspot.com/2024/08/stored-procedure-to-find-free-slots.html' title='Stored Procedure To Find Free Slots With Different Work Times Per Day Of The Week'/><author><name>Jackie Leffler</name><uri>http://www.blogger.com/profile/07006275599358224644</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5301110832091710857.post-8942854297526639618</id><published>2024-08-21T10:26:00.001-07:00</published><updated>2024-08-21T10:26:44.402-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="List"/><category scheme="http://www.blogger.com/atom/ns#" term="Mysql"/><category scheme="http://www.blogger.com/atom/ns#" term="Python"/><category scheme="http://www.blogger.com/atom/ns#" term="Sql"/><title type='text'>Inserting List In Specific Column Using Python</title><content type='html'>&lt;div class=&quot;question&quot;&gt;i have a database where there are two column sess_id and data. Now i want to insert a list from py to db how should i do this. suppose this is the list to be inserted in data colum&lt;/div&gt;&lt;h2 id=&quot;solution_1&quot;&gt;Solution 1:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;Depends on what you want to do. If you want to insert a list of values into one column you can loop through the list and insert row by row. You can also use executemany instead of execute.&lt;/p&gt;&lt;p&gt;If you&#39;re directly inputting the list then use excutemany, otherwise if you&#39;re looping through the list use execute.&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_0&quot; class=&quot;hljs language-python&quot;&gt;cursor.executemany(&lt;span class=&quot;hljs-string&quot;&gt;&quot;&quot;&quot;
              INSERT INTO table
              (data)
              VALUES (?)
              &quot;&quot;&quot;&lt;/span&gt;, data_list)
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_0&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;OR&lt;/p&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;pre&gt;&lt;code id=&quot;code_1&quot; class=&quot;hljs language-css&quot;&gt;for &lt;span class=&quot;hljs-selector-tag&quot;&gt;i&lt;/span&gt; in data_list:
    cursor.&lt;span class=&quot;hljs-built_in&quot;&gt;execute&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&quot;INSERT INTO table (data) VALUES (?)&quot;&lt;/span&gt;, i)
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_1&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;solution_2&quot;&gt;Solution 2:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;You should iterate list like this:&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_2&quot; class=&quot;hljs language-lua&quot;&gt;cursor_three=conn.cursor()
&lt;span class=&quot;hljs-keyword&quot;&gt;for&lt;/span&gt; elem &lt;span class=&quot;hljs-keyword&quot;&gt;in&lt;/span&gt; database_list:
    cursor_three.&lt;span class=&quot;hljs-built_in&quot;&gt;execute&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&#39;INSERT INTO table (data) VALUES (%s)&#39;&lt;/span&gt; % elem) 
cursor_three.commit()  
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_2&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;After that you will have &lt;em&gt;sess_id&lt;/em&gt; column empty unless you declared it autoincrement row. If you have two sorted arrays for example&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_3&quot; class=&quot;hljs language-ini&quot;&gt;&lt;span class=&quot;hljs-attr&quot;&gt;database_id_list&lt;/span&gt; = [&lt;span class=&quot;hljs-number&quot;&gt;212&lt;/span&gt;, &lt;span class=&quot;hljs-number&quot;&gt;444&lt;/span&gt;, &lt;span class=&quot;hljs-number&quot;&gt;532&lt;/span&gt;...]
&lt;span class=&quot;hljs-attr&quot;&gt;database_data_list&lt;/span&gt;= [&lt;span class=&quot;hljs-string&quot;&gt;&#39;elem212&#39;&lt;/span&gt;,&lt;span class=&quot;hljs-string&quot;&gt;&#39;elem444&#39;&lt;/span&gt;,&lt;span class=&quot;hljs-string&quot;&gt;&#39;elem532&#39;&lt;/span&gt;...]
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_3&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;You should use &lt;em&gt;zip()&lt;/em&gt; function to iterate them simulteniously, like this:&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_4&quot; class=&quot;hljs language-python&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;for&lt;/span&gt; elem &lt;span class=&quot;hljs-keyword&quot;&gt;in&lt;/span&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;zip&lt;/span&gt;(database_id_list, database_data_list):
    cursor_three.execute(&lt;span class=&quot;hljs-string&quot;&gt;&#39;INSERT INTO table (sess_id, data) VALUES (%s, %s)&#39;&lt;/span&gt; % elem)  &lt;span class=&quot;hljs-comment&quot;&gt;# elem is tuple here&lt;/span&gt;&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_4&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;Notice that this code is using string fromat function for simplness (you didn&#39;t noticed which database you&#39;re using) and it is not protected from &lt;em&gt;SQL injections&lt;/em&gt;. You better use your sql deriver&#39;s formatter, for example for psycopg2:&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_5&quot; class=&quot;hljs language-lua&quot;&gt;cursor_three.&lt;span class=&quot;hljs-built_in&quot;&gt;execute&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&#39;INSERT INTO table (data) VALUES (%s)&#39;&lt;/span&gt;, [elem])
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_5&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;/div&gt; 


</content><link rel='replies' type='application/atom+xml' href='https://sumitmotwani.blogspot.com/feeds/8942854297526639618/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://sumitmotwani.blogspot.com/2024/08/inserting-list-in-specific-column-using.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/5301110832091710857/posts/default/8942854297526639618'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/5301110832091710857/posts/default/8942854297526639618'/><link rel='alternate' type='text/html' href='https://sumitmotwani.blogspot.com/2024/08/inserting-list-in-specific-column-using.html' title='Inserting List In Specific Column Using Python'/><author><name>Jackie Leffler</name><uri>http://www.blogger.com/profile/07006275599358224644</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5301110832091710857.post-1838833446539385352</id><published>2024-08-21T09:33:00.001-07:00</published><updated>2024-08-21T09:33:47.451-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Sql Server"/><category scheme="http://www.blogger.com/atom/ns#" term="Sql Server 2008"/><title type='text'>What Are The Advantages Of Merge Over Simple If Exists?</title><content type='html'>&lt;div class=&quot;question&quot;&gt;I want to know what are the advantages of MERGE over simply using IF EXISTS. Which is the suggested approach? Does MERGE performs Update and Insert row-by-row matching conditions?&lt;/div&gt;&lt;h2 id=&quot;solution_1&quot;&gt;Solution 1:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;MERGE combines INSERT, UPDATE and DELETE logic into one DML statement, and therefore is atomic. If you are doing single row UPSERTS then the advantages are less obvious. For example, a naive implementation of an UPSERT may look like the following:&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_0&quot; class=&quot;hljs language-sql&quot;&gt;IF  &lt;span class=&quot;hljs-keyword&quot;&gt;EXISTS&lt;/span&gt; (&lt;span class=&quot;hljs-keyword&quot;&gt;SELECT&lt;/span&gt;&lt;span class=&quot;hljs-operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;FROM&lt;/span&gt; t1 &lt;span class=&quot;hljs-keyword&quot;&gt;where&lt;/span&gt; id&lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;hljs-variable&quot;&gt;@id&lt;/span&gt;)
    &lt;span class=&quot;hljs-keyword&quot;&gt;UPDATE&lt;/span&gt; t1 &lt;span class=&quot;hljs-keyword&quot;&gt;SET&lt;/span&gt; ... &lt;span class=&quot;hljs-keyword&quot;&gt;WHERE&lt;/span&gt; id&lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;hljs-variable&quot;&gt;@id&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;ELSE&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;INSERT&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;INTO&lt;/span&gt; t1 (...) &lt;span class=&quot;hljs-keyword&quot;&gt;VALUES&lt;/span&gt; (...)
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_0&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;However, without wrapping this in a transaction, it is possible that the row we&#39;re going to update will be deleted between the SELECT and the UPDATE. Adding minimal logic to address that issue give us this:&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_1&quot; class=&quot;hljs language-sql&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;BEGIN&lt;/span&gt; TRAN
IF  &lt;span class=&quot;hljs-keyword&quot;&gt;EXISTS&lt;/span&gt; (&lt;span class=&quot;hljs-keyword&quot;&gt;SELECT&lt;/span&gt;&lt;span class=&quot;hljs-operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;FROM&lt;/span&gt; t1 &lt;span class=&quot;hljs-keyword&quot;&gt;WITH&lt;/span&gt; (HOLDLOCK, UPDLOCK) &lt;span class=&quot;hljs-keyword&quot;&gt;where&lt;/span&gt; id&lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;hljs-variable&quot;&gt;@id&lt;/span&gt; )
    &lt;span class=&quot;hljs-keyword&quot;&gt;UPDATE&lt;/span&gt; t1 &lt;span class=&quot;hljs-keyword&quot;&gt;SET&lt;/span&gt; ... &lt;span class=&quot;hljs-keyword&quot;&gt;WHERE&lt;/span&gt; id&lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;hljs-variable&quot;&gt;@id&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;ELSE&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;INSERT&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;INTO&lt;/span&gt; t1 (...) &lt;span class=&quot;hljs-keyword&quot;&gt;VALUES&lt;/span&gt; (...)
&lt;span class=&quot;hljs-keyword&quot;&gt;COMMIT&lt;/span&gt;&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_1&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;This logic isn&#39;t necessary with the MERGE statement. &lt;/p&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;p&gt;There are no comparisons that should be drawn between CURSORS and the MERGE statement.&lt;/p&gt;&lt;/div&gt;&lt;h2 id=&quot;solution_2&quot;&gt;Solution 2:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;Merge will give you the option of updating, inserting and deleting data in a target table where is it matched in a source table. It is a set based operation so is not like a cursor (row by row)&lt;/p&gt;&lt;p&gt;I am not sure how you mean by advantages over &#39;IF EXISTS&#39;, but merge is a useful and flexible way of synchronizing 2 tables&lt;/p&gt;&lt;p&gt;this is a useful resource for merge &lt;a rel=&quot;nofollow noreferrer noopener&quot; href=&quot;https://www.simple-talk.com/sql/learn-sql-server/the-merge-statement-in-sql-server-2008/&quot;&gt;https://www.simple-talk.com/sql/learn-sql-server/the-merge-statement-in-sql-server-2008/&lt;/a&gt;&lt;/p&gt;&lt;/div&gt; 


</content><link rel='replies' type='application/atom+xml' href='https://sumitmotwani.blogspot.com/feeds/1838833446539385352/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://sumitmotwani.blogspot.com/2024/08/what-are-advantages-of-merge-over.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/5301110832091710857/posts/default/1838833446539385352'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/5301110832091710857/posts/default/1838833446539385352'/><link rel='alternate' type='text/html' href='https://sumitmotwani.blogspot.com/2024/08/what-are-advantages-of-merge-over.html' title='What Are The Advantages Of Merge Over Simple If Exists?'/><author><name>Jackie Leffler</name><uri>http://www.blogger.com/profile/07006275599358224644</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5301110832091710857.post-192643255156288276</id><published>2024-08-21T09:17:00.001-07:00</published><updated>2024-08-21T09:17:46.628-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Pivot"/><category scheme="http://www.blogger.com/atom/ns#" term="Postgresql"/><category scheme="http://www.blogger.com/atom/ns#" term="Sql"/><title type='text'>How Do You Convert Rows To Columns In Postgres?</title><content type='html'>&lt;div class=&quot;question&quot;&gt;I have a table with the following columns: user_id, grade. user_id | grade 1         k 1         3 2         4 2         5 3         2  I would like to be able to make a table that&lt;/div&gt;&lt;h2 id=&quot;solution_1&quot;&gt;Solution 1:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;Using filtered aggregation is typically much easier than using the &lt;code&gt;crosstab()&lt;/code&gt; function:&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_1&quot; class=&quot;hljs language-csharp&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;select&lt;/span&gt; user_id, 
       max(grade) filter(&lt;span class=&quot;hljs-keyword&quot;&gt;where&lt;/span&gt; grade = &lt;span class=&quot;hljs-string&quot;&gt;&#39;k&#39;&lt;/span&gt;) &lt;span class=&quot;hljs-keyword&quot;&gt;is&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;not&lt;/span&gt;&lt;span class=&quot;hljs-literal&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;as&lt;/span&gt; teaches_grade_k,
       max(grade) filter(&lt;span class=&quot;hljs-keyword&quot;&gt;where&lt;/span&gt; grade = &lt;span class=&quot;hljs-string&quot;&gt;&#39;1&#39;&lt;/span&gt;) &lt;span class=&quot;hljs-keyword&quot;&gt;is&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;not&lt;/span&gt;&lt;span class=&quot;hljs-literal&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;as&lt;/span&gt; teaches_grade_1,
       max(grade) filter(&lt;span class=&quot;hljs-keyword&quot;&gt;where&lt;/span&gt; grade = &lt;span class=&quot;hljs-string&quot;&gt;&#39;2&#39;&lt;/span&gt;) &lt;span class=&quot;hljs-keyword&quot;&gt;is&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;not&lt;/span&gt;&lt;span class=&quot;hljs-literal&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;as&lt;/span&gt; teaches_grade_2,
       max(grade) filter(&lt;span class=&quot;hljs-keyword&quot;&gt;where&lt;/span&gt; grade = &lt;span class=&quot;hljs-string&quot;&gt;&#39;3&#39;&lt;/span&gt;) &lt;span class=&quot;hljs-keyword&quot;&gt;is&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;not&lt;/span&gt;&lt;span class=&quot;hljs-literal&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;as&lt;/span&gt; teaches_grade_3,
       max(grade) filter(&lt;span class=&quot;hljs-keyword&quot;&gt;where&lt;/span&gt; grade = &lt;span class=&quot;hljs-string&quot;&gt;&#39;4&#39;&lt;/span&gt;) &lt;span class=&quot;hljs-function&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;is&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;not&lt;/span&gt;&lt;span class=&quot;hljs-literal&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;as&lt;/span&gt; teaches_grade_5
       &lt;span class=&quot;hljs-title&quot;&gt;max&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt;grade&lt;/span&gt;) &lt;span class=&quot;hljs-title&quot;&gt;filter&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;where&lt;/span&gt; grade = &lt;span class=&quot;hljs-string&quot;&gt;&#39;5&#39;&lt;/span&gt;&lt;/span&gt;) &lt;span class=&quot;hljs-keyword&quot;&gt;is&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;not&lt;/span&gt;&lt;span class=&quot;hljs-literal&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;as&lt;/span&gt; teaches_grade_5
&lt;span class=&quot;hljs-keyword&quot;&gt;from&lt;/span&gt; users
&lt;span class=&quot;hljs-keyword&quot;&gt;group&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;by&lt;/span&gt; user_id&lt;/span&gt;;       
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_1&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;solution_2&quot;&gt;Solution 2:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;Another option, similar to a filtered aggregation, would be to use a regular array aggregation and check for existence, like&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_2&quot; class=&quot;hljs language-sql&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;WITH&lt;/span&gt; users (user_id, grade) &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; (
    &lt;span class=&quot;hljs-keyword&quot;&gt;VALUES&lt;/span&gt;
        (&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;k&#39;&lt;/span&gt;),
        (&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;1&#39;&lt;/span&gt;),
        (&lt;span class=&quot;hljs-number&quot;&gt;2&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;4&#39;&lt;/span&gt;),
        (&lt;span class=&quot;hljs-number&quot;&gt;2&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;5&#39;&lt;/span&gt;),
        (&lt;span class=&quot;hljs-number&quot;&gt;3&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;2&#39;&lt;/span&gt;)
),
user_grades &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; (
    &lt;span class=&quot;hljs-keyword&quot;&gt;SELECT&lt;/span&gt; user_id, &lt;span class=&quot;hljs-built_in&quot;&gt;array_agg&lt;/span&gt;(grade) &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; grades
    &lt;span class=&quot;hljs-keyword&quot;&gt;FROM&lt;/span&gt; users
    &lt;span class=&quot;hljs-keyword&quot;&gt;GROUP&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;BY&lt;/span&gt; user_id
)
&lt;span class=&quot;hljs-keyword&quot;&gt;SELECT&lt;/span&gt;
    user_id,
    &lt;span class=&quot;hljs-string&quot;&gt;&#39;k&#39;&lt;/span&gt;&lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;ANY&lt;/span&gt;(grades) &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; teaches_k,
    &lt;span class=&quot;hljs-string&quot;&gt;&#39;1&#39;&lt;/span&gt;&lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;ANY&lt;/span&gt;(grades) &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; teaches_1,
    &lt;span class=&quot;hljs-string&quot;&gt;&#39;2&#39;&lt;/span&gt;&lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;ANY&lt;/span&gt;(grades) &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; teaches_2,
    &lt;span class=&quot;hljs-string&quot;&gt;&#39;3&#39;&lt;/span&gt;&lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;ANY&lt;/span&gt;(grades) &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; teaches_3,
    &lt;span class=&quot;hljs-string&quot;&gt;&#39;4&#39;&lt;/span&gt;&lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;ANY&lt;/span&gt;(grades) &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; teaches_4,
    &lt;span class=&quot;hljs-string&quot;&gt;&#39;5&#39;&lt;/span&gt;&lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;ANY&lt;/span&gt;(grades) &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; teaches_5
&lt;span class=&quot;hljs-keyword&quot;&gt;FROM&lt;/span&gt;
    user_grades
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_2&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;</content><link rel='replies' type='application/atom+xml' href='https://sumitmotwani.blogspot.com/feeds/192643255156288276/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://sumitmotwani.blogspot.com/2024/08/how-do-you-convert-rows-to-columns-in.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/5301110832091710857/posts/default/192643255156288276'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/5301110832091710857/posts/default/192643255156288276'/><link rel='alternate' type='text/html' href='https://sumitmotwani.blogspot.com/2024/08/how-do-you-convert-rows-to-columns-in.html' title='How Do You Convert Rows To Columns In Postgres?'/><author><name>Jackie Leffler</name><uri>http://www.blogger.com/profile/07006275599358224644</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5301110832091710857.post-753762366002885245</id><published>2024-08-21T08:25:00.001-07:00</published><updated>2024-08-21T08:25:27.349-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Sql"/><category scheme="http://www.blogger.com/atom/ns#" term="Sql Server"/><title type='text'>How To Get Renamed, Deleted, And Added Columns Of The Table?</title><content type='html'>&lt;div class=&quot;question&quot;&gt;Suppose I have a table in a database (TblExample) that contains three columns:  Name Age City  After some time, someone added one extra column (Country) or deleted a column (City)&lt;/div&gt;&lt;h2 id=&quot;solution_1&quot;&gt;Solution 1:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;From the tag I see you are using SQL, you can check out DDL triggers: &lt;/p&gt;&lt;p&gt;&lt;a rel=&quot;nofollow noreferrer noopener&quot; href=&quot;http://sqlserverpedia.com/blog/sql-server-security/csidba-finding-out-who-made-schema-changes/&quot;&gt;http://sqlserverpedia.com/blog/sql-server-security/csidba-finding-out-who-made-schema-changes/&lt;/a&gt;&lt;/p&gt;&lt;/div&gt;&lt;/p&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;</content><link rel='replies' type='application/atom+xml' href='https://sumitmotwani.blogspot.com/feeds/753762366002885245/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://sumitmotwani.blogspot.com/2024/08/how-to-get-renamed-deleted-and-added.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/5301110832091710857/posts/default/753762366002885245'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/5301110832091710857/posts/default/753762366002885245'/><link rel='alternate' type='text/html' href='https://sumitmotwani.blogspot.com/2024/08/how-to-get-renamed-deleted-and-added.html' title='How To Get Renamed, Deleted, And Added Columns Of The Table?'/><author><name>Jackie Leffler</name><uri>http://www.blogger.com/profile/07006275599358224644</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5301110832091710857.post-149420781728264896</id><published>2024-08-21T08:05:00.001-07:00</published><updated>2024-08-21T08:05:47.283-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term=".net"/><category scheme="http://www.blogger.com/atom/ns#" term="Asp.net"/><category scheme="http://www.blogger.com/atom/ns#" term="C#"/><category scheme="http://www.blogger.com/atom/ns#" term="Database"/><category scheme="http://www.blogger.com/atom/ns#" term="Sql Server"/><title type='text'>Adapt Beginner C# Sql Server Insert Example To Work With My Database</title><content type='html'>&lt;div class=&quot;question&quot;&gt;I have read TONS of tutorials, articles and whatever regarding my issue and honestly, due to my lack of experience I can&#39;t twist my fingers around this one so I hope some of you gu&lt;/div&gt;&lt;h2 id=&quot;solution_1&quot;&gt;Solution 1:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;blockquote&gt;&lt;p&gt;Here is some advice to get you going, learning programming is a lot of
trial and error.&lt;/p&gt;&lt;/blockquote&gt;&lt;ol&gt;&lt;li&gt;&lt;p&gt;Start off basic, litrally put three textboxes on a form/page and a
button. &lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;Double click the button to go the code-behind and view the buttons
click event. &lt;/p&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;Paste in the body of code included with your question (everything in the try-catch). &lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;Put a break-point on the Public Void Button_Click line of code and press F11 to
step through the code.&lt;/p&gt;&lt;/li&gt;&lt;/ol&gt;&lt;blockquote&gt;&lt;p&gt;&quot;one thing is having the code-behind working but how to make the buttons and textboxes working is still a misery&quot;*&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;Put the textbox as the value rather than your hardcoded values:&lt;/p&gt;&lt;p&gt;cmd.Parameters.AddWithValue(&quot;@Address&quot;, textBox1.Text);&lt;/p&gt;&lt;p&gt;You also should not insert the Id value, instead modify the EmployeeDetails table and set the ID column to in the properties set Identity Specification (IS Identity) = True. Then right click the ID column and set Primary Key. &lt;/p&gt;&lt;p&gt;Post any error messages you encounter here and when you do get get it working, an additional exercise (that will be very valuable for you) would use a database stored procedure rather than ad-hoc SQL, to safe-guard against sql-injection attacks.&lt;/p&gt;&lt;p&gt;&lt;em&gt;I&#39;m assuming you have SQL Server installed and have a &#39;employee&#39; database with a table called EmployeeDetails.&lt;/em&gt;&lt;/p&gt;&lt;/div&gt;&lt;h2 id=&quot;solution_2&quot;&gt;Solution 2:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;pre&gt;&lt;code id=&quot;code_0&quot; class=&quot;hljs language-csharp&quot;&gt;&lt;span class=&quot;hljs-function&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;protected&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;hljs-title&quot;&gt;GvManualShows_RowDataBound&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;object&lt;/span&gt; sender, GridViewRowEventArgs e&lt;/span&gt;)&lt;/span&gt;
{
    &lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;/span&gt; (e.Row.RowType == DataControlRowType.DataRow)
    {
        &lt;span class=&quot;hljs-comment&quot;&gt;//label lbl = (label)e.Row.FindControl(&quot;lblHidden&quot;);&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;/span&gt; (e.Row.Cells[&lt;span class=&quot;hljs-number&quot;&gt;14&lt;/span&gt;].Text == &lt;span class=&quot;hljs-string&quot;&gt;&quot;Y&quot;&lt;/span&gt;)
        {
            &lt;span class=&quot;hljs-comment&quot;&gt;// CheckBox cb = (CheckBox)e.Row.FindControl(&quot;chk&quot;);&lt;/span&gt;

            CheckBox chk = (CheckBox)e.Row.Cells[&lt;span class=&quot;hljs-number&quot;&gt;0&lt;/span&gt;].FindControl(&lt;span class=&quot;hljs-string&quot;&gt;&quot;chkBox&quot;&lt;/span&gt;);
            chk.Checked = &lt;span class=&quot;hljs-literal&quot;&gt;true&lt;/span&gt;;

        }
    }
}
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_0&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;solution_3&quot;&gt;Solution 3:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;It&#39;s fairly simple. You just have to modify the connection string, the query and its parameters:&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_1&quot; class=&quot;hljs language-csharp&quot;&gt;&lt;span class=&quot;hljs-function&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;private&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;hljs-title&quot;&gt;button1_Click&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;object&lt;/span&gt; sender, EventArgs e&lt;/span&gt;)&lt;/span&gt;
{
    &lt;span class=&quot;hljs-keyword&quot;&gt;try&lt;/span&gt;
    {
        &lt;span class=&quot;hljs-built_in&quot;&gt;string&lt;/span&gt; connectionString =
            &lt;span class=&quot;hljs-string&quot;&gt;&quot;server=SQLServer;&quot;&lt;/span&gt; +         &lt;span class=&quot;hljs-comment&quot;&gt;// SQLServer is your SQL server machine&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;&quot;initial catalog=employee;&quot;&lt;/span&gt; + &lt;span class=&quot;hljs-comment&quot;&gt;// employee is your database&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;&quot;user id=sa;&quot;&lt;/span&gt; +               &lt;span class=&quot;hljs-comment&quot;&gt;// sa is the login to connect the database&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;&quot;password=sa123&quot;&lt;/span&gt;;             &lt;span class=&quot;hljs-comment&quot;&gt;// sa123 is the password of the login&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;using&lt;/span&gt; (SqlConnection conn =
            &lt;span class=&quot;hljs-keyword&quot;&gt;new&lt;/span&gt; SqlConnection(connectionString))
        {
            conn.Open();
            &lt;span class=&quot;hljs-keyword&quot;&gt;using&lt;/span&gt; (SqlCommand cmd = &lt;span class=&quot;hljs-keyword&quot;&gt;new&lt;/span&gt; SqlCommand(
                &lt;span class=&quot;hljs-string&quot;&gt;&quot;INSERT INTO [NyhedTB] ([NyhedDato], [NyhedTitel], [NyhedTekst]) &quot;&lt;/span&gt; +
                &lt;span class=&quot;hljs-string&quot;&gt;&quot;VALUES (@NyhedDato, @NyhedTitel, @NyhedTekst)&quot;&lt;/span&gt;, conn))
            {
                cmd.Parameters.AddWithValue(&lt;span class=&quot;hljs-string&quot;&gt;&quot;@NyhedDato&quot;&lt;/span&gt;, textBoxDate.Text);
                cmd.Parameters.AddWithValue(&lt;span class=&quot;hljs-string&quot;&gt;&quot;@NyhedTitel&quot;&lt;/span&gt;, textBoxTitle.Text);
                cmd.Parameters.AddWithValue(&lt;span class=&quot;hljs-string&quot;&gt;&quot;@NyhedTekst&quot;&lt;/span&gt;, textBoxBody.Text);

                &lt;span class=&quot;hljs-built_in&quot;&gt;int&lt;/span&gt; rows = cmd.ExecuteNonQuery();  &lt;span class=&quot;hljs-comment&quot;&gt;// Inserted rows number&lt;/span&gt;
            }
        }
    }
    &lt;span class=&quot;hljs-keyword&quot;&gt;catch&lt;/span&gt; (SqlException ex)
    {
        &lt;span class=&quot;hljs-comment&quot;&gt;//Log exception&lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;//Display Error message&lt;/span&gt;
    }
}
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_1&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;solution_4&quot;&gt;Solution 4:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;I made changed example code with your requirements and added comments, hope it would be a bit clearer for you to understand whats going on:&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_2&quot; class=&quot;hljs language-csharp&quot;&gt;&lt;span class=&quot;hljs-function&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;static&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;hljs-title&quot;&gt;Insert&lt;/span&gt;()&lt;/span&gt;
{
    &lt;span class=&quot;hljs-keyword&quot;&gt;try&lt;/span&gt;
    {
        &lt;span class=&quot;hljs-built_in&quot;&gt;string&lt;/span&gt; connectionString =
        &lt;span class=&quot;hljs-string&quot;&gt;&quot;server=.;&quot;&lt;/span&gt; +
        &lt;span class=&quot;hljs-string&quot;&gt;&quot;initial catalog=MyDatabaseName;&quot;&lt;/span&gt; + &lt;span class=&quot;hljs-comment&quot;&gt;//here you write database name where your NyhedTB table is&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;&quot;user id=sa;&quot;&lt;/span&gt; + &lt;span class=&quot;hljs-comment&quot;&gt;//user name to connect to database&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;&quot;password=sa123&quot;&lt;/span&gt;; &lt;span class=&quot;hljs-comment&quot;&gt;//password&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;using&lt;/span&gt; (SqlConnection conn = &lt;span class=&quot;hljs-keyword&quot;&gt;new&lt;/span&gt; SqlConnection(connectionString))
        {
            conn.Open();
            &lt;span class=&quot;hljs-keyword&quot;&gt;using&lt;/span&gt; (SqlCommand cmd =
              &lt;span class=&quot;hljs-keyword&quot;&gt;new&lt;/span&gt; SqlCommand(&lt;span class=&quot;hljs-string&quot;&gt;&quot;INSERT INTO NyhedTB (NyhedDato, NyhedTitel, NyhedTekst) VALUES (@NyhedDato, @NyhedTitel, @NyhedTekst)&quot;&lt;/span&gt;, conn))
            {
                &lt;span class=&quot;hljs-comment&quot;&gt;//all &quot;things&quot; in your sql command what beggins with @ &lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;//means that it is parameter and you need to pass values for these parameters: &lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;//For @NyhedDato parameter you set text from your textbox&lt;/span&gt;
                cmd.Parameters.AddWithValue(&lt;span class=&quot;hljs-string&quot;&gt;&quot;@NyhedDato&quot;&lt;/span&gt;, txtDate.Text);

                &lt;span class=&quot;hljs-comment&quot;&gt;//For @NyhedTitel parameter you set text from title textbox&lt;/span&gt;
                cmd.Parameters.AddWithValue(&lt;span class=&quot;hljs-string&quot;&gt;&quot;@NyhedTitel&quot;&lt;/span&gt;, txtTitle.Text);

                &lt;span class=&quot;hljs-comment&quot;&gt;//For @NyhedTekst parameter you set text from content textbox&lt;/span&gt;
                cmd.Parameters.AddWithValue(&lt;span class=&quot;hljs-string&quot;&gt;&quot;@NyhedTekst&quot;&lt;/span&gt;, txtContent.Text);

                &lt;span class=&quot;hljs-comment&quot;&gt;//Execute insert command and get how many records was efected, in this case it should be rows = 1 because you inserting just one record&lt;/span&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;int&lt;/span&gt; rows = cmd.ExecuteNonQuery();
            }
        }
    }
    &lt;span class=&quot;hljs-keyword&quot;&gt;catch&lt;/span&gt; (SqlException ex)
    {
       &lt;span class=&quot;hljs-comment&quot;&gt;//Log exception&lt;/span&gt;&lt;span class=&quot;hljs-comment&quot;&gt;//Display Error message&lt;/span&gt;
    }
}
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_2&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;P.s. code not tested. And when you say &lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;I have 3 rows that has to be inserted:
Headline
Date
Content/the news itself.&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;actually you mean you want to insert &lt;code&gt;record&lt;/code&gt; with &lt;code&gt;fields&lt;/code&gt;&lt;/p&gt;&lt;/div&gt; 

</content><link rel='replies' type='application/atom+xml' href='https://sumitmotwani.blogspot.com/feeds/149420781728264896/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://sumitmotwani.blogspot.com/2024/08/adapt-beginner-c-sql-server-insert.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/5301110832091710857/posts/default/149420781728264896'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/5301110832091710857/posts/default/149420781728264896'/><link rel='alternate' type='text/html' href='https://sumitmotwani.blogspot.com/2024/08/adapt-beginner-c-sql-server-insert.html' title='Adapt Beginner C# Sql Server Insert Example To Work With My Database'/><author><name>Jackie Leffler</name><uri>http://www.blogger.com/profile/07006275599358224644</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5301110832091710857.post-5023979170237558898</id><published>2024-08-21T07:47:00.001-07:00</published><updated>2024-08-21T07:47:55.948-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Oracle"/><category scheme="http://www.blogger.com/atom/ns#" term="Select"/><category scheme="http://www.blogger.com/atom/ns#" term="Sql"/><title type='text'>How To To Give Preference To Null Value During Select</title><content type='html'>&lt;div class=&quot;question&quot;&gt;I have a table with Id      value  1000    null 1000    En 1000    Fr 1000    Es 1001    En 1001    Fr 1001    Es  Output of the select query should be as follows. (Since 1000 has&lt;/div&gt;&lt;h2 id=&quot;solution_1&quot;&gt;Solution 1:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;You can use &lt;code&gt;NOT EXISTS&lt;/code&gt; and a correlated subquery to check for the non-existence of a &lt;code&gt;NULL&lt;/code&gt; for an ID. Include these rows and also rows where &lt;code&gt;value&lt;/code&gt; is &lt;code&gt;NULL&lt;/code&gt;.&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_4&quot; class=&quot;hljs language-sql&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;SELECT&lt;/span&gt; t1.id,
       t1.value
       &lt;span class=&quot;hljs-keyword&quot;&gt;FROM&lt;/span&gt; elbat t1
       &lt;span class=&quot;hljs-keyword&quot;&gt;WHERE&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;NOT&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;EXISTS&lt;/span&gt; (&lt;span class=&quot;hljs-keyword&quot;&gt;SELECT&lt;/span&gt;&lt;span class=&quot;hljs-operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;FROM&lt;/span&gt; elbat t2
                                &lt;span class=&quot;hljs-keyword&quot;&gt;WHERE&lt;/span&gt; t2.id &lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt; t1.id
                                      &lt;span class=&quot;hljs-keyword&quot;&gt;AND&lt;/span&gt; t2.value &lt;span class=&quot;hljs-keyword&quot;&gt;IS&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;NULL&lt;/span&gt;)
              &lt;span class=&quot;hljs-keyword&quot;&gt;OR&lt;/span&gt; t1.value &lt;span class=&quot;hljs-keyword&quot;&gt;IS&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;NULL&lt;/span&gt;;
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_4&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;solution_2&quot;&gt;Solution 2:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;pre&gt;&lt;code id=&quot;code_5&quot; class=&quot;hljs language-sql&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;with&lt;/span&gt;
  t (id, &lt;span class=&quot;hljs-keyword&quot;&gt;value&lt;/span&gt;) &lt;span class=&quot;hljs-keyword&quot;&gt;as&lt;/span&gt; (
    &lt;span class=&quot;hljs-keyword&quot;&gt;select&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1000&lt;/span&gt;, &lt;span class=&quot;hljs-keyword&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;from&lt;/span&gt; dual &lt;span class=&quot;hljs-keyword&quot;&gt;union&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;all&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;select&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1000&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;En&#39;&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;from&lt;/span&gt; dual &lt;span class=&quot;hljs-keyword&quot;&gt;union&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;all&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;select&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1000&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;Fr&#39;&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;from&lt;/span&gt; dual &lt;span class=&quot;hljs-keyword&quot;&gt;union&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;all&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;select&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1000&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;Es&#39;&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;from&lt;/span&gt; dual &lt;span class=&quot;hljs-keyword&quot;&gt;union&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;all&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;select&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1001&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;En&#39;&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;from&lt;/span&gt; dual &lt;span class=&quot;hljs-keyword&quot;&gt;union&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;all&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;select&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1001&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;Fr&#39;&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;from&lt;/span&gt; dual &lt;span class=&quot;hljs-keyword&quot;&gt;union&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;all&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;select&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1001&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;Es&#39;&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;from&lt;/span&gt; dual
  )
&lt;span class=&quot;hljs-keyword&quot;&gt;select&lt;/span&gt; id, &lt;span class=&quot;hljs-keyword&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;from&lt;/span&gt;   (
         &lt;span class=&quot;hljs-keyword&quot;&gt;select&lt;/span&gt; t.&lt;span class=&quot;hljs-operator&quot;&gt;*&lt;/span&gt;, 
                &lt;span class=&quot;hljs-built_in&quot;&gt;dense_rank&lt;/span&gt;() &lt;span class=&quot;hljs-keyword&quot;&gt;over&lt;/span&gt; (&lt;span class=&quot;hljs-keyword&quot;&gt;partition&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;by&lt;/span&gt; id &lt;span class=&quot;hljs-keyword&quot;&gt;order&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;by&lt;/span&gt; nvl2(&lt;span class=&quot;hljs-keyword&quot;&gt;value&lt;/span&gt;, &lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;, &lt;span class=&quot;hljs-number&quot;&gt;0&lt;/span&gt;)) rnk
         &lt;span class=&quot;hljs-keyword&quot;&gt;from&lt;/span&gt;   t
       )
&lt;span class=&quot;hljs-keyword&quot;&gt;where&lt;/span&gt;  rnk &lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;
;

        ID VA
&lt;span class=&quot;hljs-comment&quot;&gt;---------- --&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1001&lt;/span&gt; En
      &lt;span class=&quot;hljs-number&quot;&gt;1001&lt;/span&gt; Fr
      &lt;span class=&quot;hljs-number&quot;&gt;1001&lt;/span&gt; Es
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_5&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;Functions used in this query:&lt;/p&gt;&lt;p&gt;&lt;code&gt;NVL2()&lt;/code&gt;&lt;a rel=&quot;nofollow noreferrer noopener&quot; href=&quot;https://docs.oracle.com/database/121/SQLRF/functions132.htm#SQLRF00685&quot;&gt;https://docs.oracle.com/database/121/SQLRF/functions132.htm#SQLRF00685&lt;/a&gt;&lt;/p&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;p&gt;&lt;code&gt;DENSE_RANK()&lt;/code&gt;&lt;a rel=&quot;nofollow noreferrer noopener&quot; href=&quot;https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions043.htm&quot;&gt;https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions043.htm&lt;/a&gt;&lt;/p&gt;&lt;/div&gt;&lt;h2 id=&quot;solution_3&quot;&gt;Solution 3:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;In the most recent versions of Oracle, you can actually do this without a subquery:&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_8&quot; class=&quot;hljs language-sql&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;select&lt;/span&gt; t.&lt;span class=&quot;hljs-operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;from&lt;/span&gt; t
&lt;span class=&quot;hljs-keyword&quot;&gt;order&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;by&lt;/span&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;rank&lt;/span&gt;() &lt;span class=&quot;hljs-keyword&quot;&gt;over&lt;/span&gt; (&lt;span class=&quot;hljs-keyword&quot;&gt;partition&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;by&lt;/span&gt; id &lt;span class=&quot;hljs-keyword&quot;&gt;order&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;by&lt;/span&gt; (&lt;span class=&quot;hljs-keyword&quot;&gt;case&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;when&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;is&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;then&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;))
&lt;span class=&quot;hljs-keyword&quot;&gt;fetch&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;first&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hljs-type&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;with&lt;/span&gt; ties;
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_8&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;&lt;a rel=&quot;nofollow noreferrer noopener&quot; href=&quot;https://dbfiddle.uk/?rdbms=oracle_18&amp;amp;fiddle=22eeac524d370d0100300ce02c5791a5&quot;&gt;Here&lt;/a&gt; is a db&amp;lt;&amp;gt;fiddle.&lt;/p&gt;&lt;/div&gt;&lt;h2 id=&quot;solution_4&quot;&gt;Solution 4:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;You can use &lt;code&gt;analytical function&lt;/code&gt; as following:&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_10&quot; class=&quot;hljs language-sql&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;Select&lt;/span&gt; id , &lt;span class=&quot;hljs-keyword&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;from&lt;/span&gt;
(&lt;span class=&quot;hljs-keyword&quot;&gt;Select&lt;/span&gt; t.&lt;span class=&quot;hljs-operator&quot;&gt;*&lt;/span&gt;,
       &lt;span class=&quot;hljs-built_in&quot;&gt;Coalesce&lt;/span&gt;(&lt;span class=&quot;hljs-built_in&quot;&gt;Sum&lt;/span&gt;(&lt;span class=&quot;hljs-keyword&quot;&gt;case&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;when&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;is&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;then&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;end&lt;/span&gt;) &lt;span class=&quot;hljs-keyword&quot;&gt;over&lt;/span&gt; (&lt;span class=&quot;hljs-keyword&quot;&gt;partition&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;by&lt;/span&gt; id), &lt;span class=&quot;hljs-number&quot;&gt;0&lt;/span&gt;) &lt;span class=&quot;hljs-keyword&quot;&gt;as&lt;/span&gt; cnt
 &lt;span class=&quot;hljs-keyword&quot;&gt;From&lt;/span&gt; your_table)
&lt;span class=&quot;hljs-keyword&quot;&gt;Where&lt;/span&gt; (cnt &lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;and&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;is&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;null&lt;/span&gt;)
    &lt;span class=&quot;hljs-keyword&quot;&gt;or&lt;/span&gt; cnt &lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;0&lt;/span&gt;&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_10&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;Cheers!!&lt;/p&gt;&lt;/div&gt; 

</content><link rel='replies' type='application/atom+xml' href='https://sumitmotwani.blogspot.com/feeds/5023979170237558898/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://sumitmotwani.blogspot.com/2024/08/how-to-to-give-preference-to-null-value.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/5301110832091710857/posts/default/5023979170237558898'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/5301110832091710857/posts/default/5023979170237558898'/><link rel='alternate' type='text/html' href='https://sumitmotwani.blogspot.com/2024/08/how-to-to-give-preference-to-null-value.html' title='How To To Give Preference To Null Value During Select'/><author><name>Jackie Leffler</name><uri>http://www.blogger.com/profile/07006275599358224644</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5301110832091710857.post-2785577992476318014</id><published>2024-08-21T06:53:00.001-07:00</published><updated>2024-08-21T06:53:45.741-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Sql Server"/><category scheme="http://www.blogger.com/atom/ns#" term="Stored Procedures"/><title type='text'>I Want To Write One Common Stored Procedure To Insert Data In Any Table Of My Database</title><content type='html'>&lt;div class=&quot;question&quot;&gt;I have 6 table with different fields. I want to access table name dynamically. is there any idea to do it? My code is below this is simple procedure which I want to make dynamic to&lt;/div&gt;&lt;h2 id=&quot;solution_1&quot;&gt;Solution 1:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;&lt;strong&gt;Don&#39;t go there.&lt;/strong&gt;
It&#39;s a bad idea since you will end up with a long, inefficient stored procedure that will be vulnerable to SQL injection attacks and have performance issues.
Writing an insert stored procedure for each table is the way to go.&lt;/p&gt;&lt;p&gt;You wrote you have six different tables with different columns, so writing a stored procedure to handle inserts for all of them will require you to send all the parameters for all columns as well as a parameter for the table name, and a nested if...else with 6 possible paths, one for each table.
This will end up as a long, messy, poorly written code at best, bad in each parameter: security, performance, code readability and maintainability. &lt;/p&gt;&lt;p&gt;The only way that makes some sense to achieve such a goal is to write individual insert stored procedures for each table, and then write a stored procedure that will take all of the possible parameters and the table name and inside of it decide what insert stored procedure to execute based on the value of the table name parameter. &lt;strong&gt;However&lt;/strong&gt;, you will be better off leaving conditions like these to the SQL client (your c# code in this case) then to SQL Server.&lt;/p&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;/div&gt;&lt;h2 id=&quot;solution_2&quot;&gt;Solution 2:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;Its very easy to do.........&lt;/p&gt;&lt;p&gt;Just call the sql query using Data Adapter.&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_0&quot; class=&quot;hljs language-csharp&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;select&lt;/span&gt; TABLE_NAME &lt;span class=&quot;hljs-keyword&quot;&gt;from&lt;/span&gt; INFORMATION_SCHEMA.TABLES
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_0&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;solution_3&quot;&gt;Solution 3:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;As you said you need dynamic SQL like this:&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_1&quot; class=&quot;hljs language-sql&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;Create&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;procedure&lt;/span&gt; [dbo].[Insert_Data]
(
    &lt;span class=&quot;hljs-variable&quot;&gt;@TableName&lt;/span&gt; nvarchar(&lt;span class=&quot;hljs-number&quot;&gt;512&lt;/span&gt;),
    &lt;span class=&quot;hljs-variable&quot;&gt;@Values&lt;/span&gt; nvarchar(max)
)
&lt;span class=&quot;hljs-keyword&quot;&gt;BEGIN&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;DECLARE&lt;/span&gt;&lt;span class=&quot;hljs-variable&quot;&gt;@SQL&lt;/span&gt; nvarchar(max)
    &lt;span class=&quot;hljs-keyword&quot;&gt;SELECT&lt;/span&gt;&lt;span class=&quot;hljs-variable&quot;&gt;@SQL&lt;/span&gt;&lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;&#39;INSERT INTO &#39;&lt;/span&gt;&lt;span class=&quot;hljs-operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;hljs-variable&quot;&gt;@TableName&lt;/span&gt;&lt;span class=&quot;hljs-operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;&#39; VALUES (&#39;&lt;/span&gt;&lt;span class=&quot;hljs-operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;hljs-variable&quot;&gt;@Values&lt;/span&gt;&lt;span class=&quot;hljs-operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;&#39;)&#39;&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;EXEC&lt;/span&gt;(&lt;span class=&quot;hljs-variable&quot;&gt;@SQL&lt;/span&gt;)
&lt;span class=&quot;hljs-keyword&quot;&gt;END&lt;/span&gt;&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_1&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;Note that &lt;code&gt;@Values&lt;/code&gt; will be like this &lt;code&gt;&#39;1, &#39;&#39;name&#39;&#39;, 10.2&#39;&lt;/code&gt; and with the same order of columns.&lt;/p&gt;&lt;p&gt;or&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_4&quot; class=&quot;hljs language-sql&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;Create&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;procedure&lt;/span&gt; [dbo].[Insert_Data]
(
    &lt;span class=&quot;hljs-variable&quot;&gt;@TableName&lt;/span&gt; nvarchar(&lt;span class=&quot;hljs-number&quot;&gt;512&lt;/span&gt;),
    &lt;span class=&quot;hljs-variable&quot;&gt;@Fields&lt;/span&gt; nvarchar(max),
    &lt;span class=&quot;hljs-variable&quot;&gt;@Values&lt;/span&gt; nvarchar(max)
)
&lt;span class=&quot;hljs-keyword&quot;&gt;BEGIN&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;DECLARE&lt;/span&gt;&lt;span class=&quot;hljs-variable&quot;&gt;@SQL&lt;/span&gt; nvarchar(max)
    &lt;span class=&quot;hljs-keyword&quot;&gt;SELECT&lt;/span&gt;&lt;span class=&quot;hljs-variable&quot;&gt;@SQL&lt;/span&gt;&lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;&#39;INSERT INTO &#39;&lt;/span&gt;&lt;span class=&quot;hljs-operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;hljs-variable&quot;&gt;@TableName&lt;/span&gt;&lt;span class=&quot;hljs-operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;&#39; (&#39;&lt;/span&gt;&lt;span class=&quot;hljs-operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;hljs-variable&quot;&gt;@Fields&lt;/span&gt;&lt;span class=&quot;hljs-operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;&#39;) VALUES (&#39;&lt;/span&gt;&lt;span class=&quot;hljs-operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;hljs-variable&quot;&gt;@Values&lt;/span&gt;&lt;span class=&quot;hljs-operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;&#39;)&#39;&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;EXEC&lt;/span&gt;(&lt;span class=&quot;hljs-variable&quot;&gt;@SQL&lt;/span&gt;)
&lt;span class=&quot;hljs-keyword&quot;&gt;END&lt;/span&gt;&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_4&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;To more ability to handle column order and remove identity columns.&lt;/p&gt;&lt;/div&gt;&lt;h2 id=&quot;solution_4&quot;&gt;Solution 4:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;As Robert Harvey mentioned it is a bad idea, anyway if you want to you can do something like....&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_5&quot; class=&quot;hljs language-sql&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;CREATE&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;PROCEDURE&lt;/span&gt; Insert_Data
 &lt;span class=&quot;hljs-variable&quot;&gt;@TableName&lt;/span&gt;     SYSNAME
,&lt;span class=&quot;hljs-variable&quot;&gt;@Column1&lt;/span&gt;       SYSNAME         &lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;NULL&lt;/span&gt;
,&lt;span class=&quot;hljs-variable&quot;&gt;@Column2&lt;/span&gt;       SYSNAME         &lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;NULL&lt;/span&gt; 
,&lt;span class=&quot;hljs-variable&quot;&gt;@Column3&lt;/span&gt;       SYSNAME         &lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;NULL&lt;/span&gt;
,&lt;span class=&quot;hljs-variable&quot;&gt;@Value1&lt;/span&gt;        NVARCHAR(&lt;span class=&quot;hljs-number&quot;&gt;100&lt;/span&gt;)   &lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;NULL&lt;/span&gt;
,&lt;span class=&quot;hljs-variable&quot;&gt;@Value2&lt;/span&gt;        NVARCHAR(&lt;span class=&quot;hljs-number&quot;&gt;100&lt;/span&gt;)   &lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;NULL&lt;/span&gt;
,&lt;span class=&quot;hljs-variable&quot;&gt;@Value3&lt;/span&gt;        NVARCHAR(&lt;span class=&quot;hljs-number&quot;&gt;100&lt;/span&gt;)   &lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;BEGIN&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;SET&lt;/span&gt; NOCOUNT &lt;span class=&quot;hljs-keyword&quot;&gt;ON&lt;/span&gt;;
  &lt;span class=&quot;hljs-keyword&quot;&gt;DECLARE&lt;/span&gt;&lt;span class=&quot;hljs-variable&quot;&gt;@Sql&lt;/span&gt; NVARCHAR(MAX);

&lt;span class=&quot;hljs-keyword&quot;&gt;SET&lt;/span&gt;&lt;span class=&quot;hljs-variable&quot;&gt;@Sql&lt;/span&gt;&lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt; N&lt;span class=&quot;hljs-string&quot;&gt;&#39; INSERT INTO &#39;&lt;/span&gt;&lt;span class=&quot;hljs-operator&quot;&gt;+&lt;/span&gt; QUOTENAME(&lt;span class=&quot;hljs-variable&quot;&gt;@TableName&lt;/span&gt;) 
         &lt;span class=&quot;hljs-operator&quot;&gt;+&lt;/span&gt; N&lt;span class=&quot;hljs-string&quot;&gt;&#39; ( &#39;&lt;/span&gt;&lt;span class=&quot;hljs-operator&quot;&gt;+&lt;/span&gt; STUFF(
           &lt;span class=&quot;hljs-keyword&quot;&gt;CASE&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;WHEN&lt;/span&gt;&lt;span class=&quot;hljs-variable&quot;&gt;@Column1&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;IS&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;NOT&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;THEN&lt;/span&gt; N&lt;span class=&quot;hljs-string&quot;&gt;&#39;,&#39;&lt;/span&gt;&lt;span class=&quot;hljs-operator&quot;&gt;+&lt;/span&gt; QUOTENAME(&lt;span class=&quot;hljs-variable&quot;&gt;@Column1&lt;/span&gt;) &lt;span class=&quot;hljs-keyword&quot;&gt;ELSE&lt;/span&gt; N&lt;span class=&quot;hljs-string&quot;&gt;&#39;&#39;&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;END&lt;/span&gt;&lt;span class=&quot;hljs-operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;CASE&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;WHEN&lt;/span&gt;&lt;span class=&quot;hljs-variable&quot;&gt;@Column2&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;IS&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;NOT&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;THEN&lt;/span&gt; N&lt;span class=&quot;hljs-string&quot;&gt;&#39;,&#39;&lt;/span&gt;&lt;span class=&quot;hljs-operator&quot;&gt;+&lt;/span&gt; QUOTENAME(&lt;span class=&quot;hljs-variable&quot;&gt;@Column2&lt;/span&gt;) &lt;span class=&quot;hljs-keyword&quot;&gt;ELSE&lt;/span&gt; N&lt;span class=&quot;hljs-string&quot;&gt;&#39;&#39;&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;END&lt;/span&gt;&lt;span class=&quot;hljs-operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;CASE&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;WHEN&lt;/span&gt;&lt;span class=&quot;hljs-variable&quot;&gt;@Column3&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;IS&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;NOT&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;THEN&lt;/span&gt; N&lt;span class=&quot;hljs-string&quot;&gt;&#39;,&#39;&lt;/span&gt;&lt;span class=&quot;hljs-operator&quot;&gt;+&lt;/span&gt; QUOTENAME(&lt;span class=&quot;hljs-variable&quot;&gt;@Column3&lt;/span&gt;) &lt;span class=&quot;hljs-keyword&quot;&gt;ELSE&lt;/span&gt; N&lt;span class=&quot;hljs-string&quot;&gt;&#39;&#39;&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;END&lt;/span&gt;
                 ,&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;,&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;,&lt;span class=&quot;hljs-string&quot;&gt;&#39;&#39;&lt;/span&gt;)
         &lt;span class=&quot;hljs-operator&quot;&gt;+&lt;/span&gt; N&lt;span class=&quot;hljs-string&quot;&gt;&#39; ) &#39;&lt;/span&gt;&lt;span class=&quot;hljs-operator&quot;&gt;+&lt;/span&gt; N&lt;span class=&quot;hljs-string&quot;&gt;&#39; VALUES ( &#39;&lt;/span&gt;&lt;span class=&quot;hljs-operator&quot;&gt;+&lt;/span&gt;  STUFF(
           &lt;span class=&quot;hljs-keyword&quot;&gt;CASE&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;WHEN&lt;/span&gt;&lt;span class=&quot;hljs-variable&quot;&gt;@Value1&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;IS&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;NOT&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;THEN&lt;/span&gt; N&lt;span class=&quot;hljs-string&quot;&gt;&#39;, @Value1&#39;&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;ELSE&lt;/span&gt; N&lt;span class=&quot;hljs-string&quot;&gt;&#39;&#39;&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;END&lt;/span&gt;&lt;span class=&quot;hljs-operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;CASE&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;WHEN&lt;/span&gt;&lt;span class=&quot;hljs-variable&quot;&gt;@Value2&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;IS&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;NOT&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;THEN&lt;/span&gt; N&lt;span class=&quot;hljs-string&quot;&gt;&#39;, @Value2&#39;&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;ELSE&lt;/span&gt; N&lt;span class=&quot;hljs-string&quot;&gt;&#39;&#39;&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;END&lt;/span&gt;&lt;span class=&quot;hljs-operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;CASE&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;WHEN&lt;/span&gt;&lt;span class=&quot;hljs-variable&quot;&gt;@Value3&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;IS&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;NOT&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;THEN&lt;/span&gt; N&lt;span class=&quot;hljs-string&quot;&gt;&#39;, @Value3&#39;&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;ELSE&lt;/span&gt; N&lt;span class=&quot;hljs-string&quot;&gt;&#39;&#39;&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;END&lt;/span&gt;  
             ,&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;,&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;,&lt;span class=&quot;hljs-string&quot;&gt;&#39;&#39;&lt;/span&gt;)
          &lt;span class=&quot;hljs-operator&quot;&gt;+&lt;/span&gt; N&lt;span class=&quot;hljs-string&quot;&gt;&#39; ) &#39;&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;Exec&lt;/span&gt; sp_executesql &lt;span class=&quot;hljs-variable&quot;&gt;@Sql&lt;/span&gt;
                  ,N&lt;span class=&quot;hljs-string&quot;&gt;&#39;@Value1 NVARCHAR(100),@Value2 NVARCHAR(100),@Value3 NVARCHAR(100)&#39;&lt;/span&gt;
                  ,&lt;span class=&quot;hljs-variable&quot;&gt;@Value1&lt;/span&gt;
                  ,&lt;span class=&quot;hljs-variable&quot;&gt;@Value2&lt;/span&gt;
                  ,&lt;span class=&quot;hljs-variable&quot;&gt;@Value3&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;END&lt;/span&gt;&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_5&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;/div&gt; 

</content><link rel='replies' type='application/atom+xml' href='https://sumitmotwani.blogspot.com/feeds/2785577992476318014/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://sumitmotwani.blogspot.com/2024/08/i-want-to-write-one-common-stored.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/5301110832091710857/posts/default/2785577992476318014'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/5301110832091710857/posts/default/2785577992476318014'/><link rel='alternate' type='text/html' href='https://sumitmotwani.blogspot.com/2024/08/i-want-to-write-one-common-stored.html' title='I Want To Write One Common Stored Procedure To Insert Data In Any Table Of My Database'/><author><name>Jackie Leffler</name><uri>http://www.blogger.com/profile/07006275599358224644</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5301110832091710857.post-8852993153651899276</id><published>2024-08-21T06:15:00.001-07:00</published><updated>2024-08-21T06:15:46.667-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Base64"/><category scheme="http://www.blogger.com/atom/ns#" term="C#"/><category scheme="http://www.blogger.com/atom/ns#" term="Sql Server"/><category scheme="http://www.blogger.com/atom/ns#" term="Tobase64string"/><title type='text'>Convert Image Data Type Into Base64 And Base64 Into Image Data Type</title><content type='html'>&lt;div class=&quot;question&quot;&gt;I&#39;m using Data-Type &#39;Image&#39; in MS SQL 2012 to store Image. problem: I have an image in BASE64 string in C# /9j/4AAQSkZJRgABAQEASABIAAD/4SKhRXhpZgAATU0AKgAAAAgABwESAAMAAAABAAEAAAEaA&lt;/div&gt;&lt;h2 id=&quot;solution_1&quot;&gt;Solution 1:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;I found the solution for this, Please look into this because it might be useful when you do not want to change your datatype Image in your database:&lt;/p&gt;&lt;p&gt;I sent base64 string as it is to database and there I converted it into varbinary like this:&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_0&quot; class=&quot;hljs language-sql&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;SELECT&lt;/span&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;CAST&lt;/span&gt;(N&lt;span class=&quot;hljs-string&quot;&gt;&#39;&#39;&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; xml).&lt;span class=&quot;hljs-keyword&quot;&gt;value&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&#39;xs:base64Binary(sql:variable(&quot;@Image2&quot;))&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;varbinary(max)&#39;&lt;/span&gt;)
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_0&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;and inserted it into the image column.&lt;/p&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;p&gt;then I retrieved an image in base64 like this:&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_1&quot; class=&quot;hljs language-sql&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;SELECT&lt;/span&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;cast&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&#39;&#39;&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;as&lt;/span&gt; xml).&lt;span class=&quot;hljs-keyword&quot;&gt;value&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&#39;xs:base64Binary(sql:column(&quot;img&quot;))&#39;&lt;/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&#39;varchar(max)&#39;&lt;/span&gt;) &lt;span class=&quot;hljs-keyword&quot;&gt;FROM&lt;/span&gt; imageTemp WHERE...
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_1&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;It gave me the exact Base64 string which I have sent earlier.&lt;/p&gt;&lt;p&gt;Thank You.&lt;/p&gt;&lt;/div&gt;&lt;h2 id=&quot;solution_2&quot;&gt;Solution 2:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;I believe you need help on how to store image data in sql server. &lt;/p&gt;&lt;p&gt;You can create a table like below. Please note that you may not want to create database , just a table would suffice. If you already have a table where you want to save this data, you will have to make sure that the type of column is varbinary(max).&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_2&quot; class=&quot;hljs language-sql&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;CREATE&lt;/span&gt; DATABASE MyDatabase;  
GO  
USE MyDatabase;  
GO  
&lt;span class=&quot;hljs-keyword&quot;&gt;CREATE&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;TABLE&lt;/span&gt; MyImageDatabaseTable (Id &lt;span class=&quot;hljs-type&quot;&gt;int&lt;/span&gt;, BLOBData &lt;span class=&quot;hljs-type&quot;&gt;varbinary&lt;/span&gt;(max));  
GO  
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_2&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;Then you can read the data as:&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_3&quot; class=&quot;hljs language-java&quot;&gt;&lt;span class=&quot;hljs-type&quot;&gt;SqlConnection&lt;/span&gt;&lt;span class=&quot;hljs-variable&quot;&gt;objConn&lt;/span&gt;&lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;hljs-title class_&quot;&gt;SqlConnection&lt;/span&gt;(connectionString);  
objConn.Open();  
&lt;span class=&quot;hljs-type&quot;&gt;SqlCommand&lt;/span&gt;&lt;span class=&quot;hljs-variable&quot;&gt;objCmd&lt;/span&gt;&lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;hljs-title class_&quot;&gt;SqlCommand&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&quot;Select * From MyImageDatabaseTable&quot;&lt;/span&gt;, objConn);  
&lt;span class=&quot;hljs-type&quot;&gt;SqlDataReader&lt;/span&gt;&lt;span class=&quot;hljs-variable&quot;&gt;dr&lt;/span&gt;&lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt; objCmd.ExecuteReader();  
&lt;span class=&quot;hljs-keyword&quot;&gt;while&lt;/span&gt;(dr.Read())
{
     &lt;span class=&quot;hljs-type&quot;&gt;byte&lt;/span&gt;[] myImageByteArrayData = (&lt;span class=&quot;hljs-type&quot;&gt;byte&lt;/span&gt;[]) dr[&lt;span class=&quot;hljs-string&quot;&gt;&quot;BLOBData&quot;&lt;/span&gt;];  

     &lt;span class=&quot;hljs-type&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;hljs-variable&quot;&gt;myImageBase64StringData&lt;/span&gt;&lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt; Convert.ToBase64String(myImageByteArrayData );
}
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_3&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;Hope this helps.&lt;/p&gt;&lt;/div&gt;&lt;h2 id=&quot;solution_3&quot;&gt;Solution 3:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;Answering your question&lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;How to retrieve base64 from database using this byte[] array?&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;SQL-Server will transform your binary to base64 implicitly when embedding it into XML.&lt;/p&gt;&lt;p&gt;Try this&lt;/p&gt;&lt;pre class=&quot;lang-sql prettyprint-override&quot;&gt;&lt;code id=&quot;code_4&quot; class=&quot;hljs language-sql&quot;&gt;&lt;span class=&quot;hljs-comment&quot;&gt;--I fill a binary variable with some dummy content&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;DECLARE&lt;/span&gt;&lt;span class=&quot;hljs-variable&quot;&gt;@SomeBinary&lt;/span&gt;&lt;span class=&quot;hljs-type&quot;&gt;VARBINARY&lt;/span&gt;(MAX) &lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;CAST&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&#39;This is a dummy content&#39;&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt;&lt;span class=&quot;hljs-type&quot;&gt;VARBINARY&lt;/span&gt;(MAX));
&lt;span class=&quot;hljs-comment&quot;&gt;--this is the resulting binary&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;SELECT&lt;/span&gt;&lt;span class=&quot;hljs-variable&quot;&gt;@SomeBinary&lt;/span&gt;;
&lt;span class=&quot;hljs-comment&quot;&gt;--Now you see what happens using FOR XML PATH&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;SELECT&lt;/span&gt; (&lt;span class=&quot;hljs-keyword&quot;&gt;SELECT&lt;/span&gt;&lt;span class=&quot;hljs-variable&quot;&gt;@SomeBinary&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;FOR&lt;/span&gt; XML PATH(&lt;span class=&quot;hljs-string&quot;&gt;&#39;&#39;&lt;/span&gt;))
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_4&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;We can re-convert this easily&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_5&quot; class=&quot;hljs language-sql&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;DECLARE&lt;/span&gt;&lt;span class=&quot;hljs-variable&quot;&gt;@base64&lt;/span&gt;&lt;span class=&quot;hljs-type&quot;&gt;VARCHAR&lt;/span&gt;(MAX)&lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;&#39;VGhpcyBpcyBhIGR1bW15IGNvbnRlbnQ=&#39;&lt;/span&gt;;
&lt;span class=&quot;hljs-keyword&quot;&gt;SELECT&lt;/span&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;CAST&lt;/span&gt;(&lt;span class=&quot;hljs-variable&quot;&gt;@base64&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; XML).&lt;span class=&quot;hljs-keyword&quot;&gt;value&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&#39;.&#39;&lt;/span&gt;,&lt;span class=&quot;hljs-string&quot;&gt;&#39;varbinary(max)&#39;&lt;/span&gt;);
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_5&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;Probably you&#39;ll have to use a &lt;code&gt;CAST(YourImageTypedColumn AS VARBINARY(MAX))&lt;/code&gt; first.&lt;/p&gt;&lt;p&gt;But it might be easier to retrieve this as binary and to the conversion on your client side application.&lt;/p&gt;&lt;/div&gt;&lt;h2 id=&quot;solution_4&quot;&gt;Solution 4:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;I use this method bellow, to convert bytearray from database to an image.&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_7&quot; class=&quot;hljs language-php&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;public&lt;/span&gt; Bitmap ConvertByteArrayToBitmap(byte[] &lt;span class=&quot;hljs-keyword&quot;&gt;Array&lt;/span&gt;)
{
    &lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;/span&gt; (&lt;span class=&quot;hljs-keyword&quot;&gt;Array&lt;/span&gt; == &lt;span class=&quot;hljs-literal&quot;&gt;null&lt;/span&gt;) &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;hljs-literal&quot;&gt;null&lt;/span&gt;;

    using (System.IO.MemoryStream ms = &lt;span class=&quot;hljs-keyword&quot;&gt;new&lt;/span&gt; System.IO.MemoryStream())
    {
        ms.Write(&lt;span class=&quot;hljs-keyword&quot;&gt;Array&lt;/span&gt;, &lt;span class=&quot;hljs-number&quot;&gt;0&lt;/span&gt;, &lt;span class=&quot;hljs-keyword&quot;&gt;Array&lt;/span&gt;.Length);
        ms.Position = &lt;span class=&quot;hljs-number&quot;&gt;0&lt;/span&gt;L;

        &lt;span class=&quot;hljs-keyword&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;new&lt;/span&gt; Bitmap(ms);
    }
}
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_7&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;Regards.&lt;/p&gt;&lt;/div&gt;&lt;h2 id=&quot;solution_5&quot;&gt;Solution 5:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;You can Convert binary to Base64 using XML and the hint &quot;for xml path&quot;&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_8&quot; class=&quot;hljs language-sql&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;select&lt;/span&gt; file_name,ColumnWithBinary,ColumnToSwFinalResult
&lt;span class=&quot;hljs-keyword&quot;&gt;from&lt;/span&gt; TableWithBinary
&lt;span class=&quot;hljs-keyword&quot;&gt;cross&lt;/span&gt; apply (&lt;span class=&quot;hljs-keyword&quot;&gt;select&lt;/span&gt; ColumnWithBinary &lt;span class=&quot;hljs-string&quot;&gt;&#39;*&#39;&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;for&lt;/span&gt; xml path(&lt;span class=&quot;hljs-string&quot;&gt;&#39;&#39;&lt;/span&gt;)) T (ColumnToSwFinalResult)
GO
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_8&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;&lt;a rel=&quot;nofollow noreferrer noopener&quot; href=&quot;https://i.stack.imgur.com/Kg5Qp.png&quot;&gt;&lt;img alt=&quot;enter image description here&quot; src=&quot;https://i.stack.imgur.com/Kg5Qp.png&quot;&gt;&lt;/a&gt;&lt;/p&gt;&lt;/div&gt; 

</content><link rel='replies' type='application/atom+xml' href='https://sumitmotwani.blogspot.com/feeds/8852993153651899276/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://sumitmotwani.blogspot.com/2024/08/convert-image-data-type-into-base64-and.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/5301110832091710857/posts/default/8852993153651899276'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/5301110832091710857/posts/default/8852993153651899276'/><link rel='alternate' type='text/html' href='https://sumitmotwani.blogspot.com/2024/08/convert-image-data-type-into-base64-and.html' title='Convert Image Data Type Into Base64 And Base64 Into Image Data Type'/><author><name>Jackie Leffler</name><uri>http://www.blogger.com/profile/07006275599358224644</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5301110832091710857.post-4623592563123013310</id><published>2024-08-21T05:19:00.001-07:00</published><updated>2024-08-21T05:19:31.898-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Database"/><category scheme="http://www.blogger.com/atom/ns#" term="Heroku"/><category scheme="http://www.blogger.com/atom/ns#" term="Laravel"/><category scheme="http://www.blogger.com/atom/ns#" term="Php"/><category scheme="http://www.blogger.com/atom/ns#" term="Sqlite"/><title type='text'>Deploying Laravel On Heroku &quot;pdoexception In Connector.php Line 55&quot; (sqlite)</title><content type='html'>&lt;div class=&quot;question&quot;&gt;I tried to deploy my first Laravel project on Heroku. It&#39;s a small project, so I&#39;m using SQLite as my database. After deploying the project using git push heroku master, and clicki&lt;/div&gt;&lt;h2 id=&quot;solution_1&quot;&gt;Solution 1:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;I&#39;ve also encountered this on Heroku. &lt;/p&gt;&lt;p&gt;I added:&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_0&quot; class=&quot;hljs language-json&quot;&gt;&lt;span class=&quot;hljs-punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;hljs-attr&quot;&gt;&quot;require&quot;&lt;/span&gt;&lt;span class=&quot;hljs-punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;hljs-punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;hljs-attr&quot;&gt;&quot;ext-pdo_sqlite&quot;&lt;/span&gt;&lt;span class=&quot;hljs-punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;&quot;*&quot;&lt;/span&gt;&lt;span class=&quot;hljs-punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;hljs-punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_0&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;solution_2&quot;&gt;Solution 2:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;Sqlite is setup as a shared extension on Heroku, but has to be enabled through &lt;code&gt;composer.json&lt;/code&gt; i believe.&lt;/p&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;p&gt;Try adding:&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_2&quot; class=&quot;hljs language-json&quot;&gt;&lt;span class=&quot;hljs-punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;hljs-attr&quot;&gt;&quot;require&quot;&lt;/span&gt;&lt;span class=&quot;hljs-punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;hljs-punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;hljs-attr&quot;&gt;&quot;sqlite3&quot;&lt;/span&gt;&lt;span class=&quot;hljs-punctuation&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;&quot;*&quot;&lt;/span&gt;&lt;span class=&quot;hljs-punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;hljs-punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_2&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;To your &lt;code&gt;composer.json&lt;/code&gt; file.&lt;/p&gt;&lt;/div&gt; 


</content><link rel='replies' type='application/atom+xml' href='https://sumitmotwani.blogspot.com/feeds/4623592563123013310/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://sumitmotwani.blogspot.com/2024/08/deploying-laravel-on-heroku.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/5301110832091710857/posts/default/4623592563123013310'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/5301110832091710857/posts/default/4623592563123013310'/><link rel='alternate' type='text/html' href='https://sumitmotwani.blogspot.com/2024/08/deploying-laravel-on-heroku.html' title='Deploying Laravel On Heroku &quot;pdoexception In Connector.php Line 55&quot; (sqlite)'/><author><name>Jackie Leffler</name><uri>http://www.blogger.com/profile/07006275599358224644</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5301110832091710857.post-3317400873532973844</id><published>2024-08-21T04:42:00.001-07:00</published><updated>2024-08-21T04:42:57.405-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="C#"/><category scheme="http://www.blogger.com/atom/ns#" term="Sql"/><category scheme="http://www.blogger.com/atom/ns#" term="Sql Server"/><title type='text'>Complex / Algorithmic Sql Query</title><content type='html'>&lt;div class=&quot;question&quot;&gt;I am wondering if its possible to implement an SQL Query that will act like a sort of algorithm to calculate a certain figure for me based on the following table: This is the initi&lt;/div&gt;&lt;h2 id=&quot;solution_1&quot;&gt;Solution 1:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;Sure... try something like this.&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_0&quot; class=&quot;hljs language-csharp&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;select&lt;/span&gt; e1.Requestor, 
       e1.TimeOfAction &lt;span class=&quot;hljs-keyword&quot;&gt;as&lt;/span&gt; LoginTime, 
       (&lt;span class=&quot;hljs-function&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;select&lt;/span&gt;&lt;span class=&quot;hljs-title&quot;&gt;min&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt;ActivityTime&lt;/span&gt;)
        &lt;span class=&quot;hljs-keyword&quot;&gt;from&lt;/span&gt; EventLog &lt;span class=&quot;hljs-keyword&quot;&gt;where&lt;/span&gt; TimeOfAction &amp;gt; e1.TimeOfAction 
        &lt;span class=&quot;hljs-keyword&quot;&gt;and&lt;/span&gt; Activity&lt;/span&gt; = &lt;span class=&quot;hljs-string&quot;&gt;&#39;Logout&#39;&lt;/span&gt;) &lt;span class=&quot;hljs-keyword&quot;&gt;as&lt;/span&gt; LogoutTime
&lt;span class=&quot;hljs-keyword&quot;&gt;from&lt;/span&gt; EventLog e1
&lt;span class=&quot;hljs-keyword&quot;&gt;where&lt;/span&gt; e1.ActivityType = &lt;span class=&quot;hljs-string&quot;&gt;&#39;Login&#39;&lt;/span&gt;
order &lt;span class=&quot;hljs-keyword&quot;&gt;by&lt;/span&gt; Requestor, LoginTime
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_0&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;Second Solution... See if this works better for ya.&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_1&quot; class=&quot;hljs language-csharp&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;select&lt;/span&gt; requestor,
    (&lt;span class=&quot;hljs-function&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;select&lt;/span&gt;&lt;span class=&quot;hljs-title&quot;&gt;min&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt;activitytime&lt;/span&gt;)
     &lt;span class=&quot;hljs-keyword&quot;&gt;from&lt;/span&gt; eventlog 
     &lt;span class=&quot;hljs-keyword&quot;&gt;where&lt;/span&gt; activitytime &amp;lt; e.activitytime
     &lt;span class=&quot;hljs-keyword&quot;&gt;and&lt;/span&gt; activity&lt;/span&gt; = &lt;span class=&quot;hljs-string&quot;&gt;&#39;Login&#39;&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;and&lt;/span&gt; e.activity = &lt;span class=&quot;hljs-string&quot;&gt;&#39;Logout&#39;&lt;/span&gt;) &lt;span class=&quot;hljs-keyword&quot;&gt;as&lt;/span&gt; LoginTime, 
    (&lt;span class=&quot;hljs-function&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;select&lt;/span&gt;&lt;span class=&quot;hljs-title&quot;&gt;max&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt;activitytime&lt;/span&gt;)
     &lt;span class=&quot;hljs-keyword&quot;&gt;from&lt;/span&gt; eventlog 
     &lt;span class=&quot;hljs-keyword&quot;&gt;where&lt;/span&gt; activitytime &amp;gt; e.activitytime
     &lt;span class=&quot;hljs-keyword&quot;&gt;and&lt;/span&gt; activity&lt;/span&gt; = &lt;span class=&quot;hljs-string&quot;&gt;&#39;Logout&#39;&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;and&lt;/span&gt; e.activity = &lt;span class=&quot;hljs-string&quot;&gt;&#39;Login&#39;&lt;/span&gt;) &lt;span class=&quot;hljs-keyword&quot;&gt;as&lt;/span&gt; LogoutTime, 
&lt;span class=&quot;hljs-keyword&quot;&gt;from&lt;/span&gt; eventlog e
order &lt;span class=&quot;hljs-keyword&quot;&gt;by&lt;/span&gt; requestor, logintime
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_1&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;solution_2&quot;&gt;Solution 2:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;pre&gt;&lt;code id=&quot;code_2&quot; class=&quot;hljs language-csharp&quot;&gt;&lt;span class=&quot;hljs-function&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;select&lt;/span&gt;&lt;span class=&quot;hljs-title&quot;&gt;min&lt;/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt;TimeOfAction&lt;/span&gt;) Login, &lt;span class=&quot;hljs-literal&quot;&gt;null&lt;/span&gt; Logout, Requestor
  &lt;span class=&quot;hljs-keyword&quot;&gt;from&lt;/span&gt; EventLog
 &lt;span class=&quot;hljs-keyword&quot;&gt;where&lt;/span&gt; Activity&lt;/span&gt; = &lt;span class=&quot;hljs-string&quot;&gt;&#39;Login&#39;&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;group&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;by&lt;/span&gt; Requestor
union
&lt;span class=&quot;hljs-keyword&quot;&gt;select&lt;/span&gt;&lt;span class=&quot;hljs-literal&quot;&gt;null&lt;/span&gt; Login, max(TimeOfAction) Logout, Requestor
  &lt;span class=&quot;hljs-keyword&quot;&gt;from&lt;/span&gt; EventLog
 &lt;span class=&quot;hljs-keyword&quot;&gt;where&lt;/span&gt; Activity = &lt;span class=&quot;hljs-string&quot;&gt;&#39;Logout&#39;&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;group&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;by&lt;/span&gt; Requestor
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_2&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;solution_3&quot;&gt;Solution 3:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;Heres an option for you using some CTEs and row_numbers. Basically, it orders the events for each user, then finds the list of logins that either follow a logout or nothing, then finds a list of logouts that proceed logins or nothing, then associates them into pairs.&lt;/p&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;pre&gt;&lt;code id=&quot;code_3&quot; class=&quot;hljs language-sql&quot;&gt;;&lt;span class=&quot;hljs-keyword&quot;&gt;with&lt;/span&gt; events &lt;span class=&quot;hljs-keyword&quot;&gt;as&lt;/span&gt; (
  &lt;span class=&quot;hljs-keyword&quot;&gt;select&lt;/span&gt;&lt;span class=&quot;hljs-operator&quot;&gt;*&lt;/span&gt;,
         &lt;span class=&quot;hljs-built_in&quot;&gt;row_number&lt;/span&gt;() &lt;span class=&quot;hljs-keyword&quot;&gt;over&lt;/span&gt;(&lt;span class=&quot;hljs-keyword&quot;&gt;partition&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;by&lt;/span&gt; Requestor &lt;span class=&quot;hljs-keyword&quot;&gt;order&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;by&lt;/span&gt; TimeOfAction) &lt;span class=&quot;hljs-type&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;from&lt;/span&gt; EventLog
), logins &lt;span class=&quot;hljs-keyword&quot;&gt;as&lt;/span&gt; (
  &lt;span class=&quot;hljs-keyword&quot;&gt;select&lt;/span&gt; e1.Activity, e1.TimeOfAction, e1.Requestor,
         &lt;span class=&quot;hljs-built_in&quot;&gt;row_number&lt;/span&gt;() &lt;span class=&quot;hljs-keyword&quot;&gt;over&lt;/span&gt;(&lt;span class=&quot;hljs-keyword&quot;&gt;partition&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;by&lt;/span&gt; e1.Requestor &lt;span class=&quot;hljs-keyword&quot;&gt;order&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;by&lt;/span&gt; e1.TimeOfAction) &lt;span class=&quot;hljs-type&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;from&lt;/span&gt; events e1
    &lt;span class=&quot;hljs-keyword&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;join&lt;/span&gt; events e2 &lt;span class=&quot;hljs-keyword&quot;&gt;on&lt;/span&gt; e1.Requestor&lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;e2.Requestor
                       &lt;span class=&quot;hljs-keyword&quot;&gt;and&lt;/span&gt; e1.row&lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;e2.row&lt;span class=&quot;hljs-operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;where&lt;/span&gt; e1.Activity&lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;&#39;Login&#39;&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;and&lt;/span&gt; e1.Activity&lt;span class=&quot;hljs-operator&quot;&gt;!=&lt;/span&gt;isnull(e2.Activity, &lt;span class=&quot;hljs-string&quot;&gt;&#39;Logout&#39;&lt;/span&gt;)
), logouts &lt;span class=&quot;hljs-keyword&quot;&gt;as&lt;/span&gt; (
  &lt;span class=&quot;hljs-keyword&quot;&gt;select&lt;/span&gt; e1.Activity, e1.TimeOfAction, e1.Requestor,
         &lt;span class=&quot;hljs-built_in&quot;&gt;row_number&lt;/span&gt;() &lt;span class=&quot;hljs-keyword&quot;&gt;over&lt;/span&gt;(&lt;span class=&quot;hljs-keyword&quot;&gt;partition&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;by&lt;/span&gt; e1.Requestor &lt;span class=&quot;hljs-keyword&quot;&gt;order&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;by&lt;/span&gt; e1.TimeOfAction) &lt;span class=&quot;hljs-type&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;from&lt;/span&gt; events e1
    &lt;span class=&quot;hljs-keyword&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;join&lt;/span&gt; events e2 &lt;span class=&quot;hljs-keyword&quot;&gt;on&lt;/span&gt; e1.Requestor&lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;e2.Requestor
                       &lt;span class=&quot;hljs-keyword&quot;&gt;and&lt;/span&gt; e1.row&lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;e2.row&lt;span class=&quot;hljs-number&quot;&gt;-1&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;where&lt;/span&gt; e1.Activity&lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;&#39;Logout&#39;&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;and&lt;/span&gt; e1.Activity&lt;span class=&quot;hljs-operator&quot;&gt;!=&lt;/span&gt;isnull(e2.Activity, &lt;span class=&quot;hljs-string&quot;&gt;&#39;Login&#39;&lt;/span&gt;)
)
&lt;span class=&quot;hljs-keyword&quot;&gt;select&lt;/span&gt; i.Requestor, i.TimeOfAction &lt;span class=&quot;hljs-keyword&quot;&gt;as&lt;/span&gt; loginTime, o.TimeOfAction &lt;span class=&quot;hljs-keyword&quot;&gt;as&lt;/span&gt; logoutTime
&lt;span class=&quot;hljs-keyword&quot;&gt;from&lt;/span&gt; logins i
  &lt;span class=&quot;hljs-keyword&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;join&lt;/span&gt; logouts o &lt;span class=&quot;hljs-keyword&quot;&gt;on&lt;/span&gt; i.Requestor&lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;o.Requestor
                     &lt;span class=&quot;hljs-keyword&quot;&gt;and&lt;/span&gt; i.row&lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;o.row
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_3&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;NOTE: Query performance may be (drastically?) increased by splitting off some, or all, of the CTE queries into temp tables. i.e. something like the following:&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_4&quot; class=&quot;hljs language-sql&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;select&lt;/span&gt;&lt;span class=&quot;hljs-operator&quot;&gt;*&lt;/span&gt;,
       &lt;span class=&quot;hljs-built_in&quot;&gt;row_number&lt;/span&gt;() &lt;span class=&quot;hljs-keyword&quot;&gt;over&lt;/span&gt;(&lt;span class=&quot;hljs-keyword&quot;&gt;partition&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;by&lt;/span&gt; Requestor &lt;span class=&quot;hljs-keyword&quot;&gt;order&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;by&lt;/span&gt; TimeOfAction) &lt;span class=&quot;hljs-type&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;into&lt;/span&gt; #events
&lt;span class=&quot;hljs-keyword&quot;&gt;from&lt;/span&gt; EventLog

&lt;span class=&quot;hljs-keyword&quot;&gt;select&lt;/span&gt; e1.Activity, e1.TimeOfAction, e1.Requestor,
       &lt;span class=&quot;hljs-built_in&quot;&gt;row_number&lt;/span&gt;() &lt;span class=&quot;hljs-keyword&quot;&gt;over&lt;/span&gt;(&lt;span class=&quot;hljs-keyword&quot;&gt;partition&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;by&lt;/span&gt; e1.Requestor &lt;span class=&quot;hljs-keyword&quot;&gt;order&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;by&lt;/span&gt; e1.TimeOfAction) &lt;span class=&quot;hljs-type&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;into&lt;/span&gt; #logins
&lt;span class=&quot;hljs-keyword&quot;&gt;from&lt;/span&gt; #events e1
  &lt;span class=&quot;hljs-keyword&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;join&lt;/span&gt; #events e2 &lt;span class=&quot;hljs-keyword&quot;&gt;on&lt;/span&gt; e1.Requestor&lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;e2.Requestor
                     &lt;span class=&quot;hljs-keyword&quot;&gt;and&lt;/span&gt; e1.row&lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;e2.row&lt;span class=&quot;hljs-operator&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;where&lt;/span&gt; e1.Activity&lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;&#39;Login&#39;&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;and&lt;/span&gt; e1.Activity&lt;span class=&quot;hljs-operator&quot;&gt;!=&lt;/span&gt;isnull(e2.Activity, &lt;span class=&quot;hljs-string&quot;&gt;&#39;Logout&#39;&lt;/span&gt;)

&lt;span class=&quot;hljs-keyword&quot;&gt;select&lt;/span&gt; e1.Activity, e1.TimeOfAction, e1.Requestor,
       &lt;span class=&quot;hljs-built_in&quot;&gt;row_number&lt;/span&gt;() &lt;span class=&quot;hljs-keyword&quot;&gt;over&lt;/span&gt;(&lt;span class=&quot;hljs-keyword&quot;&gt;partition&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;by&lt;/span&gt; e1.Requestor &lt;span class=&quot;hljs-keyword&quot;&gt;order&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;by&lt;/span&gt; e1.TimeOfAction) &lt;span class=&quot;hljs-type&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;into&lt;/span&gt; #logouts
&lt;span class=&quot;hljs-keyword&quot;&gt;from&lt;/span&gt; #events e1
  &lt;span class=&quot;hljs-keyword&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;join&lt;/span&gt; #events e2 &lt;span class=&quot;hljs-keyword&quot;&gt;on&lt;/span&gt; e1.Requestor&lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;e2.Requestor
                     &lt;span class=&quot;hljs-keyword&quot;&gt;and&lt;/span&gt; e1.row&lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;e2.row&lt;span class=&quot;hljs-number&quot;&gt;-1&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;where&lt;/span&gt; e1.Activity&lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;&#39;Logout&#39;&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;and&lt;/span&gt; e1.Activity&lt;span class=&quot;hljs-operator&quot;&gt;!=&lt;/span&gt;isnull(e2.Activity, &lt;span class=&quot;hljs-string&quot;&gt;&#39;Login&#39;&lt;/span&gt;)

&lt;span class=&quot;hljs-keyword&quot;&gt;select&lt;/span&gt; i.Requestor, i.TimeOfAction &lt;span class=&quot;hljs-keyword&quot;&gt;as&lt;/span&gt; loginTime, o.TimeOfAction &lt;span class=&quot;hljs-keyword&quot;&gt;as&lt;/span&gt; logoutTime
&lt;span class=&quot;hljs-keyword&quot;&gt;from&lt;/span&gt; #logins i
  &lt;span class=&quot;hljs-keyword&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;join&lt;/span&gt; #logouts o &lt;span class=&quot;hljs-keyword&quot;&gt;on&lt;/span&gt; i.Requestor&lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;o.Requestor
                      &lt;span class=&quot;hljs-keyword&quot;&gt;and&lt;/span&gt; i.row&lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;o.row

&lt;span class=&quot;hljs-keyword&quot;&gt;drop&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;table&lt;/span&gt; #logouts
&lt;span class=&quot;hljs-keyword&quot;&gt;drop&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;table&lt;/span&gt; #logins
&lt;span class=&quot;hljs-keyword&quot;&gt;drop&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;table&lt;/span&gt; #events
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_4&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;/div&gt; 

</content><link rel='replies' type='application/atom+xml' href='https://sumitmotwani.blogspot.com/feeds/3317400873532973844/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://sumitmotwani.blogspot.com/2024/08/complex-algorithmic-sql-query.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/5301110832091710857/posts/default/3317400873532973844'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/5301110832091710857/posts/default/3317400873532973844'/><link rel='alternate' type='text/html' href='https://sumitmotwani.blogspot.com/2024/08/complex-algorithmic-sql-query.html' title='Complex / Algorithmic Sql Query'/><author><name>Jackie Leffler</name><uri>http://www.blogger.com/profile/07006275599358224644</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5301110832091710857.post-3124275569401599799</id><published>2024-08-21T04:24:00.001-07:00</published><updated>2024-08-21T04:24:57.275-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Postgresql"/><category scheme="http://www.blogger.com/atom/ns#" term="Scanning"/><category scheme="http://www.blogger.com/atom/ns#" term="Sequential"/><category scheme="http://www.blogger.com/atom/ns#" term="Sql"/><title type='text'>Is Sequential Disk Read Actually Sequential?</title><content type='html'>&lt;div class=&quot;question&quot;&gt;I&#39;m using PostgreSQL 9.4.  First, I have postgreSQL installed on a system with the only one ssd-drive. I&#39;m trying to understand what sequential read is and end up with some issue.&lt;/div&gt;&lt;h2 id=&quot;solution_1&quot;&gt;Solution 1:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;&quot;sequential scan&quot; means a table was read from the beginning to the end, sequentially row by row. It means nothing in terms of how data is read from physical storage.&lt;/p&gt;&lt;p&gt;So the term is about logical reads.&lt;/p&gt;&lt;p&gt;Not sure if the answer needs more explanation.&lt;/p&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;/div&gt; 

</content><link rel='replies' type='application/atom+xml' href='https://sumitmotwani.blogspot.com/feeds/3124275569401599799/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://sumitmotwani.blogspot.com/2024/08/is-sequential-disk-read-actually.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/5301110832091710857/posts/default/3124275569401599799'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/5301110832091710857/posts/default/3124275569401599799'/><link rel='alternate' type='text/html' href='https://sumitmotwani.blogspot.com/2024/08/is-sequential-disk-read-actually.html' title='Is Sequential Disk Read Actually Sequential?'/><author><name>Jackie Leffler</name><uri>http://www.blogger.com/profile/07006275599358224644</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5301110832091710857.post-7899359822279057967</id><published>2024-08-21T04:05:00.001-07:00</published><updated>2024-08-21T04:05:31.553-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Pylons"/><category scheme="http://www.blogger.com/atom/ns#" term="Pyramid"/><category scheme="http://www.blogger.com/atom/ns#" term="Python"/><category scheme="http://www.blogger.com/atom/ns#" term="Sqlalchemy"/><title type='text'>Insert Not Working For Sqlalchemy Database Session</title><content type='html'>&lt;div class=&quot;question&quot;&gt;Why isn&#39;t a record being inserted? There is an id returned but when I check the database there is no new record. From models.py from zope.sqlalchemy import ZopeTransactionExtension&lt;/div&gt;&lt;h2 id=&quot;solution_1&quot;&gt;Solution 1:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;This is because you are not using ORM to insert new rows threfore transaction doesn&#39;t know it should commit on it&#39;s own, because transaction state is not marked as dirty.&lt;/p&gt;&lt;p&gt;Place the following code after you &lt;code&gt;DBSession.execute&lt;/code&gt; the query in your views.py.&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_1&quot; class=&quot;hljs language-java&quot;&gt;from zope.sqlalchemy &lt;span class=&quot;hljs-keyword&quot;&gt;import&lt;/span&gt;&lt;span class=&quot;hljs-type&quot;&gt;mark_changed&lt;/span&gt;&lt;span class=&quot;hljs-variable&quot;&gt;session&lt;/span&gt;&lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt; DBSession()
session.execute(...your query...)
mark_changed(session)
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_1&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;At this point transaction should be able to properly commit your query, alternatively use ORM to insert the new row.&lt;/p&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;p&gt;Here is a bit more on this subject:&lt;/p&gt;&lt;p&gt;&lt;a rel=&quot;nofollow noreferrer noopener&quot; href=&quot;https://pypi.python.org/pypi/zope.sqlalchemy/0.7.4#id15&quot;&gt;https://pypi.python.org/pypi/zope.sqlalchemy/0.7.4#id15&lt;/a&gt;&lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;By default, zope.sqlalchemy puts sessions in an &#39;active&#39; state when they are first used. ORM write operations automatically move the session into a &#39;changed&#39; state. This avoids unnecessary database commits. Sometimes it is necessary to interact with the database directly through SQL. It is not possible to guess whether such an operation is a read or a write. Therefore we must manually mark the session as changed when manual SQL statements write to the DB.&lt;/p&gt;&lt;/blockquote&gt;&lt;/div&gt;&lt;h2 id=&quot;solution_2&quot;&gt;Solution 2:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;try &lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_2&quot; class=&quot;hljs language-lua&quot;&gt;DBSession.&lt;span class=&quot;hljs-built_in&quot;&gt;flush&lt;/span&gt;()
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_2&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;after execute&lt;/p&gt;&lt;/div&gt; 


</content><link rel='replies' type='application/atom+xml' href='https://sumitmotwani.blogspot.com/feeds/7899359822279057967/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://sumitmotwani.blogspot.com/2024/08/insert-not-working-for-sqlalchemy.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/5301110832091710857/posts/default/7899359822279057967'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/5301110832091710857/posts/default/7899359822279057967'/><link rel='alternate' type='text/html' href='https://sumitmotwani.blogspot.com/2024/08/insert-not-working-for-sqlalchemy.html' title='Insert Not Working For Sqlalchemy Database Session'/><author><name>Jackie Leffler</name><uri>http://www.blogger.com/profile/07006275599358224644</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5301110832091710857.post-1955463445681951004</id><published>2024-08-21T03:29:00.001-07:00</published><updated>2024-08-21T03:29:58.666-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Sql"/><category scheme="http://www.blogger.com/atom/ns#" term="Sql Server 2008"/><category scheme="http://www.blogger.com/atom/ns#" term="Stored Procedures"/><title type='text'>Coalesce In Where Clause?</title><content type='html'>&lt;div class=&quot;question&quot;&gt;Is there a good way to express within SQL to select a specific row if a column is matched, otherwise to select a more general row? Something like: SELECT name FROM names&lt;/div&gt;&lt;h2 id=&quot;solution_1&quot;&gt;Solution 1:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;@Blorgbeard answer (using top 1 and order by) is probably the best way but just to be different you could also use a sub-query and not exists:&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_0&quot; class=&quot;hljs language-less&quot;&gt;&lt;span class=&quot;hljs-selector-tag&quot;&gt;SELECT&lt;/span&gt;&lt;span class=&quot;hljs-selector-tag&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;hljs-selector-tag&quot;&gt;FROM&lt;/span&gt;&lt;span class=&quot;hljs-selector-tag&quot;&gt;names&lt;/span&gt;&lt;span class=&quot;hljs-selector-tag&quot;&gt;WHERE&lt;/span&gt; (colA = &lt;span class=&quot;hljs-variable&quot;&gt;@colA&lt;/span&gt;) &lt;span class=&quot;hljs-selector-tag&quot;&gt;OR&lt;/span&gt; (colA = &lt;span class=&quot;hljs-string&quot;&gt;&#39;ALL&#39;&lt;/span&gt; AND NOT EXISTS(
                                                SELECT name FROM names 
                                                WHERE colA = &lt;span class=&quot;hljs-variable&quot;&gt;@colA&lt;/span&gt;)) 
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_0&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;I guess the &quot;advantage&quot; of this is that it uses more standard sql.&lt;/p&gt;&lt;/div&gt;&lt;h2 id=&quot;solution_2&quot;&gt;Solution 2:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;This query, although not pretty, should do what you want:&lt;/p&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;pre&gt;&lt;code id=&quot;code_1&quot; class=&quot;hljs language-less&quot;&gt;&lt;span class=&quot;hljs-selector-tag&quot;&gt;SELECT&lt;/span&gt;&lt;span class=&quot;hljs-selector-tag&quot;&gt;TOP&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hljs-selector-tag&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;hljs-selector-tag&quot;&gt;FROM&lt;/span&gt;&lt;span class=&quot;hljs-selector-tag&quot;&gt;names&lt;/span&gt;&lt;span class=&quot;hljs-selector-tag&quot;&gt;WHERE&lt;/span&gt; (colA = &lt;span class=&quot;hljs-variable&quot;&gt;@colA&lt;/span&gt; OR colA = &lt;span class=&quot;hljs-string&quot;&gt;&#39;ALL&#39;&lt;/span&gt;) 
&lt;span class=&quot;hljs-selector-tag&quot;&gt;ORDER&lt;/span&gt;&lt;span class=&quot;hljs-selector-tag&quot;&gt;BY&lt;/span&gt;&lt;span class=&quot;hljs-selector-tag&quot;&gt;CASE&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;WHEN&lt;/span&gt; colA=&lt;span class=&quot;hljs-string&quot;&gt;&#39;ALL&#39;&lt;/span&gt; THEN &lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt; ELSE &lt;span class=&quot;hljs-number&quot;&gt;0&lt;/span&gt; END
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_1&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;Edit: For multiple columns, I think what you would want is this:&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_2&quot; class=&quot;hljs language-less&quot;&gt;&lt;span class=&quot;hljs-selector-tag&quot;&gt;SELECT&lt;/span&gt;&lt;span class=&quot;hljs-selector-tag&quot;&gt;TOP&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;hljs-selector-tag&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;hljs-selector-tag&quot;&gt;FROM&lt;/span&gt;&lt;span class=&quot;hljs-selector-tag&quot;&gt;names&lt;/span&gt;&lt;span class=&quot;hljs-selector-tag&quot;&gt;WHERE&lt;/span&gt; (colA = &lt;span class=&quot;hljs-variable&quot;&gt;@colA&lt;/span&gt; AND colB = &lt;span class=&quot;hljs-variable&quot;&gt;@colB&lt;/span&gt;) &lt;span class=&quot;hljs-selector-tag&quot;&gt;OR&lt;/span&gt; (colA = &lt;span class=&quot;hljs-string&quot;&gt;&#39;ALL&#39;&lt;/span&gt;) 
&lt;span class=&quot;hljs-selector-tag&quot;&gt;ORDER&lt;/span&gt;&lt;span class=&quot;hljs-selector-tag&quot;&gt;BY&lt;/span&gt;&lt;span class=&quot;hljs-selector-tag&quot;&gt;CASE&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;WHEN&lt;/span&gt; colA=&lt;span class=&quot;hljs-string&quot;&gt;&#39;ALL&#39;&lt;/span&gt; THEN &lt;span class=&quot;hljs-number&quot;&gt;1&lt;/span&gt; ELSE &lt;span class=&quot;hljs-number&quot;&gt;0&lt;/span&gt; END
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_2&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;I&#39;m assuming that there is just one &lt;code&gt;ALL&lt;/code&gt; row, so there&#39;s no need to check for &lt;code&gt;colB=&#39;ALL&#39;&lt;/code&gt;, and that the &quot;key&quot; for the table is &lt;code&gt;colA&lt;/code&gt; and &lt;code&gt;colB&lt;/code&gt; combined - so you&#39;re not interested in rows where &lt;code&gt;colA&lt;/code&gt; matches but &lt;code&gt;colB&lt;/code&gt; doesn&#39;t, for example.&lt;/p&gt;&lt;/div&gt;&lt;h2 id=&quot;solution_3&quot;&gt;Solution 3:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;COALESCE is useful when joining multiple tables and you want the first non-null value in specific order.&lt;/p&gt;&lt;p&gt;Ex.&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_9&quot; class=&quot;hljs language-sql&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;SELECT&lt;/span&gt;
   TheID&lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;COALESCE&lt;/span&gt;(Table1.TheID,Table2.TheID,&lt;span class=&quot;hljs-keyword&quot;&gt;NULL&lt;/span&gt;)
&lt;span class=&quot;hljs-keyword&quot;&gt;FROM&lt;/span&gt;
   Table1 
   &lt;span class=&quot;hljs-keyword&quot;&gt;INNER&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;JOIN&lt;/span&gt; Table2 &lt;span class=&quot;hljs-keyword&quot;&gt;ON&lt;/span&gt; Table1.TheID&lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;Table2.TheID
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_9&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;One alternative way to achieve what you are asking apart from the example you posted would be to set a nullable parameter and only match if it has a value:&lt;/p&gt;&lt;p&gt;Ex.&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_10&quot; class=&quot;hljs language-less&quot;&gt;&lt;span class=&quot;hljs-selector-tag&quot;&gt;CREATE&lt;/span&gt;&lt;span class=&quot;hljs-selector-tag&quot;&gt;Test&lt;/span&gt;(&lt;span class=&quot;hljs-variable&quot;&gt;@Param1&lt;/span&gt; INT=NULL)
&lt;span class=&quot;hljs-selector-tag&quot;&gt;AS&lt;/span&gt;&lt;span class=&quot;hljs-selector-tag&quot;&gt;SELECT&lt;/span&gt;
    *
&lt;span class=&quot;hljs-selector-tag&quot;&gt;FROM&lt;/span&gt;&lt;span class=&quot;hljs-selector-tag&quot;&gt;Table&lt;/span&gt;&lt;span class=&quot;hljs-selector-tag&quot;&gt;WHERE&lt;/span&gt;
    (&lt;span class=&quot;hljs-variable&quot;&gt;@Param1&lt;/span&gt; IS NULL OR IDField=&lt;span class=&quot;hljs-variable&quot;&gt;@Param1&lt;/span&gt;)
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_10&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;If all non-matching records require a record as well as matches then I guess you could join the table on itself to get matches and mismatched.&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_11&quot; class=&quot;hljs language-sql&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;SELECT&lt;/span&gt;
    Calc&lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;CASE&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;WHEN&lt;/span&gt; Table2.IDField &lt;span class=&quot;hljs-keyword&quot;&gt;IS&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;THEN&lt;/span&gt; Table.Value &lt;span class=&quot;hljs-keyword&quot;&gt;ELSE&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;&#39;ALL&#39;&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;END&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;FROM&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;Table&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;INNER&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;JOIN&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;Table&lt;/span&gt; Table2 &lt;span class=&quot;hljs-keyword&quot;&gt;ON&lt;/span&gt; Table2.IDField&lt;span class=&quot;hljs-operator&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;hljs-variable&quot;&gt;@Param1&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;WHERE&lt;/span&gt;
    (Table.IDField&lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;hljs-variable&quot;&gt;@Param1&lt;/span&gt;)
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_11&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;/div&gt; 

</content><link rel='replies' type='application/atom+xml' href='https://sumitmotwani.blogspot.com/feeds/1955463445681951004/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://sumitmotwani.blogspot.com/2024/08/coalesce-in-where-clause.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/5301110832091710857/posts/default/1955463445681951004'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/5301110832091710857/posts/default/1955463445681951004'/><link rel='alternate' type='text/html' href='https://sumitmotwani.blogspot.com/2024/08/coalesce-in-where-clause.html' title='Coalesce In Where Clause?'/><author><name>Jackie Leffler</name><uri>http://www.blogger.com/profile/07006275599358224644</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5301110832091710857.post-258637998687616026</id><published>2024-08-21T03:12:00.001-07:00</published><updated>2024-08-21T03:12:09.950-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term=".net"/><category scheme="http://www.blogger.com/atom/ns#" term="Sql"/><category scheme="http://www.blogger.com/atom/ns#" term="Sql Server"/><category scheme="http://www.blogger.com/atom/ns#" term="Sql Server 2008"/><category scheme="http://www.blogger.com/atom/ns#" term="Vb.net"/><title type='text'>Sql Server Datediff Vs .net Datediff</title><content type='html'>&lt;div class=&quot;question&quot;&gt;SQL Server: DECLARE @Date1 DATETIME SET @Date1 = &#39;2012-26-12 12:00:05 AM&#39;  DECLARE @Date2 DATETIME SET @Date2 = &#39;2012-28-12 12:00:00 AM&#39;  SELECT @Date1, @Date2, DATEDIFF(DAY,@Date1&lt;/div&gt;&lt;h2 id=&quot;solution_1&quot;&gt;Solution 1:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;Actually the VB.NET expression is returning you the correct values since the total number of &lt;code&gt;hours&lt;/code&gt; is not equal to &lt;code&gt;48&lt;/code&gt; .Hence it is returning 1&lt;/p&gt;&lt;p&gt;In SQL Server , DateDiff function returns a round off value .&lt;/p&gt;&lt;p&gt;Try dividing the value with total number of seconds in day which is &lt;code&gt;86400.0&lt;/code&gt;&lt;/p&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;pre&gt;&lt;code id=&quot;code_3&quot; class=&quot;hljs language-less&quot;&gt;&lt;span class=&quot;hljs-selector-tag&quot;&gt;SELECT&lt;/span&gt; @&lt;span class=&quot;hljs-selector-tag&quot;&gt;Date1&lt;/span&gt;, @&lt;span class=&quot;hljs-selector-tag&quot;&gt;Date2&lt;/span&gt;, &lt;span class=&quot;hljs-selector-tag&quot;&gt;DATEDIFF&lt;/span&gt;(ss,&lt;span class=&quot;hljs-variable&quot;&gt;@Date1&lt;/span&gt;,&lt;span class=&quot;hljs-variable&quot;&gt;@Date2&lt;/span&gt;) /&lt;span class=&quot;hljs-number&quot;&gt;86400.0&lt;/span&gt;&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_3&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;The value returned will be &lt;code&gt;1.9999421&lt;/code&gt; instead of &lt;code&gt;2&lt;/code&gt;&lt;/p&gt;&lt;p&gt;The difference between the 2 date in seconds is &lt;code&gt;172795&lt;/code&gt; which is less than &lt;code&gt;172800&lt;/code&gt; (Total seconds for 2 days).Hence the function should be returning only 1 if your not rounding off the result&lt;/p&gt;&lt;/div&gt;&lt;h2 id=&quot;solution_2&quot;&gt;Solution 2:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;&lt;code&gt;Day DateDiff&lt;/code&gt; round down the number returned. Try to return hours and divide by 24 to get count of days&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_9&quot; class=&quot;hljs language-bash&quot;&gt;DateDiff(DateInterval.Hour,Convert.ToDateTime(&lt;span class=&quot;hljs-string&quot;&gt;&quot;26-12-2012 12:00:05 AM&quot;&lt;/span&gt;), 
Convert.ToDateTime(&lt;span class=&quot;hljs-string&quot;&gt;&quot;28-12-2012 12:00:00 AM&quot;&lt;/span&gt;))/24
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_9&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;/div&gt; 


</content><link rel='replies' type='application/atom+xml' href='https://sumitmotwani.blogspot.com/feeds/258637998687616026/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://sumitmotwani.blogspot.com/2024/08/sql-server-datediff-vs-net-datediff.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/5301110832091710857/posts/default/258637998687616026'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/5301110832091710857/posts/default/258637998687616026'/><link rel='alternate' type='text/html' href='https://sumitmotwani.blogspot.com/2024/08/sql-server-datediff-vs-net-datediff.html' title='Sql Server Datediff Vs .net Datediff'/><author><name>Jackie Leffler</name><uri>http://www.blogger.com/profile/07006275599358224644</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5301110832091710857.post-687690239437388410</id><published>2024-08-21T01:39:00.001-07:00</published><updated>2024-08-21T01:39:49.220-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Date"/><category scheme="http://www.blogger.com/atom/ns#" term="Group By"/><category scheme="http://www.blogger.com/atom/ns#" term="Sql"/><category scheme="http://www.blogger.com/atom/ns#" term="Sql Server"/><title type='text'>How Do I Include Empty Rows In A Single Group By Day(date_field) Sql Query?</title><content type='html'>&lt;div class=&quot;question&quot;&gt;I&#39;m using MS SQL Server but welcome comparitive solutions from other databases. This is the basic form of my query.  It returns the number of calls per day from the &#39;incidentsm1&#39; t&lt;/div&gt;&lt;h2 id=&quot;solution_1&quot;&gt;Solution 1:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;How about something like this? &lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_0&quot; class=&quot;hljs language-sql&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;SELECT&lt;/span&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;COUNT&lt;/span&gt;(incident_id) &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; &quot;Calls&quot;,
  &lt;span class=&quot;hljs-built_in&quot;&gt;MAX&lt;/span&gt;(open_time),
  days.open_day
&lt;span class=&quot;hljs-keyword&quot;&gt;FROM&lt;/span&gt;
(
  &lt;span class=&quot;hljs-keyword&quot;&gt;select&lt;/span&gt; datepart(dd,dateadd(&lt;span class=&quot;hljs-keyword&quot;&gt;day&lt;/span&gt;,&lt;span class=&quot;hljs-number&quot;&gt;-6&lt;/span&gt;,getdate())) &lt;span class=&quot;hljs-keyword&quot;&gt;as&lt;/span&gt; open_day &lt;span class=&quot;hljs-keyword&quot;&gt;union&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;select&lt;/span&gt; datepart(dd,dateadd(&lt;span class=&quot;hljs-keyword&quot;&gt;day&lt;/span&gt;,&lt;span class=&quot;hljs-number&quot;&gt;-5&lt;/span&gt;,getdate())) &lt;span class=&quot;hljs-keyword&quot;&gt;as&lt;/span&gt; open_day &lt;span class=&quot;hljs-keyword&quot;&gt;union&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;select&lt;/span&gt; datepart(dd,dateadd(&lt;span class=&quot;hljs-keyword&quot;&gt;day&lt;/span&gt;,&lt;span class=&quot;hljs-number&quot;&gt;-4&lt;/span&gt;,getdate())) &lt;span class=&quot;hljs-keyword&quot;&gt;as&lt;/span&gt; open_day &lt;span class=&quot;hljs-keyword&quot;&gt;union&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;select&lt;/span&gt; datepart(dd,dateadd(&lt;span class=&quot;hljs-keyword&quot;&gt;day&lt;/span&gt;,&lt;span class=&quot;hljs-number&quot;&gt;-3&lt;/span&gt;,getdate())) &lt;span class=&quot;hljs-keyword&quot;&gt;as&lt;/span&gt; open_day &lt;span class=&quot;hljs-keyword&quot;&gt;union&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;select&lt;/span&gt; datepart(dd,dateadd(&lt;span class=&quot;hljs-keyword&quot;&gt;day&lt;/span&gt;,&lt;span class=&quot;hljs-number&quot;&gt;-2&lt;/span&gt;,getdate())) &lt;span class=&quot;hljs-keyword&quot;&gt;as&lt;/span&gt; open_day &lt;span class=&quot;hljs-keyword&quot;&gt;union&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;select&lt;/span&gt; datepart(dd,dateadd(&lt;span class=&quot;hljs-keyword&quot;&gt;day&lt;/span&gt;,&lt;span class=&quot;hljs-number&quot;&gt;-1&lt;/span&gt;,getdate())) &lt;span class=&quot;hljs-keyword&quot;&gt;as&lt;/span&gt; open_day &lt;span class=&quot;hljs-keyword&quot;&gt;union&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;select&lt;/span&gt; datepart(dd,dateadd(&lt;span class=&quot;hljs-keyword&quot;&gt;day&lt;/span&gt;, &lt;span class=&quot;hljs-number&quot;&gt;0&lt;/span&gt;,getdate())) &lt;span class=&quot;hljs-keyword&quot;&gt;as&lt;/span&gt; open_day 
) days
&lt;span class=&quot;hljs-keyword&quot;&gt;left&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;join&lt;/span&gt; 
(
 &lt;span class=&quot;hljs-keyword&quot;&gt;SELECT&lt;/span&gt;
   incident_id,
   opened_by,
   open_time &lt;span class=&quot;hljs-operator&quot;&gt;-&lt;/span&gt; (&lt;span class=&quot;hljs-number&quot;&gt;9.0&lt;/span&gt;&lt;span class=&quot;hljs-operator&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;24&lt;/span&gt;) &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; open_time,
   DATEPART(dd, (open_time&lt;span class=&quot;hljs-operator&quot;&gt;-&lt;/span&gt;(&lt;span class=&quot;hljs-number&quot;&gt;9.0&lt;/span&gt;&lt;span class=&quot;hljs-operator&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;24&lt;/span&gt;))) &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; open_day
 &lt;span class=&quot;hljs-keyword&quot;&gt;FROM&lt;/span&gt; incidentsm1 
 &lt;span class=&quot;hljs-keyword&quot;&gt;WHERE&lt;/span&gt; DATEDIFF(&lt;span class=&quot;hljs-keyword&quot;&gt;DAY&lt;/span&gt;, open_time&lt;span class=&quot;hljs-operator&quot;&gt;-&lt;/span&gt;(&lt;span class=&quot;hljs-number&quot;&gt;9.0&lt;/span&gt;&lt;span class=&quot;hljs-operator&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;24&lt;/span&gt;), GETDATE()) &lt;span class=&quot;hljs-operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;7&lt;/span&gt;
) inc1 &lt;span class=&quot;hljs-keyword&quot;&gt;ON&lt;/span&gt; days.open_day &lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt; incidents.open_day
&lt;span class=&quot;hljs-keyword&quot;&gt;GROUP&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;BY&lt;/span&gt; days.open_day
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_0&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;I&#39;ve only tested it on a simplified table schema, but I think it should work. You might need to tinker with the dateadd stuff..&lt;/p&gt;&lt;/div&gt;&lt;h2 id=&quot;solution_2&quot;&gt;Solution 2:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;I would suggest the usage of a &lt;a rel=&quot;nofollow noreferrer noopener&quot; href=&quot;http://www.sqlservercentral.com/articles/Date+Manipulation/65195/&quot;&gt;date table&lt;/a&gt;. With an existing date table in place, you can perform a RIGHT OUTER JOIN to the date table to bring in your missing days.&lt;/p&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;/div&gt;&lt;h2 id=&quot;solution_3&quot;&gt;Solution 3:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;Can you create a table variable with the dates that you need and then &lt;code&gt;RIGHT JOIN&lt;/code&gt; onto it? For example,&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_2&quot; class=&quot;hljs language-sql&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;DECLARE&lt;/span&gt;&lt;span class=&quot;hljs-variable&quot;&gt;@dateTable&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;TABLE&lt;/span&gt; ([&lt;span class=&quot;hljs-type&quot;&gt;date&lt;/span&gt;] SMALLDATETIME)

&lt;span class=&quot;hljs-keyword&quot;&gt;INSERT&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;INTO&lt;/span&gt;&lt;span class=&quot;hljs-variable&quot;&gt;@dateTable&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;VALUES&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&#39;26 FEB 2009&#39;&lt;/span&gt;)
&lt;span class=&quot;hljs-keyword&quot;&gt;INSERT&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;INTO&lt;/span&gt;&lt;span class=&quot;hljs-variable&quot;&gt;@dateTable&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;VALUES&lt;/span&gt;(&lt;span class=&quot;hljs-string&quot;&gt;&#39;27 FEB 2009&#39;&lt;/span&gt;)
&lt;span class=&quot;hljs-comment&quot;&gt;-- etc&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;SELECT&lt;/span&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;COUNT&lt;/span&gt;(&lt;span class=&quot;hljs-operator&quot;&gt;*&lt;/span&gt;) &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; &quot;Calls&quot;,
  &lt;span class=&quot;hljs-built_in&quot;&gt;MAX&lt;/span&gt;(open_time),
  open_day
&lt;span class=&quot;hljs-keyword&quot;&gt;FROM&lt;/span&gt; 
  (
&lt;span class=&quot;hljs-keyword&quot;&gt;SELECT&lt;/span&gt;
 incident_id,
 opened_by,
 open_time &lt;span class=&quot;hljs-operator&quot;&gt;-&lt;/span&gt; (&lt;span class=&quot;hljs-number&quot;&gt;9.0&lt;/span&gt;&lt;span class=&quot;hljs-operator&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;24&lt;/span&gt;) &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; open_time,
 DATEPART(dd, (open_time&lt;span class=&quot;hljs-operator&quot;&gt;-&lt;/span&gt;(&lt;span class=&quot;hljs-number&quot;&gt;9.0&lt;/span&gt;&lt;span class=&quot;hljs-operator&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;24&lt;/span&gt;))) &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; open_day
   &lt;span class=&quot;hljs-keyword&quot;&gt;FROM&lt;/span&gt; incidentsm1
   &lt;span class=&quot;hljs-keyword&quot;&gt;RIGHT&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;JOIN&lt;/span&gt;&lt;span class=&quot;hljs-variable&quot;&gt;@dateTable&lt;/span&gt; dates
   &lt;span class=&quot;hljs-keyword&quot;&gt;ON&lt;/span&gt; incidentsm1.open_day &lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt; dates.date
   &lt;span class=&quot;hljs-keyword&quot;&gt;WHERE&lt;/span&gt; 
 DATEDIFF(&lt;span class=&quot;hljs-keyword&quot;&gt;DAY&lt;/span&gt;, open_time&lt;span class=&quot;hljs-operator&quot;&gt;-&lt;/span&gt;(&lt;span class=&quot;hljs-number&quot;&gt;9.0&lt;/span&gt;&lt;span class=&quot;hljs-operator&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;24&lt;/span&gt;), GETDATE())&lt;span class=&quot;hljs-operator&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;hljs-number&quot;&gt;7&lt;/span&gt;

  ) inc1
&lt;span class=&quot;hljs-keyword&quot;&gt;GROUP&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;BY&lt;/span&gt; open_day
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_2&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;The more ideal situation however, would be to have a table object with the dates in&lt;/p&gt;&lt;/div&gt;&lt;h2 id=&quot;solution_4&quot;&gt;Solution 4:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;Can you create the set of dates as part of your query? Something along the lines of:&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_3&quot; class=&quot;hljs language-sql&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;SELECT&lt;/span&gt;&lt;span class=&quot;hljs-built_in&quot;&gt;COUNT&lt;/span&gt;(&lt;span class=&quot;hljs-operator&quot;&gt;*&lt;/span&gt;) &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; Calls, ...
    &lt;span class=&quot;hljs-keyword&quot;&gt;FROM&lt;/span&gt; incidentsm1 &lt;span class=&quot;hljs-keyword&quot;&gt;RIGHT&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;OUTER&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;JOIN&lt;/span&gt;
         (&lt;span class=&quot;hljs-keyword&quot;&gt;SELECT&lt;/span&gt; date_values
            &lt;span class=&quot;hljs-keyword&quot;&gt;FROM&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;TABLE&lt;/span&gt;((&lt;span class=&quot;hljs-string&quot;&gt;&#39;27 Feb 2009&#39;&lt;/span&gt;), (&lt;span class=&quot;hljs-string&quot;&gt;&#39;28 Feb 2009&#39;&lt;/span&gt;), (&lt;span class=&quot;hljs-string&quot;&gt;&#39;1 Mar 2009&#39;&lt;/span&gt;),
                       (&lt;span class=&quot;hljs-string&quot;&gt;&#39;2 Mar 2009&#39;&lt;/span&gt;), (&lt;span class=&quot;hljs-string&quot;&gt;&#39;3 Mar 2009&#39;&lt;/span&gt;), (&lt;span class=&quot;hljs-string&quot;&gt;&#39;4 Mar 2009&#39;&lt;/span&gt;),
                       (&lt;span class=&quot;hljs-string&quot;&gt;&#39;5 Mar 2009&#39;&lt;/span&gt;)) &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; date_list
         )
         &lt;span class=&quot;hljs-keyword&quot;&gt;ON&lt;/span&gt; ...
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_3&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;This is inspired by a sort of hybrid of Informix and DB2 notations and is pretty much guaranteed to be syntactically incorrect in both. Basically, is there a way in your DBMS of creating a literal table on the fly. One possibility - ugly but barely doable - would be to do a 7-way UNION of date literals selected from &#39;dual&#39; or some table expression that guarantees one row (in Informix terms, &lt;code&gt;SELECT MDY(2,28,2009) FROM &quot;informix&quot;.systables WHERE tabid = 1 UNION ...&lt;/code&gt;).&lt;/p&gt;&lt;/div&gt; 

</content><link rel='replies' type='application/atom+xml' href='https://sumitmotwani.blogspot.com/feeds/687690239437388410/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://sumitmotwani.blogspot.com/2024/08/how-do-i-include-empty-rows-in-single.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/5301110832091710857/posts/default/687690239437388410'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/5301110832091710857/posts/default/687690239437388410'/><link rel='alternate' type='text/html' href='https://sumitmotwani.blogspot.com/2024/08/how-do-i-include-empty-rows-in-single.html' title='How Do I Include Empty Rows In A Single Group By Day(date_field) Sql Query?'/><author><name>Jackie Leffler</name><uri>http://www.blogger.com/profile/07006275599358224644</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5301110832091710857.post-777508526212670813</id><published>2024-08-21T01:21:00.001-07:00</published><updated>2024-08-21T01:21:48.064-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Login"/><category scheme="http://www.blogger.com/atom/ns#" term="Permissions"/><category scheme="http://www.blogger.com/atom/ns#" term="Sql"/><category scheme="http://www.blogger.com/atom/ns#" term="Sql Server"/><title type='text'>Mssqlsm 2008 Odd Login Failed On Select Statement</title><content type='html'>&lt;div class=&quot;question&quot;&gt;I have a variety of users on my SQL server, all of which CAN login to the server; however one user, once logged in cannot run anything (select, alter, edit) on a specific view - bu&lt;/div&gt;&lt;h2 id=&quot;solution_1&quot;&gt;Solution 1:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;You probably have either&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;p&gt;linked server issues. That is, the views access a linked server where the login mapping is incorrect&lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;You have 2 views in different schemas. Do you use &lt;code&gt;select * from view&lt;/code&gt; or &lt;code&gt;select * from dbo.view&lt;/code&gt;: are you &lt;em&gt;consistent&lt;/em&gt;?&lt;/p&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;h2 id=&quot;solution_2&quot;&gt;Solution 2:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot; style=&quot;height: auto !important;&quot;&gt;&lt;p&gt;When you do a &lt;code&gt;select 1 * from ViewName&lt;/code&gt; and you get a &lt;code&gt;Login Failed&lt;/code&gt;, it possibly indicates one of the following:&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Referencing table/view from another database:&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;This user may not have permissions to view objects in a different database.&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Referencing table/view from Remote Server or a Linked Server:&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;This user may be using a mapping for either one of those servers where the login information may be out of date or may not have the proper credentials.&lt;/p&gt;&lt;/div&gt; 


</content><link rel='replies' type='application/atom+xml' href='https://sumitmotwani.blogspot.com/feeds/777508526212670813/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://sumitmotwani.blogspot.com/2024/08/mssqlsm-2008-odd-login-failed-on-select.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/5301110832091710857/posts/default/777508526212670813'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/5301110832091710857/posts/default/777508526212670813'/><link rel='alternate' type='text/html' href='https://sumitmotwani.blogspot.com/2024/08/mssqlsm-2008-odd-login-failed-on-select.html' title='Mssqlsm 2008 Odd Login Failed On Select Statement'/><author><name>Jackie Leffler</name><uri>http://www.blogger.com/profile/07006275599358224644</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5301110832091710857.post-361843269677500669</id><published>2024-08-20T22:44:00.001-07:00</published><updated>2024-08-20T22:44:57.589-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Heidisql"/><category scheme="http://www.blogger.com/atom/ns#" term="Mysql"/><category scheme="http://www.blogger.com/atom/ns#" term="Sql"/><title type='text'>Trying To Get The Rooms Of Hotels That Got The Most Profit In A Query</title><content type='html'>&lt;div class=&quot;question&quot;&gt;I&#39;m trying to implement a query which give me the sum of most profitable room in each hotel(25 hotels) Below is my query: SELECT hotels.hotel_id,rooms.room_id,hotel_name,room_numbe&lt;/div&gt;&lt;h2 id=&quot;solution_1&quot;&gt;Solution 1:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;Try this query:&lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_0&quot; class=&quot;hljs language-vbnet&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;SELECT&lt;/span&gt; * &lt;span class=&quot;hljs-keyword&quot;&gt;FROM&lt;/span&gt; 
(&lt;span class=&quot;hljs-keyword&quot;&gt;SELECT&lt;/span&gt; hotels.hotel_id,rooms.room_id,hotel_name,room_number,SUM(rooms.room_price) &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; profit,COUNT(rooms.room_id) &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt; COUNT
&lt;span class=&quot;hljs-keyword&quot;&gt;FROM&lt;/span&gt; hotels,rooms,bookings
&lt;span class=&quot;hljs-keyword&quot;&gt;WHERE&lt;/span&gt; hotels.hotel_id=rooms.hotel_id
&lt;span class=&quot;hljs-built_in&quot;&gt;AND&lt;/span&gt; rooms.room_id=bookings.room_id
&lt;span class=&quot;hljs-keyword&quot;&gt;GROUP&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;BY&lt;/span&gt; rooms.room_id) a &lt;span class=&quot;hljs-keyword&quot;&gt;GROUP&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;BY&lt;/span&gt; hotel_id;
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_0&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;&lt;strong&gt;Edit:&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;This might do it:&lt;/p&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;pre&gt;&lt;code id=&quot;code_1&quot; class=&quot;hljs language-vbnet&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;SELECT&lt;/span&gt; hotel_id,room_id,room_number,MAX(a.tc) &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;&quot;Count&quot;&lt;/span&gt;,MAX(tp) &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;&quot;MostProfit&quot;&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;FROM&lt;/span&gt;
(&lt;span class=&quot;hljs-keyword&quot;&gt;SELECT&lt;/span&gt; hotel_id,rooms.room_id,room_number,COUNT(rooms.room_id) &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;&quot;tc&quot;&lt;/span&gt;,SUM(room_price) &lt;span class=&quot;hljs-keyword&quot;&gt;AS&lt;/span&gt;&lt;span class=&quot;hljs-string&quot;&gt;&quot;tp&quot;&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;FROM&lt;/span&gt; rooms &lt;span class=&quot;hljs-keyword&quot;&gt;JOIN&lt;/span&gt; bookings 
&lt;span class=&quot;hljs-keyword&quot;&gt;ON&lt;/span&gt; rooms.room_id=bookings.room_id
&lt;span class=&quot;hljs-keyword&quot;&gt;GROUP&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;BY&lt;/span&gt; rooms.room_id) a &lt;span class=&quot;hljs-keyword&quot;&gt;GROUP&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;BY&lt;/span&gt; hotel_id
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_1&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&quot;solution_2&quot;&gt;Solution 2:

&lt;/h2&gt;&lt;div class=&quot;answer-desc&quot;&gt;&lt;p&gt;Please try below once: &lt;/p&gt;&lt;pre&gt;&lt;code id=&quot;code_2&quot; class=&quot;hljs language-sql&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;SELECT&lt;/span&gt; RO_BOOK.HOTEL_ID,
       RO_BOOK.ROOM_ID,
       RO_BOOK.ROOM_NUMBER,
       RO_BOOK.TOTAL_BOOKINGS,
       &lt;span class=&quot;hljs-built_in&quot;&gt;MAX&lt;/span&gt;(RO_BOOK.TOTAL_EARNINGS) PROFITS
&lt;span class=&quot;hljs-keyword&quot;&gt;FROM&lt;/span&gt;(
    &lt;span class=&quot;hljs-keyword&quot;&gt;SELECT&lt;/span&gt;  ROOMS.HOTEL_ID
        ROOMS.ROOM_ID,
        ROOMS.ROOM_NUMBER,
        &lt;span class=&quot;hljs-built_in&quot;&gt;COUNT&lt;/span&gt;(ROOMS.ROOM_ID) TOTAL_BOOKINGS
        &lt;span class=&quot;hljs-built_in&quot;&gt;SUM&lt;/span&gt;(ROOMS.ROOM_PRICE) TOTAL_EARNINGS
    &lt;span class=&quot;hljs-keyword&quot;&gt;FROM&lt;/span&gt; 
        ROOMS, BOOKINGS
    &lt;span class=&quot;hljs-keyword&quot;&gt;WHERE&lt;/span&gt; 
        BOOKINGS.ROOM_ID &lt;span class=&quot;hljs-operator&quot;&gt;=&lt;/span&gt; ROOMS.ROOM_ID
        &lt;span class=&quot;hljs-keyword&quot;&gt;GROUP&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;BY&lt;/span&gt; ROOMS.ROOM_ID) RO_BOOK
&lt;span class=&quot;hljs-keyword&quot;&gt;GROUP&lt;/span&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;BY&lt;/span&gt; RO_BOOK.HOTEL_ID ;
&lt;/code&gt;&lt;button class=&quot;copy-btn btn btn-secondary btn-sm&quot; onclick=&quot;copyCode(this,&#39;code_2&#39;)&quot;&gt;Copy&lt;/button&gt;&lt;/pre&gt;&lt;p&gt;It is similar to @tcadidot0 code, but column MAX(a.tc) AS &quot;Count&quot; return the maximum count irrespective of ROOM_ID.
For eg:
if hotel 1 has 2 rooms say, R100 and R200. The Cost of R100 be 1000 and R200 be 100.
No of times R100 booked be 1, and R200 be 3.
So the query would return:
HOTEL 1 , R100, COUNT 2, PROFIT 1000.&lt;/p&gt;&lt;p&gt;Please correct me if I got the question wrong.&lt;/p&gt;&lt;/div&gt; 


</content><link rel='replies' type='application/atom+xml' href='https://sumitmotwani.blogspot.com/feeds/361843269677500669/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://sumitmotwani.blogspot.com/2024/08/trying-to-get-rooms-of-hotels-that-got.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/5301110832091710857/posts/default/361843269677500669'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/5301110832091710857/posts/default/361843269677500669'/><link rel='alternate' type='text/html' href='https://sumitmotwani.blogspot.com/2024/08/trying-to-get-rooms-of-hotels-that-got.html' title='Trying To Get The Rooms Of Hotels That Got The Most Profit In A Query'/><author><name>Jackie Leffler</name><uri>http://www.blogger.com/profile/07006275599358224644</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>