Some SQL help please

Pho3nix

The Legend
Joined
Jul 31, 2009
Messages
32,827
Reaction score
3,033
Location
On the toilet
Hi,

I have 2 SQL scripts. 1 works and the other does not.
Have to rewrite this as the server they are working on is SQL 2000 :sick:

Any help would be appreciated.

Working :
Code:
with cte_p as
(
	SELECT EmployeeId,[PAL],[AAL],
    FROM [dbo].[A1] 
    PIVOT( SUM(CurrentImpliedBalance)
          FOR TypeCode IN ([PAL],[AAL])) AS P
)
select 
		EmployeeId,SUM([PAL]) AS PAL,SUM([AAL]) 
from cte_p where EmployeeId = 3039 group by EmployeeId
order  by 1

Not working
Code:
Select EmployeeId
    ,SUM( Case When TypeCode = '[PAL]' Then ISNULL(CurrentImpliedBalance,0) End ) As [PAL]
    ,SUM( Case When TypeCode = '[AAL]' Then ISNULL(CurrentImpliedBalance,0) End ) As [AAL]
From [A1] As P
where EmployeeId = 3039
Group By EmployeeId
ORDER BY 1
 
Don't you need an ELSE in those case statements?

CASE WHEN code = 'a' THEN 'b' ELSE 'c' END
 
Why are the columns in the second query surrounded with quotes? This normally indicates a string but if I read your first query correctly those are columns and you are checking typecode against the value in the columns? Maybe I'm wrong but only ever use square brackets for columns with strange characters or match a sql keyword.
 
Why are the columns in the second query surrounded with quotes? This normally indicates a string but if I read your first query correctly those are columns and you are checking typecode against the value in the columns? Maybe I'm wrong but only ever use square brackets for columns with strange characters or match a sql keyword.

And I owe you a beer :)
Copy paste fail which is why it wasn't doing the sum properly. Thanks :)
 
Hi,

I have 2 SQL scripts. 1 works and the other does not.
Have to rewrite this as the server they are working on is SQL 2000 :sick:

Any help would be appreciated.

Working :
Code:
with cte_p as
(
	SELECT EmployeeId,[PAL],[AAL],
    FROM [dbo].[A1] 
    PIVOT( SUM(CurrentImpliedBalance)
          FOR TypeCode IN ([PAL],[AAL])) AS P
)
select 
		EmployeeId,SUM([PAL]) AS PAL,SUM([AAL]) 
from cte_p where EmployeeId = 3039 group by EmployeeId
order  by 1

Not working
Code:
Select EmployeeId
    ,SUM( Case When TypeCode = '[PAL]' Then ISNULL(CurrentImpliedBalance,0) End ) As [PAL]
    ,SUM( Case When TypeCode = '[AAL]' Then ISNULL(CurrentImpliedBalance,0) End ) As [AAL]
From [A1] As P
where EmployeeId = 3039
Group By EmployeeId
ORDER BY 1
WHAAAAAATTTTTT?????
Can't believe it. Worked 3 years ago at place using 2000. Its ancient.
 
Top
Sign up to the MyBroadband newsletter
X