Solved

SQL Question using COUNT and DISTINCT with multiple colulmns

Posted on 2004-09-15
12
1,290 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 200 total points
ID: 12069698

ooooooooookkkkkkkkkkk...


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

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

0
Webinar: Security & Encryption in the MySQL world

Join Percona’s Solutions Engineer, Dimitri Vanoverbeke as he presents “Security and Encryption in the MySQL world” on Thursday, July 6, 2017 at 7:00 am PDT / 10:00 am EDT (UTC-7).

 
LVL 12

Accepted Solution

by:
pique_tech earned 100 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 200 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

Webinar: MariaDB® Server 10.2: The Complete Guide

Join Percona’s Chief Evangelist, Colin Charles as he presents MariaDB Server 10.2: The Complete Guide on Tuesday, June 27, 2017 at 7:00 am PDT / 10:00 am EDT (UTC-7).

Question has a verified solution.

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

This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

705 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