I want to insert the data from one of my tables to another This is the filled table that I have:
Act (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
birth_date DATETIME,
death_date DATETIME,
place VARCHAR(20)
);
In this table some of the values are NULL - for example if it's a birth act then death_date is NULL.
And this is the table I want to transfer data into:
Population_Growth (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
number_of_births INT,
number_of_deaths INT,
place VARCHAR(20),
time DATE
);
In the end I would like this table to show number of deaths and number of births in the same place and the same date. As for numbers of births and deaths I would do something like this:
INSERT INTO Population_Growth (number_of_births, number_of_deaths) VALUES
((COUNT (birth_date) FROM Act WHERE birth_date IS NOT NULL)),
((COUNT (death_date) FROM Act WHERE death_date IS NOT NULL));
However I don't know how to relate it with the corresponding time and date.
One way to get the specified result would be a query like this:
SELECT c.place
, c.time
, SUM(c.count_births) AS number_of_births
, SUM(c.count_deaths) AS number_of_deaths
FROM (
SELECT b.place
, DATE(b.birth_date) AS `time`
, SUM(1) AS count_births
, SUM(0) AS count_deaths
FROM Act b
WHERE b.birth_date IS NOT NULL
GROUP BY b.place, DATE(b.birth_date)
UNION ALL
SELECT d.place
, DATE(d.death_date) AS `time`
, SUM(0) AS count_births
, SUM(1) AS count_deaths
FROM Act d
WHERE d.death_date IS NOT NULL
GROUP BY d.place, DATE(d.death_date)
) c
GROUP BY c.place, c.time
To turn that into an insert, precede the SELECT with:
INSERT INTO Population_Growth (place, `time`, number_of_births, number_of_deaths)
FOLLOWUP
The query above casts the DATETIME columns to a DATE datatype (by truncating the time portion.)
To get the summary by year and month, but still as a form valid for assignment to a DATE
column, one option is to replace all of the expressions:
DATE(b.birth_date)
DATE(d.death_date)
(in the query above) with these expressions:
DATE_FORMAT(b.birth_date,'%Y-%m-01')
DATE_FORMAT(d.death_date,'%Y-%m-01')
The new expressions will extract the year and month from the DATETIME column, and set the day component to the 1st of the month.
If I understand correctly, you want to get the count of non-NULL values from two date columns. Here is one approach to getting the summary:
select place, d,
(select count(*) from act where act.birth_date = pd.d and act.place = pd.place) as births,
(select count(*) from act where act.death_date = pd.d and act.place = pd.place) as deaths
from (select place, birth_date as d from act union
select place, death_date from act
) pd;
You can then put this into population_growth
by doing:
INSERT INTO Population_Growth(place, time, number_of_births, number_of_deaths)
select place, d,
(select count(*) from act where act.birth_date = pd.d and act.place = pd.place) as births,
(select count(*) from act where act.death_date = pd.d and act.place = pd.place) as deaths
from (select place, birth_date as d from act union
select place, death_date from act
) pd;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.