Hi I have a database with a column containing email addresses and a second column containing category and a third column containing date.
What I want to do is count the number of unique email addresses in category 'A' between multiple date ranges. So I have this:
SELECT COUNT(DISTINCT email) as counter
FROM table
WHERE category = "A" AND date < "2015-12" AND date > "2015-11";
Then I'll do a separate query for the second date range.
NOW HERE'S MY PROBLEM:
If an email address appears in month one, and also in month two it will go on the count for both months because it's unique within the range I'm querying.
How do I create a query that will count the unique email addresses for a year let's say, then count the distinct entries in a month period without including the duplicates?
Thanks!
If you want custom date you can set the value to vars
$custom_date_begin = "2015-01-10";
$custom_date_end = "2015-02-10";
then for vars and the month group by you can use somethings like this
"SELECT COUNT(DISTINCT email) as counter
FROM table
WHERE category = 'A'
AND date >= '$custom_begin_date' AND date <='$custom_end_date'
group by MONTH (date);"
Information provided is not enough for writing adequate query. So I'll try to guess details.
Lets assume that we need to count number of unique emails for each month and category.
The query could be like the following:
SELECT dt, category, COUNT(*) AS cnt
FROM (
SELECT LEFT(`date`,7) AS dt, category, email
FROM table
GROUP BY LEFT(`date`,7), category, email
) x
GROUP BY dt, category
If you have variable date ranges, then you'd better group on daily basis, and then count emails via script for each date range.
Related
I am trying to put together a query so that when I add a record, the database assigns it a sequential number based on how many records there are in the next year, then adding the next number, e.g.
2016/1
2016/2
This is the query I have so far:
CustomQuery("Select Count(*) as count1 from ApplicationRegister where ClientFk='".$values['ClientFk']."' AND ???
What I want is to use the 2nd logic operator to say YEAR(now)=Year(ApplicationDate), where ApplicationDate is a date field within the database.
How do do this?
I am trying to make PHP / MySQL Search Form. When a user wants to search he need to fill 3 input fields: FromDate, ToDate and SucategoryID.
There are 2 tables: Items and ItemsBlockDates
In Table Items, there is info about the item and it is identified by ID.
In Table ItemBlockDates, there is info about a period when Item will not be displayed in search results. It contains: ItemID, FromDate, and Date fields. Also, there is a possibility to define more than one Block Date for one item.
Now, I want to select all Items where FromDate and To Date inputs does not match with any of rows in ItemsBlockDates with same ItemID.
I wrote a query, but there is a problem:
SELECT *
FROM Items
LEFT JOIN ItemBlockDates ON Items.ID = ItemBlockDates.ItemID
WHERE Items.SubcategoryID = :SubcategoryID
AND ItemBlockDates.FromDate NOT BETWEEN CAST(:FromDate AS DATETIME) AND CAST(:ToDate AS DATETIME)
When I run this query it does not display Items where Block Dates are not set, also it displays items where more than one Block Date is set.
I want to select all Items where FromDate and To Date inputs does not
match with any of rows in ItemsBlockDates with same ItemID.
To me, this suggests NOT EXISTS:
SELECT i.*
FROM Items i
WHERE i.SubcategoryID = :SubcategoryID AND
NOT EXISTS (SELECT 1
FROM ItemBlockDates ibd
WHERE i.ID = ibd.ItemID AND
ibd.FromDate BETWEEN CAST(:FromDate AS DATETIME) AND CAST(:ToDate AS DATETIME)
);
If you are passing in a parameter, I'm not sure why you need to convert to a DATETIME. You should be able to pass it in with the correct type.
I'm making a restaurant web manager.
I need to get the total of all the months of a year.
Do I need to make a query for each month, or there is a way to make it all in one query with a FOR (loop).
Each month has different orders with different values which has to been summed to the month to show them separatedly.
I can't get a clue of how to do it if it's not with each month one by one.
I think this way may get it.
for ($sum=0; $sum < 12; $sum++) {
$result = mysqli_query($mysqli,"SELECT table_number, SUM(price) as totalprice FROM orders WHERE MONTH(dates) = '$sum' && YEAR(dates) = '$year' GROUP BY table_number");
}
I have a mysql database.
The orders are placed on a table "orders" whith following structure:
.id(int)
.name(varchar)
.price(int)
.table_number(int)
.date (date / Y.m.d)
I assume you have a MySQL database. And I assume you have one table where all orders are stored. I assume you have a field value (double) and a field time (timestamp). Try
SELECT
YEAR(`time`), MONTH(`time`), SUM(`value`)
FROM `table`
GROUP BY YEAR(`time`), MONTH(`time`)
ORDER BY YEAR(`time`) ASC, MONTH(`time`) ASC;
Also check the MySQL manual:
https://dev.mysql.com/doc/refman/5.7/en/group-by-functions.html
https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html
Make a table of whatever you want to sum. Add a column in it of the data you want to sum for each month and add another column in the table which stores the month every row belongs to.
Now query this table with php to get all the rows of the table and make an arrray of all the months, check which month every row belongs to and add the data you want to sum to that specific months total summation.
Like this, you can easily find the sum for each month and can achieve what you are trying to do.
I have been all over the internet to find answer for this, but I can't find any. (I might be asking incorrectly?)
I have a table which looks like this:
I have been using the query:
SELECT title, date, SUM(money)
FROM payments
WHERE username = '$username_n' and type=0
GROUP BY title
It does the trick, and it groups the identical titles and sum up their values
But as you can see, it only gives me ONE date, and I would like to be able to make a slider, where I can sort the date from the date, but I can't do that if it creates only one date for each grouped title.
How can I create multiple dates, but still get rid of the duplicated titles?
Try this:
SELECT title, date, SUM(money) FROM payments
WHERE username = '$username_n' and type=0 GROUP BY title, date(`date`);
It is a little weird with fields in the field list that are not aggregated or part of the grouping. As you had before with the date field:
SELECT title, date, SUM(money) FROM payments
WHERE username = '$username_n' and type=0
GROUP BY title
You group by title, and aggregate the money field, all good. The date field is not part of grouping or aggregation. What mysql is doing, it will just take the field value for the date field in the aggregation. Thats why you only see one date value in your result. As code-monk explained you need to use date field in the grouping. In MS SQL Server your query would actually error our, as MS SQL Server doesnt allow fields that are not part of grouping or aggregated, which I think is very good.
Below query will give date values in comma separated manner. You can use PHP/server side code to explode those by comma and create a list/array and return it to client side i.e. JavaScript. Use javascript to sort the list.
SELECT title, group_concat(date) as `dates`, SUM(money)
FROM payments
WHERE username = '$username_n' and type=0
GROUP BY title
I need to count employ's salary which depends on the number of rows added per months (with a unique picture_id, so if the same picture ID appears twice it counts only one time) - so as I understand I need to group by both picture_id and by months.
It works perfectly fine if I do it separately i.e.
group by MONTH(TimeAdded)
or
GROUP BY picture_id
Here is the code
$data_qqq = mysql_query("SELECT MONTHNAME(TimeAdded), COUNT(MONTH(TimeAdded)) FROM Picture_Tag WHERE UserID = $user_id GROUP BY MONTH(TimeAdded)");
but this counts number of entries added each months even if there are 20 rows with the same picture_id.
Now if I try
$data_qqq = mysql_query("SELECT MONTHNAME(TimeAdded), COUNT(MONTH(TimeAdded)) FROM Picture_Tag WHERE UserID = $user_id GROUP BY MONTH(TimeAdded),**picture_id**");
I get a very strange messed up results...
Any idea how to count number of rows added each months with the unique picture_id?
Without samples it can be difficult to visualize the data, but if you wish to count the number of unique picture_id's each month then use the COUNT(DISTINCT picture_id). But also note when grouping by "month" you most probably also need to group by year otherwise the result could be badly skewed. (Alternatively use a where clause to only the select the single year you are interested in.)
Try this:
SELECT
YEAR(TimeAdded)
, MONTH(TimeAdded)
, COUNT(DISTINCT picture_id)
FROM Picture_Tag
WHERE UserID = $USER_ID
GROUP BY
YEAR(TimeAdded)
, MONTH(TimeAdded)