473,799 Members | 3,382 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need help generating SQL select statement

Given these two tables in DB/2:
Date Activity
------------------- --------
2005-01-02 00:00:00 1
2005-01-05 00:00:00 2
2005-01-05 00:00:01 1
2005-01-07 00:00:00 2
2005-01-17 00:00:00 2
2005-02-02 00:00:00 1
2005-02-03 00:00:00 1
2005-02-03 00:00:01 3
2005-02-20 00:00:00 1
2005-02-21 00:00:00 1
2005-02-22 00:00:00 1
2005-02-22 00:00:01 1
2004-12-22 00:00:00 3

ACTIVITY_ID ACTIVITY NAME
1 HELLO
2 GOODBYE
3 TEST
4 FOOBAR
5 CAT
6 DOG
How can I generate a query to produce the following (only select
activities 1,2,3,4 from the activity table)
YEAR MONTH ACTIVITY COUNT
2005 2 HELLO 6
2005 2 GOODBYE 0
2005 2 TEST 1
2005 2 FOOBAR 0
2005 1 HELLO 2
2005 1 GOODBYE 3
2005 1 TEST 0
2005 1 FOOBAR 0
2004 12 HELLO 0
2004 12 GOODBYE 0
2004 12 TEST 1
2004 12 FOOBAR 0

Nov 12 '05 #1
3 1461
"elubin_nos pam" <el****@yahoo.c om> wrote in message
news:11******** *************@l 41g2000cwc.goog legroups.com...
Given these two tables in DB/2:
Date Activity
------------------- --------
2005-01-02 00:00:00 1
2005-01-05 00:00:00 2
2005-01-05 00:00:01 1
2005-01-07 00:00:00 2
2005-01-17 00:00:00 2
2005-02-02 00:00:00 1
2005-02-03 00:00:00 1
2005-02-03 00:00:01 3
2005-02-20 00:00:00 1
2005-02-21 00:00:00 1
2005-02-22 00:00:00 1
2005-02-22 00:00:01 1
2004-12-22 00:00:00 3

ACTIVITY_ID ACTIVITY NAME
1 HELLO
2 GOODBYE
3 TEST
4 FOOBAR
5 CAT
6 DOG
How can I generate a query to produce the following (only select
activities 1,2,3,4 from the activity table)
YEAR MONTH ACTIVITY COUNT
2005 2 HELLO 6
2005 2 GOODBYE 0
2005 2 TEST 1
2005 2 FOOBAR 0
2005 1 HELLO 2
2005 1 GOODBYE 3
2005 1 TEST 0
2005 1 FOOBAR 0
2004 12 HELLO 0
2004 12 GOODBYE 0
2004 12 TEST 1
2004 12 FOOBAR 0

This sounds like a homework assignment. BTW the product is DB2, not DB/2.

select year(date_col), month(date_col) , activity_name, count(*)
from table1 a, table2 b
where activity between 1 and 4
and a.activity = b.activity
group by year(date_col), month(date_col) , activity_name
Not sure about the sort sequence you want, but you can add an Order By at
the end and use descending sequence if necessary.
You cannot name a column "date" since that is reserved word.
Nov 12 '05 #2
No, not a homework assignment. I am a java programmer and just don't
know SQL well enough. I have tried your simplistic answer but it
doesn't answer my question. For this report, the result for each month
always needs all 4 activities, even if they are 0.... which I
understand is a RIGHT JOIN of some sort, but do not know how to write
myself.

Your query
select year(a.datetime ), month(a.datetim e), b.activity, count(*)
from user_log a, logging_activit y b
where b.activity_id between 1 and 4
and a.activity = b.activity_id
group by year(a.datetime ), month(a.datetim e), b.activity
produces only
2005 2 SEARCH 3
2005 3 HOMEPAGE 1
2005 3 SEARCH 1

Nov 12 '05 #3
Finally solved it... for anyone interested....

WITH R AS ( SELECT DISTINCT A.ACTIVITY AS ACTIVITY, A.ACTIVITY_ID AS
ACTIVITY_ID, MONTH(B.DATETIM E) AS MONTH, YEAR(B.DATETIME ) AS YEAR FROM
LOG B, ACTIVITIES A WHERE A.ACTIVITY IN
('HELLO','GOODB YE','TEST','FOO BAR'))
SELECT R.ACTIVITY,R.MO NTH,R.YEAR,(SEL ECT COUNT(*) FROM LOG U WHERE
R.ACTIVITY_ID=U .ACTIVITY AND R.MONTH=MONTH(U .DATETIME) AND
R.YEAR=YEAR(U.D ATETIME) ) FROM R ORDER BY R.YEAR DESC,R.MONTH
DESC,R.ACTIVITY _ID ASC

Nov 12 '05 #4

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

Similar topics

1
1512
by: LRW | last post by:
This is mySQL 101. I know it's supposed to be super simple, but I'm an idiot. I can't get my join to work (1st time I've tried doing joins.) I've read http://www.mysql.com/doc/en/JOIN.html, but I'm just not groking it. When I try to do the same thing, I can't "convert" one field into the data from another. Here's what I have. To test this concept I made two tables on a database called db_join:
0
381
by: LRW | last post by:
This has to do with mySQL 101. I know it's supposed to be super simple, but I'm an idiot. I can't get my join to work (1st time I've tried doing joins.) I've read http://www.mysql.com/doc/en/JOIN.html, but I'm just not groking it. When I try to do the same thing, I can't "convert" one field into the data from another. Here's what I have. To test this concept I made two tables on a
12
2515
by: Martin_Hurst | last post by:
Has some one come up with a similar type script that could be used in a Postgresql database? The script below was created for a SQLServer database. Thx, -Martin ++++++++++++++++++++++++++++++++++++++ http://searchdatabase.techtarget.com/tip/1,289483,sid13_gci913717,00.html In the early stages of application design DBA or a developer creates a data
6
5553
by: Poul Møller Hansen | last post by:
I have made a stored procedure, containing this part for generating a unique reference number. SET i = 0; REPEAT SET i = i + 1; SELECT RAND() INTO reference FROM SYSIBM.SYSDUMMY1; SET p_reference = ref_prefix || SUBSTR(CAST(reference AS CHAR(12)),3);
9
5011
by: YONETANI Tomokazu | last post by:
Hi. You can use the following SQL to construct rows with column names on the fly, rather than from an existing table like sysibm.sysdummy1: SELECT * FROM TABLE ( VALUES (0, 1, 2), (3, 4, 5), (6, 7, 8) ) X(foo, bar, baz); or WITH X(foo, bar, baz) AS ( VALUES (0, 1, 2), (3, 4, 5), (6, 7, 8) ) SELECT * FROM X;
5
1975
by: Henry | last post by:
Dear all, Is there any DB2 SQL statement that could generate rows of intermediate dates by providing the start and end dates?? If 2006-1-1 and 2006-1-31 are provided to the SQL, the output will look like : 2006-1-1 2006-1-2
4
2317
by: rbronco28 | last post by:
I am pretty new at doing web development. I can make simple pages, but recently received a request from a small limo business I do web development for. We are currently working on a reservation form online for customers to fill out personal info, event details, and payment options. In the form we ask the customer to select what limo they want in addition to how many hours they want the limo for. The problem I am running into is getting a...
5
1826
by: Arun Srinivasan | last post by:
I am trying to generate sql from sql. Like select 'select count(*) from ' || tabschema ..............syscat.tables where.....; is there a way to run this with a single statement? I tried with temp1(sql) as ( select 'select count(*) from ' || tabschema ..............syscat.tables where.....;
1
903
muaddubby
by: muaddubby | last post by:
Hi Anyone know if its possible to generate a SELECT statement for SQL Server based on a schema? The schema is currently loaded in a dataset, but I see no way of doing this in that class. Are there any helper for this? Perhaps an SQL Server class? Thx.
0
9544
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10490
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...
0
10259
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10030
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9077
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5467
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
5589
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4145
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3761
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.