473,513 Members | 2,668 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Advanced SQL - Extracting values by months

Help!

I am trying to write an SQL statement (without much luck) to extract
the total number of searches and orders from a particular customer,
grouped into days/months/weeks (depending on the report type.
The tables are as follows:
Searches
--------------
search_id (PKEY)
customer_id (FKEY)
product_id
cost
date
.....
Orders
-----------
order_id (PKEY)
search_id (FKEY)
order_value
date
....
Groups
-----------
group_id (PKEY)
customer_id
....
Customers
---------------
customer_id (PKEY)
.....
What I want to achieve from this is
For all customers with Group_id = '17'
Mth 1 1244(Searches) 846(Orders)
Mth 2 2345(Searches) 1834(Orders)
Mth 3 2342(Searches) 1954(Orders)
Mth 4 2425(Searches) 2134(Orders)
Mth 5 5235(Searches) 4253(Orders)
I am clearly nowhere near the level I need to be to achieve this.
Can it be achieved in a single statement?
Thanks

May 28 '07 #1
7 1967
whitsey wrote:
Help!

I am trying to write an SQL statement (without much luck) to extract
the total number of searches and orders from a particular customer,
grouped into days/months/weeks (depending on the report type.
The tables are as follows:
Searches
--------------
search_id (PKEY)
customer_id (FKEY)
product_id
cost
date
....
Orders
-----------
order_id (PKEY)
search_id (FKEY)
order_value
date
...
Groups
-----------
group_id (PKEY)
customer_id
...
Customers
---------------
customer_id (PKEY)
....
What I want to achieve from this is
For all customers with Group_id = '17'
Mth 1 1244(Searches) 846(Orders)
Mth 2 2345(Searches) 1834(Orders)
Mth 3 2342(Searches) 1954(Orders)
Mth 4 2425(Searches) 2134(Orders)
Mth 5 5235(Searches) 4253(Orders)
I am clearly nowhere near the level I need to be to achieve this.
Can it be achieved in a single statement?
Yes, a join combined with GROUP BY. Use MONTH(date) to coarsify to the
month.
Why don't you post your best shot and we go from there?

Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
May 28 '07 #2
On May 28, 9:34 pm, Serge Rielau <srie...@ca.ibm.comwrote:
whitsey wrote:
Help!
I am trying to write an SQL statement (without much luck) to extract
the total number of searches and orders from a particular customer,
grouped into days/months/weeks (depending on the report type.
The tables are as follows:
Searches
--------------
search_id (PKEY)
customer_id (FKEY)
product_id
cost
date
....
Orders
-----------
order_id (PKEY)
search_id (FKEY)
order_value
date
...
Groups
-----------
group_id (PKEY)
customer_id
...
Customers
---------------
customer_id (PKEY)
....
What I want to achieve from this is
For all customers with Group_id = '17'
Mth 1 1244(Searches) 846(Orders)
Mth 2 2345(Searches) 1834(Orders)
Mth 3 2342(Searches) 1954(Orders)
Mth 4 2425(Searches) 2134(Orders)
Mth 5 5235(Searches) 4253(Orders)
I am clearly nowhere near the level I need to be to achieve this.
Can it be achieved in a single statement?

Yes, a join combined with GROUP BY. Use MONTH(date) to coarsify to the
month.
Why don't you post your best shot and we go from there?

Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab- Hide quoted text -

- Show quoted text -
This is where I've started however this is returning no rows... Also,
I am unable to extract values by months...

SELECT
COUNT(SEARCHES.SEARCH_ID),
COUNT(SEARCHES.SEARCH_ID)
FROM
SEARCHES
INNER JOIN ORDERS
ON SEARCHES.SEARCH_ID = ORDERS.SEARCH_ID
INNER JOIN GROUPS
ON GROUPS.CUSTOMER_ID = SEARCHES.CUSTOMER_ID
WHERE
(GROUPS.GROUP_ID = 17)

May 30 '07 #3
On May 30, 10:31 am, whitsey <lysterfiel...@gmail.comwrote:
On May 28, 9:34 pm, Serge Rielau <srie...@ca.ibm.comwrote:


whitsey wrote:
Help!
I am trying to write an SQL statement (without much luck) to extract
the total number of searches and orders from a particular customer,
grouped into days/months/weeks (depending on the report type.
The tables are as follows:
Searches
--------------
search_id (PKEY)
customer_id (FKEY)
product_id
cost
date
....
Orders
-----------
order_id (PKEY)
search_id (FKEY)
order_value
date
...
Groups
-----------
group_id (PKEY)
customer_id
...
Customers
---------------
customer_id (PKEY)
....
What I want to achieve from this is
For all customers with Group_id = '17'
Mth 1 1244(Searches) 846(Orders)
Mth 2 2345(Searches) 1834(Orders)
Mth 3 2342(Searches) 1954(Orders)
Mth 4 2425(Searches) 2134(Orders)
Mth 5 5235(Searches) 4253(Orders)
I am clearly nowhere near the level I need to be to achieve this.
Can it be achieved in a single statement?
Yes, a join combined with GROUP BY. Use MONTH(date) to coarsify to the
month.
Why don't you post your best shot and we go from there?
Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab- Hide quoted text -
- Show quoted text -

This is where I've started however this is returning no rows... Also,
I am unable to extract values by months...

SELECT
COUNT(SEARCHES.SEARCH_ID),
COUNT(SEARCHES.SEARCH_ID)
FROM
SEARCHES
INNER JOIN ORDERS
ON SEARCHES.SEARCH_ID = ORDERS.SEARCH_ID
INNER JOIN GROUPS
ON GROUPS.CUSTOMER_ID = SEARCHES.CUSTOMER_ID
WHERE
(GROUPS.GROUP_ID = 17)- Hide quoted text -

- Show quoted text -
I've uploaded a screen shot of the tables and their relationship here:
http://picasaweb.google.com/shannon.whitty/Temporary

Also, the 2nd COUNT() clause should read COUNT(ORDERS.SEARCH_ID) my
mistake!

May 30 '07 #4
On 28 May 2007 01:30:35 -0700, whitsey <ly***********@gmail.com>
wrote:
>Help!

I am trying to write an SQL statement (without much luck) to extract
the total number of searches and orders from a particular customer,
grouped into days/months/weeks (depending on the report type.
The tables are as follows:
Searches
--------------
search_id (PKEY)
customer_id (FKEY)
product_id
cost
date
....
Orders
-----------
order_id (PKEY)
search_id (FKEY)
order_value
date
...
Groups
-----------
group_id (PKEY)
customer_id
...
Customers
---------------
customer_id (PKEY)
....
What I want to achieve from this is
For all customers with Group_id = '17'
Mth 1 1244(Searches) 846(Orders)
Mth 2 2345(Searches) 1834(Orders)
Mth 3 2342(Searches) 1954(Orders)
Mth 4 2425(Searches) 2134(Orders)
Mth 5 5235(Searches) 4253(Orders)
I am clearly nowhere near the level I need to be to achieve this.
Can it be achieved in a single statement?
Thanks

I took a quick shot at it:

SELECT
MONTH(Searches.Search_Date),
COUNT(*) Searches,
(
SELECT
COUNT(*)
FROM
Orders
WHERE
Orders.Customer_Id = Searches.Customer_Id
AND MONTH(Orders.Order_Date) = MONTH(Searches.Search_Date)
) Orders
FROM
Searches
WHERE
EXISTS
(
SELECT
*
FROM
Groups
WHERE
Groups.Customer_Id = Searches.Customer_Id
AND Groups.Group_Id = '17'
)
GROUP BY
MONTH(Search_Date)

I used a sub-select for no particular reason. a join ought to work as
well.

B.
May 30 '07 #5
On May 31, 3:06 am, Brian Tkatch <N/Awrote:
On 28 May 2007 01:30:35 -0700, whitsey <lysterfiel...@gmail.com>
wrote:


Help!
I am trying to write an SQL statement (without much luck) to extract
the total number of searches and orders from a particular customer,
grouped into days/months/weeks (depending on the report type.
The tables are as follows:
Searches
--------------
search_id (PKEY)
customer_id (FKEY)
product_id
cost
date
....
Orders
-----------
order_id (PKEY)
search_id (FKEY)
order_value
date
...
Groups
-----------
group_id (PKEY)
customer_id
...
Customers
---------------
customer_id (PKEY)
....
What I want to achieve from this is
For all customers with Group_id = '17'
Mth 1 1244(Searches) 846(Orders)
Mth 2 2345(Searches) 1834(Orders)
Mth 3 2342(Searches) 1954(Orders)
Mth 4 2425(Searches) 2134(Orders)
Mth 5 5235(Searches) 4253(Orders)
I am clearly nowhere near the level I need to be to achieve this.
Can it be achieved in a single statement?
Thanks

I took a quick shot at it:

SELECT
MONTH(Searches.Search_Date),
COUNT(*) Searches,
(
SELECT
COUNT(*)
FROM
Orders
WHERE
Orders.Customer_Id = Searches.Customer_Id
AND MONTH(Orders.Order_Date) = MONTH(Searches.Search_Date)
) Orders
FROM
Searches
WHERE
EXISTS
(
SELECT
*
FROM
Groups
WHERE
Groups.Customer_Id = Searches.Customer_Id
AND Groups.Group_Id = '17'
)
GROUP BY
MONTH(Search_Date)

I used a sub-select for no particular reason. a join ought to work as
well.

B.- Hide quoted text -

- Show quoted text -
Got it and it works perfectly, Thanks! ( I used a join myself )
However, the problem I have is that it is calculating the month of
january as a total of every january ever entered in the database!!! I
have tried to narrow it fown by outputting month & year but cannot get
it to work.

I changed MONTH(Searches.Search_Date) to
MONTH(Searches.Search_Date)'/'YEAR(Searches.Search_Date) but it didn't
like that.
I tried CAST(Searches.Search_Date AS DATE FORMAT 'mm/yy') however it
failed at "FORMAT"

Also, the months are in the order Jan - Dec. I am using this for a
graph and want the last month to be this month (May-07) and them going
back historically for a predefined period.

I can't seem to find much help on DATE functions on the web - maybe
I'm not searching right!

Thanks

May 31 '07 #6
http://publib.boulder.ibm.com/infoce...c/r0000814.htm
INTEGER(Searched.Search_date) / 100

Cheers
Serge

--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
May 31 '07 #7
On 31 May 2007 00:19:46 -0700, whitsey <ly***********@gmail.com>
wrote:
<SNIP>
>
Got it and it works perfectly, Thanks! ( I used a join myself )
You're most welcome. Glad i could be of service.
>However, the problem I have is that it is calculating the month of
january as a total of every january ever entered in the database!!! I
have tried to narrow it fown by outputting month & year but cannot get
it to work.

I changed MONTH(Searches.Search_Date) to
MONTH(Searches.Search_Date)'/'YEAR(Searches.Search_Date) but it didn't
like that.
I tried CAST(Searches.Search_Date AS DATE FORMAT 'mm/yy') however it
failed at "FORMAT"
The problem is that month is not divided into years. The solution is
to GROUP BY years as well.

GROUP BY
YEAR(Searches.Search_Date),
MONTH(Searches.Search_Date)

That will first section off all data by years, and then--within each
yearly group--it will further section them by month.
>Also, the months are in the order Jan - Dec. I am using this for a
graph and want the last month to be this month (May-07) and them going
back historically for a predefined period.
Can't you use:

ORDER BY
YEAR(Searches.Search_Date) DESC,
MONTH(Searches.Search_Date) DESC
>I can't seem to find much help on DATE functions on the web - maybe
I'm not searching right!
Optimally, you should be reviewing all FUNCTIONs availble in the DB2
documentation. It's a good list to be familiar with.

B.
May 31 '07 #8

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

Similar topics

5
2936
by: Michael Hill | last post by:
Hi, folks. I am writing a Javascript program that accepts (x, y) data pairs from a text box and then analyzes that data in various ways. This is my first time using text area boxes; in the past,...
11
1642
by: vdrab | last post by:
Hi all, Is there some sort of coherent source (dead tree format, maybe?) on some of the more advanced features of python (decorators, metaclasses, etc)? I'm sort of looking for a python book...
16
10945
by: Preben Randhol | last post by:
Hi A short newbie question. I would like to extract some values from a given text file directly into python variables. Can this be done simply by either standard library or other libraries? Some...
6
2604
by: Amma | last post by:
Hello Every one , Pls help me to extracting number from a text file since I am new to perl programming . I have a file and need to extract the number after semicolon in that ...
1
1666
by: prashanth23in | last post by:
This is my first page <form name="form1" method="post" action="ins_comp.php" onsubmit="return form_chk();"> this is next page where i'm printing the posted values
0
9287
by: Sharath | last post by:
Quality Globe is Glad to Offer you the Fast Track course on Automation, QTP Basics and Advanced, and Quality Center Starting Date: June 4th, 2007 Timings: 10 AM to 3:30 PM Duration: 50 Hours ...
3
1715
by: MrDeej | last post by:
Hello! I am somewhat stuck on this problem. I have 1 table which contains an expiry date for products, named TBL productinfo and one table wich contains the exception in how long the product is...
6
1303
by: Steven | last post by:
I have got the return value of MonthsOfYear Property of the IMonthlyTrigger Interface, but how to return months? http://msdn2.microsoft.com/en-us/library/aa380736(VS.85).aspx My return value is...
9
2604
Catalyst159
by: Catalyst159 | last post by:
I have a form which is used to calculate residential Floor Area Ratio (FAR). The form is structured into seven parts as follows: Part A: Maximum FAR and Floor Area: Part B: Gross Floor Area of...
0
7257
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
7157
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
7379
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,...
0
7535
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...
1
7098
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...
0
7521
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5682
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3232
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...
0
3221
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.