473,545 Members | 1,995 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Adding GROUP BY empties the result set?

Using MySQL 5.0.15

I am no expert so maybe I am deeply misundertanding how this should
work. I have a relatively complex quey with joins and a subquery (it is
probably far from optimized, but that is another issue; right now I am
more interested in correctness than speed). If I do not put a GROUP BY
clause then the query returns a number of rows, as expected. If I add a
GROUP BY clause, I get an empty set! My understanding of the semantics
is that GROUP BY should act on the same set that would have been
returned by the query without it, so it does not make sense to me that
the set becomes completely empty. Depending on the column(s) one groups
on, in the "worst" case there should be one row left. I did notice by
using EXPLAIN that MySQL does not execute the query the same way with
and without the GROUP BY, but that should only affect the speed of the
query, not the end result.

Here is my query (without the GROUP BY):

SELECT
transaction_sta tus.transaction _id as this_id,
person_id, payment_method, status, transaction_ite m.sale_item_id,
transaction_tim estamp, YEAR(transactio n_timestamp) as year
FROM transaction_sta tus
LEFT JOIN transaction USING (transaction_id )
LEFT JOIN transaction_ite m USING (transaction_id )
LEFT JOIN sale_item USING (sale_item_id)
WHERE
(TRUE,transacti on_status_id) =
(SELECT (status = "Completed" ), transaction_sta tus_id
FROM transaction_sta tus
WHERE transaction_id = this_id
ORDER BY transaction_tim estamp desc
LIMIT 1)

Cheers,

Jean

Nov 23 '05 #1
3 2258
At least I solved my problem by placing the subquery in the FROM
clause, but I am still curious about my original question.

Cheers,

Jean

Nov 23 '05 #2
lagj wrote:
Using MySQL 5.0.15 ...
If I do not put a GROUP BY
clause then the query returns a number of rows, as expected. If I add a
GROUP BY clause, I get an empty set! ...
Here is my query (without the GROUP BY):

....

The question about the GROUP BY behavior is hard to answer without
knowing which field you were using in the GROUP BY clause!

Also, typically you'd use GROUP BY if you have an aggregate function in
your SELECT list, such as COUNT(), SUM(), MAX(), etc. Read the page
http://dev.mysql.com/doc/refman/5.0/...functions.html

There are some mentions that some aggregate functions ignore NULLs.
So if you are GROUPing BY a field that is always NULL (because of your
outer joins), it could yield no results.

Regards,
Bill K.
Nov 23 '05 #3
Bill Karwin wrote:
The question about the GROUP BY behavior is hard to answer without
knowing which field you were using in the GROUP BY clause!


Good point Bill. Yeah, I am at least aware of the point of using GROUP
BY... Here was my original query with the GROUP BY (the one that
returned an empty set):

SELECT
transaction_sta tus.transaction _id as this_id,
person_id, payment_method, status, transaction_ite m.sale_item_id,
transaction_tim estamp, YEAR(transactio n_timestamp) as year,
SUM(quantity * (transaction_it em.sale_item_id IN (8,9,12))) as
adultBanquet,
SUM(quantity * (transaction_it em.sale_item_id = 13)) as childBanquet,
SUM(quantity * (transaction_it em.sale_item_id IN (8,9,10,11))) as
convention,
SUM(quantity * (transaction_it em.sale_item_id IN (14,15))) as dayPass

FROM transaction_sta tus
LEFT JOIN transaction USING (transaction_id )
LEFT JOIN transaction_ite m USING (transaction_id )
LEFT JOIN sale_item USING (sale_item_id)
WHERE
(TRUE,transacti on_status_id) =
(SELECT (status = "Completed" ), transaction_sta tus_id
FROM transaction_sta tus
WHERE transaction_id = this_id
ORDER BY transaction_tim estamp desc
LIMIT 1)
GROUP BY year, person_id

Neither quantity nor sale_id_id can be NULL, so I don't think your
possible explanation is it. Thanks for trying. It's not that important
to me now anyway since I found another way.

The query that gave me the result I wanted is:

SELECT transaction_sta tus.transaction _id,
person_id,
payment_method, status,
transaction_ite m.sale_item_id, quantity,transa ction_item.amou nt,
transaction_tim estamp, YEAR(transactio n_timestamp) as year,
SUM(quantity * (transaction_it em.sale_item_id IN (13,14,17))) as
num_banq,
SUM(quantity * (transaction_it em.sale_item_id = 18)) as
num_banq_child,
SUM(quantity * (transaction_it em.sale_item_id IN (13,14,15,16)))
as convention,
SUM(quantity * (transaction_it em.sale_item_id IN (19,20))) as
dayPass

FROM transaction_sta tus
INNER JOIN (SELECT
transaction_id, MAX(transaction _timestamp) as timetag
FROM transaction_sta tus
GROUP BY transaction_id) as T USING (transaction_id )
LEFT JOIN transaction USING (transaction_id )
LEFT JOIN transaction_ite m USING (transaction_id )
LEFT JOIN sale_item USING (sale_item_id)
WHERE
transaction_sta tus.transaction _timestamp = T.timetag
AND
status = "Completed"
AND
YEAR(transactio n_timestamp) = YEAR(NOW())
GROUP BY person_id

Cheers,

Jean

Nov 23 '05 #4

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

Similar topics

5
5303
by: Stephen Miller | last post by:
Hi, I am trying to add a staggered running total and average to a query returning quarterly CPI data. I need to add 4 quarterly data points together to calculate a moving 12-month sum (YrCPI), and then to complicate things, calculate a moving average of the 12-month figure (AvgYrCPI). Given the sample data:
4
28814
by: Chad Richardson | last post by:
I've always been mistified why you can't use a column alias in the group by clause (i.e. you have to re-iterate the entire expression in the group by clause after having already done it once in the select statement). I'm mostly a SQL hobbiest, so it's possible that I am not doing this in the most efficient manner. Anyone care to comment on...
2
2348
by: swhite76 | last post by:
I have a section of code that adds some double values and gives an incorrect result. This occurs with data that isn't really waht I would call high precision. An example is the following code snippet: --------------- double a = 2.7; double b = 2.7; double c = 0.001; double result=0;
5
5911
by: surrealtrauma | last post by:
the requirement is : Create a class called Rational (rational.h) for performing arithmetic with fractions. Write a program to test your class. Use Integer variables to represent the private data of the class – the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it is declared....
1
1127
by: Asha | last post by:
Hello, below is a xml file which I’m working with. I want my result to be like this <group name="grp 4"> <report> <name>MTIS_1</name> </report> <report> <name>MTIS_1</name> </report />
4
2894
by: rodchar | last post by:
hey all, what's the best way to automate adding a user to local group on the computer and have it be part of administrators? thanks, rodchar
17
12751
by: Sri | last post by:
How do you add an n-bit number in C? Regards, Sri
1
3121
by: Janroel | last post by:
Hi all, I have 2 webservers running IIS 6.0, and I use forms with get and post. When a form is sent, and the user hits the browser back button: Webserver 1 redisplays de original form data in the form (IIS log file show no new entry), so probably the form/page was cached somewhere Webserver 2 empties the original form (IIS log shows a new...
5
2049
by: =?Utf-8?B?anVzdGluc2FyYWNlbm8=?= | last post by:
I'm using .net 2.0 and am having a problem adding doubles: double x = 63881.97 + 34297.98; The result of this addition in Visual Studio is 98179.950000000012 This is obviously the incorrect result. However, if I change the 34297.98 to 34297.97, it adds the numbers correctly without the erronious decimal palces. Does anyone have any...
0
7410
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...
0
7668
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. ...
0
7773
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...
0
5984
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...
0
4960
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...
0
3466
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...
0
3448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1025
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
722
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...

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.