Forums before death by AOL, social media and spammers... "We can't have nice things"
|    comp.databases.ms-sqlserver    |    Notorious Rube Goldberg contraption    |    19,505 messages    |
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
|    Message 17,747 of 19,505    |
|    Hugo Kornelis to SQL Learner    |
|    Re: Return valuse based on multiple "if     |
|    06 Feb 10 23:42:58    |
   
   97dda0ba   
   From: hugo@perFact.REMOVETHIS.info.INVALID   
      
   On Sat, 6 Feb 2010 11:43:38 -0800 (PST), SQL Learner wrote:   
      
   >Hi All,   
   >   
   >Hope someone can help to solve this SQL question.   
   >   
   >I have the following table called '#temptable' (it is a temp table)   
   >   
   >source price state   
   >A 1 NY   
   >B 2 NY   
   >C 3 NY   
   >B 4 CA   
   >C 5 CA   
   >C 6 TX   
   >   
   >I need a SQL statement that performs the following pesudo code:   
   >   
   >if source A exists then (select * from #temptable where source = 'A')   
   >if source A not exists then check if source B exist.   
   >if source B exists then (select * from #temptable where source = 'B')   
   >if source B not exists then check if source C exist.   
   >if source C exists then (select * from #temptable where source = 'C')   
   >if source C not exists then print 'Nothing is found.'   
   >   
   >The following code can be used to create the temp table for testing:   
   >   
   >create table #TempTable (source char(1), price int, state char (2))   
   >insert into #TempTable values ('A', 1, 'NY')   
   >insert into #TempTable values ('B', 2, 'NY')   
   >insert into #TempTable values ('C', 3, 'NY')   
   >insert into #TempTable values ('B', 4, 'CA')   
   >insert into #TempTable values ('C', 5, 'CA')   
   >insert into #TempTable values ('C',6, 'TX')   
   >   
   >Thanks.   
   >   
   >SQL Learner   
   >   
      
   Hi SQL Learner,   
      
   How about:   
      
   SELECT source, price, state   
   FROM #TempTable   
   WHERE source = (SELECT MIN(source) FROM #TempTable);   
      
   --   
   Hugo Kornelis, SQL Server MVP   
   My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   
|
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
(c) 1994, bbs@darkrealms.ca