Solved

SQL Question using COUNT and DISTINCT with multiple colulmns

Posted on 2004-09-15
12
1,283 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

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!

Question has a verified solution.

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

Suggested Solutions

I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
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…

930 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

11 Experts available now in Live!

Get 1:1 Help Now