?
Solved

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

Posted on 2013-05-15
11
Medium Priority
?
363 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 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 2000 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

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
MSSQL DB-maintenance also needs implementation of multiple activities. However, unprecedented errors can hamper the database management. In that case, deploying Stellar SQL Database Toolkit ensures fast and accurate database and backup repair as wel…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Suggested Courses

864 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