pulling the following information with a single select [duplicate] - mysql

This question already has answers here:
Count columns according to dates in SQL
(3 answers)
Closed 8 years ago.
I have the following information in my mysql table
Eventually I would like to make a select that would count the entries for a page_id but just for the specific date , meaning I would like to have the following output:
84 - 7 - 09/23/2013
85 - 4 - 09/23/2013
84 - 1 - 09/24/2013
Can it be done in a single select ?

select page_id, count(*), date
from table_name
group by page_id, date

SELECT page_id, count(page_id), date
FROM table
GROUP BY page_id, date
You need to SELECT the 3 fields. Then you need to COUNT one of those (page_id) since you need to count how many repetitions you got. And the last step, for the query to run (and also to make sense) is to GROUP BY the other 2 fields.
Hope you get a clearer idea on how to query the table.

Related

I need to choose the row with same user_id,and raised time which has has greater duration than the previous duplicate value [duplicate]

This question already has answers here:
MySQL query, MAX() + GROUP BY
(7 answers)
Closed 7 months ago.
user_id
raisedtime
cleartime
duration
3_dsr
2022-07-22
2022-07-23
20
3_dsr
2022-07-22
2022-07-24
22
1_DSR
2022-07-24
2022-07-26
21
1_DSR
2022-07-24
2022-07-26
21
I tried this and cant get further with this
select user_id,raisedtime, max(duration) as md
from alarms
group by user_id,raisedtime
having count(*)>1
NOTE
You need to extract the table with below query and identify max duration using python pandas. IN SQL you will only get this far. Also, using Max(duration) will give you row with MAXIMUM duration but it cannot guarantee, it will check if it MAX from previous occurrence of user_id and raisedtime pair.
SELECT user_id, raisedtime, duration as md
FROM alarms
GROUP BY user_id, raisedtime
HAVING count(*)>1
You can use python pandas library to identify MAX for pair of unique user_id and raisetime pair and check if that occurrence is greater than earlier occurrence of same pair.

How can I get the MAX(total) with the whole row and its data? MySQL [duplicate]

This question already has answers here:
MySQL: Select top n max values?
(3 answers)
Closed 1 year ago.
I have table with billings have some of columns like (order number, date, and total) I want to get the MAX(total) in specific date with whole row and its data.
Here is the table:
Click here to show the picture
I have already wrote some line in MySQL but it came with some mistake
SELECT * FROM biling WHERE total=(SELECT MAX(total) FROM biling WHERE date='2021-10-26')
Let's say the part two of this query equals 50 it came with the maximum billing indeed but what if there is more than one row with the same of MAX(total) in this day I Just want to calculate just one row.
I hope you getting my point guys. And thanks in advance.
Adding LIMIT 1 to the end of your select seems to be the solution.
Updated!
The simple solution that is needed is
SELECT * FROM biling WHERE date='2021-10-26' ORDER BY total DESC LIMIT 1

select from table by option into two different variables (php & mysql) [duplicate]

This question already has answers here:
MySQL: SUM of a column based on a value of another column
(2 answers)
Closed 1 year ago.
Im trying to select from a single table with two options and output to two different variables.
My old query was to calculate all deposits, and then run another one to calculate all withdrawals but this takes long time because there are many rows.
I want to get all amount of Deposits & get all amount of withdrawals in just one query (in php).
Example: SELECT SUM(amount) FROM transactions WHERE cId=10 ...help here...
output will be:
Variable 1: 5500 (deposits)
Variable 2: 2500 (withdrawals)
I solved it like this:
SELECT (SELECT SUM(amount) FROM transactions WHERE cId=1 AND tStatus!='Withdrawal') AS deposits,
(SELECT SUM(amount) FROM transactions WHERE cId=1 AND tStatus='Withdrawal') AS withdrawals
Final result:

Group avarage actions per day in a SQL query for MySQL [duplicate]

This question already has answers here:
MySQL count and group by day
(5 answers)
Closed 5 years ago.
i have a table which has fields like this:
id | created | action_type | value
Created is a timestamp in a varchar field. What i need to do is to calculate the average actions per day in one query. All need to be grouped by one day.
so i need something like this
2017-10-01 : 15
2017-10-02 : 20
How can i achieve this?
:)
Presumably, you just want a group by:
select date(created), count(*)
from t
group by date(created);

MYSQL SELECT * doesn't work with GROUP BY and HAVING [duplicate]

This question already has answers here:
Fetch the rows which have the Max value for a column for each distinct value of another column
(35 answers)
Closed 8 years ago.
SELECT * FROM or_mail
GROUP BY campaign_id
HAVING date_time = MAX(date_time);
SELECT campaign_id, date_time FROM or_mail
GROUP BY campaign_id
HAVING date_time = MAX(date_time);
The 1st query returns 13 records. The 2nd returns 35.
Why are records missing from the first query!? Why should what I'm selecting matter at all?
This is your query:
SELECT campaign_id, date_time
FROM or_mail
GROUP BY campaign_id
HAVING date_time = MAX(date_time);
You are aggregating by campaign_id. That means that the results will have one row per campaign_id. What date_time goes on the row? Well, an arbitrary value from one of the matching rows. Just one value, an arbitrary one. The same is true of the having clause. In other words, the query does not do what you expect it to do.
Whether you know it or not, you are using a group by extension that is particular to MySQL (you can read the documentation here). The documentation specifically warns against using the extension this say. (There would be no problem if date_time were the same on all rows with the same campaign_id, but that is not the case.)
The following is the query that you actually want:
SELECT campaign_id, date_time
FROM or_mail om
WHERE not exists (select 1
from or_mail om2
where om2.campaign_id = om.campaign_id and
om2.date_time > date_time
);
What this says is: Return results from all rows in or_mail with the property that there is no larger date_time with the same campaign_id.
HAVING date_time = MAX(date_time);
It shouldn't... Did you wait a while before running second query? If so, then a bunch of records could have been created.