473,624 Members | 2,637 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_I D = GROUPS.SITE_ID
WHERE
(GROUPS.GROUP_I D = 6)) AS "SEARCHES",
(SELECT
COUNT(*)
FROM
GROUPS
INNER JOIN SEARCHES
INNER JOIN ORDERS ON
SEARCHES.SEARCH _ID = ORDERS.SEARCH_I D ON
SEARCHES.SITE_I D = GROUPS.SITE_ID
WHERE
(GROUPS.GROUP_I D = 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 18105
HOw about:

SELECT
(SELECT
COUNT(*)
FROM
SEARCHES
INNER JOIN GROUPS ON
SEARCHES.SITE_I D = GROUPS.SITE_ID
WHERE
(GROUPS.GROUP_I D = 6)) AS "SEARCHES",
(SELECT
COUNT(*)
FROM
GROUPS
INNER JOIN SEARCHES
INNER JOIN ORDERS ON
SEARCHES.SEARCH _ID = ORDERS.SEARCH_I D ON
SEARCHES.SITE_I D = GROUPS.SITE_ID
WHERE
(GROUPS.GROUP_I D = 6)) AS "ORDERS"
FROM SYSIBM.SYSDUMMY 1

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

SELECT
(SELECT
COUNT(*)
FROM
SEARCHES
INNER JOIN GROUPS ON
SEARCHES.SITE_I D = GROUPS.SITE_ID
WHERE
(GROUPS.GROUP_I D = 6)) AS "SEARCHES",
(SELECT
COUNT(*)
FROM
GROUPS
INNER JOIN SEARCHES
INNER JOIN ORDERS ON
SEARCHES.SEARCH _ID = ORDERS.SEARCH_I D ON
SEARCHES.SITE_I D = GROUPS.SITE_ID
WHERE
(GROUPS.GROUP_I D = 6)) AS "ORDERS"
FROM SYSIBM.SYSDUMMY 1
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_I D = GROUPS.SITE_ID
WHERE
(GROUPS.GROUP_I D = 6)) AS "SEARCHES",
(SELECT
COUNT(*)
FROM
GROUPS
INNER JOIN SEARCHES
INNER JOIN ORDERS ON
SEARCHES.SEARCH _ID = ORDERS.SEARCH_I D ON
SEARCHES.SITE_I D = GROUPS.SITE_ID
WHERE
(GROUPS.GROUP_I D = 6)) AS "ORDERS"
FROM SYSIBM.SYSDUMMY 1
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_I D = 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_I D ON
SEARCHES.SITE_I D = 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_I D = 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_I D ON
SEARCHES.SITE_I D = 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_I D = 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_I D ON
SEARCHES.SITE_I D = 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.s earch_date), or even
INTEGER(date(se arches.search_d ate)).

-Chris

Jun 1 '07 #6

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

Similar topics

6
4286
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. Based on the number of variables passed through a GET string, I want to multiply the total number of selected items for each together to see how many possible combinations the selected items are generating. The following snippet of code...
7
6161
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 'patch' in an mdb file and use say ado or even a passthrough statement to execute it. The problem I've got Is I access (?) seems to require me to execute one ddl at a time, otherwise the script breaks at the 'go' statement. While I _could_ do...
5
12256
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 to the database. SQLSTATE=57030. Background: I created a linked server to DB2 8.1 database which called GRR_DB2Server. In my stored procedure p_FetchRawData, I need to read some data from this linked server GRR_DB2Server and insert them into
3
7719
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 Java. It is faster than the desktop application (written in VFP). 1. Can I get the same results using MS SQL Express Advanced and Access for the internet version if used with .net? 2. Are the select statements limited in power, accuracy, size, etc....
4
6927
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 needs to get a count of the ratings, then the sum of them to get the percentage. I am getting a error Exception Details: System.IndexOutOfRangeException: columncount
92
4689
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 have a form where you can add and delete multiple serial information. This works wonderful. But now for every serial information i add i need to be able to add and remove multiple parts to that serial. heres an example of what i mean serial...
58
8054
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 the parts section an i seem to be having trouble. When i try to insert into the parts section i get the error Invalid character value for cast specification. But not sure what i am doing wrong. Here is what i am using to insert. All the sections...
482
27556
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. what i am trying to display previously entered multiple fields. I am able to get my serial fields to display correctly, but i can not display my parts fields correctly. Currently this is what it does serial information 1 parts 1
9
2928
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 cf,count(result=absent) as ca; i know about three select count statements with three where clause
0
8251
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8688
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8352
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6115
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5570
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4085
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4188
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1800
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1496
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.