Clubbed total sale for month using PHP query [closed] - php

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I need to get a total sale from the given table. Where showing total daily sale and the grand total at the end. My table is like:
Date | Bill No. | Product | Amount | Total Bill Amt. |
01/04/2015 | 001 | A | 150 | 650 << |
01/04/2015 | 001 | B | 300 | 650 |
01/04/2015 | 001 | C | 200 | 650 |
01/04/2015 | 002 | B | 10 | 80 << |
01/04/2015 | 002 | D | 70 | 80 |
02/04/2015 | 003 | Z | 60 | 110 << |
02/04/2015 | 003 | Y | 50 | 110 |
Where results should be:
Date | Total Sale |
01/04/2015 | 730 |
02/04/2015 | 190 |

For 02/04/2015, it should be 110 and not 190.
SELECT `Date`,SUM(`Amount`) AS daily_sale FROM `table` GROUP BY `Date`.

To get the different days dates, depending on the structure of your table, I assume you have a column for id which is a primary key to help track all those table rows by id.
To get the daily Total Bill Amt, apply same query format used by #web-normad annd you'll get your total.
SELECT DATE(Date),SUM(Total Bill Amt) AS daily_sale FROM `table` GROUP BY `Date`.
Test it out and see if it works.

Related

sum and group display in mySql PHP [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 days ago.
Improve this question
I have a table below.
-----------------------------------------------
id | items | user | department | value | date |
-----------------------------------------
1 | books | john | dept-1 | 5 | 1-1-20
-----------------------------------------
2 | fruits | john | dept-1 | 10 | 3-1-20
-----------------------------------------
3 | books | rehman | dept-2 | 8 | 1-1-20
-----------------------------------------
4 | fruits | rehman | dept-2 | 20 | 9-1-20
-----------------------------------------
5 | woolens | john | dept-1 | 15 | 1-1-20
-----------------------------------------
6 | woolens | vinay | dept-1 | 9 | 3-1-20
-----------------------------------------
7 | veggies | sunil | dept-2| 20 | 10-1-20
-----------------------------------------
now I want to display these items like
department | user | value
--------------------------
dept-1 |
| john | 30
| vinay | 9
dept-2 |
| rehman | 28
| sunil | 20
how can I achieve this in mysql, this will be call in ajax and display in a table by php.
can someone help pls.
I tried doing grouping and used while loop but not able to execute desired output.

mysql - many to many query from bridge table [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
i have a task to complete. there is a many to many relationship. the bridge table has been made which looks like
left id right id
+----------+---------+
| 1 | 1 |
| 1 | 2 |
| 2 | 1 |
| 2 | 2 |
| 2 | 8 |
| 3 | 1 |
| 3 | 2 |
| 3 | 4 |
| 4 | 1 |
| 4 | 2 |
| 4 | 3 |
| 4 | 5 |
| 5 | 1 |
| 5 | 2 |
| 5 | 4 |
| 5 | 6 |
| 5 | 7 |
+----------+---------+
i have to display the left id = right id in one row
for example
for left id 1
left1 | right1 righ 2
for left id 3
left3 | right1 right2 right 4
how do i do this ? i have tried joining table , doesn't work
I think you can use a simple query to acheive this using GROUP BY and GROUP_CONCAT()
SELECT left_id, GROUP_CONCAT(right_id SEPARATOR ' ') as rigth_id
FROM left-right
GROUP BY left_id;
This is a reasonably straightforward application of GROUP_CONCAT() and GROUP BY. (http://sqlfiddle.com/#!9/ed7e1/2/0)
SELECT leftId,
GROUP_CONCAT(rightId ORDER BY rightId) rightIds
FROM bridge
GROUP BY leftId

Code to fetch Data from 2 tables & enter in 3rd Table in php [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Table1 - Payout Requests
`
|--------|-----------|-----------|
| id | Receiver | Balance |
|--------|-----------|-----------|
| 1 | user1 | 2000 |
|--------|-----------|-----------|
| 2 | user2 | 1500 |
|--------|-----------|-----------|`
Table2 - Donars
`
|--------|-----------|-----------|
| id | Donar | Amount |
|--------|-----------|-----------|
| 1 | love | 1500 |
|--------|-----------|-----------|
| 2 | don1 | 1000 |
|--------|-----------|-----------|`
Now my question; What piese of code will make Adjustments of both the tables so that data from these two tables goes in 3rd table in the following way
Table3 - Links
`
|--------|-----------|-----------|----------|
| id | Donar | Receiver | Amount |
|--------|-----------|-----------|----------|
| 1 | love | user1 | 1500 |
|--------|-----------|-----------|----------|
| 2 | don1 | user1 | 500 |
|--------|-----------|-----------|----------|
| 3 | don1 | user2 | 500 |
|--------|-----------|-----------|----------|`
AND now Table 1 & Table 2 will be Follows -
Table1 - Payout Requests
`
|--------|-----------|-----------|
| id | Receiver | Balance |
|--------|-----------|-----------|
| 2 | user2 | 1000 |
|--------|-----------|-----------|`
Table2 - Donars
`
|--------|-----------|-----------|
| id | Donar | Amount |
|--------|-----------|-----------|
| Nothing |
|--------|-----------|-----------|`
In the first step you should have a foreign key on Donars table which save id of Payout Requests. for example pr_id
You can use INNER JOIN like this:
SELECT `t2`.`id`, `t2`.`Donar`, `t1`.`Receiver `, `t1`.Balance - `t2`.`Amount` AS `Adjustment` FROM `table1` AS `t1` INNER JOIN `table2` AS `t2` ON `t2`.`pr_id` = `t1`.id

Sum top three totals and group result by [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Here is my problem...
table looks like this:
|id |coutry |sum
| 0 | USA | 30
| 1 | USA | 60
| 2 | USA | 90
| 3 | USA | 80
| 4 | GER | 40
| 5 | GER | 90
| 6 | SWE | 30
| 7 | SWE | 10
| 8 | SWE | 90
| 9 | SWE | 40
and result (top three scores if exists) should be like this:
|coutry |total
| USA | 230
| GER | 130
| SWE | 160
Any change to solve this by sql only? I'll fetch final result using php foreach..
The limit keyword should do the trick:
SELECT country, SUM(`sum`)
FROM my_table
GROUP BY country
ORDER BY 2 DESC
LIMIT 3
You need to establish a row number to get the top 3 sums per country. To do this with MySql, you have to use user-defined variables.
SELECT country, SUM(`sum`)
FROM (
SELECT *,
#rn:=IF(#prevCountry=country,#rn+1,1) rn,
#prevCountry:=country
FROM yourtable, (SELECT #rn:=0, #prevCountry:='') t
ORDER BY country, `sum` DESC
) t
WHERE rn <= 3
GROUP BY country
ORDER BY country
SQL Fiddle Demo
Try it here because #Mureinik have right.
SQL Fiddle
Order by country DESC solve "TOP" problem.

PHP code how to list the total value of each user [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
table A
-----------------
a_id | user |
-----------------
1 |adam |
2 |jose |
3 |adam |
4 |adam |
5 |anne |
6 |jose |
table B
--------------------------------------
b_id | user | value1 | value2
--------------------------------------
1 |adam | 33 | 9
2 |jose | 46 |88
3 |adam | 77 |21
4 |adam | 81 |15
5 |anne | 11 |67
6 |jose | 45 |6
how can I view the total value of value1 and value2.
example: I want my list view page look like this.
--------------------------------------------------------
user | total value of value1 | total value of value2
---------------------------------------------------------
adam |191 | 45
jose |91 | 94
anne |11 | 67
pls. help.
You could always try some sort of aggregate query such as the following
SELECT user, SUM(value1) AS v1sum FROM B GROUP BY user

Categories