473,883 Members | 1,713 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

GROUP BY problem in conjunction with a SUM()

Perhaps some kind soul could help me out with an SQL I have been trying all
day to get to work, where one colum is just not summing up the way I want it
to.

I have the following data

GRP_SEQ ITEM_SEQ NR_ITEMS PERSONS_SEQ

1 A 2 aa
1 A 2 bb
1 A 2 cc
1 B 3 dd
1 B 3 ee
2 C 1 ff
2 C 1 gg

An item is part of a group. The items are sold in specified numbers and are
associated with persons.

The first result I want to acheive is list each item and the number of items
used by the number of persons associated with the item:

select
GRP_SEQ
,ITEM_SEQ
,sum(ITEM_NR)/count(ITEM_SEQ)
,count(PERSONS_ SEQ)
from
GRP
inner join ITEM on ITEM_GRPSEQ=GRP _SEQ
left outer join PERSONS on PERSONS_ITEMSEQ =ITEM_SEQ
where
ITEM_GRPSEQ=GRP _SEQ
group by ITEM_SEQ,GRP_SE Q
order by 2,1

The result is

1 A 2 3
1 B 3 2
2 C 1 2

This works fine.

The next SQL is the one I am having problems with. I want to group the items
by their group and return the total number of items sold for that group of
items and count the number of persons that used the items of that group.
What I have come up with is the following:

select
GRP_SEQ
,???
,count(PERSONS_ SEQ)
from
GRP
inner join ITEM on ITEM_GRPSEQ=GRP _SEQ
left outer join PERSONS on PERSONS_ITEMSEQ =ITEM_SEQ
where
ITEM_GRPSEQ=GRP _SEQ
group by GRP_SEQ
order by 1

The result I am expecting to acheive is

1 5 5
2 1 2

but I am have not been able to work out how I can I can sum the ITEM_NR for
each item only once for each item. I could of course modify the SQL to

select
GRP_SEQ
,(select sum(i2.ITEM_NR) from ITEM i2 where i2.ITEM_GRPSEQ= GRP_SEQ)
,count(PERSONS_ SEQ)
from
GRP
inner join ITEM i1 on i1.ITEM_GRPSEQ= GRP_SEQ
left outer join PERSONS on PERSONS_ITEMSEQ =i1.ITEM_SEQ
where
i1.ITEM_GRPSEQ= GRP_SEQ
group by GRP_SEQ
order by 1

but there must be some method to achieve the result without using the costly
subselect. Somehow I think there is event something in the first SQL that I
am overseeing that would allow me to replace the
"sum(ITEM_N R)/count(ITEM_SEQ) " with something more elegant.

Perhaps some kind soul could help me out ....

Regards

Rudolf Bargholz





Feb 18 '07
10 10010
Another idea.
------------------- Commands Entered ------------------------------
select GRP_SEQ
, MOD(SUM(DISTINC T ITEM_SEQ*1000+I TEM_NR),1000) AS "number of
items sold"
, count(PERSON_SE Q) AS "number of persons"
from GRP
inner join
ITEM i
on i.ITEM_GRPSEQ=G RP_SEQ
left outer join
PERSON2
on PERSON_ITEMSEQ= i.ITEM_SEQ
group by GRP_SEQ
order by 1;
-------------------------------------------------------------------

GRP_SEQ number of items sold number of persons
----------- -------------------- -----------------
1 3 5
2 1 1

2 record(s) selected.

Feb 19 '07 #11

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

Similar topics

4
1612
by: ree32 | last post by:
I know when you are using group by functions you have to include all the columns in the GROUP BY clause. But what I am having problems when using a case statement to determine whether to sum of not a column. eg. SELECT Country, Case WHEN Age<15 THEN Sum(Income) ELSE NULL END AS YouthIncome, Case WHEN Age>65 THEN Sum(Income) ELSE NULL END AS PensionIncome FROM WORLDTABLE
4
10936
by: Stefan Bauer | last post by:
Hi NG, I have problem... I'm currently using UDB v8.1 for Linux. Here is the table "test": ID1 ID2 ID3 VALUE ----------------- 1 0 1 23 1 0 2 9
4
6101
by: Hans | last post by:
Hi, I want to create a report. The query looks like this: SELECT A.Name, Sum(A.Hours) AS FROM A GROUP BY A.Name; Now I want in my report to have a last line with the total hours from all. It doesn't work with an simple SUM-Text-filed, I will see an error message (3612) that this is nor possible with GROUP BY.
7
4553
by: Darin | last post by:
I have a report that sub-totals on a group, then grand-totals at the report footer. If there's only one group, the sub-total and grand total are redundant, so I only want to show one of them. I know how to count the groups, then hide the report footer if there's only one group, but my problem is I want to hide the group footer (sub-total), not the report footer (because the report footer references what the grand total is for, which is...
5
1545
by: sangram.0149 | last post by:
hi ! could someone plz help me on this one,its kinda urgent i hav a result set which looks something like this country_code sum(amount) 1 100
3
4114
by: Sebastian | last post by:
Hello all I have a report where I have two nested groups. I know there are only three standard options for running sum: None, Over Group and Over All. I have a MyTextBox in detail section where the data is summed over group. But the data is summed over the second group. When a new group begins MyTextBox value is resetting to zero. I need a running sum over the first group so when another second group begins MyTextBox value will
2
2549
by: jammer | last post by:
Hey All, I have a report based on a query that joins a 'parent' and 'child' table. Each row in the query corresponds to a row in the child table, with a few fields from the Parent table. At the bottom of the report, in the footer, I total some values from the Child table.
7
3666
by: Sunil Korah | last post by:
Hi, I haven't used access reports much. I have a problem in getting the total of a group. I have 3 fields, ProgName (Program name), Pname (Participant's name) and PCategory (Participant category) PCategory can have values 'SC', 'ST' and 'Others'
1
1711
by: bflemi3 | last post by:
My previous post went unanswered. I now know what the problem is but can not think of a practical work around. Hopefully by making my question less confusing someone will be able to help...here goes... I have 3 CASE expressions in my SELECT list (bolded font) that should be included in the GROUP BY clause. The problem is that the 3 CASE expressions have aggregates in them so when i put them in the GROUP BY i get the error that you can't have...
0
9777
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
11109
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
10726
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...
1
10833
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,...
0
9558
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
7114
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
5979
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4602
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
4198
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.