Solved

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

Posted on 2013-05-15
11
311 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
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

708 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

13 Experts available now in Live!

Get 1:1 Help Now