MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 8th, 2004, 04:57 AM
niruairc niruairc is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 3 niruairc User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Different select abilities where no data

I'm really in need of help with this one!!
I am writing a script which will provide me with financial info.
This will then be used to upload to a management system.
I have tried using the following code;
select
year.mem_desc as year,
line.mem_name as line,
unit.mem_name as unit,
ver.mem_name,
version =
Case
when ver.mem_name like 'Actual' then 'Actual Input'
when ver.mem_name like 'Forecast' then 'Actual Input'
when ver.mem_name not like 'Actual' then ver.mem_name
end,
cust1.mem_name as product,
cust2.mem_name as costcentre,
cust3.mem_name geographic_market,
cust4.mem_name as segment,
Jan =
case
when Jan is null and ver.mem_name like 'Actual'
then select Jan from finloc where year.mem_desc like year and line.mem_name like line and
unit.mem_name like unit and ver.mem_name like 'Forecast'
when ver.mem_name like 'Forecast'
then select Jan from finloc where year.mem_desc like year and line.mem_name like line and
unit.mem_name like unit and ver.mem_name like 'Actual'
when Jan is not null then convert(varchar(50),Jan)
end,


This doesnt work!! I need to be able to say that if the columns are blank or null for Actual then take Forecast, a kind of merge statement is needed but I dont know how to write one, can someone please please help!!

Reply With Quote
  #2  
Old September 8th, 2004, 05:42 AM
swampBoogie swampBoogie is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jan 2003
Location: Paris Uppland
Posts: 1,771 swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 11 h 59 m 28 sec
Reputation Power: 37
Code:
case
when Jan is null and ver.mem_name = 'Actual'
then (select Jan from finloc where year.mem_desc = year and line.mem_name = line and
unit.mem_name = unit and ver.mem_name = 'Forecast')
when ver.mem_name = 'Forecast'
then (select Jan from finloc where year.mem_desc = year and line.mem_name = line and
unit.mem_name = unit and ver.mem_name =  'Actual')
when Jan is not null then cast(jan as varchar(50))
end as Jan

Reply With Quote
  #3  
Old September 8th, 2004, 08:36 AM
niruairc niruairc is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 3 niruairc User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thks swampboogie, although I'm getting an invalid syntax on 'From' can you check all the code to see how i can resolve it and test the code,
All code as follows:

select
year.mem_desc as year,
line.mem_name as line,
unit.mem_name as unit,
ver.mem_name,
version =
Case
when ver.mem_name like 'Actual' then 'Actual Input'
when ver.mem_name like 'Forecast' then 'Actual Input'
when ver.mem_name not like 'Actual' then ver.mem_name
end,
cust1.mem_name as product,
cust2.mem_name as costcentre,
cust3.mem_name geographic_market,
cust4.mem_name as segment,
Jan =
case
when Jan is null and ver.mem_name = 'Actual'
then (select Jan from finloc where year.mem_desc = year and line.mem_name = line and
unit.mem_name = unit and ver.mem_name = 'Forecast')
when ver.mem_name = 'Forecast'
then (select Jan from finloc where year.mem_desc = year and line.mem_name = line and
unit.mem_name = unit and ver.mem_name = 'Actual')
when Jan is not null then cast(jan as varchar(50))
end,
Feb =
case
when Feb is null and ver.mem_name = 'Actual'
then (select Feb from finloc where year.mem_desc = year and line.mem_name = line and
unit.mem_name = unit and ver.mem_name = 'Forecast')
when ver.mem_name = 'Forecast'
then (select Feb from finloc where year.mem_desc = year and line.mem_name = line and
unit.mem_name = unit and ver.mem_name = 'Actual')
when Feb is not null then cast(Feb as varchar(50))
end,
Mar =
case
when Mar is null and ver.mem_name = 'Actual'
then (select Mar from finloc where year.mem_desc = year and line.mem_name = line and
unit.mem_name = unit and ver.mem_name = 'Forecast')
when ver.mem_name = 'Forecast'
then (select Mar from finloc where year.mem_desc = year and line.mem_name = line and
unit.mem_name = unit and ver.mem_name = 'Actual')
when Mar is not null then cast(Mar as varchar(50))
end,
Apr =
case
when Apr is null and ver.mem_name = 'Actual'
then (select Apr from finloc where year.mem_desc = year and line.mem_name = line and
unit.mem_name = unit and ver.mem_name = 'Forecast')
when ver.mem_name = 'Forecast'
then (select Apr from finloc where year.mem_desc = year and line.mem_name = line and
unit.mem_name = unit and ver.mem_name = 'Actual')
when Apr is not null then cast(Apr as varchar(50))
end,
May =
case
when May is null and ver.mem_name = 'Actual'
then (select May from finloc where year.mem_desc = year and line.mem_name = line and
unit.mem_name = unit and ver.mem_name = 'Forecast')
when ver.mem_name = 'Forecast'
then (select May from finloc where year.mem_desc = year and line.mem_name = line and
unit.mem_name = unit and ver.mem_name = 'Actual')
when May is not null then cast(May as varchar(50))
end,
Jun =
case
when Jun is null and ver.mem_name = 'Actual'
then (select Jun from finloc where year.mem_desc = year and line.mem_name = line and
unit.mem_name = unit and ver.mem_name = 'Forecast')
when ver.mem_name = 'Forecast'
then (select Jun from finloc where year.mem_desc = year and line.mem_name = line and
unit.mem_name = unit and ver.mem_name = 'Actual')
when Jun is not null then cast(Jun as varchar(50))
end,
Jul =
case
when Jul is null and ver.mem_name = 'Actual'
then (select Jul from finloc where year.mem_desc = year and line.mem_name = line and
unit.mem_name = unit and ver.mem_name = 'Forecast')
when ver.mem_name = 'Forecast'
then (select Jul from finloc where year.mem_desc = year and line.mem_name = line and
unit.mem_name = unit and ver.mem_name = 'Actual')
when Jul is not null then cast(Jul as varchar(50))
end,
Aug =
case
when Aug is null and ver.mem_name = 'Actual'
then (select Aug from finloc where year.mem_desc = year and line.mem_name = line and
unit.mem_name = unit and ver.mem_name = 'Forecast')
when ver.mem_name = 'Forecast'
then (select Aug from finloc where year.mem_desc = year and line.mem_name = line and
unit.mem_name = unit and ver.mem_name = 'Actual')
when Aug is not null then cast(Aug as varchar(50))
end,
Sep =
case
when Sep is null and ver.mem_name = 'Actual'
then (select Sep from finloc where year.mem_desc = year and line.mem_name = line and
unit.mem_name = unit and ver.mem_name = 'Forecast')
when ver.mem_name = 'Forecast'
then (select Sep from finloc where year.mem_desc = year and line.mem_name = line and
unit.mem_name = unit and ver.mem_name = 'Actual')
when Sep is not null then cast(Sep as varchar(50))
end,
Oct =
case
when Oct is null and ver.mem_name = 'Actual'
then (select Oct from finloc where year.mem_desc = year and line.mem_name = line and
unit.mem_name = unit and ver.mem_name = 'Forecast')
when ver.mem_name = 'Forecast'
then (select Oct from finloc where year.mem_desc = year and line.mem_name = line and
unit.mem_name = unit and ver.mem_name = 'Actual')
when Oct is not null then cast(Oct as varchar(50))
end,
Nov =
case
when Nov is null and ver.mem_name = 'Actual'
then (select Nov from finloc where year.mem_desc = year and line.mem_name = line and
unit.mem_name = unit and ver.mem_name = 'Forecast')
when ver.mem_name = 'Forecast'
then (select Nov from finloc where year.mem_desc = year and line.mem_name = line and
unit.mem_name = unit and ver.mem_name = 'Actual')
when Nov is not null then cast(Nov as varchar(50))
end,
Dec =
case
when Dec is null and ver.mem_name = 'Actual'
then (select Dec from finloc where year.mem_desc = year and line.mem_name = line and
unit.mem_name = unit and ver.mem_name = 'Forecast')
when ver.mem_name = 'Forecast'
then (select Dec from finloc where year.mem_desc = year and line.mem_name = line and
unit.mem_name = unit and ver.mem_name = 'Actual')
when Dec is not null then cast(Dec as varchar(50))
end,
from year, line, unit, ver, cust1, cust2, cust3, cust4, finloc
where
year.mem_id = finloc.year_id and
line.mem_id = finloc.line_id and
unit.mem_id = finloc.unit_id and
ver.mem_id = finloc.ver_id and
cust1.mem_id = finloc.cust1_id and
cust2.mem_id = finloc.cust2_id and
cust3.mem_id = finloc.cust3_id and
cust4.mem_id = finloc.cust4_id and
(line.mem_id='90000012' or line.mem_id='90000013'
or line.mem_id='90000014' or line.mem_id='90000015' or line.mem_id='90000016'
or line.mem_id='90000035' or line.mem_id='90000046' or line.mem_id='90000045'
or line.mem_id='90000044' or line.mem_id='90000043' or line.mem_id='90000042'
or line.mem_id='90000041' or line.mem_id='90000040' or line.mem_id='90000039'
or line.mem_id='90000038' or line.mem_id='90000022' or line.mem_id='90000036'
or line.mem_id='90000047' or line.mem_id='90000034' or line.mem_id='90000033'
or line.mem_id='90000032' or line.mem_id='90000031' or line.mem_id='90000030'
or line.mem_id='90000029' or line.mem_id='90000028' or line.mem_id='90000027'
or line.mem_id='90000026' or line.mem_id='90000025' or line.mem_id='90000289'
or line.mem_id='90000290' or line.mem_id='90000291' or line.mem_id='90000292'
or line.mem_id='90000293' or line.mem_id='90000037' or line.mem_id='90000017'
or line.mem_id='90000294' or line.mem_id='90000018' or line.mem_id='90000019'
or line.mem_id='90000020' or line.mem_id='90000021' or line.mem_id='90000023'
or line.mem_id='90000048' or line.mem_id='90000049' or line.mem_id='90000052'
or line.mem_id='90000050' or line.mem_id='90000053' or line.mem_id='90000051'
or line.mem_id='90000054' or line.mem_id='90000055' or line.mem_id='90000056'
or line.mem_id='90000057' or line.mem_id='90000058' or line.mem_id='90000059'
or line.mem_id='90000060' or line.mem_id='90000061' or line.mem_id='90000062'
or line.mem_id='90000063' or line.mem_id='90000064' or line.mem_id='90000065'
or line.mem_id='90000066' or line.mem_id='90000067' or line.mem_id='90000068'
or line.mem_id='90000069' or line.mem_id='90000070' or line.mem_id='90000071'
or line.mem_id='90000072' or line.mem_id='90000073' or line.mem_id='90000074'
or line.mem_id='90000075' or line.mem_id='90000076' or line.mem_id='90000077'
or line.mem_id='90000078' or line.mem_id='90000295' or line.mem_id='90000855'
or line.mem_id='90000856' or line.mem_id='90000857' or line.mem_id='90000858'
or line.mem_id='90000880' or line.mem_id='90000079' or line.mem_id='90000080'
or line.mem_id='90000081' or line.mem_id='90000082' or line.mem_id='90000083'
or line.mem_id='90000084' or line.mem_id='90000085' or line.mem_id='90000086'
or line.mem_id='90000087' or line.mem_id='90000088' or line.mem_id='90000089'
or line.mem_id='90000090' or line.mem_id='90000091' or line.mem_id='90000092'
or line.mem_id='90000093' or line.mem_id='90000094' or line.mem_id='90000095'
or line.mem_id='90000096' or line.mem_id='90000097' or line.mem_id='90000098'
or line.mem_id='90000099')
and (ver.mem_name like 'Budget' or ver.mem_name like 'Prior Year'or ver.mem_name like 'Actual' or ver.mem_name like 'Forecast')
ORDER BY ver.mem_name DESC;
go




Quote:
Originally Posted by swampBoogie
Code:
case
when Jan is null and ver.mem_name = 'Actual'
then (select Jan from finloc where year.mem_desc = year and line.mem_name = line and
unit.mem_name = unit and ver.mem_name = 'Forecast')
when ver.mem_name = 'Forecast'
then (select Jan from finloc where year.mem_desc = year and line.mem_name = line and
unit.mem_name = unit and ver.mem_name =  'Actual')
when Jan is not null then cast(jan as varchar(50))
end as Jan

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > Different select abilities where no data


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway