Author |
Topic |
Rune Thandy
Starting Member
3 Posts |
Posted - 2007-04-03 : 07:45:48
|
I've got the following two tables:-costcode with costcodeid,costcodedesc,costcodestatus (which can be Active or Discontinued)-tallycombi with projectid,projectdesc,itemid,itemdesc,unit,combistatus (Active or Discontinued).In the costcode table the costcodeid is unique.In the tallycombi table the combination of projectid and itemid is unique.The costcodeid and costcodedesc for a certain project are inserted in the tallycombi table.For a costcode item the unit will always be A.There is another table for combining projects with other items, but that is not relevant for this problem.In the table tallycombi a project and an item are linked together and can have the combistatus Active or Discontinued.Now I need to show all Active costcodes and when they exist in the tallycombi table for a given projectid, the status of that combination has to be shown as well.So for example:costcodeid----------costcodedesc-------------costcodestatus00001---------------costcodeone--------------Active00012---------------costcodetwo--------------Discontinued00123---------------costcodethree------------Active01234---------------costcodefour-------------Activeprojectid----projectdesc-----itemid-----itemdesc-----------unit------combistatus999---------projectone------00123-----costcodethree------A---------Discontinued456---------projecttwo------00123-----costcodethree------A---------Active456---------projecttwo------00001-----costcodeone--------A--------ActiveThe output for projectid='999' should becostcodeid-----costcodedesc---------combistatus00001----------costcodeone----------NULL00123----------costcodethree--------Discontinued01234----------costcodefour---------NULLHow can I accomplish this? |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-04-03 : 08:37:54
|
Yes, that edit made a huge difference  SELECT cc.CostCodeID, cc.CostCodeDesc, MAX(NULLIF(tt.CombiStatus, cc.CostCodeStatus)) AS CombistatusFROM CostCode AS ccLEFT JOIN TallyTable AS tt ON tt.ItemID = cc.CostCodeIDWHERE cc.CostCodeStatus = 'Active'GROUP BY cc.CostCodeID, cc.CostCodeDescORDER BY cc.CostCodeID, cc.CostCodeDesc Peter LarssonHelsingborg, Sweden |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-04-03 : 08:39:50
|
[code]SELECT cc.CostCodeID, cc.CostCodeDesc, tt.CombiStatus AS CombistatusFROM @CostCode AS ccLEFT JOIN @TallyTable AS tt ON tt.ItemID = cc.CostCodeID AND tt.CombiStatus <> 'Active'WHERE cc.CostCodeStatus = 'Active'ORDER BY cc.CostCodeID, cc.CostCodeDesc[/code]Peter LarssonHelsingborg, Sweden |
 |
|
Rune Thandy
Starting Member
3 Posts |
Posted - 2007-04-03 : 08:50:49
|
I accidently pressed submit instead of preview. And while I was editing, you were so fast that you already answered the thread. Where do I put in the projectid? |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-04-03 : 09:46:27
|
In a WHERE clause?Your sample data do not provide enough information to determine that according to your expected output.SELECT cc.CostCodeID, cc.CostCodeDesc, tt.CombiStatus AS CombistatusFROM @CostCode AS ccLEFT JOIN @TallyTable AS tt ON tt.ItemID = cc.CostCodeID AND tt.ProjectID = 456WHERE cc.CostCodeStatus = 'Active'ORDER BY cc.CostCodeID, cc.CostCodeDesc Peter LarssonHelsingborg, Sweden |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-04-03 : 10:37:45
|
http://sql-server-performance.com/forum/topic.asp?TOPIC_ID=20515MadhivananFailing to plan is Planning to fail |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-04-03 : 10:42:43
|
How many people's time has be occupied?Peter LarssonHelsingborg, Sweden |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-04-03 : 10:51:11
|
As many as possibleorUntil he/she is satisfied with the answerMadhivananFailing to plan is Planning to fail |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-04-03 : 10:53:40
|
He can't find the time to answer either...Waste of time here.Peter LarssonHelsingborg, Sweden |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-04-03 : 10:59:15
|
It is common to some newbiesMadhivananFailing to plan is Planning to fail |
 |
|
Rune Thandy
Starting Member
3 Posts |
Posted - 2007-04-04 : 03:33:13
|
I had to find an anser as quick as possible, so I tried more forums.It's not for myself, but for work.And I had gone home after you posted your posts.But I do thank you for your time and energy you put in. |
 |
|
|