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

<channel>
	<title>Cogniza &#187; SQL</title>
	<atom:link href="http://cogniza.com/wordpress/tag/sql/feed/" rel="self" type="application/rss+xml" />
	<link>http://cogniza.com/wordpress</link>
	<description>Business-Intelligence Specialists</description>
	<lastBuildDate>Sat, 30 Apr 2011 13:22:21 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>SQL Server: Date Arithmetic</title>
		<link>http://cogniza.com/wordpress/2009/11/05/sql-server-date-arithmetic/</link>
		<comments>http://cogniza.com/wordpress/2009/11/05/sql-server-date-arithmetic/#comments</comments>
		<pubDate>Fri, 06 Nov 2009 02:32:24 +0000</pubDate>
		<dc:creator>Craig Buchanan</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Technique]]></category>
		<category><![CDATA[Date]]></category>
		<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false">http://www.cogniza.com/blog/?p=106</guid>
		<description><![CDATA[Useful, dynamically-generated dates for SQL Server.

--[1st day  of current month]: use the GetDate(), Month(), and Year() functions to determine the current month and year.   Build a string using these values in MM/01/YYYY format.  Convert the String to DateTime.
 CAST( CAST( Month(GetDate()) AS Char(2) ) + '/01/' + CAST( Year(GetDate()) AS Char(4)) [...]]]></description>
			<content:encoded><![CDATA[<p>Useful, dynamically-generated dates for SQL Server.<span id="more-106"></span><br />
<code><br />
--[1st day  of current month]: use the GetDate(), Month(), and Year() functions to determine the current month and year.   Build a string using these values in MM/01/YYYY format.  Convert the String to DateTime.<br />
 CAST( CAST( Month(GetDate()) AS Char(2) ) + '/01/' + CAST( Year(GetDate()) AS Char(4)) AS DateTime)<br />
</code><code><br />
--[Last day of current month]: add a month to [1st day of current month], then subtract a day.<br />
DateAdd(m, 1, CAST( CAST( Month(GetDate()) AS Char(2) ) + '/01/' + CAST( Year(GetDate()) AS Char(4)) AS DateTime)) -1<br />
</code><code><br />
--[1st day of prior month]: subtract a month from [1st day of current month].<br />
DateAdd(m, -1, CAST( CAST( Month(GetDate()) AS Char(2) ) + '/01/' + CAST( Year(GetDate()) AS Char(4)) AS DateTime))<br />
</code><code><br />
--[Last day of prior month]:  subtract a day from [1st day of current month].<br />
CAST( CAST( Month(GetDate()) AS Char(2) ) + '/01/' + CAST( Year(GetDate()) AS Char(4)) AS DateTime) -1<br />
</code></p>
]]></content:encoded>
			<wfw:commentRss>http://cogniza.com/wordpress/2009/11/05/sql-server-date-arithmetic/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Oracle: Object Dependencies</title>
		<link>http://cogniza.com/wordpress/2005/11/20/oracle-object-dependencies/</link>
		<comments>http://cogniza.com/wordpress/2005/11/20/oracle-object-dependencies/#comments</comments>
		<pubDate>Mon, 21 Nov 2005 00:22:24 +0000</pubDate>
		<dc:creator>Craig Buchanan</dc:creator>
				<category><![CDATA[Oracle]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[PL/SQL]]></category>

		<guid isPermaLink="false">http://www.cogniza.com/blog/?p=28</guid>
		<description><![CDATA[SQL query that lists Oracle views and functions and the functions and tables that are contained in them.]]></description>
			<content:encoded><![CDATA[<p>This article demonstrates a SQL query that lists Oracle views and functions and the functions and tables that are contained in them.</p>
<p><span id="more-28"></span> <code><br />
SELECT  OBJECT_TYPE,OWNER,NAME<br />
,REFERENCED_TYPE REF_OBJECT_TYPE<br />
,REFERENCED_OWNER REF_OWNER<br />
,REFERENCED_NAME REF_NAME<br />
FROM	ALL_DEPENDENCIES<br />
WHERE	TYPE IN ('VIEW','FUNCTION')	--show views and functions<br />
--show the functions and tables that they contain<br />
AND	REFERENCED_TYPE IN ('FUNCTION','TABLE')<br />
ORDER BY 1,2,3;<br />
</code></p>
]]></content:encoded>
			<wfw:commentRss>http://cogniza.com/wordpress/2005/11/20/oracle-object-dependencies/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Crystal Reports: Using SQL Expression Fields</title>
		<link>http://cogniza.com/wordpress/2005/11/07/crystal-reports-using-sql-expression-fields/</link>
		<comments>http://cogniza.com/wordpress/2005/11/07/crystal-reports-using-sql-expression-fields/#comments</comments>
		<pubDate>Mon, 07 Nov 2005 14:15:37 +0000</pubDate>
		<dc:creator>Craig Buchanan</dc:creator>
				<category><![CDATA[Business Objects Enterprise]]></category>
		<category><![CDATA[Crystal Reports]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://www.cogniza.com/blog/?p=26</guid>
		<description><![CDATA[This article discusses the syntax requirements for SQL Expression fields.]]></description>
			<content:encoded><![CDATA[<p>This document discusses the syntax requirements for SQL Expression fields.</p>
<p><span id="more-26"></span>The contents of a SQL Expression field is any valid SQL statement that returns a scalar value. The resulting SQL is added to the report&#8217;s SELECT clause. For example:<br />
<code><br />
SELECT	PAT_NAME,HOSP_ADMSN_TIME,HOSP_DISCH_TIME,<br />
--SQL Expression<br />
(<br />
SELECT	MAX(HOSP_DISCH_TIME)<br />
FROM	PAT_ENC_HSP PAT_ENC_HSP2<br />
WHERE	PAT_ENC_HSP2.PAT_ID=PAT_ENC_HSP.PAT_ID<br />
AND	PAT_ENC_HSP2.PAT_ENC_CSN_ID &lt; PAT_ENC_HSP.PAT_ENC_CSN_ID<br />
)<br />
--/SQL Expression<br />
FROM	PAT_ENC_HSP<br />
</code></p>
<h2>Limitations</h2>
<p>SQL Expression fields may only return a scalar value (one column and one row).<br />
Parameter fields may not be used in SQL Expressions.</p>
<h2>Features</h2>
<p>A database [Oracle|SQL Server|Sybase] scalar function may be used in a SQL Expression field.<br />
Case statements may be used in SQL Expression fields. For example (Oracle syntax):<br />
<code><br />
(<br />
CASE<br />
WHEN PAT_ENC_HSP.HOSP_DISCH_TIME IS NULL THEN<br />
'F'<br />
WHEN PAT_ENC_HSP.HOSP_DISCH_TIME &gt;= PAT_ENC_HSP.HOSP_ADMSN_TIME-7 THEN<br />
'T'<br />
ELSE<br />
'F'<br />
END<br />
)<br />
</code></p>
<h2>Syntax</h2>
<p>The SQL statement needs to be enclosed in parenthesis:<br />
<code><br />
(<br />
SELECT	MAX(HOSP_DISCH_TIME)<br />
FROM	PAT_ENC_HSP PAT_ENC_HSP2<br />
WHERE	PAT_ENC_HSP2.PAT_ID=PAT_ENC_HSP.PAT_ID<br />
AND	PAT_ENC_HSP2.PAT_ENC_CSN_ID &lt; PAT_ENC_HSP.PAT_ENC_CSN_ID<br />
)<br />
</code></p>
<p>Double-quotation marks aren&#8217;t required if tables in the main query aren&#8217;t aliased:<br />
<code><br />
(<br />
--will compile<br />
SELECT	MAX(HOSP_DISCH_TIME)<br />
FROM	PAT_ENC_HSP PAT_ENC_HSP2<br />
WHERE	PAT_ENC_HSP2.PAT_ID=PAT_ENC_HSP.PAT_ID<br />
AND	PAT_ENC_HSP2.PAT_ENC_CSN_ID &lt; PAT_ENC_HSP.PAT_ENC_CSN_ID<br />
)</code></p>
<p>(<br />
&#8211;will compile<br />
SELECT	MAX(&#8220;HOSP_DISCH_TIME&#8221;)<br />
FROM	&#8220;PAT_ENC_HSP&#8221; &#8220;PAT_ENC_HSP2&#8243;<br />
WHERE	&#8220;PAT_ENC_HSP2&#8243;.&#8221;PAT_ID&#8221;=&#8221;PAT_ENC_HSP&#8221;.&#8221;PAT_ID&#8221;<br />
AND	&#8220;PAT_ENC_HSP2&#8243;.&#8221;PAT_ENC_CSN_ID&#8221; &lt; &#8220;PAT_ENC_HSP&#8221;.&#8221;PAT_ENC_CSN_ID&#8221;<br />
)<br />
Double-quotation marks are required if tables in the main query are aliased:<br />
<code><br />
(<br />
--will compile<br />
SELECT	MAX(HOSP_DISCH_TIME)<br />
FROM	PAT_ENC_HSP PAT_ENC_HSP_<br />
WHERE	PAT_ENC_HSP2.PAT_ID="PAT_ENC_HSP_alias".PAT_ID<br />
AND	PAT_ENC_HSP2.PAT_ENC_CSN_ID &lt; "PAT_ENC_HSP_alias".PAT_ENC_CSN_ID<br />
)</code></p>
<p>(<br />
&#8211;will not compile<br />
SELECT	MAX(HOSP_DISCH_TIME)<br />
FROM	PAT_ENC_HSP PAT_ENC_HSP_<br />
WHERE	PAT_ENC_HSP2.PAT_ID=PAT_ENC_HSP_alias.PAT_ID<br />
AND	PAT_ENC_HSP2.PAT_ENC_CSN_ID &lt; PAT_ENC_HSP_alias.PAT_ENC_CSN_ID<br />
)<br />
When using aggregate functions in the SQL Expression, do not include the &#8216;local&#8217; table&#8217;s alias:<br />
<code><br />
(<br />
--will not compile<br />
SELECT	MAX(PAT_ENC_HSP2.HOSP_DISCH_TIME)<br />
FROM	PAT_ENC_HSP PAT_ENC_HSP2<br />
WHERE	PAT_ENC_HSP2.PAT_ID=PAT_ENC_HSP.PAT_ID<br />
AND	PAT_ENC_HSP2.PAT_ENC_CSN_ID &lt; PAT_ENC_HSP.PAT_ENC_CSN_ID<br />
)</code></p>
<p>(<br />
&#8211;will compile<br />
SELECT	MAX(HOSP_DISCH_TIME)<br />
FROM	PAT_ENC_HSP PAT_ENC_HSP2<br />
WHERE	PAT_ENC_HSP2.PAT_ID=PAT_ENC_HSP.PAT_ID<br />
AND	PAT_ENC_HSP2.PAT_ENC_CSN_ID &lt; PAT_ENC_HSP.PAT_ENC_CSN_ID<br />
)<br />
When using aggregate functions in the SQL Expression, you can include the &#8216;main&#8217; query&#8217;s table&#8217;s alias:<br />
<code><br />
(<br />
--will compile<br />
SELECT	MAX(PAT_ENC_HSP.HOSP_DISCH_TIME)<br />
FROM	PAT_ENC_HSP PAT_ENC_HSP2<br />
WHERE	PAT_ENC_HSP2.PAT_ID=PAT_ENC_HSP.PAT_ID<br />
AND	PAT_ENC_HSP2.PAT_ENC_CSN_ID &lt; PAT_ENC_HSP.PAT_ENC_CSN_ID<br />
)<br />
</code></p>
<p>If you are using a SQL Expression as a subquery and wish to link it to the detail row of your main report, do not include the table you wish to link to in the FROM clause of the subquery. For example:<br />
<code><br />
(<br />
SELECT	MAX("IP_FLWSHT_MEAS_SP"."MEAS_VALUE")<br />
FROM	IP_FLWSHT_MEAS IP_FLWSHT_MEAS_SP,<br />
IP_FLWSHT_REC IP_FLWSHT_REC_SP<br />
WHERE	"IP_FLWSHT_MEAS_SP"."FLO_MEAS_ID" in ('11')<br />
AND	"IP_FLWSHT_MEAS_SP"."FSD_ID" = "IP_FLWSHT_REC_SP"."FSD_ID"<br />
AND	"IP_FLWSHT_REC_SP"."INPATIENT_DATA_ID" = "PAT_ENC_HSP"."INPATIENT_DATA_ID"<br />
)<br />
</code></p>
<p>The above query links to the PAT_ENC_HSP table in the main report by linking to the INPATIENT_DATA_ID field. To accomplish this, PAT_ENC_HSP is omitted from the FROM clause in the query.</p>
]]></content:encoded>
			<wfw:commentRss>http://cogniza.com/wordpress/2005/11/07/crystal-reports-using-sql-expression-fields/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
	</channel>
</rss>

