473,656 Members | 2,824 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

VIEW and GROUP BY

Hi,
I have a problem with one of my view. Basically, I need to create a
view
to sum the amount in Table A grouped by the EMPLOYEE_ID. But I also
need the ability to limit the date range.

What I need to do is creating a view with the ability to limit the
date
range want the result sets to be broken down by employee_id (not
dates).
Is there an easy way to accomplish this ?

Table_A
-----------
ID EMPLOYEE_ID DATE AMOUNT
1 1 01/01/2006 100.00
2 1 01/02/2006 50.00
3 2 01/02/2006 25.00
4 1 01/03/2006 10.00
5 2 01/03/2006 15.00
6 3 01/03/2006 5.00
7 1 01/04/2006 10.00
8 2 01/04/2006 20.00
9 3 01/04/2006 30.00

---------------------------------------------------------------------
SELECT EMPLOYEE_ID,DAT E,SUM(AMOUNT)
FROM Table_A
GROUP BY EMPLOYEE_ID,DAT E ;
--------------------------------------------------------------------
This query will give me the DATE column, so that I can
pass in the date range I want in the view, but the result
sets will also be broken down by employee and date.

--------------------------------------------------------
SELECT EMPLOYEE,SUM(AM OUNT)
FROM Table_A
GROUP BY EMPLOYEE_ID;
--------------------------------------------------------
This query will provide me what I need, but when
I create the view over it, there is no way to pass in
the date range I want.

Any experts out there ? :-) I must accomplish this
in a view. I would appreciate any advices anyone can
give.

Thanks

May 23 '06 #1
9 2184
ha*********@yah oo.com wrote:
Hi,
I have a problem with one of my view. Basically, I need to create a
view
to sum the amount in Table A grouped by the EMPLOYEE_ID. But I also
need the ability to limit the date range.
[snip] This query will provide me what I need, but when
I create the view over it, there is no way to pass in
the date range I want.

Any experts out there ? :-) I must accomplish this
in a view. I would appreciate any advices anyone can
give.


Is an SQL table function absolutely out of the question? Wouldn't be a
great deal different to accessing a view; you'd wind up using something
like the following to access the function:

SELECT
fields
FROM
TABLE(myfunc(st artdate, enddate)) AS T
Dave.

--

May 23 '06 #2
Exactly, I agree with Dave that function can be the solution.
Using view to achieve what you need, first of all rows have to be
filtered by date, and then grouped, eg.:

select employee_id, sum(amount)
from table_a
where date < '2006-01-04'
group by employee_id;

In view definition you cannot specify variables, but only fixed value
or value selected from another table. But function can accept a
variable passed as an argument.

create function employee_sum(p_ date date)
returns table (employee_id int, sum_amount int)
begin atomic
return
select employee_id,sum (amount)
from table_a
where date < p_date
group by employee_id;

end !

using the function:
select * from table(employee_ sum(date('2006-01-04'))) as employee_sum

PS. If you for 100% need a view you can create a special table and join
that table within the view. But before executing the select you need to
update min, max dates in the table, which is not very elegant solution,
roughly saying.

-- Artur Wronski

May 23 '06 #3
Arthur,

Thanks for the suggestion. It worked out really great when I tried
it
in the SQL Editor. But for the time being, our users are running their
reports though EXCEL ODBC. And they can't really execute any
stored procedures or functions through it. All they can access is
either user tables or views.

So I must try to make this work via view for now. I will keep
looking
to see what I can do with what I have. I have been experimenting a lot
with CTE (WITH Clause) or using GROUPING SETS without success.

Thank you

May 24 '06 #4
ha*********@yah oo.com wrote:
Arthur,

Thanks for the suggestion. It worked out really great when I tried
it
in the SQL Editor. But for the time being, our users are running their
reports though EXCEL ODBC. And they can't really execute any
stored procedures or functions through it. All they can access is
either user tables or views.

So I must try to make this work via view for now. I will keep
looking
to see what I can do with what I have. I have been experimenting a lot
with CTE (WITH Clause) or using GROUPING SETS without success.


Erm ... Using Excel ODBC shouldn't preclude users from accessing table
functions. As far as DB2 is concerned Excel is "just another ODBC
client", and I certainly have no troubles using table functions via
ODBC in other applications.

Oh, hold on a sec ... I've just figured it out ... you mean they're
accessing DB2 via Excel's "Query Wizard" or the MS Query interface
neither of which will list table functions? I've just tried out the MS
Query interface and it won't even let me add a table function reference
in a manual SQL statement! Urgh ... crap.

If you're stuck using Excel, there is another option which would allow
you to use table functions: use a ADO (ActiveX Data Objects) in a VB
script to send SQL to DB2 directly and retrieve the results into a
worksheet. Advantage: you can use any SQL you want. Disadvantage:
you've got to build the user interface (if you want one), and deal with
VB (urgh).

If you want to try that last option, let me know; I've got an Excel
file sat around that allows you to enter some SQL, execute it against
an ODBC data source, and retrieve the results into a worksheet. It's
very basic, but demonstrates the techniques (I mostly made it so I'd
have something to refer to whenever I needed to remember how to do this
:-)
HTH,

Dave.

--

May 24 '06 #5
Hi Dave,
Thank you very much for all the info. Yes, I was referring to the
MS Query. That's what our users have been running their EXCEL
reports against. Something better should be coming along in the
future, but I'm stuck with this for now. So I've kinda gotta work with
what I was given :-) I don't have much knowledge on this VB stuff,
but it does sound fantastic and flexible. Would you show me the
rope ? Now I have more google search to do haha.

Thanks very much you guys are awesome,
Nick

May 24 '06 #6
ha*********@yah oo.com wrote:
Hi,
I have a problem with one of my view. Basically, I need to create a
view
to sum the amount in Table A grouped by the EMPLOYEE_ID. But I also
need the ability to limit the date range.

What I need to do is creating a view with the ability to limit the
date
range want the result sets to be broken down by employee_id (not
dates).
Is there an easy way to accomplish this ?

Table_A
-----------
ID EMPLOYEE_ID DATE AMOUNT
1 1 01/01/2006 100.00
2 1 01/02/2006 50.00
3 2 01/02/2006 25.00
4 1 01/03/2006 10.00
5 2 01/03/2006 15.00
6 3 01/03/2006 5.00
7 1 01/04/2006 10.00
8 2 01/04/2006 20.00
9 3 01/04/2006 30.00

---------------------------------------------------------------------
SELECT EMPLOYEE_ID,DAT E,SUM(AMOUNT)
FROM Table_A
GROUP BY EMPLOYEE_ID,DAT E ;
--------------------------------------------------------------------
This query will give me the DATE column, so that I can
pass in the date range I want in the view, but the result
sets will also be broken down by employee and date.

--------------------------------------------------------
SELECT EMPLOYEE,SUM(AM OUNT)
FROM Table_A
GROUP BY EMPLOYEE_ID;
--------------------------------------------------------
This query will provide me what I need, but when
I create the view over it, there is no way to pass in
the date range I want.

Any experts out there ? :-) I must accomplish this
in a view. I would appreciate any advices anyone can
give.

Thanks

What is wrong with:

SELECT EMPLOYEE_ID,DAT E,SUM(AMOUNT),
FROM (SELECT * FROM table_a where date between 'fd' and 'ld') t1
GROUP BY EMPLOYEE_ID,DAT E
May 24 '06 #7
ha*********@yah oo.com wrote:
Hi Dave,
Thank you very much for all the info. Yes, I was referring to the
MS Query. That's what our users have been running their EXCEL
reports against. Something better should be coming along in the
future, but I'm stuck with this for now. So I've kinda gotta work with
what I was given :-) I don't have much knowledge on this VB stuff,
but it does sound fantastic and flexible. Would you show me the
rope ? Now I have more google search to do haha.


Flexible, yes. Fantastic ... well ... it's VB ... I tend to swear
profusely when coding in VB (and sometimes it makes its way into the
comments :-)

That said, I think the comments in this particular worksheet are
relatively benign (I must have been feeling very relaxed when I wrote
it!). Anyway, you can grab a copy of it from:

http://www.waveform.plus.com/ExcelQuery.xls

Unless you need to present your users with a list of ODBC data sources
within the worksheet, you can ignore the stuff in the "odbc" module
(which contains a little bit of the ODBC API translated into VB simply
for the purpose of populating the data sources drop down on the main
page). The stuff that'll be of primary interest to you is the code
behind the Execute button in Sheet1 (see the QueryBtn_Click subroutine).

Drop me a mail if you need a hand with any of the code (Excel & VB is
rather off-topic for this newsgroup, even if they are being used to
access DB2 :-)
HTH,

Dave.
--

May 24 '06 #8
Hi Bob,

Thanks for the reply. My problem with your solution is that 'fd'
and 'ld'
can't be hard coded. I don't know the date ranges that the user will
pass in.
And views cannot be created with variables unlike procedures or
functions.
Unless I'm missing something :-)

Nick

May 24 '06 #9
Thanks Dave,
This looks to be something really neat. I'm looking at the worksheet

and the codes. I'll let you know what I run into next :-)

Nick

May 24 '06 #10

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

Similar topics

3
17982
by: M. Mehta | last post by:
It seems that you can not create a materialized view if you are using outer joins...can someone please verify this? Thanks M. Mehta Please follow my example below: created 2 tables:
11
4813
by: Experienced but Undocumented | last post by:
Hi, I have a table with three columns: datetime, subject, & author. (There are more columns, but these are the only relavent ones.) Some rows will have identical subjects. For each subject, I need to select the author with the most recent date/time stamp. Any ideas?
5
1440
by: Karen Bailey | last post by:
Hi, I am attempting to create a view that will rollup or group like, consecutive data. I have created a view using unions, but the statement is extremely slow. Here is example data Colour Table:
1
680
by: TimG | last post by:
Hello, I am putting together a row level security plan for our sales database. I will give a brief description of the method I am thinking of using to give you an idea of how I will need to be able to discover the group or login the user is using to access the data. I have a table called salesfact, it has all the sales info for all the branches of our company. Each order(row) that is inserted has an entry in the division_number column...
3
2201
by: ChrisRath | last post by:
I have a table that I want to have a precalulcate length on a character field and group and sum up. Thought I could do this by creating a view with a group by clause that includes the sum function. Unfortunately, the compiler complains with: A clustered index cannot be created on the view 'MyView' because the index key includes columns which are not in the GROUP BY clause. Wish I could verbalize the problem a little better, but the...
3
3801
by: Thomas R. Hummel | last post by:
I am using SQL Server 2000, SP3. I created an updatable partitioned view awhile ago and it has been running smoothly for some time. The partition is on a DATETIME column and it is partitioned by month. Each month a stored procedure is scheduled that creates the new month's table, and alters the view to include it. Again... working like a charm for quite some time. This past weekend I moved some of the first tables onto a new file...
1
3334
by: jtwright | last post by:
I've got a view that creates a parent child relationship, this view is used in Analysis Services to create a dimension in a datastore. This query tends to deadlock after about 10 days of running smoothly. Only way to fix it is to reboot the box, I can recycle the services for a quick fix but that usually only works for the next 1-2 times I call the view. This view is used to create a breakdown of the bill-to locations from...
5
2325
by: Sim Zacks | last post by:
I just did a dump and restore of my database and one of my views did not recreate. The error received was : pg_restore.exe: could not execute query: ERROR: column reference "pricinggroupid" is ambiguous I checked the function in the original database, using PGAdmin, and the system seemed to have slightly changed my query. Part of my query was a virtual table (i.e joining on (select * from ...) as tablename ) and the system changed...
5
2218
by: Roy Gourgi | last post by:
Hi, Is there a way to group rows by some columns in such a way that I can clearly see them in a distinguished fashion when I look at a view or table. This is my code below but when I try using GROUP BY it is not giving me the desired effect that I would like? For example, let's say I would like to group by the first 5 columns (i.e. V1,V2,V3,V4,V5) and let's say the first 3 rows of my view or table contain the values 1,2,3,4,5 for...
0
8382
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
8600
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
7311
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...
1
6162
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
5629
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
4150
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
4300
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
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
1930
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.