Computing.Net > Forums > Programming > Informix SQL - Count within a Count

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Informix SQL - Count within a Count

Reply to Message Icon

Name: skisalomon77
Date: May 14, 2008 at 17:42:20 Pacific
OS: XP Pro
CPU/Ram: Intel/2GB
Product: Dell
Comment:

I'm basically trying to do a count within a count, or a subquery. The below query will show me the transaction numbers that appear one or more times between a set of dates for each store. I'm using Informix SQL.

SELECT store,
COUNT(transaction_number) AS NumOccurrences
FROM sales
WHERE date between '06-20-2007' AND '07/31/2007'
GROUP BY store
HAVING ( COUNT(transaction_number) >= 1 )

The results will look like this

store   numoccurrances
1          3
1          1
1          2
5          1
5          1
5          2

What I want to do next, and am having trouble with, is to subquery how many times each store appears after the above query is complete. It will look like this because stores 1 and 5 each appeared 3 times from the first query.

store   numoccurrances
1          3
5          3



Sponsored Link
Ads by Google

Response Number 1
Name: Razor2.3
Date: May 14, 2008 at 21:13:48 Pacific
Reply:

Without having used Informix SQL before, I'd assume it'd go something like this:

SELECT store, COUNT(NumOccurrences)
FROM (SELECT store, COUNT(transaction_number) AS NumOccurrences
FROM sales
WHERE date between '06-20-2007' AND '07/31/2007'
GROUP BY store
HAVING ( COUNT(transaction_number) >= 1 )
)
GROUP BY store


0

Response Number 2
Name: skisalomon77
Date: May 15, 2008 at 11:19:54 Pacific
Reply:

Hey Razor2.3,

Thanks for the reply!

I understand what your doing. I modified my query a little. The inner query beginning with the second SELECT function, works fine by itself.

SELECT store,
COUNT(store) AS Total
FROM (SELECT store, transaction_number,
      COUNT(transaction_number) AS NumOccurrences
      FROM sales
      WHERE date between '06-20-2007' AND '07/31/2007'
      GROUP BY store, transaction_number
      HAVING ( COUNT(transaction_number) >= 1 )
     )
GROUP BY store

When I make it a subquery, it doesn't work at all. I receive the following error:

"37000(-201)[Informix][Informix ODBC Driver][Informix]A syntax error has occurred"

The -201 code means that there is missing or extra punctuation, or that something is mispelled, but I'm not seeing it and am still plugging away at it.


0

Response Number 3
Name: skisalomon77
Date: May 15, 2008 at 15:41:02 Pacific
Reply:

I think I found the problem. The syntax I'm using is for Informix Dynamic Server (IDS) version 10. I believe I'm using verion 9.3. I'll have to locate the SQL syntax for IDS 9.3.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Informix SQL - Count within a Count

Launch a new script within a .cmd f www.computing.net/answers/programming/launch-a-new-script-within-a-cmd-f/11558.html

Piping text within a batch file www.computing.net/answers/programming/piping-text-within-a-batch-file/11068.html

HELP: C macro #if within a #define www.computing.net/answers/programming/help-c-macro-if-within-a-define/6716.html