SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Filter suppliers that dindn't sell


Filter suppliers that dindn't sell

Author
Message
pnomadewolf
pnomadewolf
SSC Rookie
SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)

Group: General Forum Members
Points: 32 Visits: 29
I have the following query:
SELECT
fc.no, fc.nome,
'FANO1' = SUM(Case When year(datalc)=2010 Then fc.ecred Else 0 End),
'Dif' = SUM(Case When year(datalc)=2010 Then fc.ecred Else 0 End)-SUM(Case When year(datalc)=2009 Then fc.ecred Else 0 End)
FROM
fc(nolock) left join fo on fc.fostamp=fo.fostamp
WHERE
fc.fano1 <> 0
Group By
fc.no, fc.nome
Order By
fc.nome


This query returns an error that fano1 isn't found. It works flawlessly without the WHERE clause. But i need it to ensure that i don't get lines with suppliers that didn't sell...
Is there any other way i can rule out the rows where fano1 is equal to 0?

Other info:

fc.no => int
fc.nome => varchar
fc.datalc => datetime
fc.ecred => float


pnomadewolf
pnomadewolf
SSC Rookie
SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)

Group: General Forum Members
Points: 32 Visits: 29
Solved like this:
SELECT
fc.no, fc.nome,
'FANO1' = SUM(Case When year(datalc)=2010 Then fc.ecred Else 0 End),
'Dif' = SUM(Case When year(datalc)=2010 Then fc.ecred Else 0 End)-SUM(Case When year(datalc)=2009 Then fc.ecred Else 0 End)
FROM
fc(nolock) left join fo on fc.fostamp=fo.fostamp
Group By
fc.no, fc.nome
HAVING
SUM(Case When year(datalc)=2010 Then fc.ecred Else 0 End) <> 0
Order By
fc.nome


Dan.Humphries
Dan.Humphries
SSCrazy
SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)

Group: General Forum Members
Points: 2437 Visits: 1212
try this. The problem comes from the fact that fan01 does not physically exist it is a named output of the query.

select t.* from 
(
SELECT
fc.no, fc.nome,
'FANO1' = SUM(Case When year(datalc)=2010 Then fc.ecred Else 0 End),
'Dif' = SUM(Case When year(datalc)=2010 Then fc.ecred Else 0 End)-SUM(Case When year(datalc)=2009 Then fc.ecred Else 0 End)
FROM fc(nolock)
left join fo on fc.fostamp=fo.fostamp
) t
WHERE
t.fano1 <> 0
Group By
t.no, t.nome
Order By
t.nome



Dan

If only I could snap my figures and have all the correct indexes apear and the buffer clean and.... Start day dream here.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search