﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / Administering / SQL Server 2005  / select query / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Thu, 23 May 2013 21:49:55 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: select query</title><link>http://www.sqlservercentral.com/Forums/Topic855592-146-1.aspx</link><description>My first thought would be to do the case statement shown earlier.  This seems to be cleaner code if someone else has to look at it in the future.  To those that are doing the 'Union All'.  If you are trying to get the max date wouldn't just using the 'Union' statement be more efficient?  Since doing just 'Union' will eliminate any duplicate entries.  'Union All' puts in every entry into the result set.</description><pubDate>Wed, 24 Apr 2013 07:26:52 GMT</pubDate><dc:creator>belowery</dc:creator></item><item><title>RE: select query</title><link>http://www.sqlservercentral.com/Forums/Topic855592-146-1.aspx</link><description>You have to alias the subquery in this case:select max(myDate) from ( select field_1 myDate from myTable union all select field_2 myDate from myTable union all select field_3 myDate from myTable) x</description><pubDate>Wed, 24 Apr 2013 05:43:46 GMT</pubDate><dc:creator>Steve Shurts</dc:creator></item><item><title>RE: select query</title><link>http://www.sqlservercentral.com/Forums/Topic855592-146-1.aspx</link><description>[quote][b]fadewumi (4/23/2013)[/b][hr]why is differntial  backup  is so important[/quote]Please post new questions to a new thread.  This question has nothing to do with this thread.And, fyi, a differential backup is a backup of all changes made to a database since the last full backup.</description><pubDate>Tue, 23 Apr 2013 17:04:09 GMT</pubDate><dc:creator>Lynn Pettis</dc:creator></item><item><title>RE: select query</title><link>http://www.sqlservercentral.com/Forums/Topic855592-146-1.aspx</link><description>why is differntial  backup  is so important</description><pubDate>Tue, 23 Apr 2013 16:50:37 GMT</pubDate><dc:creator>fadewumi</dc:creator></item><item><title>RE: select query</title><link>http://www.sqlservercentral.com/Forums/Topic855592-146-1.aspx</link><description>what is differential backup .</description><pubDate>Tue, 23 Apr 2013 16:48:00 GMT</pubDate><dc:creator>fadewumi</dc:creator></item><item><title>RE: select query</title><link>http://www.sqlservercentral.com/Forums/Topic855592-146-1.aspx</link><description>--sample Datadeclare @tbl table(  id      int ,field_1 date ,field_2 date ,field_3 date )insert into @tbl values(1,'20120823','20120824','20120825')--Queryselect max(a.mydate),a.idfrom (      select id,field_1 mydate from @tbl      union all      select id,field_2 from @tbl      union all      select id,field_3 from @tbl     )agroup by a.id     I think this is what you require.... Please provide some sample day from next time.. So, that it will be easy to answer</description><pubDate>Mon, 24 Sep 2012 23:58:47 GMT</pubDate><dc:creator>yerram.vishwanatham</dc:creator></item><item><title>RE: select query</title><link>http://www.sqlservercentral.com/Forums/Topic855592-146-1.aspx</link><description>--Sample Datadeclare @tbl table(  id      int primary key ,field_1 date ,field_2 date ,field_3 date )insert into @tbl values(1,'20120823','20120824','20120825')--Queryselect max(a.mydate),a.idfrom (      select id,MAX(field_1) mydate from @tbl group by id      union all      select id,MAX(field_2) from @tbl group by id      union all      select id,MAX(field_3) from @tbl group by id     )agroup by a.id    I think this is what required for you.......So, from the next time whenever you post a query make sure that you provide sample data like above so that one can easily answer your query.........[font="Comic Sans MS"]Vishwanath[/font]</description><pubDate>Mon, 24 Sep 2012 23:47:22 GMT</pubDate><dc:creator>yerram.vishwanatham</dc:creator></item><item><title>RE: select query</title><link>http://www.sqlservercentral.com/Forums/Topic855592-146-1.aspx</link><description>I thought I had posted my reply prior so I apologize if this is a duplicate post...Here is an alternative you may want to try to get the max date value form your columns.  I thought this was simple but had to get help from a peer.[code="sql"]declare @dt table ( akey int, d1 datetime, d2 datetime, d3 datetime )	insert  into @dt values (1, '2012-01-01', getdate(), '2012-09-01' )	insert  into @dt values (2, getdate(), '2012-01-01', '2012-09-01' )	insert  into @dt values (3, '2012-01-01', '2012-09-01', getdate() )select    akey, max(dates)from    ( select akey, d1, d2, d3 from @dt ) p    unpivot (		dates for datevals in ( d1, d2, d3 )	)AS unpvtgroup by akey[/code]</description><pubDate>Mon, 24 Sep 2012 12:06:46 GMT</pubDate><dc:creator>The Timmer</dc:creator></item><item><title>RE: select query</title><link>http://www.sqlservercentral.com/Forums/Topic855592-146-1.aspx</link><description>I thought this would be an easy one but had to get some help too.  Try this...[code="sql"]declare @dt table ( akey int, d1 datetime, d2 datetime, d3 datetime )	insert  into @dt values (1, '2012-01-01', getdate(), '2012-09-01' )	insert  into @dt values (2, getdate(), '2012-01-01', '2012-09-01' )	insert  into @dt values (3, '2012-01-01', '2012-09-01', getdate() )select    akey, max(dates)from    ( select akey, d1, d2, d3 from @dt ) p    unpivot (		dates for datevals in ( d1, d2, d3 )	)AS unpvtgroup by akey[/code]</description><pubDate>Mon, 24 Sep 2012 11:46:12 GMT</pubDate><dc:creator>The Timmer</dc:creator></item><item><title>RE: select query</title><link>http://www.sqlservercentral.com/Forums/Topic855592-146-1.aspx</link><description>It can also be done using Pivot Concept...very simpleSELECT Max(date) FROM   (SELECT date         FROM   (SELECT date1,                        date2,                        date3                 FROM   sample) AS X                UNPIVOT (date                        FOR sample IN (date1,                                       date2,                                       date3)) AS unpivoty) AS Z</description><pubDate>Sat, 22 Sep 2012 21:19:37 GMT</pubDate><dc:creator>vivekswamy85</dc:creator></item><item><title>RE: select query</title><link>http://www.sqlservercentral.com/Forums/Topic855592-146-1.aspx</link><description>select max(myDate) from ( select field_1 myDate from myTable union all select field_2 myDate from myTable union all select field_3 myDate from myTable) as Temp It will return the maximum date of the three date columns values.Example :If u r data would be like belowselect * from myTable field_1	field_2	field_31/1/2010	2/2/2010	3/3/20101/1/2011	2/2/2011	3/3/2011Query Returns :2011-03-03</description><pubDate>Tue, 01 Mar 2011 07:38:49 GMT</pubDate><dc:creator>sureshraghavendrarao</dc:creator></item><item><title>RE: select query</title><link>http://www.sqlservercentral.com/Forums/Topic855592-146-1.aspx</link><description>select Max(date) from (select date1 from table1union allselect date2 from table1union allselect date3 from table1 ) as tbl</description><pubDate>Tue, 30 Mar 2010 09:35:46 GMT</pubDate><dc:creator>igggis</dc:creator></item><item><title>RE: select query</title><link>http://www.sqlservercentral.com/Forums/Topic855592-146-1.aspx</link><description>Looks like someone beat me to the punch.</description><pubDate>Thu, 28 Jan 2010 13:51:43 GMT</pubDate><dc:creator>AJHall</dc:creator></item><item><title>RE: select query</title><link>http://www.sqlservercentral.com/Forums/Topic855592-146-1.aspx</link><description>Didn't notice the missing column name(s)...</description><pubDate>Thu, 28 Jan 2010 13:50:36 GMT</pubDate><dc:creator>Florian Reischl</dc:creator></item><item><title>RE: select query</title><link>http://www.sqlservercentral.com/Forums/Topic855592-146-1.aspx</link><description>Your approach should work, doesn't it?Maybe this could be a better performance:[code]SELECT      MAX(         CASE WHEN field1 &amp;gt; field2 THEN            CASE WHEN field1 &amp;gt; field3 THEN               field1            ELSE field3 END         ELSE            CASE WHEN field2 &amp;gt; field3 THEN               field2            ELSE field3 END         END         )   FROM myTable[/code]GreetsFlo</description><pubDate>Thu, 28 Jan 2010 13:48:52 GMT</pubDate><dc:creator>Florian Reischl</dc:creator></item><item><title>RE: select query</title><link>http://www.sqlservercentral.com/Forums/Topic855592-146-1.aspx</link><description>[quote][b]charipg (1/28/2010)[/b][hr]Please correct the below query..........select max(myDate) from ( select field_1 myDate from myTable union all select field_2 myDate from myTable union all select field_3 myDate from myTable)[/quote]If you add an alias at the end of your query to give your UNION statement a "pseudo table name", the query will return a result.Note: Since you didn't mention whether you get an error message (or even what that error would have been) I'm just guessing that this is what you're looking for...[code="sql"]select max(myDate) from ( select field_1 myDate from myTable union all select field_2 myDate from myTable union all select field_3 myDate from myTable) [b]uniontable [/b][/code]</description><pubDate>Thu, 28 Jan 2010 13:46:55 GMT</pubDate><dc:creator>LutzM</dc:creator></item><item><title>RE: select query</title><link>http://www.sqlservercentral.com/Forums/Topic855592-146-1.aspx</link><description>Please correct the below query..........select max(myDate) from ( select field_1 myDate from myTable union all select field_2 myDate from myTable union all select field_3 myDate from myTable)</description><pubDate>Thu, 28 Jan 2010 13:37:30 GMT</pubDate><dc:creator>charipg</dc:creator></item><item><title>RE: select query</title><link>http://www.sqlservercentral.com/Forums/Topic855592-146-1.aspx</link><description>[quote][b]charipg (1/28/2010)[/b][hr]I am not able to provide the sample data.can you pls provide the sample query?[/quote]You don't have to provide your real data. Get the basic concept and transform it into a sample table with fake column names and fake values. But make sure it represents what you're struggling with.Please remember we don't see what you see. So you have to help us help you.</description><pubDate>Thu, 28 Jan 2010 13:19:17 GMT</pubDate><dc:creator>LutzM</dc:creator></item><item><title>RE: select query</title><link>http://www.sqlservercentral.com/Forums/Topic855592-146-1.aspx</link><description>[quote][b]charipg (1/28/2010)[/b][hr]I am not able to provide the sample data.can you pls provide the sample query?[/quote]?</description><pubDate>Thu, 28 Jan 2010 13:15:16 GMT</pubDate><dc:creator>Florian Reischl</dc:creator></item><item><title>RE: select query</title><link>http://www.sqlservercentral.com/Forums/Topic855592-146-1.aspx</link><description>I am not able to provide the sample data.can you pls provide the sample query?</description><pubDate>Thu, 28 Jan 2010 13:14:06 GMT</pubDate><dc:creator>charipg</dc:creator></item><item><title>RE: select query</title><link>http://www.sqlservercentral.com/Forums/Topic855592-146-1.aspx</link><description>Try MAX with an included CASE WHEN.GreetsFlo</description><pubDate>Thu, 28 Jan 2010 13:13:38 GMT</pubDate><dc:creator>Florian Reischl</dc:creator></item><item><title>RE: select query</title><link>http://www.sqlservercentral.com/Forums/Topic855592-146-1.aspx</link><description>[quote][b]charipg (1/28/2010)[/b][hr]selecting one greatest value from 3 coloumns in a table.?[/quote]Yes.How about sample data including your current query as requested before?</description><pubDate>Thu, 28 Jan 2010 13:10:19 GMT</pubDate><dc:creator>LutzM</dc:creator></item><item><title>RE: select query</title><link>http://www.sqlservercentral.com/Forums/Topic855592-146-1.aspx</link><description>selecting one greatest value from 3 coloumns in a table.?</description><pubDate>Thu, 28 Jan 2010 13:07:48 GMT</pubDate><dc:creator>charipg</dc:creator></item><item><title>RE: select query</title><link>http://www.sqlservercentral.com/Forums/Topic855592-146-1.aspx</link><description>selecting one greatest value from 3 coloumns.?</description><pubDate>Thu, 28 Jan 2010 13:06:28 GMT</pubDate><dc:creator>charipg</dc:creator></item><item><title>RE: select query</title><link>http://www.sqlservercentral.com/Forums/Topic855592-146-1.aspx</link><description>Sounds like homework.If not, please provide sample data, expected result and what you've tried so far as described in the first link in my signature.</description><pubDate>Thu, 28 Jan 2010 13:03:44 GMT</pubDate><dc:creator>LutzM</dc:creator></item><item><title>select query</title><link>http://www.sqlservercentral.com/Forums/Topic855592-146-1.aspx</link><description>i need a select query for below.there is 3 date colums in a table.how to find out the greatest value from these 3 columns.</description><pubDate>Thu, 28 Jan 2010 12:59:44 GMT</pubDate><dc:creator>charipg</dc:creator></item></channel></rss>