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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…
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.

751 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