473,386 Members | 1,924 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Advanced SQL - Multiple Count statements in one select

Here is what I have:

SELECT
(SELECT
COUNT(*) AS SEARCHES
FROM
SEARCHES
INNER JOIN GROUPS ON
SEARCHES.SITE_ID = GROUPS.SITE_ID
WHERE
(GROUPS.GROUP_ID = 6)) AS "SEARCHES",
(SELECT
COUNT(*)
FROM
GROUPS
INNER JOIN SEARCHES
INNER JOIN ORDERS ON
SEARCHES.SEARCH_ID = ORDERS.SEARCH_ID ON
SEARCHES.SITE_ID = GROUPS.SITE_ID
WHERE
(GROUPS.GROUP_ID = 6)) AS "ORDERS"
FROM GROUPS, SEARCHES, ORDERS

each inner select statement returns results when executed on their own
however when combined into a single select, I get nothing!

Also, how do I break the results down by months?

May 30 '07 #1
5 18092
HOw about:

SELECT
(SELECT
COUNT(*)
FROM
SEARCHES
INNER JOIN GROUPS ON
SEARCHES.SITE_ID = GROUPS.SITE_ID
WHERE
(GROUPS.GROUP_ID = 6)) AS "SEARCHES",
(SELECT
COUNT(*)
FROM
GROUPS
INNER JOIN SEARCHES
INNER JOIN ORDERS ON
SEARCHES.SEARCH_ID = ORDERS.SEARCH_ID ON
SEARCHES.SITE_ID = GROUPS.SITE_ID
WHERE
(GROUPS.GROUP_ID = 6)) AS "ORDERS"
FROM SYSIBM.SYSDUMMY1

May 30 '07 #2
On May 30, 10:00 am, Otto Carl Marte <Otto.Ma...@gmail.comwrote:
HOw about:

SELECT
(SELECT
COUNT(*)
FROM
SEARCHES
INNER JOIN GROUPS ON
SEARCHES.SITE_ID = GROUPS.SITE_ID
WHERE
(GROUPS.GROUP_ID = 6)) AS "SEARCHES",
(SELECT
COUNT(*)
FROM
GROUPS
INNER JOIN SEARCHES
INNER JOIN ORDERS ON
SEARCHES.SEARCH_ID = ORDERS.SEARCH_ID ON
SEARCHES.SITE_ID = GROUPS.SITE_ID
WHERE
(GROUPS.GROUP_ID = 6)) AS "ORDERS"
FROM SYSIBM.SYSDUMMY1
For month break down, try:
SELECT s.MONTH, s.SEARCHES, o.ORDERS from
(SELECT
COUNT(*) as SEARCHES
FROM
SEARCHES
INNER JOIN GROUPS ON
SEARCHES.SITE_ID = GROUPS.SITE_ID
WHERE
(GROUPS.GROUP_ID = 6)) AS "SEARCHES",
(SELECT
COUNT(*)
FROM
GROUPS
INNER JOIN SEARCHES
INNER JOIN ORDERS ON
SEARCHES.SEARCH_ID = ORDERS.SEARCH_ID ON
SEARCHES.SITE_ID = GROUPS.SITE_ID
WHERE
(GROUPS.GROUP_ID = 6)) AS "ORDERS"
FROM SYSIBM.SYSDUMMY1
MONTH(Searches.Search_Date

May 30 '07 #3
Sorry, hit the wrong button last time.

Try:
SELECT s.MONTH, s.SEARCHES, o.ORDERS
from (SELECT searches.search_date // 100 as MONTH,
COUNT(*) as SEARCHES
FROM
SEARCHES
INNER JOIN GROUPS ON
SEARCHES.SITE_ID = GROUPS.SITE_ID
WHERE
GROUPS.GROUP_ID = 6
GROUP BY searches.search_Date // 100) s
full outer join (SELECT searches.search_date // 100 as MONTH,
COUNT(*) as ORDERS
FROM
GROUPS
INNER JOIN SEARCHES
INNER JOIN ORDERS ON
SEARCHES.SEARCH_ID = ORDERS.SEARCH_ID ON
SEARCHES.SITE_ID = GROUPS.SITE_ID
WHERE
GROUPS.GROUP_ID = 6
GROUP BY searches.search_Date // 100) o
ON s.MONTH = o.MONTH

This should get to the amounts broken down by month (well, year and
month). If you truely want them by month (and January from this year
and last year summed up together), then you can substitute
MONTH(searches.search_date)
for
searches.search_date // 100

-Chris

May 30 '07 #4
On May 31, 9:20 am, ChrisC <cunningham...@gmail.comwrote:
Sorry, hit the wrong button last time.

Try:
SELECT s.MONTH, s.SEARCHES, o.ORDERS
from (SELECT searches.search_date // 100 as MONTH,
COUNT(*) as SEARCHES
FROM
SEARCHES
INNER JOIN GROUPS ON
SEARCHES.SITE_ID = GROUPS.SITE_ID
WHERE
GROUPS.GROUP_ID = 6
GROUP BY searches.search_Date // 100) s
full outer join (SELECT searches.search_date // 100 as MONTH,
COUNT(*) as ORDERS
FROM
GROUPS
INNER JOIN SEARCHES
INNER JOIN ORDERS ON
SEARCHES.SEARCH_ID = ORDERS.SEARCH_ID ON
SEARCHES.SITE_ID = GROUPS.SITE_ID
WHERE
GROUPS.GROUP_ID = 6
GROUP BY searches.search_Date // 100) o
ON s.MONTH = o.MONTH

This should get to the amounts broken down by month (well, year and
month). If you truely want them by month (and January from this year
and last year summed up together), then you can substitute
MONTH(searches.search_date)
for
searches.search_date // 100

-Chris
Thanks Chris, that is exactly what I was looking for however my dates
are stored as TIMESTAMP and when I attempt to perform the arithmetic
conversion I get an error stating that one of the operands is not
numeric. I'm assuming that the TIMESTAMP can't be converted to a
number? I'm searching for a solution but can't really find
anything...

Do you have any other suggestions?

May 31 '07 #5
On May 30, 6:50 pm, whitsey <lysterfiel...@gmail.comwrote:
On May 31, 9:20 am, ChrisC <cunningham...@gmail.comwrote:


Sorry, hit the wrong button last time.
Try:
SELECT s.MONTH, s.SEARCHES, o.ORDERS
from (SELECT searches.search_date // 100 as MONTH,
COUNT(*) as SEARCHES
FROM
SEARCHES
INNER JOIN GROUPS ON
SEARCHES.SITE_ID = GROUPS.SITE_ID
WHERE
GROUPS.GROUP_ID = 6
GROUP BY searches.search_Date // 100) s
full outer join (SELECT searches.search_date // 100 as MONTH,
COUNT(*) as ORDERS
FROM
GROUPS
INNER JOIN SEARCHES
INNER JOIN ORDERS ON
SEARCHES.SEARCH_ID = ORDERS.SEARCH_ID ON
SEARCHES.SITE_ID = GROUPS.SITE_ID
WHERE
GROUPS.GROUP_ID = 6
GROUP BY searches.search_Date // 100) o
ON s.MONTH = o.MONTH
This should get to the amounts broken down by month (well, year and
month). If you truely want them by month (and January from this year
and last year summed up together), then you can substitute
MONTH(searches.search_date)
for
searches.search_date // 100
-Chris

Thanks Chris, that is exactly what I was looking for however my dates
are stored as TIMESTAMP and when I attempt to perform the arithmetic
conversion I get an error stating that one of the operands is not
numeric. I'm assuming that the TIMESTAMP can't be converted to a
number? I'm searching for a solution but can't really find
anything...

Do you have any other suggestions?- Hide quoted text -

- Show quoted text -
To convert the timestamp into a date (sothtat you can turn it into an
integer), you need to wrap it with a DATE() function. In other words,
replace searches.search_date with date(searches.search_date), or even
INTEGER(date(searches.search_date)).

-Chris

Jun 1 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Ben Hallert | last post by:
Hi guys, I'm trying to figure out what bone headed mistake I made on something I put together. I've got a form (named 'context') that has a variable number of select-multiple inputs on it. ...
7
by: Glenn Davy | last post by:
Hidely hodley everyone I'd like to run a series of of sql ddl statements against an msde2000 server. Normally I just deploy cmd file that impliments as osql statement, but I'd like to store the...
5
by: alingsjtu | last post by:
Hello, every body. When execute dynamic generated multiple OPENQUERY statements (which linkes to DB2) in SQLServer, I always got SQL1040N The maximum number of applications is already connected...
3
by: | last post by:
I'm planning to transport a desktop application to the web. A spin-off of this application has already been put on the web by another programmer. He used ColdFusion with MS SQL, Access, VC, and...
4
by: vertigo262 | last post by:
Is it possible to use to select statements in a stored procedure? I am building a movie rating system, what I am doing is creating a table with movies and individual user ratings. The code...
92
by: bonneylake | last post by:
Hey Everyone, Well i was hoping someone could explain the best way i could go about this. i have a few ideas on how i could go about this but i am just not sure if it would work. Right now i...
58
by: bonneylake | last post by:
Hey Everyone, Well recently i been inserting multiple fields for a section in my form called "serial". Well now i am trying to insert multiple fields for the not only the serial section but also...
482
by: bonneylake | last post by:
Hey Everyone, Well i am not sure if this is more of a coldfusion problem or a javscript problem. So if i asked my question in the wrong section let me know an all move it to the correct place. ...
9
by: kkshansid | last post by:
i hav a table with a column named result where values are either of the three pass ,fail,absent can i get multiple count in same row like select count(result=pass) as cp,count(result=fail) as...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.