Mysql query multiple select statements output on multiple columns









up vote
2
down vote

favorite












Table



+------+-------+------------------+
|CLIENT| VALUE | DATETIME |
+------+-------+------------------+
| A | 1 | 2018-11-10 09:00 |
| B | 1 | 2018-11-10 09:00 |
| C | 1 | 2018-11-10 09:00 |
| D | 2 | 2018-11-10 08:00 |
| E | 2 | 2018-11-10 08:00 |
| F | 3 | 2018-11-10 08:00 |
| A | 1 | 2018-11-10 07:00 |
| B | 2 | 2018-11-10 07:00 |
| C | 2 | 2018-11-10 07:00 |
| D | 3 | 2018-11-10 06:00 |
| E | 1 | 2018-11-10 06:00 |
| F | 2 | 2018-11-10 06:00 |
| A | 1 | 2018-11-08 08:00 |
| B | 2 | 2018-11-08 08:00 |
| C | 2 | 2018-11-08 08:00 |
| D | 1 | 2018-11-08 08:00 |
| E | 1 | 2018-11-08 07:00 |
| F | 2 | 2018-11-08 07:00 |


I'm newbie to mysql, and I'm in trouble with this query.



I have only one table named "table" with three columns.



This table records many data every day at different time from a specific set of client A,B,C,D,E,F



With one query I need to create new table with one row for each client and with the following 4 columns:



  1. first column should contain the newest value recordered in the table for each client

  2. second column should contain the percentage of time the value is equal to 1 for each client during the last 24 hours

  3. third column should contain the percentage of time the value is equal to 1 for each client during the last 7 days

  4. as previous column but during the last 30 days

I hope someone can help me.



What I would like to receive:



+------+-------------+-----------+--------------+--------------+
|CLIENT| NEWEST VALUE| LAST 24 H | LAST 7 DAYS | LAST 30 DAYS |
+------+-------------+-----------+--------------+--------------+
| A | 1 | 100% | 100% | ... |
| B | 1 | 50% | 66% | ... |
| C | 1 | 50% | 33% | ... |
| D | 2 | 0% | 33% | ... |
| E | 2 | 50% | 66% | ... |
| F | 3 | 0% | 0% | ... |


This piece of code works fine to create the "NEWST VALUE" column



SELECT
client,
value,
max(datetime)
FROM
table
GROUP BY
client;


and this one create the "LAST 24 H" column



SELECT
client,
count(if(value = 1,1, null))/count(value),
FROM
table
WHERE
date(datetime) < CURRENT_DATE() - interval 1 day
GROUP BY
repository_name;


but I'm not able to put all the output together in one new table










share|improve this question























  • If the query is still not resolved, can you please provide a db-fiddle.com OR, Create Table and Insert Into statements would be helpful, so that we can reproduce the case
    – Madhur Bhaiya
    Nov 10 at 17:57














up vote
2
down vote

favorite












Table



+------+-------+------------------+
|CLIENT| VALUE | DATETIME |
+------+-------+------------------+
| A | 1 | 2018-11-10 09:00 |
| B | 1 | 2018-11-10 09:00 |
| C | 1 | 2018-11-10 09:00 |
| D | 2 | 2018-11-10 08:00 |
| E | 2 | 2018-11-10 08:00 |
| F | 3 | 2018-11-10 08:00 |
| A | 1 | 2018-11-10 07:00 |
| B | 2 | 2018-11-10 07:00 |
| C | 2 | 2018-11-10 07:00 |
| D | 3 | 2018-11-10 06:00 |
| E | 1 | 2018-11-10 06:00 |
| F | 2 | 2018-11-10 06:00 |
| A | 1 | 2018-11-08 08:00 |
| B | 2 | 2018-11-08 08:00 |
| C | 2 | 2018-11-08 08:00 |
| D | 1 | 2018-11-08 08:00 |
| E | 1 | 2018-11-08 07:00 |
| F | 2 | 2018-11-08 07:00 |


I'm newbie to mysql, and I'm in trouble with this query.



I have only one table named "table" with three columns.



This table records many data every day at different time from a specific set of client A,B,C,D,E,F



With one query I need to create new table with one row for each client and with the following 4 columns:



  1. first column should contain the newest value recordered in the table for each client

  2. second column should contain the percentage of time the value is equal to 1 for each client during the last 24 hours

  3. third column should contain the percentage of time the value is equal to 1 for each client during the last 7 days

  4. as previous column but during the last 30 days

I hope someone can help me.



What I would like to receive:



+------+-------------+-----------+--------------+--------------+
|CLIENT| NEWEST VALUE| LAST 24 H | LAST 7 DAYS | LAST 30 DAYS |
+------+-------------+-----------+--------------+--------------+
| A | 1 | 100% | 100% | ... |
| B | 1 | 50% | 66% | ... |
| C | 1 | 50% | 33% | ... |
| D | 2 | 0% | 33% | ... |
| E | 2 | 50% | 66% | ... |
| F | 3 | 0% | 0% | ... |


This piece of code works fine to create the "NEWST VALUE" column



SELECT
client,
value,
max(datetime)
FROM
table
GROUP BY
client;


and this one create the "LAST 24 H" column



SELECT
client,
count(if(value = 1,1, null))/count(value),
FROM
table
WHERE
date(datetime) < CURRENT_DATE() - interval 1 day
GROUP BY
repository_name;


but I'm not able to put all the output together in one new table










share|improve this question























  • If the query is still not resolved, can you please provide a db-fiddle.com OR, Create Table and Insert Into statements would be helpful, so that we can reproduce the case
    – Madhur Bhaiya
    Nov 10 at 17:57












up vote
2
down vote

favorite









up vote
2
down vote

favorite











Table



+------+-------+------------------+
|CLIENT| VALUE | DATETIME |
+------+-------+------------------+
| A | 1 | 2018-11-10 09:00 |
| B | 1 | 2018-11-10 09:00 |
| C | 1 | 2018-11-10 09:00 |
| D | 2 | 2018-11-10 08:00 |
| E | 2 | 2018-11-10 08:00 |
| F | 3 | 2018-11-10 08:00 |
| A | 1 | 2018-11-10 07:00 |
| B | 2 | 2018-11-10 07:00 |
| C | 2 | 2018-11-10 07:00 |
| D | 3 | 2018-11-10 06:00 |
| E | 1 | 2018-11-10 06:00 |
| F | 2 | 2018-11-10 06:00 |
| A | 1 | 2018-11-08 08:00 |
| B | 2 | 2018-11-08 08:00 |
| C | 2 | 2018-11-08 08:00 |
| D | 1 | 2018-11-08 08:00 |
| E | 1 | 2018-11-08 07:00 |
| F | 2 | 2018-11-08 07:00 |


I'm newbie to mysql, and I'm in trouble with this query.



I have only one table named "table" with three columns.



This table records many data every day at different time from a specific set of client A,B,C,D,E,F



With one query I need to create new table with one row for each client and with the following 4 columns:



  1. first column should contain the newest value recordered in the table for each client

  2. second column should contain the percentage of time the value is equal to 1 for each client during the last 24 hours

  3. third column should contain the percentage of time the value is equal to 1 for each client during the last 7 days

  4. as previous column but during the last 30 days

I hope someone can help me.



What I would like to receive:



+------+-------------+-----------+--------------+--------------+
|CLIENT| NEWEST VALUE| LAST 24 H | LAST 7 DAYS | LAST 30 DAYS |
+------+-------------+-----------+--------------+--------------+
| A | 1 | 100% | 100% | ... |
| B | 1 | 50% | 66% | ... |
| C | 1 | 50% | 33% | ... |
| D | 2 | 0% | 33% | ... |
| E | 2 | 50% | 66% | ... |
| F | 3 | 0% | 0% | ... |


This piece of code works fine to create the "NEWST VALUE" column



SELECT
client,
value,
max(datetime)
FROM
table
GROUP BY
client;


and this one create the "LAST 24 H" column



SELECT
client,
count(if(value = 1,1, null))/count(value),
FROM
table
WHERE
date(datetime) < CURRENT_DATE() - interval 1 day
GROUP BY
repository_name;


but I'm not able to put all the output together in one new table










share|improve this question















Table



+------+-------+------------------+
|CLIENT| VALUE | DATETIME |
+------+-------+------------------+
| A | 1 | 2018-11-10 09:00 |
| B | 1 | 2018-11-10 09:00 |
| C | 1 | 2018-11-10 09:00 |
| D | 2 | 2018-11-10 08:00 |
| E | 2 | 2018-11-10 08:00 |
| F | 3 | 2018-11-10 08:00 |
| A | 1 | 2018-11-10 07:00 |
| B | 2 | 2018-11-10 07:00 |
| C | 2 | 2018-11-10 07:00 |
| D | 3 | 2018-11-10 06:00 |
| E | 1 | 2018-11-10 06:00 |
| F | 2 | 2018-11-10 06:00 |
| A | 1 | 2018-11-08 08:00 |
| B | 2 | 2018-11-08 08:00 |
| C | 2 | 2018-11-08 08:00 |
| D | 1 | 2018-11-08 08:00 |
| E | 1 | 2018-11-08 07:00 |
| F | 2 | 2018-11-08 07:00 |


I'm newbie to mysql, and I'm in trouble with this query.



I have only one table named "table" with three columns.



This table records many data every day at different time from a specific set of client A,B,C,D,E,F



With one query I need to create new table with one row for each client and with the following 4 columns:



  1. first column should contain the newest value recordered in the table for each client

  2. second column should contain the percentage of time the value is equal to 1 for each client during the last 24 hours

  3. third column should contain the percentage of time the value is equal to 1 for each client during the last 7 days

  4. as previous column but during the last 30 days

I hope someone can help me.



What I would like to receive:



+------+-------------+-----------+--------------+--------------+
|CLIENT| NEWEST VALUE| LAST 24 H | LAST 7 DAYS | LAST 30 DAYS |
+------+-------------+-----------+--------------+--------------+
| A | 1 | 100% | 100% | ... |
| B | 1 | 50% | 66% | ... |
| C | 1 | 50% | 33% | ... |
| D | 2 | 0% | 33% | ... |
| E | 2 | 50% | 66% | ... |
| F | 3 | 0% | 0% | ... |


This piece of code works fine to create the "NEWST VALUE" column



SELECT
client,
value,
max(datetime)
FROM
table
GROUP BY
client;


and this one create the "LAST 24 H" column



SELECT
client,
count(if(value = 1,1, null))/count(value),
FROM
table
WHERE
date(datetime) < CURRENT_DATE() - interval 1 day
GROUP BY
repository_name;


but I'm not able to put all the output together in one new table







mysql sql mariadb multiple-columns multiple-select






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 days ago









marc_s

565k12610911243




565k12610911243










asked Nov 10 at 16:06









Luca Folin

132




132











  • If the query is still not resolved, can you please provide a db-fiddle.com OR, Create Table and Insert Into statements would be helpful, so that we can reproduce the case
    – Madhur Bhaiya
    Nov 10 at 17:57
















  • If the query is still not resolved, can you please provide a db-fiddle.com OR, Create Table and Insert Into statements would be helpful, so that we can reproduce the case
    – Madhur Bhaiya
    Nov 10 at 17:57















If the query is still not resolved, can you please provide a db-fiddle.com OR, Create Table and Insert Into statements would be helpful, so that we can reproduce the case
– Madhur Bhaiya
Nov 10 at 17:57




If the query is still not resolved, can you please provide a db-fiddle.com OR, Create Table and Insert Into statements would be helpful, so that we can reproduce the case
– Madhur Bhaiya
Nov 10 at 17:57












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










You can use conditional aggregation. Assuming pre-8.0 MySQL, only the most recent value is really tricky. Here is one approach:



select t.client,
max(case when t.datetime = c.maxdt then t.value end) as most_recent_value,
avg(case when t.datetime >= now() - interval 1 day
then (t.value = 1)
end) as last_day_percentage,
avg(case when t.datetime >= now() - interval 7 day
then (t.value = 1)
end) as last_7day_percentage,
avg(case when t.datetime >= now() - interval 30 day
then (value = 1)
end) as last_30day_percentage
from t join
(select t.client, max(t.datetime) as maxdt
from t
group by t.client
) c
on c.client = t.client
group by t.client;


Note that this logic uses a MySQL extension where boolean values are treated as numbers in a numeric context, with 1 for true and 0 for false.



The average produces "0" or "1" for the time period in question, with NULL values for any other record. The avg() function ignores NULL values.






share|improve this answer




















  • thk you so much this solve my problem
    – Luca Folin
    Nov 11 at 9:21










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240792%2fmysql-query-multiple-select-statements-output-on-multiple-columns%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote



accepted










You can use conditional aggregation. Assuming pre-8.0 MySQL, only the most recent value is really tricky. Here is one approach:



select t.client,
max(case when t.datetime = c.maxdt then t.value end) as most_recent_value,
avg(case when t.datetime >= now() - interval 1 day
then (t.value = 1)
end) as last_day_percentage,
avg(case when t.datetime >= now() - interval 7 day
then (t.value = 1)
end) as last_7day_percentage,
avg(case when t.datetime >= now() - interval 30 day
then (value = 1)
end) as last_30day_percentage
from t join
(select t.client, max(t.datetime) as maxdt
from t
group by t.client
) c
on c.client = t.client
group by t.client;


Note that this logic uses a MySQL extension where boolean values are treated as numbers in a numeric context, with 1 for true and 0 for false.



The average produces "0" or "1" for the time period in question, with NULL values for any other record. The avg() function ignores NULL values.






share|improve this answer




















  • thk you so much this solve my problem
    – Luca Folin
    Nov 11 at 9:21














up vote
0
down vote



accepted










You can use conditional aggregation. Assuming pre-8.0 MySQL, only the most recent value is really tricky. Here is one approach:



select t.client,
max(case when t.datetime = c.maxdt then t.value end) as most_recent_value,
avg(case when t.datetime >= now() - interval 1 day
then (t.value = 1)
end) as last_day_percentage,
avg(case when t.datetime >= now() - interval 7 day
then (t.value = 1)
end) as last_7day_percentage,
avg(case when t.datetime >= now() - interval 30 day
then (value = 1)
end) as last_30day_percentage
from t join
(select t.client, max(t.datetime) as maxdt
from t
group by t.client
) c
on c.client = t.client
group by t.client;


Note that this logic uses a MySQL extension where boolean values are treated as numbers in a numeric context, with 1 for true and 0 for false.



The average produces "0" or "1" for the time period in question, with NULL values for any other record. The avg() function ignores NULL values.






share|improve this answer




















  • thk you so much this solve my problem
    – Luca Folin
    Nov 11 at 9:21












up vote
0
down vote



accepted







up vote
0
down vote



accepted






You can use conditional aggregation. Assuming pre-8.0 MySQL, only the most recent value is really tricky. Here is one approach:



select t.client,
max(case when t.datetime = c.maxdt then t.value end) as most_recent_value,
avg(case when t.datetime >= now() - interval 1 day
then (t.value = 1)
end) as last_day_percentage,
avg(case when t.datetime >= now() - interval 7 day
then (t.value = 1)
end) as last_7day_percentage,
avg(case when t.datetime >= now() - interval 30 day
then (value = 1)
end) as last_30day_percentage
from t join
(select t.client, max(t.datetime) as maxdt
from t
group by t.client
) c
on c.client = t.client
group by t.client;


Note that this logic uses a MySQL extension where boolean values are treated as numbers in a numeric context, with 1 for true and 0 for false.



The average produces "0" or "1" for the time period in question, with NULL values for any other record. The avg() function ignores NULL values.






share|improve this answer












You can use conditional aggregation. Assuming pre-8.0 MySQL, only the most recent value is really tricky. Here is one approach:



select t.client,
max(case when t.datetime = c.maxdt then t.value end) as most_recent_value,
avg(case when t.datetime >= now() - interval 1 day
then (t.value = 1)
end) as last_day_percentage,
avg(case when t.datetime >= now() - interval 7 day
then (t.value = 1)
end) as last_7day_percentage,
avg(case when t.datetime >= now() - interval 30 day
then (value = 1)
end) as last_30day_percentage
from t join
(select t.client, max(t.datetime) as maxdt
from t
group by t.client
) c
on c.client = t.client
group by t.client;


Note that this logic uses a MySQL extension where boolean values are treated as numbers in a numeric context, with 1 for true and 0 for false.



The average produces "0" or "1" for the time period in question, with NULL values for any other record. The avg() function ignores NULL values.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 10 at 16:11









Gordon Linoff

743k32285390




743k32285390











  • thk you so much this solve my problem
    – Luca Folin
    Nov 11 at 9:21
















  • thk you so much this solve my problem
    – Luca Folin
    Nov 11 at 9:21















thk you so much this solve my problem
– Luca Folin
Nov 11 at 9:21




thk you so much this solve my problem
– Luca Folin
Nov 11 at 9:21

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240792%2fmysql-query-multiple-select-statements-output-on-multiple-columns%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

Museum of Modern and Contemporary Art of Trento and Rovereto