Q
Problem solve Get help with specific problems with your technologies, process and projects.

# Aggregating date content

I have a table that stores dob (mm/dd/yyyy)in MySQL DB. But now I need to aggregate that table's contents like this:

```Group 1: <= 16 years
Group 2: >=17 E <= 25
Group 3: >=26 E <= 40
Group 4: >=41 years
```
MySQL gives the ages, but how can I aggregate them in those groups?

Ideally you would want to use the IF() function to get the aggregates for this table. The IF() function takes three arguments and returns a single value:

```mysql> SELECT IF(1 = 2, 'True', 'False');
+----------------------------+
| IF(1 = 2, 'True', 'False') |
+----------------------------+
| False                      |
+----------------------------+
1 row in set (0.44 sec)
```
As you can see, the first argument is evaluated, and if the first argument evaluates true, the second argument is passed, otherwise the third argument is passed. So how do we use this to aggregate the situation you provided? First we create the aggregates:
```IF(age <= 16, 1,  0)
IF(age >= 17 AND dob <=  25, 1, 0)
IF(age >=  26 AND dob <= 40, 1, 0)
IF(age >= 41, 1, 0)
```
Then we simply provide a sum of the columns and format it as a SELECT query:
```SELECT SUM(IF(age <= 16, 1,  0)) AS VeryYoung,
SUM(IF(age >= 17 AND age <=  25, 1, 0)) AS Young,
SUM(IF(age >=  26 AND age <= 40, 1, 0)) AS GettingThere,
SUM(IF(age >= 41, 1, 0)) AS Others
FROM mytable;
```
Here's a sample of the output you can expect on a table with ages 5, 10, 15...45, 50:
```+-----------+-------+--------------+--------+
| VeryYoung | Young | GettingThere | Others |
+-----------+-------+--------------+--------+
|         3 |     2 |            3 |      2 |
+-----------+-------+--------------+--------+
1 row in set (0.06 sec)
```
And there you have it. As you indicated, you already can determine the age, so we won't go into that in this question.

#### Have a question for an expert?

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

#### Start the conversation

Send me notifications when other members comment.

## SearchWindowsServer

Microsoft put its branding on a relatively new hyper-converged infrastructure offering that capitalizes on the software-defined ...

• ### How to manage Exchange hybrid mail flow rules

Duplicating your existing transport rule collection won't work when you move to an Exchange hybrid deployment. Learn how to set ...

• ### How to repair Windows Server using Windows SFC and DISM

Administrators can diagnose and treat a buggy server operating system by using the Windows SFC and DISM utilities for image ...

## SearchServerVirtualization

• ### How to deploy graphics devices using Hyper-V DDA

Admins can map a VM to a GPU with Hyper-V, but they must first complete a few steps, such as prepare the VM and dismount the PCIe...

• ### Learn to integrate VMs and containers in your data center

The VMs vs. containers debate is popular among admins, but integrations of the two technologies have begun to emerge with the ...

• ### Dockerfile commands for containerizing applications

Admins can use Dockerfiles to containerize their applications and ease deployment. They can implement several commands to help ...

## SearchCloudComputing

• ### Review these Azure Service Bus best practices

When applications talk, you need to listen. Learn about Microsoft's cloud messaging service, its main features, best practices to...

• ### Instill cloud change management best practices

Automation is essential to change management in the cloud. Discover the best practices that ensure your IT team is in sync and ...

• ### How much cloud does an IT disaster recovery plan need?

It's not easy to get the right mix of traditional and cloud-based DR resources. Here's how to strike a balance between what's ...

Close