Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL QUERY - Count Results of a Column based on uniqueness of another Column

Posted on 2006-07-19
4
Medium Priority
?
470 Views
Last Modified: 2011-08-18
Hello,

I have a query situation that is not coming clear to me, I would appreciate an experts help on the following:

DB: MSSQL 2005

Scenario:

I have data that looks similar to this:

 Col1      Col2      Col3
ABCDE  12345    07/11/2006
FGHIJ    65431    07/11/2006
ABCDE  12345    07/11/2006
XYXSY  55321    07/11/2006
DFJGH  35332    07/11/2006
ABCDE  12345    07/12/2006
HFGGS  35530   07/12/2006
ABCDE  65432   07/12/2006
LJJSF   55351   07/12/2006

I am trying to build a query that will count, by day, the number of occurrences of COL1, but COL2 must be unique. So, in this example, for 07/11/2006 ABCDE would have a count of 1, because COL2 is a duplicate. In other words, it is ok for COL1 to duplicate, but not COL2. For 07/12/2006, ABCDE would have a count of 2 - two occurrences because COL2 is unique in each case. I have had this working when the requirement was only to count COL1 by day. When we added the described criteria, I haven't been able to get my arms around it. I've been working with sub queries, but can't seem to get the results I am looking for.

I hope this makes sense. Your assistance is appreciated.

Best,
mwheeler_fsd
0
Comment
Question by:mwheeler_fsd
[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
  • 2
4 Comments
 
LVL 26

Expert Comment

by:DireOrbAnt
ID: 17140205
SELECT Col1, Col3, count(*) AS 'Count'
FROM MyTable
GROUP BY Col1, Col3
0
 
LVL 7

Accepted Solution

by:
FDzjuba earned 2000 total points
ID: 17140252
SELECT col1,,col3,COUNT(DISTINCT col1+col2)
      FROM Table
GROUP BY col1,col3
0
 

Author Comment

by:mwheeler_fsd
ID: 17140311
Thanks to all for their input.

The COL1+COL2 got the uniqueness required. I did not realize you could compound. Many thanks - I also learned a new technique!

Points awarded and gratitute to all.

Best,
mwheeler_fsd
0
 

Author Comment

by:mwheeler_fsd
ID: 17140318
"gratitude" :)
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

661 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