Hi lets say I have a table like this:
________________________________________________________
|
| id | order | date | item | price|
-------------------------------------------------------
| 1 1st date i1 10,00|
|-------------------------------------------------------
| 2 2nd date u2 15,00|
|-------------------------------------------------------
| 3 1st date i1 20,00|
|-------------------------------------------------------
| 4 2nd date u2 30,00|
|-------------------------------------------------------
I need to get total sum of items containing same order name and print DISTINCT orders like:
order_name - date - total sum. or better way to create new tabel and store there total sum of each order and in this table add sum id in each row?
This will literally return order_name - date - total sum, assuming your date column is date
SELECT CONCAT(order, " - ", date, " - ", SUM(price)) as Result
FROM orders
GROUP BY `order`
To add a quantity, try this:
SELECT CONCAT(order, " - ", date, " - ", CASE WHEN quantity > 1 THEN (quantity * SUM(price)) ELSE SUM(price) END) as Result
FROM orders
GROUP BY `order`
Use GROUP BY:
SELECT SUM(`price`)
FROM `orders`
GROUP BY `order`
http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html
Related
I have following table 'persons' with same persons in different rows
id | firstname | surname | date_created
------------------------------------------------------
3 | Nelli | Schaller | 2017-08-22 20:57:19
------------------------------------------------------
4 | Carl | Schaller | 2019-06-21 08:29:45
------------------------------------------------------
48 | Nelli | Schaller | 2020-06-25 13:06:09
------------------------------------------------------
49 | Carl | Schaller | 2020-06-25 13:06:09
What I want to get are all unique Schallers with the biggest id / newest date_created value.
I tried this
SELECT id, CONCAT(surname, ", ", firstname) AS person, date_created
FROM persons
WHERE
surname LIKE "schall%"
GROUP by firstname, surname
ORDER BY date_createdDESC, surname ASC LIMIT 0, 10
but get only as expected the first two entries (id 3 and 4) but I need 48 and 49.
As mentioned in some comment in this case the LIKE statement isn't necessary but in real live it will be the source for an autocomplete field so I need the LIKE
Any idea how to manage that?
Use NOT EXISTS:
SELECT p.id, CONCAT(p.surname, ', ', p.firstname) AS person, p.date_created
FROM persons p
WHERE p.surname LIKE '%schall%'
AND NOT EXISTS (SELECT 1 FROM persons WHERE firstname = p.firstname AND surname = p.surname AND id > p.id)
ORDER BY p.date_created DESC, person
If the condition to pick the latest of each group is the column date_created then change:
...AND id > p.id
with
...AND date_created > p.date_created
You could use subquery with group for max id
select t.max_id, t.person, m.date_created
from (
SELECT max(id) max_id, CONCAT(surname, ", ", firstname) AS person
FROM persons
WHERE surname LIKE "schall%"
ORDER BY date_createdDESC, surname ASC
GROUP BY CONCAT(surname, ", ", firstname)
) t
inner join persons m ON CONCAT(m.surname, ", ", m.firstname) = t.person
and m-id = t.max_id
SELECT p.*
FROM persons p
LEFT JOIN persons p2 ON p2.firstname = p.firstname
AND p2.lastname = p.lastname
AND p2.date_created > p.date_created
WHERE p2.id IS NULL
This is SQL Server syntax but MySQL is probably similar.
I'm assuming your id field doesn't need to be checked as well as the date_created since it's an identity column and would be larger anyway for the latter created records, but obviously adjust to your actual data.
s.no Name type of vehicle allotted
1 ram bus
2 krish car
3 raj bus
4 albert car
5 johnson bike
6 raghu car
consider this is a table now i want to retrieve the type of vehicle which is there more number of times in the table and count of it. is there any particular method(query).
You have to use COUNT and GROUP BY.
Count will give you the number of rows grouped by type field.
SELECT COUNT(id) as count, type FROM table_name GROUP BY type
The output should be like this:
count | type
2 | bus
3 | car
1 | Bike
If you need just the highest one, append to the query:
ORDER BY count DESC LIMIT 1
I think you have to count and select the top 1 row.
Query
SELECT t.* FROM(
SELECT `type`, COUNT(`type`) `count`
FROM `your_table_name`
GROUP BY `type`
)t
ORDER BY `count` DESC LIMIT 1;
well this question is related to my previous question How to left join 2 tables with SUM() and MAX() grouped by date
what i changed is i added user_id column (auto incremented) and want to select value with highest user_id per date
i have table loadhistory ( wanted to "select only value with highest user_id per date" group by and order by date DESC.)
so in this case, i want to select 150 for 2015-02-27 since it has the highest user_id in that date and 50 for 2015-02-28
| user_id | customer_id | date | bal |
1 1 2015-02-27 100
2 1 2015-02-27 150
3 1 2015-02-28 150
4 1 2015-02-28 50
and table transactionrecord (want to sum up values per date using SUM(bal) group by and order by date DESC)
| user_id |customer_id | date | bal |
1 1 2015-02-27 50
2 1 2015-02-27 20
3 1 2015-02-28 10
And i want to join the 2 tables which would look like this:
| date | balance | amount paid |
2015-02-28 50 10
2015-02-27 150 70
this is the code so far (i used the code i got from my previous question and edited it here in my new question hoping to arrive desired result but did not)
SELECT a.customer_id, a.date, (b.bal AS bal WHERE b.user_id = MAX(b.user_id) , a.paid
FROM (
SELECT customer_id, date, SUM(bal) AS paid
FROM transactionrecord
GROUP BY customer_id, date
) AS a LEFT JOIN loadhistory AS b
ON a.customer_id = b.customer_id AND a.date = b.date
WHERE a.customer_id = 1
GROUP BY a.customer_id, a.date, a.paid
ORDER BY a.date DESC
help please. thanks in advance
MySQL use the first row by group by, so you must order it before you can use group by like this:
SELECT * FROM (SELECT * FROM `loadhistory` ORDER BY user_id DESC) history GROUP BY date
So you can use the following query as solution:
SELECT h.date,h.bal as balance, t.amount as 'amount paid' FROM
(SELECT * FROM (SELECT * FROM `loadhistory` ORDER BY user_id DESC) history GROUP BY date) as h
JOIN
(SELECT SUM(bal) as amount, customer_id, date FROM `transactionrecord` GROUP BY date) as t
ON h.date = t.date AND h.customer_id = t.customer_id
ORDER BY date DESC
Im having the below sql table structure and im trying to grab values from 2 tables using codeigniter.
table: salesman
id sales_name ref_id
1 kevin 174
2 mike 574
3 nick 777
table: sales_report
id salesman_id product purchased_date dispatched
1 2 BF0214 04-November-2011 Yes
2 2 CF0474 09-November-2011 No
3 2 BF0111 10-November-2011 No
4 3 BF0714 15-November-2011 Yes
5 3 BF0435 15-November-2011 Yes
6 2 BF0335 18-November-2011 Yes
7 1 BF0714 22-November-2011 Yes
8 1 BF0335 25-November-2011 Yes
im passing the salesman_id to the model to grab and display the values in my view.
my html table is as below
Ref ID | Salesman Name | Last product Sold | Sold Date | Dispatched Status
the problem that im having how can query to get the sales_name and ref_id from the salesman table and get the most recent product name, dispatched and purchase_date from from the sales_report table?
eg:
Ref ID | Salesman Name | Last product Sold | Sold Date | Dispatched Status
174 kevin BF0335 25-November-2011 Yes
574 mike BF0335 18-November-2011 Yes
777 nick BF0435 15-November-2011 Yes
Use
GROUP BY
ORDER BY
for getting this done
You need to use SELECT values from the sales_report, LEFT JOIN salesman to get in the sales_name and ref_id, ORDER BY the purchased_date (DESC to get the most recent one first instead of the oldest first), and GROUP BY the salesman.id to get it to only return one row per salesman.
SELECT * FROM sales_report LEFT JOIN salesman ON sales_report.salesman_id = salesman.id ORDER BY purchased_date DESC GROUP BY salesman.id
You should find last product by two criteras: its sold date and by its id; because some products may be sold in one day. So, try this one -
SELECT s.ref_id, s.sales_name, sr.product, sr.purchased_date, sr.dispatched FROM salesman s
JOIN (
SELECT salesman_id, product, purchased_date, dispatched FROM (
SELECT salesman_id, product, purchased_date, dispatched, IF(#salesman_id = salesman_id, #i := #i + 1, #i := 1) n, #salesman_id := salesman_id
FROM sales_report, (SELECT #i:= 0, #salesman_id = NULL) vars
ORDER BY salesman_id ASC, purchased_date DESC, id DESC) t
WHERE t.n = 1
) sr
ON s.id = sr.salesman_id;
I want to know how to display report by month using PHP and Mysql
Example of Report on the web page:
January 2011
==============
Store Name | Total Order Cost
SHOP A | £123
SHOP B | £100
February 2011
==============
Store Name | Total Order Cost
SHOP A | £123
SHOP B | £100
SHOP C | £99.40
I have mysql tables
tbl_shop
ShopID
ShopName
tbl_order
OrderID
ShopID
OrderDate
Total
You will need to iterate trough the result of the query and create an multidimensional array using the month/year combination as keys. The query below should be a good indication on how to fetch the required information from your database.
SELECT
MONTH(to.OrderDate),
YEAR(to.OrderDate),
SUM(to.Total),
to.*
FROM tbl_order as to
INNER JOIN tbl_shop as ts ON ts.ShopID = to.ShopID
GROUP BY to.ShopID, MONTH(to.OrderDate), YEAR(to.OrderDate)
Note that I havn't tested this query - please handle it as pseudo-code. You might need to throw around the GROUP BY fields a little and test if it works.
Use
SELECT ShopName,SUM(Total),MONTHNAME(OrderDate) from tbl_shop
left join tbl_order
on tbl_shop.ShopID = tbl_order.ShopID
GROUP By(ShopID),MONTHNAME(OrderDate)
will give you All months report.
Use this query:
SELECT monthname(o.OrderDate) as Month, year(o.OrderDate) as Year,
s.ShopName, sum(o.Total) as Total
FROM tbl_Shop s JOIN tbl_Order o
ON s.ShopID=o.ShopID
GROUP BY o.shopID,year(o.Orderdate),month(o.Orderdate)
ORDER BY year(o.OrderDate),month(o.OrderDate)
then for every row from result if month+year has changed (a variable $last_date could help) then print
$Month $Year
==============
Store Name | Total Order Cost
$ShopName | $Total
else print
$ShopName | $Total