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.ua 2 1460
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.
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_SUMMED
else 0
) as sum_from_range_1 ,
sum(
if date_column between start2 and end2
then COLUMN_BEING_SUMMED
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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...
|
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...
|
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
|
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.
| |
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...
|
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
|
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;
|
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
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
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...
|
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...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |