?
Solved

SQL Question using COUNT and DISTINCT with multiple colulmns

Posted on 2004-09-15
12
Medium Priority
?
1,292 Views
Last Modified: 2008-01-09
This is hopefully an easy question, but...
I am writing a query where I need to count the distinct combinations of values in two columns

For example:

Select col1, col2 from MyTable returns

A, 1
A, 2
A, 2
A, 3
A, 4
B, 1
B, 2
B, 2
C, 1
C, 1

I need to find the number of different combinations like:

Select distinct col1, col2 from MyTable

which will return:

A, 1
A, 2
A, 3
A, 4
B, 1
B, 2
C, 1

My problem is how to incorporate COUNT() into the SQL statement.

For instance I can write:

Select count( distinct col1) from MyTable

however this will return 3, instead of 7

I found a reference online to use the concat operator CONCAT or ||, but I haven't beenable  to get it to
work with INFORMIX SQL

Example: [DOES NOT WORK UNDER INFORMIX]

Select count( distinct concat(col1, col2) ) from MyTable

SO, I am wondering if there is a better/another way to do this or a trick to make it work under INFORMIX SQL

Thanks
0
Comment
Question by:jjhalko
[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
  • 6
  • 3
  • 3
12 Comments
 
LVL 10

Expert Comment

by:Jay Toops
ID: 12068877
select col1, col2, count(*) as total
group by col1, col2

JAY
0
 

Author Comment

by:jjhalko
ID: 12069181
I thought about this.  Perhaps I am missing something but when I do this the query returns:

col1  col2  total
A, 1, 1
A, 2, 2
A, 3, 1
A, 4, 1
B, 1, 1
B, 2, 2
C, 1, 2

What I would like the query to return is

7




0
 
LVL 10

Assisted Solution

by:Jay Toops
Jay Toops earned 800 total points
ID: 12069698

ooooooooookkkkkkkkkkk...


select count(*) from ( select col1, col2 group by col1, col2 )

Jay
ps .. i hope informix supports this ...

0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 12

Accepted Solution

by:
pique_tech earned 400 total points
ID: 12071248
Or, an alternate to Jay:

SELECT COUNT(*) FROM (SELECT DISTINCT col1, col2 FROM table name)

I too don't know whether informix will support.
0
 

Author Comment

by:jjhalko
ID: 12071531
Thanks Jay, pique_tech

   I tried the examples but they don't work under informix

   I can get a solution to work under sql server so its good to know how this is supposed to work, but I guess now my main is that I need to get this to work under informix and I guess that's really been the heart of the problem.  I've been looking through the grammer of the Informix SQL and nothing pops to mind so far... I'll see if I can find some informix related sites online to see if this is really possible or not.  It kind of irks me that it's not supported, perhaps a quirk in the the version I'm using.

   Anyway thanks for your effort.  I'll keep plugging away.  I'll award partial credit atleast when I close the question.   I am also working on  a workaround which isn't great but due to the consistency of the data I should be able to split the query into two sql statements each with a single distinct clause and add together the count of each query.  

John.
0
 
LVL 10

Expert Comment

by:Jay Toops
ID: 12074266
Can you create a temp table then run the count on that?

Jay
0
 
LVL 10

Assisted Solution

by:Jay Toops
Jay Toops earned 800 total points
ID: 12074308
ANSWER: YES YOU CAN...


SELECT DISTINCT col1, col2 FROM tablename into temp MyTempTable
SELECT COUNT(*) FROM MyTempTable

Jay
0
 
LVL 12

Expert Comment

by:pique_tech
ID: 12074565
That'd get you around the nested query thing if that's what Informix was choking on.

Except, wouldn't it be (MS SQL, maybe Informix sytax has the elements in different order?)
SELECT DISTINCT col1, col2 INTO MyTempTable FROM tablename
SELECT COUNT(*) FROM MyTempTable
0
 
LVL 10

Expert Comment

by:Jay Toops
ID: 12074609
LOL... yes it would in microsoft land..
but we are in annother dimension....

JAY
0
 
LVL 12

Expert Comment

by:pique_tech
ID: 12075243
Thought maybe so.  I was feeling a little dizzy.

jjahlko--good luck.  I work in an environment where there aren't a lot of people to ask these simple-seeming questions to so I understand how frustrating they can be.
0
 
LVL 10

Expert Comment

by:Jay Toops
ID: 12075285
Yeah..
hey cool pique.. you'll be at the 10k mark soon ..

free questions.. nice isn't it ..

Jay

0
 

Author Comment

by:jjhalko
ID: 12076922
I am closing the question since I found a workaround.

I awarded points to the sql statements with the subqueries since these are valid and really the answer to the question I asked (minus the quirks with informix). Thanks to jltoops and pique_tech for your posts.

John.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Azure Functions is a solution for easily running small pieces of code, or "functions," in the cloud. This article shows how to create one of these functions to write directly to Azure Table Storage.
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

770 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