• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1298
  • Last Modified:

SQL Question using COUNT and DISTINCT with multiple colulmns

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
jjhalko
Asked:
jjhalko
  • 6
  • 3
  • 3
3 Solutions
 
Jay ToopsCommented:
select col1, col2, count(*) as total
group by col1, col2

JAY
0
 
jjhalkoAuthor Commented:
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
 
Jay ToopsCommented:

ooooooooookkkkkkkkkkk...


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

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

0
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.

 
pique_techCommented:
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
 
jjhalkoAuthor Commented:
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
 
Jay ToopsCommented:
Can you create a temp table then run the count on that?

Jay
0
 
Jay ToopsCommented:
ANSWER: YES YOU CAN...


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

Jay
0
 
pique_techCommented:
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
 
Jay ToopsCommented:
LOL... yes it would in microsoft land..
but we are in annother dimension....

JAY
0
 
pique_techCommented:
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
 
Jay ToopsCommented:
Yeah..
hey cool pique.. you'll be at the 10k mark soon ..

free questions.. nice isn't it ..

Jay

0
 
jjhalkoAuthor Commented:
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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

  • 6
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now