473,804 Members | 2,205 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Two alike conditions in query. Is it possible?

Hello, All!
Example. I have a table with information about payments (id, amount,
pay_date, customer_id). Can I select two sums for two different date ranges,
grouped by 'customer_id' (for each customer) in one query?

--
With best regards, Gregory Konev. E-mail: vo****@ml.net.u a
Jul 23 '05 #1
2 1476
Gregory Konev wrote:
Example. I have a table with information about payments (id, amount,
pay_date, customer_id). Can I select two sums for two different date ranges,
grouped by 'customer_id' (for each customer) in one query?


Not as fields of a single record in the result set. But there are a
couple of ways you could get the two sums in separate records.

You could use UNION to combine two separate queries, and then sort them
together by the customer_id. See example of using ORDER BY over two
unioned queries at http://dev.mysql.com/doc/mysql/en/union.html

You could also GROUP BY both the customer_id and an expression on the date:

SELECT t.customer_id, SUM(t.amount),
CASE WHEN t.pay_date BETWEEN '2003-01-01' AND '2003-12-31' THEN 'FY2003'
WHEN t.pay_date BETWEEN '2004-01-01' AND '2004-12-31' THEN 'FY2004'
WHEN t.pay_date BETWEEN '2005-01-01' AND '2005-12-31' THEN 'FY2005'
END AS `date_range`
FROM t
GROUP BY t.customer_id, date_range;

Regards,
Bill K.
Jul 23 '05 #2
Gregory Konev (vo****@ml.net. ua) wrote:
: Hello, All!
: Example. I have a table with information about payments (id, amount,
: pay_date, customer_id). Can I select two sums for two different date ranges,
: grouped by 'customer_id' (for each customer) in one query?

select data from both date ranges grouped by customer id.

The question then becomes what value is being summed for each sum.

The value inside the sum must be a calculation that checks the date. If
the date is in the range then use the value, else use 0.

pseudo code

select
sum(
if date_column between start1 and end1
then COLUMN_BEING_SU MMED
else 0
) as sum_from_range_ 1 ,
sum(
if date_column between start2 and end2
then COLUMN_BEING_SU MMED
else 0
) as sum_from_range_ 2
from
the_table
group by id
mysql has functions like IF and various date functions, but I don't use
mysql enough to simply show you which ones to use here. You'll have to
look that up.


--

This space not for rent.
Jul 23 '05 #3

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

Similar topics

2
5614
by: Paris_Sucks | last post by:
I'm trying to redirect when testing for certain condidtions as shown below. When the conditions are ture, it redirects, but still goes ahead and processes the sql query. What am I doing wrong??? And then sometimes when the conditions are correct, it doens't redirect. It appears to be very inconsistent. Any suggestions would be greatly appreciated. //Check for repeat name $result = mysql_query("SELECT * FROM survey WHERE FirstName =
6
1706
by: Pedro Alves | last post by:
Hi I'm having serious problems with a mission critical app that runs on postgres (and has been running for the past 3 years). It's rather large, and lately things are not going well. The planner is getting 'nuts', choosing ways that never end and completly stops the database. The strange thing is that running the same query with the same database
4
1493
by: ime | last post by:
Hi to all. I'm making a web application in which users enter text for a person's name, last name, sex, etc. But user doesn't have to populate all text boxes, so I don't know how to make query without these conditions (for example if I have an empty string for a name) . I've got 9 text boxes, so I can't write all combinations - it's to many of them. Thanks a lot
2
5492
by: Strasser | last post by:
Problem: Query record selection criteria reads Like & "*" BUT, then Access changes it to non-functioning Alike & "*" Any ideas? Thank you in advance. Strasser
3
6215
by: Simon | last post by:
Dear reader, For matching of two fields it is required that the fields has the same content. But now I have the situation that two text fields has not precisely the same content but there is some minute deference.
1
1262
by: KRN000 | last post by:
hello every one i'm developing a web site using asp.net , with VB as the programming language. i want to ask: how can I implement "if else" conditions on the execution of a sql query? For example,write in the handler of a button :" IF radiobutton1 is selected THEN EXECUTE sqldatasource1 (or procedure1 ) ELSE EXECUTE sqldatasource2 ( or procedure2)". So I want execution of sql query to be appended with "IF ELSE" conditions. thank you
2
2586
by: Igor | last post by:
1. Are stored procedures WITH ENCRYPTION slower than the ones without encryption? 2. Should i put most restrictive conditions first or last in WHERE? In which order does MSSQL execute conditions? Or MSSQL determents what would be best and does not bother with the way i sorted conditions? for example: SELECT * FROM
6
3110
by: jackal_on_work | last post by:
Hi Faculties, I have two queries which give me the same output. -- Query 1 SELECT prod.name, cat.name FROM products prod INNER JOIN categories cat ON prod.category_id = cat.id WHERE cat.id = 1;
12
25133
by: =?ISO-8859-1?Q?Ren=E9?= | last post by:
Hi, is there a rule of thumb what is better/faster/more performant in SQL Server 2005? a) SELECT * FROM A INNER JOIN B ON B.ID = A.ID AND B.Cond1 = 1 AND B.Cond2 = 2 b) SELECT * FROM A INNER JOIN B ON B.ID = A.ID WHERE B.Cond1 = 1 AND B.Cond2 = 2
0
9715
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
10600
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
10097
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
9175
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
6867
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
5535
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
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4313
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
3835
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.