Solved

SQL Question using COUNT and DISTINCT with multiple colulmns

Posted on 2004-09-15
12
1,282 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
  • 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Database tuning – How to start and what to tune. This question is frequently asked by many people, both online and offline. There is no hard and fast rule-of-thumb for performance tuning, however, before beginning the tuning process one should a…
I guess that all of us know that caching the data usually increase the performance, but I worried if all of us are aware about the risk that caching the data provides and how to minimize this.  That’s the reason why I decided to write this short art…
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…
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…

708 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

13 Experts available now in Live!

Get 1:1 Help Now