Question : Confusing SQL

I am tring to do a Case statement in SQL.  I want it to be sort of like when in Java we say if project is 101, then do this.  Else if project is 102, do something else.  Else if project is 103, do this.  I cannot figure out the syntax.  Here is what I have now:

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
SELECT 

Project, 
[pjt_entity]+'-' AS Task, 

Sum (case when [acct]='directlabor'then [units] ELSE 0 END) AS ZinHrs,
Sum(case when ([ACCT]='SUBCONTRACT' or [ACCT]='HEI' or [ACCT]='NGTS' or [acct]='ANALEX') then [UNITS] ELSE 0 End) AS SubHrs,
Sum(case when([acct]='direct`labor') then [amount] Else 0 End) AS Labor,
Sum(case when([ACCT]='SUBCONTRACT' Or [ACCT]='HEI' Or [ACCT]='NGTS' Or [acct]='ANALEX') then [AMOUNT] ELSE 0 End) AS Sub,

Sum(case when[ACCT]='EQUIPMENT' then [AMOUNT] ELSE 0 End) AS Equipment, 
Sum(case when[acct]='fabrication' then [amount] ELSE 0 End) AS Fabrication,
Sum(case when[acct]='material' then [amount] ELSE 0 End) AS Material, 

Sum(case when([acct]='misc odcs' Or [acct]='EDUC/TRAIN' Or [ACCT]='SUPPLIES') then [amount] else 0 end) AS MiscODC,
Sum(case when[ACCT]='TRAVEL' then [AMOUNT] ELSE 0 End) AS Travel, 
Sum(case when([acct] Like 'alloc%' And [acct]<>'ALLOCFEE') then [AMOUNT] else 0 end) AS Indirect,
Sum(case when[ACCT]='ALLOCFEE'then [AMOUNT] else 0 end) AS Fee,
Sum(amount) AS TotalCost 

FROM [MyDatabase].dbo.[MyTable]
WHERE ((fiscalno >= '200812' and (fiscalno)<=@fiscalno) AND ((acct)<>'BTD'))
GROUP BY project, [pjt_entity]+'-'
HAVING ((@proj = 'space102')) 
ORDER BY project, [pjt_entity]+'-';


1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
SELECT

Project, 
[pjt_entity]+'-' AS Task, 

Sum (case when [acct]='directlabor'then [units] ELSE 0 END) AS ZinHrs,
Sum(case when ([ACCT]='SUBCONTRACT' or [ACCT]='HEI' or [ACCT]='NGTS' or [acct]='ANALEX') then [UNITS] ELSE 0 End) AS SubHrs,
Sum(case when([acct]='directlabor') then [amount] Else 0 End) AS Labor,
Sum(case when([ACCT]='SUBCONTRACT' Or [ACCT]='HEI' Or [ACCT]='NGTS' Or [acct]='ANALEX') then [AMOUNT] ELSE 0 End) AS Sub,

Sum(case when[ACCT]='EQUIPMENT' then [AMOUNT] ELSE 0 End) AS Equipment, 
Sum(case when[acct]='fabrication' then [amount] ELSE 0 End) AS Fabrication,
Sum(case when[acct]='material' then [amount] ELSE 0 End) AS Material, 

Sum(case when([acct]='misc odcs' Or [acct]='EDUC/TRAIN' Or [ACCT]='SUPPLIES') then [amount] else 0 end) AS MiscODC,
Sum(case when[ACCT]='TRAVEL' then [AMOUNT] ELSE 0 End) AS Travel, 
Sum(case when([acct] Like 'alloc%' And [acct]<>'ALLOCFEE') then [AMOUNT] else 0 end) AS Indirect,
Sum(case when[ACCT]='ALLOCFEE'then [AMOUNT] else 0 end) AS Fee,
Sum(amount) AS TotalCost 

FROM [MyDatabase].dbo.[MyTable]
WHERE ((fiscalno >= '201001' and (fiscalno)<=@fiscalno) AND ((acct)<>'BTD'))
GROUP BY project, [pjt_entity]+'-'
HAVING ((@proj = 'space101')) 
ORDER BY project, [pjt_entity]+'-';


1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
SELECT

Project, 
[pjt_entity]+'-' AS Task, 

Sum (case when [acct]='directlabor'then [units] ELSE 0 END) AS ZinHrs,
Sum(case when ([ACCT]='SUBCONTRACT' or [ACCT]='HEI' or [ACCT]='NGTS' or [acct]='ANALEX') then [UNITS] ELSE 0 End) AS SubHrs,
Sum(case when([acct]='directlabor') then [amount] Else 0 End) AS Labor,
Sum(case when([ACCT]='SUBCONTRACT' Or [ACCT]='HEI' Or [ACCT]='NGTS' Or [acct]='ANALEX') then [AMOUNT] ELSE 0 End) AS Sub,

Sum(case when[ACCT]='EQUIPMENT' then [AMOUNT] ELSE 0 End) AS Equipment, 
Sum(case when[acct]='fabrication' then [amount] ELSE 0 End) AS Fabrication,
Sum(case when[acct]='material' then [amount] ELSE 0 End) AS Material, 

Sum(case when([acct]='misc odcs' Or [acct]='EDUC/TRAIN' Or [ACCT]='SUPPLIES') then [amount] else 0 end) AS MiscODC,
Sum(case when[ACCT]='TRAVEL' then [AMOUNT] ELSE 0 End) AS Travel, 
Sum(case when([acct] Like 'alloc%' And [acct]<>'ALLOCFEE') then [AMOUNT] else 0 end) AS Indirect,
Sum(case when[ACCT]='ALLOCFEE'then [AMOUNT] else 0 end) AS Fee,
Sum(amount) AS TotalCost 

FROM [MyDatabase].dbo.[MyTable]
WHERE ((fiscalno >= '201001' and (fiscalno)<=@fiscalno) AND ((acct)<>'BTD'))
GROUP BY project, [pjt_entity]+'-'
HAVING ((@proj = 'space106')) 
ORDER BY project, [pjt_entity]+'-';

Answer : Confusing SQL

You would have to do this in a stored procedure or something and use:

IF (@proj = '1')
BEGIN

END

IF (@proj = '2')
BEGIN

END

And so on...
Random Solutions  
 
programming4us programming4us