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
293 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:mwmiller78
3 Comments
 
LVL 76

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 73

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:mwmiller78
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.
Viewers will learn how the fundamental information of how to create a table.

759 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

24 Experts available now in Live!

Get 1:1 Help Now