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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

counting nulls and blank fields together 3

Status
Not open for further replies.

FALCONSEYE

Programmer
Jul 30, 2004
1,158
US
i have a table as follows:

campaignName status occurence
yahoo null 5
yahoo 4
someOther 1:Vfd 6
...

the status on the second row is blank. how can i do count(1) whenever the status is null or blank for the same campaignName? for example,when i query the table for the above example i want to display

campaign status
yahoo 9 approved
someOther 6 1:Vfd

thanks for the help in advance



 
Code:
SELECT SUM(
           CASE Status
                WHEN IS NULL 1
                WHEN '' 1
                ELSE 0 END) AS mmm
Something like that (not tested)

Borislav Borissov
 
something along these lines...

SUM (CASE WHEN status is null OR status='' then occurence else 0 end) as mytotal

-DNG
 
Won't this work?
Code:
SELECT CampaignName AS [Compaign],
       COUNT(Occurance),
       Status
GROUP BY CampaignName, Status
FROM tablename

If that returns the values you want, then all you have to do is change it to do the concatenation of the count and status.

-SQLBill

Posting advice: FAQ481-4875
 
Borislav, DNG that's actually what i tried and it doesn't produce the result i wanted. SQLBill, your solution is similar too. Whenever the status is null or blank that means it's approved so i want to

campaignName status occurence
yahoo null 5
yahoo 4


9 approved yahoo.

this is what i have so far

select
sum(case when status is null OR status = '' then 1
else 0 end) as errorCount,

campaignName as campaignName,

(case when status is null OR status = '' then 'approved'
else status end)
as errorMessage


from transactionlog, campaign, orders
where orders.orderid = transactionlog.orderid
and campaign.campaignid = orders.campaignid
and orders.orderdate between '10/10/2005' and '10/12/2005'
group by campaignName, status
order by campaignName desc

thanks for the help...

 
I guess blank is semantically the same as NULL (not entered value, unknown) so...

Code:
select campaignName, isnull(status, 'approved') as status, sumocc
from
(	select campaignName, nullif(status, '') as status, sum(occurence) as sumocc
	from blah
	group by campaignName, nullif(status, '')
) somename
-- order by campaignName

------
"There's a man... He's bald and wears a short-sleeved shirt, and somehow he's very important to me. I think his name is Homer."
(Jack O'Neill, Stargate)
[banghead]
 
Code:
create table blah (campaignName varchar(20), status varchar(8), occurence int)

insert into blah values ('yahoo', null, 5)
insert into blah values ('yahoo', '', 4)
insert into blah values ('someOther', '1:Vfd', 6)

<query from above>

drop table blah
?

------
"There's a man... He's bald and wears a short-sleeved shirt, and somehow he's very important to me. I think his name is Homer."
(Jack O'Neill, Stargate)
[banghead]
 
Er... there is no need for temp table.

------
"There's a man... He's bald and wears a short-sleeved shirt, and somehow he's very important to me. I think his name is Homer."
(Jack O'Neill, Stargate)
[banghead]
 
if one solution stands unqiue and perfect out of many suggested solutions...you can undoubtedly say that solution was from vongrunt...

star for you...

-DNG
 
You should probably also find out why this field is being populated inconsistently. Either set a default value to avoid the nulls, or alter whatever procdure/app is inserting nulls or blanks to keep the data consistent. Then you wouldn't have to do these little dances for a simple select statement.
Assuming of course, you have access to make these kinds of changes.
 
I agree... long-term-wise it is better to make data consistent. Queries get simpler and you don't have to [banghead] about SQL patc... erm, hacks written some 6 months ago...

------
"There's a man... He's bald and wears a short-sleeved shirt, and somehow he's very important to me. I think his name is Homer."
(Jack O'Neill, Stargate)
[banghead]
 
Not sure why an inline view is needed here?
Code:
select    campaignname, 
          case when status is null or status = '' then 'approved' else status end, 
          sum(occurence)
from      #blah
group by  campaignname, 
          case when status is null or status = '' then 'approved' else status end

Regards,
AA
 
why doesn't anyone ever mention COALESCE?

instead of this --

... status is null OR status = ''

why doesn't anyone ever code this --

... coalesce(status,'') = ''

[ponder]

r937.com | rudy.ca
 
> Not sure why an inline view is needed here?

Of course it is not needed. I was trying to illustrate how NULLIF() works (hoping someone would run derived table code separately).

> why doesn't anyone ever mention COALESCE?

why doesn't anyone ever uses NULLIF? It is designed exactly to eliminate "fake NULLs". I guess difference between these two the is academic (e.g. nullif(status,'') is null)... and conventional boolean expressions (if this then that) are easier to understand.

------
"There's a man... He's bald and wears a short-sleeved shirt, and somehow he's very important to me. I think his name is Homer."
(Jack O'Neill, Stargate)
[banghead]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top