Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Nullif WordCount 2

Status
Not open for further replies.

michaela18

Technical User
Sep 9, 2009
37
US
I have the following:


Select (1+len(NULLIF(LTRIM(CodeList),''))-len(replace(NULLIF(LTRIM(CodeList),''),' ',''))) * count (*) as 'IBM_ACS'
from history
where order in ('In process', 'Order')
and date = '20100210'
and PromoCode IN ('107', 'VW109', '118')
group by codelist


This will count the number of words in a record then multiply that by the count. Some records in the Codelist field has blanks, but my select still counts them. How can I count everything without the group by as 1 sum, and without the blanks/nulls.
 
If I understand correctly.... try this:

Code:
Select  (1+len(NULLIF(LTRIM(CodeList),''))-len(replace(NULLIF(LTRIM(CodeList),''),' ',''))) *  count (*) as 'IBM_ACS'
from history
where order in ('In process', 'Order')
and date = '20100210'
and PromoCode IN ('107', 'VW109', '118')
[!]and codelist > ''[/!]
group by codelist

-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
This is what I want but without the group by

It is now only showing those records that are populated but showing as:


APPLE 1
PC 5

I just want it to show 6
 
if i remove group by i get this "is invalid in the select list because it is not contained in an aggregate function and there is no GROUP BY clause."

and if I put in Sum at the beginning of my select statement and remove the group by, it would work but it won't retrieve the right information.
 
try this:

Code:
Select  [!]Sum([/!](1+len(NULLIF(LTRIM(CodeList),''))-len(replace(NULLIF(LTRIM(CodeList),''),' ','')))[!])[/!] as 'IBM_ACS'
from history
where order in ('In process', 'Order')
and date = '20100210'
and PromoCode IN ('107', 'VW109', '118')


Notice that the COUNT is no longer necessary.

-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top