rhservan
asked on
How can I return a count of one when there are more than one in the count?
Select Order#, Date
Order# Date
3456 01/15/2013
3456 01/18/2013
3456 01/30/2013
3457 01/25/2013
3458 02/15/2013
Select Order#, Count(date)
group by Order#
Order# Date
3456 3
3457 1
3458 1
Anytime there is more than one date, as in 3456 above, I need to only return a count of 1.
How can I do this?
Order# Date
3456 01/15/2013
3456 01/18/2013
3456 01/30/2013
3457 01/25/2013
3458 02/15/2013
Select Order#, Count(date)
group by Order#
Order# Date
3456 3
3457 1
3458 1
Anytime there is more than one date, as in 3456 above, I need to only return a count of 1.
How can I do this?
Select Order#, 1 as Date group by Order#
Select Order#, 1 as date
group by Order#
group by Order#
<potentially stupid question>
>Anytime there is more than one date, as in 3456 above, I need to only return a count of 1.
Then what's the purpose of having a count?
>Anytime there is more than one date, as in 3456 above, I need to only return a count of 1.
Then what's the purpose of having a count?
ASKER
Simply when the query returns more than 1 date then I need the other dates ignored.
Then it looks like hard-coding 1 would be the correct answer, as the first two experts posted.
Or, you don't even need the 1..
Or, you don't even need the 1..
SELECT DISTINCT Order# FROM YourTable
ASKER
But I still need the date value returned.
>But I still need the date value returned.
Okay, but I think you need to eyeball the desired return recordset in your question, and make sure it's correct, as I don't see any dates in it.
For example, for 3456, what would the date value be you want returned: 01/15/2013 (min), 01/18/2013, or 01/30/2013 (max)?
Okay, but I think you need to eyeball the desired return recordset in your question, and make sure it's correct, as I don't see any dates in it.
For example, for 3456, what would the date value be you want returned: 01/15/2013 (min), 01/18/2013, or 01/30/2013 (max)?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>>what's the purpose of having a count?
to invent new ways of getting there?
select
order#
, (count(*)+1) - count(*) as forced_to_one_the_hard_way _option_1
, case when count(*) > 0 then count(*) / count(*)
else (count(*)+1) / (count(*) +1)
end as forced_to_one_the_hard_way _option_2
, 1 as forced_to_one_the_fixed_wa y
from yourtable
group by order#
-- sorry, but a count is a count, apologies for the flippancy in the above
to invent new ways of getting there?
select
order#
, (count(*)+1) - count(*) as forced_to_one_the_hard_way
, case when count(*) > 0 then count(*) / count(*)
else (count(*)+1) / (count(*) +1)
end as forced_to_one_the_hard_way
, 1 as forced_to_one_the_fixed_wa
from yourtable
group by order#
-- sorry, but a count is a count, apologies for the flippancy in the above
ok, flippancy aside now. In many questions what emerges eventually is that "the most recent" record is required (i.e. the full record matching the most recent date), so maybe this will help?
ORDERID ROW_REF
3457 1
3456 1
3458 1
see it at: http://sqlfiddle.com/#!3/2adec/2
& :) with flippancies at work: http://sqlfiddle.com/#!3/2adec/3 (couldn't help it)
select
OrderID
, row_ref
from (
select
*
, row_number() over (partition by OrderID order by OrdDate DESC) as row_ref
from orders
) as o
where row_ref = 1
order by
OrdDate
it produces:ORDERID ROW_REF
3457 1
3456 1
3458 1
see it at: http://sqlfiddle.com/#!3/2adec/2
& :) with flippancies at work: http://sqlfiddle.com/#!3/2adec/3 (couldn't help it)
My compliments on an excellent flippancy.