Solved

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

Posted on 2013-05-15
11
341 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

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
 

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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Suggested Solutions

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

828 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