Solved

Getting a COUNT of records in a result set without using a GROUP BY clause (SQL query help)

Posted on 2014-10-06
3
315 Views
Last Modified: 2014-10-06
I have the following query:

SELECT DISTINCT s.SUPPLIER_CODE AS SupplierCode, l.SUPPLIER_NAME AS SupplierName from MEDMGR.CMTRS_TB_VALIDATED_SUPPLIERS s, MEDMGR.CMTRS_TO_BE_VALIDATED v, MEDMGR.CMTR_VALIDATION_SUPPLIER_LIST l where s.CMTR_ELID=v.CMTR_ELID and RECORD_DATE > 1412476555  and l.SUPPLIER_CODE= s.SUPPLIER_CODE and l.QUALITY_INDEX != -1

Open in new window


(The date above is just a UNIX timestamp from yesterday)
It appears that I will not be able to use a GROUP BY clause.
I need to add a COUNT of the distinct SUPPLIER_CODE and the number of times it appears in the result set without using a GROUP BY in the statement.



Results should look like this...
SUPPLIERCODE           SUPPLIER_NAME       SUPPLIERCODECN
SupplierA                       WidgetCo101                        2
SupplierB                       Mikes Toy Co.                       1
SupplierC                       Goober Enterprises             3
0
Comment
Question by:Mike Miller
[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
3 Comments
 
LVL 77

Accepted Solution

by:
slightwv (䄆 Netminder) earned 250 total points
ID: 40364666
Why can't you use a group by?

To get a count based on a value, group by is the standard.

You might be able to muck up perfectly good SQL and make the select a lot more expensive to run but why would you?
0
 
LVL 74

Assisted Solution

by:sdstuber
sdstuber earned 250 total points
ID: 40364725
similar to your previous question, simply extend the group by to include the name


 
SELECT s.supplier_code AS suppliercode, l.supplier_name AS suppliername, COUNT(*) AS suppliercodecn
    FROM medmgr.cmtrs_tb_validated_suppliers s,
         medmgr.cmtrs_to_be_validated v,
         medmgr.cmtr_validation_supplier_list l
   WHERE s.cmtr_elid = v.cmtr_elid
     AND record_date > 1412476555
     AND l.supplier_code = s.supplier_code
     AND l.quality_index != -1
GROUP BY s.supplier_code, l.supplier_name

Open in new window


or, if the name might change for a given supplier code but you only want to show one, then you'll have to pick one.

Use could use an aggregate for that as well, for example MIN


 
 SELECT s.supplier_code AS suppliercode,
         MIN(l.supplier_name) AS suppliername,
         COUNT(*) AS suppliercodecn
    FROM medmgr.cmtrs_tb_validated_suppliers s,
         medmgr.cmtrs_to_be_validated v,
         medmgr.cmtr_validation_supplier_list l
   WHERE s.cmtr_elid = v.cmtr_elid
     AND record_date > 1412476555
     AND l.supplier_code = s.supplier_code
     AND l.quality_index != -1
GROUP BY s.supplier_code

Open in new window

0
 

Author Closing Comment

by:Mike Miller
ID: 40364746
You're right. I was thinking I couldn't use that clause, but it turns out I can. Thanks for bearing with me.
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
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…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how the fundamental information of how to create a table.

626 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