Solved

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

Posted on 2013-05-15
11
355 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 66

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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 

Author Comment

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

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
 

Author Comment

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

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 49

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 49

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 66

Expert Comment

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

Featured Post

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

696 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