SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Need a single answer for multiple conditions.
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

fparker
Starting Member

27 Posts

Posted - 11/21/2011 :  17:12:16  Show Profile  Reply with Quote
Doing a data transfer from one system to another and am stuck on one aspect.

It kind of goes like this.

Select
column1
,column2
,column3
...

Then I need to calculate a values as such.

Query a column in another table and if there is one row returned then if that row has a 1 or 12 in it "String1" else "String2" <<-- this part i think i have solved

if there is more than one row returned, then all values must be 1 or 12 to get "String1", else "String2" << this part i am stumped on


I thought about writing a case statement but it seems really complicated.

TIA on any ideas on how to tackle this.


--
f

Edited by - fparker on 11/21/2011 17:35:16

fparker
Starting Member

27 Posts

Posted - 11/21/2011 :  17:40:55  Show Profile  Reply with Quote
Think i got it.

Thanks :)


--
f
Go to Top of Page

fparker
Starting Member

27 Posts

Posted - 11/21/2011 :  17:45:06  Show Profile  Reply with Quote
(select case (select count(w.how)
from wline w
where w.lineid = l.lineid )
when 1 then
(select case
(select w.how
from line w
where w.lineid = l.lineid )
when 1 then 'string1'
when 12 then 'string1'
else 'string2'
end)
else
case when (select count(w.how)
from wline w
where w.lineid = l.lineid and w.how in (1,12))
=
(select count(x.how)
from wline x
where x.lineid = l.lineid )
then 'string1'
else 'string2'
end

end )

l.linid is part of the bigger query.





--
f

Edited by - fparker on 11/21/2011 17:46:14
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 11/22/2011 :  01:05:13  Show Profile  Reply with Quote
dont overcomplicate things. i think what you need is this

select other columns...,
case when coalesce(l.cnt,0) = 0 then 'string1' else 'string2' end
from (mainquery) m
left join (select lineid,count(case when how in (1,12) then null else how end) as cnt
            from wline
            group by lineid
            )l
on l.lineid = m.lineid


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

fparker
Starting Member

27 Posts

Posted - 11/22/2011 :  16:38:55  Show Profile  Reply with Quote
thanks. i will try that out.


--
f
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 11/22/2011 :  23:43:33  Show Profile  Reply with Quote
wc
let us know how you got on

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

fparker
Starting Member

27 Posts

Posted - 11/29/2011 :  14:00:50  Show Profile  Reply with Quote
On the second to last line, what is the l for? the line is )l
btw, we are on sql 2003. sorry that I left that out.

--
f

Edited by - fparker on 11/29/2011 14:02:38
Go to Top of Page

DonAtWork
Flowing Fount of Yak Knowledge

2161 Posts

Posted - 11/29/2011 :  14:46:22  Show Profile  Reply with Quote
The )l is aliasing the derived table, so that you can reference it by that name.
Also, now the compiler can tell what you mean in the ON clause by l.lineid

http://weblogs.sqlteam.com/jeffs/archive/2008/05/13/question-needed-not-answer.aspx
How to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

For ultra basic questions, follow these links.
http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp
Go to Top of Page

fparker
Starting Member

27 Posts

Posted - 11/29/2011 :  15:19:28  Show Profile  Reply with Quote
got it, thanks.


--
f
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 11/30/2011 :  01:39:54  Show Profile  Reply with Quote
quote:
Originally posted by fparker

On the second to last line, what is the l for? the line is )l
btw, we are on sql 2003. sorry that I left that out.

--
f


there's no such version of sql server
i think you probably meant sql 2008

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000