Solved

How can I return a count of one when there are more than one in the count?

Posted on 2013-05-15
11
325 Views
Last Modified: 2013-05-16
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?
0
Comment
Question by:rhservan
  • 4
  • 2
  • 2
  • +2
11 Comments
 
LVL 3

Expert Comment

by:pjevin
ID: 39169506
Select Order#, 1 as Date group by Order#
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 39169507
Select Order#, 1 as date
group by Order#
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 39169517
<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?
0
 

Author Comment

by:rhservan
ID: 39169638
Simply when the query returns more than 1 date then I need the other dates ignored.
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 39169647
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..

SELECT DISTINCT  Order#  FROM YourTable

Open in new window

0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:rhservan
ID: 39169650
But I still need the date value returned.
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 39169663
>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)?
0
 
LVL 3

Accepted Solution

by:
pjevin earned 500 total points
ID: 39169667
As in an actual date or your value of 1?

Select Order#, 1 as Date group by Order#  gives you your

Order#                    Date
         3456                        1        
         3457                        1
         3458                        1

Select Order#, Max(Date) as Date group by Order#  gives you one date value per order number (the max date value in this case).

Order#                    Date
         3456                        01/30/2013        
         3457                        01/25/2013
         3458                        02/15/2013
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39170120
>>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_way
from yourtable
group by order#

-- sorry, but a count is a count, apologies for the flippancy in the above
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39170240
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?
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

Open in new window

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)
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 39171048
My compliments on an excellent flippancy.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now