I have
|name | number |
|andi | 40 |
|irfan | 30 |
|lia | 60 |
|andi | 50 |
|lia | 10 |
|sali | 60 |
I want to display like this order by max
name | number |
andi | 90 |
lia | 70 |
sali | 60 |
irfan | 30 |
My code looks like this:
SELECT name,
COUNT(name)
FROM tablename
GROUP BY name
ORDER BY MAX(number)
And it's not working. The result just work group by name, and not short by max number
name | number |
lia | 70 |
sali | 60 |
andi | 90 |
irfan | 30 |
You have to do Select * from tablename Order By Number DESC
You want sum() not count():
SELECT name, SUM(number)
FROM tablename
GROUP BY name
ORDER BY SUM(number) DESC;
SELECT name,
sum(number) as score
FROM Test2
GROUP BY name
ORDER BY score DESC
What you have to do is
- Sum the column 'number'
- set it as score
- order it by score DESC
Hope this help you
Related
Currently I am using this: SELECT ident,COUNT(*) FROM sales GROUP BY ident order by COUNT(*) DESC LIMIT 3.
I use that to group each ident from sales table, and count the number of rows for each ident, and limit this to 3 rows. Now I want to only select the rows that were added the current month.
This is how the table looks like, sales table:
| ID | ident | prdnr | transfer |
| 1 | HD762 | 7362781 | 2020-08-10 16:25:26 |
| 2 | JJ313 | 4563456 | 2020-08-08 16:25:26 |
| 3 | HD762 | 4363453 | 2020-08-08 16:25:26 |
| 4 | JJ313 | 2326256 | 2020-08-08 16:25:26 |
| 5 | HD762 | 8356345 | 2020-08-07 16:25:26 |
| 6 | JJ844 | 3473563 | 2020-08-07 16:25:26 |
I think this should be the correct query:
SELECT ident,COUNT(*) FROM sales WHERE MONTH(transfer) = MONTH(CURRENT_DATE()) AND YEAR(transfer) = YEAR(CURRENT_DATE()) GROUP BY ident order by COUNT(*) DESC LIMIT 3
Somehow this does not gives me the correct count. What am I doing wrong?
Based on your requirement , a simple way is getting the MySQL current time and subtract a month to get the values
The code :-
SELECT ident,COUNT() FROM sales WHERE transfer > date_sub(now(),
interval 1 month) GROUP BY ident order by COUNT() DESC LIMIT 3;
The output from the query
HD762 3
JJ313 2
JJ844 1
Please let me know if this is the expected result you're looking for.
Reference :-
https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html
I'm having a hard time getting results from a DB the way I need them, this is my table:
+----+---------------------+---------------------+-----------+------------+--------+
| id | opendate | closedate | openprice | closeprice | sameid |
+----+---------------------+---------------------+-----------+------------+--------+
| 1 | 2015-09-20 19:17:52 | NULL | 10 | NULL | 20 |
| 2 | NULL | 2015-09-20 20:17:00 | NULL | 35 | 20 |
| 3 | 2015-09-15 19:17:52 | NULL | 15 | NULL | 10 |
| 4 | NULL | 2015-09-16 20:17:00 | NULL | 25 | 10 |
+----+---------------------+---------------------+-----------+------------+--------+
I need to get all the rows grouped by the column sameid like this:
+----+---------------------+---------------------+-----------+------------+--------+
| id | opendate | closedate | openprice | closeprice | sameid |
+----+---------------------+---------------------+-----------+------------+--------+
| 1 | 2015-09-20 19:17:52 | 2015-09-20 20:17:00 | 10 | 35 | 20 |
| 3 | 2015-09-15 19:17:52 | 2015-09-16 20:17:00 | 15 | 25 | 10 |
+----+---------------------+---------------------+-----------+------------+--------+
And this is what I have tried so far:
(SELECT * FROM table WHERE opendate >= '2015-08-08 00:00:01') UNION (SELECT * FROM table WHERE closedate <= '2015-10-15 23:59:59')
I can get all the rows but I can not fin a way to group them by sameid, I tried using GROUP BY sameid without success.
Hope you guys can help me
Thank you
UPDATE
The table was designed that way long ago, (not by me) and there is too much information stored, I'm not allowed to redesign the DB schema either.
If your table format exactly what you describe, then this should work:
SELECT id, MAX(opendate), MAX(closedate), MAX(openprice), MAX(closeprice), sameid FROM table GROUP BY sameid;
However, I think you should redesign your database schema, seperate open info and close info into 2 rows (they could be in 2 tables or in same table). It's would be better for you to work with rather than trying some workaround.
Regards,
Try this
SELECT t1.opendate, t2.closedate, t1.openprice, t2.closeprice FROM `table` t1 JOIN `table` t2 ON t2.sameid = t1.sameid WHERE t1.opendate >= '2015-08-08 00:00:01' AND t2.closedate <= '2015-10-15 23:59:59' AND t1.opendate IS NOT NULL AND t2.closedate IS NOT NULL
This will produce your exact output.
SELECT
MIN(id) as id,
MAX(open_date) as open_date,
MAX(close_date) as close_date,
MAX(open_price) as open_price,
MAX(close_price) as close_price,
sameid
FROM
`table`
GROUP BY
sameid
ORDER BY id ASC
I have two table tbl_issue_log, tbl_magazine_issue
tbl_issue_log
============
+----------+--------+--------+-----------+---------------------+
| issue_id | mag_id | log_id | operation | updated_time |
+----------+--------+--------+-----------+---------------------+
| 2 | 1 | 1 | 1 | 2014-01-30 21:29:44 |
| 3 | 4 | 1 | 1 | 2015-01-30 21:29:44 |
| 2 | 1 | 1 | 3 | 2015-01-31 21:29:44 |
+----------+--------+--------+-----------+---------------------+
tbl_magazine_issue
=================
+----------+-------------+-------------+------------------+------------+------------+-------------------+---------------+
| ISSUE_ID | ISSUE_NAME | MAGAZINE_ID | COVER_PAGE_THUMB | FROM_DATE | TO_DATE | issue_description | login_page_no |
+----------+-------------+-------------+------------------+------------+------------+-------------------+---------------+
| 2 | test issue | 1 | cover page | 2014-01-30 | 2015-01-30 | sdssdg fsdf | 20 |
| 3 | test issue1 | 4 | cover page1 | 2014-01-30 | 2015-01-30 | sdssdg fsdf | 20 |
+----------+-------------+-------------+------------------+------------+------------+-------------------+---------------+
in tbl_issue_log contain multiple records for same issue id. i want only one issue at a time
and this must latest updated time.
My query is this
SELECT
`tbl_issue_log`.`operation`,
`tbl_magazine_issue`.`ISSUE_ID`,
`tbl_magazine_issue`.`ISSUE_NAME`,
`tbl_magazine_issue`.`MAGAZINE_ID`,
`tbl_magazine_issue`.`COVER_PAGE_THUMB`,
`tbl_magazine_issue`.`FROM_DATE`,
`tbl_magazine_issue`.`TO_DATE`,
`tbl_magazine_issue`.`issue_description`,
`tbl_magazine_issue`.`login_page_no`
FROM
`tbl_issue_log`
LEFT JOIN
`tbl_magazine_issue` ON tbl_magazine_issue.ISSUE_ID = tbl_issue_log.issue_id
WHERE
(tbl_issue_log.mag_id = '1')
AND (tbl_magazine_issue.ISSUE_STATUS = 3)
AND (tbl_issue_log.updated_time > '2014-02-25 00:42:22')
GROUP BY tbl_issue_log.issue_id
ORDER BY tbl_issue_log updated_time DESC;
Here i got issue id based output . But not getting the latest updated timeed record.
If any one about this please help me.
Try this
SELECT * FROM
(SELECT
`tbl_issue_log`.`operation`,
`tbl_magazine_issue`.`ISSUE_ID`,
`tbl_magazine_issue`.`ISSUE_NAME`,
`tbl_magazine_issue`.`MAGAZINE_ID`,
`tbl_magazine_issue`.`COVER_PAGE_THUMB`,
`tbl_magazine_issue`.`FROM_DATE`,
`tbl_magazine_issue`.`TO_DATE`,
`tbl_magazine_issue`.`issue_description`,
`tbl_magazine_issue`.`login_page_no`
FROM
`tbl_issue_log`
LEFT JOIN
`tbl_magazine_issue` ON tbl_magazine_issue.ISSUE_ID = tbl_issue_log.issue_id
WHERE
(tbl_issue_log.mag_id = '1')
AND (tbl_magazine_issue.ISSUE_STATUS = 3)
AND (tbl_issue_log.updated_time > '2014-02-25 00:42:22')
ORDER BY tbl_issue_log.updated_time DESC ) TEMP_TABLE
GROUP BY ISSUE_ID
Can you try changing order by clause as
ORDER BY tbl_issue_log.updated_time DESC;
Edit ---
As you are grouping on issue_id, mysql will select first row that matches the issue_id. The order by runs later which essentially does not return what you are looking for. You may need to use a subquery approach for this.
select some_table.* FROM
(
SELECT
MAX(tbl_issue_log.updated_time) AS updated_time,
`tbl_issue_log`.`operation`,
`tbl_magazine_issue`.`ISSUE_ID`,
`tbl_magazine_issue`.`ISSUE_NAME`,
`tbl_magazine_issue`.`MAGAZINE_ID`,
`tbl_magazine_issue`.`COVER_PAGE_THUMB`,
`tbl_magazine_issue`.`FROM_DATE`,
`tbl_magazine_issue`.`TO_DATE`,
`tbl_magazine_issue`.`issue_description`,
`tbl_magazine_issue`.`login_page_no`
FROM
`tbl_issue_log`
LEFT JOIN
`tbl_magazine_issue` ON tbl_magazine_issue.ISSUE_ID = tbl_issue_log.issue_id
WHERE
(tbl_issue_log.mag_id = '1')
AND (tbl_magazine_issue.ISSUE_STATUS = 3)
AND (tbl_issue_log.updated_time > '2014-02-25 00:42:22')
GROUP BY tbl_issue_log.issue_id
) some_table
ORDER BY some_table.updated_time DESC;
I have table :
==========================================================
|id | before | after | freq | id_sentence | document_id |
==========================================================
| 1 | a | b | 1 | 0 | 1 |
| 2 | c | d | 1 | 1 | 1 |
| 3 | e | f | 1 | 1 | 1 |
| 4 | g | h | 2 | 0 | 2 |
==========================================================
I want to get the number of data depend on the id_sentence and freq so the result must be 1 2 1
here's the code :
$query = mysql_query("SELECT freq FROM tb where document_id='$doc_id' ");
while ($row = mysql_fetch_array($query)) {
$stem_freq = $row['freq'];
$total = $total+$stem_freq;
but the result is still wrong. please, help me.. thank you :)
If I understand your question, you are trying the calculate the sum of freq for each distinct id_sentence for a particular document_id.
Try the following SQL:
SELECT id_sentence, SUM(freq) as freq FROM tb WHERE document_id = 1 GROUP BY(id_sentence)
The result will be rows of data with the id_sentence and corresponding total freq. No need to manually sum things up afterwards.
See this SQL Fiddle: http://sqlfiddle.com/#!2/691ed/8
I think you could do something like
SELECT count(*) AS count FROM tb GROUP BY(id_sentence,freq)
to do the counting you want. You could even do something like
SELECT count(*) AS count, id_sentence, freq FROM tb GROUP BY(id_sentence,freq)
to know which id_sentence,freq combination the count is for.
I want to write a php script to compare rows from the database and then adds the values together if payment_id are matches. Based on payment_id:
Example:
+----+------------+-----------+--------+
| id | payment_id | cheque_id | amount |
+----+------------+-----------+--------+
| 1 | 1000 | MB101 | 20 |
| 2 | 1000 | MB102 | 20 |
| 3 | 1111 | MB113 | 20 |
+----+------------+-----------+--------+
Result required
+------+--------------+----+
| 1000 | MB101/MB102 | 40 |
| 1111 | MB113 | 20 |
+------+--------------+----+
Trying to merge cheque column as string. For 'Amount' column I know that should be use SUM.
Any suggestions is appreciated,
Thanks
use GROUP BY
select payment_id,group_concat(cheque_id SEPARATOR '/') as cheque_ids,
SUM(amount) as amount
FROM table name
GROUP BY payment_id
I found the solution
SELECT payment_id, group_concat(cheque_id SEPARATOR '/') AS cheque_ids,
SUM(amount) AS amount
FROM tbl_name
GROUP BY payment_id