I have an issue with mysql queries,
in fact, I have to build a web page with charts, and I need to fetch the data from the database as follow:
1- Get the total of data received per month, per centre ( it mean country ) for the current year,
2- Get the total of data wich has NOT been done per month, per centre for the current year,
3- Get the total of data which has not been done AND the date exceed 20 days , per month, per centre for the current year.
So, all in all, I'm able to fetch the data for all thoses queries, no problem about that.
The issue I am facing is, I need those queries embedded into 1 single query returning me a table like that:
| monthname | total | totalNotDone | totalExceed20Days |
| January | 52 | 3 | 1 |
| February | 48 | 4 | 0 |
| March | 54 | 1 | 3 |
etc.
Here is a sqlfiddle showing the issue :
edited : http://sqlfiddle.com/#!2/8cc9c/1
Any help would be greatly appreciated guys, I'm really stuck.
Your basic queries are fine. What you need to do is treat each of them as a virtual table, and LEFT JOIN them together. Then your toplevel SELECT can choose the appropriate values for your overall table.
SELECT afftotal.date,
afftotal.centre_id,
afftotal.total AS total,
af20.total AS total_20,
afempty.total AS total_empty
FROM (
/* select total of affaires per month and per centre for this year */
select month(aff_date) AS `date`,
centre_id,
count(*) AS `total`
from affaires
where year(aff_date) = 2014
group by month(aff_date), centre_id
) AS afftotal
LEFT JOIN (
/* select total of affaires per month and per centre for this year where the affaire has been done
before 20 days. */
select month(`affaires`.`aff_date`) AS `date`,
centre_id,
count(*) AS `total`
from `affaires`
where year(`affaires`.`aff_date`) = 2014
and DATEDIFF(`affaires`.`aff_date`, `affaires`.`date_creation_pdl`) > 20
group by monthname(`affaires`.`aff_date`), centre_id
) AS af20 ON afftotal.date = af20.date
AND afftotal.centre_id = af20.centre_id
LEFT JOIN (
/* select total of affaires where the date_creation_pdl is empty */
select month(affaires.aff_date) as `date`,
centre_id,
count(*) as total
from affaires
where date_creation_pdl is null
and year(affaires.aff_date) = 2014
group by monthname(affaires.aff_date)
) AS afempty ON afftotal.date = afempty.date
AND afftotal.centre_id = afempty.centre_id
ORDER BY afftotal.centre_id, afftotal.date
http://sqlfiddle.com/#!2/d563e/24/0
Notice that this is summarizing both by centre_id and date, so you can get all the centre_id values in a single query.
Notice also that the ORDER BY clause is placed at the end of the whole query.
What you have are three subqueries, three virtual tables if you will, each with three columns: a date, a centre_id, and a total. You LEFT JOIN them together ON two of those columns.
I had to muck around with your queries a bit to make them have similar column names and column data formats, so the LEFT JOIN operations have a regular structure.
Related
I have table as following:
hours | ... | task_assigned | task_deadline | task_completion
----------------------------------------------------------------
123 | ... | 2019-08-01 | - | -
234 | ... | - | 2018-08-01 | 2019-08-01
145 | ... | 2017-08-01 | 2017-08-01 | 2018-01-01
I want to calculate total hours for each year, i.e. grouping by year.
Currently I'm only taking into account task_completion field.
If there's no value in task_completion field, the record is not included in SUM calculation.
To elaborate further, say for year 2019, row 1 and 1 both should be considered. Hence the total hours should be 123 + 234 = 357.
And for year 2018, row 2 and 3.
Similarly, for year 2017, row 3.
SELECT YEAR(task_completion) as year, ROUND(SUM(total_hours), 2) as hours
FROM task
GROUP BY year
HAVING year BETWEEN '$year_from' AND '$year_to'
The resultset:
year | hours
--------------------
2017 | <somevalue>
2018 | <somevalue>
2019 | <somevalue>
How can I include other two date fields too?
You want to consider each row once for each of its years. Use UNION to get these years:
select year, round(sum(total_hours), 2) as hours
from
(
select year(task_assigned) as year, total_hours from task
union
select year(task_deadline) as year, total_hours from task
union
select year(task_completion) as year, total_hours from task
) years_and_hours
group by year
having year between $year_from and $year_to
order by year;
If you want to consider a row with one year twice or thrice also as often in the sum, then change UNION to UNION ALL.
Basically, you want to unpivot the data. I will assume that the - represents a NULL value and your dates are real dates.
select year(dte) as year, sum(total_hours) as hours
from ((select task_assigned as dte, total_hours
from task
) union all
(select task_deadline, total_hours
from task
) union all
(select task_completion, total_hours
from task
)
) d
where dte is not null
group by year(dte)
order by year(dte);
Based on your sample data, the round() is not necessary so I removed it.
If you want to filter for particular years, the filtering should be in a where clause -- so it filters the data before aggregation.
Change the where to:
where year(dte) >= ? and year(dte) <= ?
or:
where dte >= ? and dte <= ?
to pass in the dates.
The ? are for parameter placeholders. Learn how to use parameters rather than munging query strings.
This answer is no langer valid with the updated request.
If I understand correctly, you want to use task_assigned if the task_completion is still null. Use COALEASCE for this.
SELECT
YEAR(COALESCE(task_completion, task_assigned)) as year,
ROUND(SUM(total_hours), 2) as hours
FROM task
GROUP BY year
HAVING year BETWEEN $year_from AND $year_to
ORDER BY year;
(I don't think you actually want to use task_deadline, too, for how could a task get completed before getting assigned first? If such can occur, then include it in the COALESCE expression. Probably: COALESCE(task_completion, task_assigned, task_deadline)` then.)
I want to sum the Amount depend on the Type field.
Example: type:water sum of all the Amount related to water.
And i want to put the in a table depend on the year.
water=>sum of Amount =>year:2017
table:
-------
year | Type | Amount
2017 | water | 200
2018 | water | 300
My query:
SELECT SUM(Amount_expenses_table), year(Date_expenses_table) FROM
bacci.expenses_table WHERE year(Date_expenses_table)='2017'and
Type_expenses_table = 'water' GROUP BY monthname(date_expenses_table);
I want to get not only water many things and put the result in vb.net, so i thing to make a table for this and put the sum in the table like above
Below query will meet your requirement:
SELECT year(Date_expenses_table) , Type_expenses_table, SUM(Amount_expenses_table) FROM
bacci.expenses_table WHERE year(Date_expenses_table)='2017' GROUP BY year(Date_expenses_table) , Type_expenses_table;
I have a table of recipes, and I want to show a weekly value for each of them. The values are votes cast for them. My problem is that I want to make an excel-like table with all available fridays on my db, add a column for each recipe, and put it's value for the friday on that column, if any value exists.
Now apparently the easiest join doesn't work so I wrote two queries: one to get all ids for my recipes and one for the values to show. The first (MySql) query is just a select id from recipes, the second is like this:
select d.date,perc from
(SELECT date FROM weekly where YEAR(date)=2014 group by date) as d
left join weekly on d.date = weekly.date and weekly.id_rec= :idrec
Any idea how to merge those two queries? Running two queries makes everything slow down, but when I tried to merge them I didn't get the correct results.
Data:
sql fiddle
The result should be something like:
Dates | Recipe A | Recipe B | ...
Date 1 | 0.005 | 0.11 |
Date 2 | 0 | 0 |
Date 3 | 0 | 0.1 |
Note that Date 2 doesn't exist for Recipe A and B, but for some other do.
You should be able to merge the two queries like this:
SELECT recipes.id, votes.date, votes.perc FROM recipes
RIGHT JOIN
(select weekly.id_rec, d.date, perc from
(SELECT weekly.id_rec, date FROM weekly where YEAR(date) = 2014 group by date) as d left join weekly on d.date = weekly.date) as votes
ON votes.id_rec = recipes.id
SQL Fiddle
I have 1 table with similar data:
CustomerID | ProjectID | DateListed | DateCompleted
123456 | 045 | 07-29-2010 | 04-03-2011
123456 | 123 | 10-12-2011 | 11-30-2011
123456 | 157 | 12-12-2011 | 02-10-2012
123456 | 258 | 06-07-2011 | NULL
Basically, a customer contacts us, we get a project on our list, and we mark it completed when we're done with it.
What I'm after is a simple (you'd think, at least) count of all projects, with expected output like below:
YEAR | TotalListed | TotalCompleted
2010 | 1 | 0
2011 | 3 | 2
2012 | 0 | 1
However, my query below - because of the join - isn't showing 2012's count, because there's been no listed project for 2012. However, I can't really reverse the query, as then 2010's count wouldn't show up (since nothing was completed in 2010).
I'm open to any suggestions, or tips like how to do this. I've pondered a temp table, is that the best way to go? I'm open to anything that gets me what I need!
(If the code looks familiar, ya'll helped me get the subquery made! MySQL Subquery with main query data variable)
SELECT YEAR(p1.DateListed) AS YearListed, COUNT(p1.ProjectID) As Listed, PreQuery.Completed
FROM(
SELECT YEAR(DateCompleted) AS YearCompleted, COUNT(ProjectID) AS Completed
FROM projects
WHERE CustomerID = 123456 AND DateListed >= DATE_SUB(Now(), INTERVAL 5 YEAR)
GROUP BY YEAR(DateCompleted)
) PreQuery
RIGHT OUTER JOIN projects p1 ON PreQuery.YearCompleted = YEAR(p1.DateListed)
WHERE CustomerID = 123456 AND DateListed >= DATE_SUB(Now(), INTERVAL 5 YEAR)
GROUP BY YearListed
ORDER BY p1.DateListed
After reviewing your table, query, and expected results - I believe I have found a more-revised query to suit your needs. It is a fairly-full rewrite of your existing query though, but I've tested it with your given data and received the same results you want/expect:
SELECT
years.`year`,
SUM(IF(YEAR(DateListed) = years.`year`, 1, 0)) AS TotalListed,
SUM(IF(YEAR(DateCompleted) = years.`year`, 1, 0)) AS TotalCompleted
FROM
projects
LEFT JOIN (
SELECT DISTINCT `year` FROM (
SELECT YEAR(DateListed) AS `year` FROM projects
UNION SELECT YEAR(DateCompleted) AS `year` FROM projects WHERE DateCompleted IS NOT NULL
) as year_inner
) AS years
ON YEAR(DateListed) = `year`
OR YEAR(DateCompleted) = `year`
WHERE
CustomerID = 123456 AND DateListed >= DATE_SUB(Now(), INTERVAL 5 YEAR)
GROUP BY
years.`year`
ORDER BY
years.`year`
To explain, we should start with the inner query (aliased as year_inner). It selects a full list of years in the DateListed and DateCompleted columns and then selects a DISTINCT list of those to create the years alias sub-query. This sub-query is used to get a full list of "years" that we want data for. Doing it this way, opposed to a sub-query with counts and groupings will allow you to only have to define the WHERE clause on the outermost query (though, if efficiency becomes an issue with thousands and thousands of records, you could always add a WHERE clause to the inner query too; or an index to the date columns).
After we've built our inner queries, we join the projects table on the results with a LEFT JOIN for the DateListed or DateCompleted's YEAR() value - which will allow us to bring back null columns too!
For the field selections, we use the year column from our inner query to assure that we get a full list of years to display. Then, we compare the current row's DateListed & DateCompleted YEAR() value to the current year; if they're equal, add 1 - else add 0. When we GROUP BY year, our SUM() will count all of the 1's for that year for each column and give you the output you want (hopefully, of course =P).
I posted about this a few weeks ago, but I don't think I asked the question clearly because the answers I got were not what I was looking for. I think it's best to start again.
I'm trying to query a database to retrieve the number of unique entries over time. The data looks something like this:
Day | UserID
1 | A
1 | B
2 | B
3 | A
4 | B
4 | C
5 | D
I'd like the query result to look this this
Time Span | COUNT(DISTINCT UserID)
Day 1 to Day 1 | 2
Day 1 to Day 2 | 2
Day 1 to Day 3 | 2
Day 1 to Day 4 | 3
Day 1 to Day 5 | 4
If I do something like
SELECT COUNT(DISTINCT `UserID`) FROM `table` GROUP BY `Day`
, the distinct counts will not consider user IDs of previous days.
Any Ideas? The data set I'm using is quite large, so multiple-queries and post processing takes a long time (that's how I'm currently doing it).
Thanks
You can use a subquery
Sample table
create table visits (day int, userid char(1));
insert visits values
(1,'a'),
(1,'b'),
(2,'b'),
(3,'a'),
(4,'b'),
(4,'c'),
(5,'d');
The query
select d.day, (select count(distinct userid) from visits where day<=d.day)
from (select distinct day from visits) d
how about something like this:
SELECT Count(UserID), Day
FROM
(SELECT Count(UserID) as Logons, UserID, Day
FROM yourDailyLog
GROUP BY Day, UserID)
GROUP BY Day
The inner select should eliminate the duplicate visits by a same user on a given day.
Stay away from DISTINCT. It is usually a questionable approach to almost any SQL problem.
Wait: I see now that you want the time period to increase over time. That makes things a little trickier. Why don't you aggregate the rest of this information in code rather than doing it all through sql?