Some SQL help please

Pho3nix

The Legend
Joined
Jul 31, 2009
Messages
30,589
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
 

Hamster

Resident Rodent
Joined
Aug 22, 2006
Messages
42,928
Don't you need an ELSE in those case statements?

CASE WHEN code = 'a' THEN 'b' ELSE 'c' END
 

Kosmik

Honorary Master
Joined
Sep 21, 2007
Messages
25,665
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.
 

Pho3nix

The Legend
Joined
Jul 31, 2009
Messages
30,589
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 :)
 

freddster

Expert Member
Joined
Dec 13, 2013
Messages
2,470
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