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 1 year ago.
Improve this question
ID
Name
min_amount
charge
1
Standard
50.00
3.00
2
Express
50.00
5.00
3
Standard
100.00
2.00
4
Express
100.00
0.00
so I have this table in SQL and my requirement is to calculate shipping based on min_amount (subtotal). for eg: if the subtotal is 45.00 I want to get the row with ID 1,2 (because 45.00 < 50.00). and similarly if subtotal is 55 it should be the row with ID 3,4.
SELECT id,min_amount, MAX(charge) as shipping FROM `shippings` WHERE min_amount > 45.00 GROUP BY name
this is working. but when the value changes to 101.00, this returns nothing I want it to return the max min_amount row which is 3,4
what should I change? and what is the eloquent query for this, since I'm using laravel but can be done without it.
You need to get the highest min amount value first and use it to make a decision on upper value using IF condition in WHERE clause,
SELECT id, name, min_amount, MAX(charge) as shipping
FROM shippings,
(SELECT MAX(min_amount) as min_amount_limit FROM shippings) z
WHERE min_amount >= IF(z.min_amount_limit < 101.00, z.min_amount_limit, 101.00)
GROUP BY name;
Working Fiddle
Related
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 1 year ago.
Improve this question
This is the table structure:
ID Name Amount
1 AAA 500
2 BBB 100
3 CCC 300
4 DDD 300
5 EEE 200
6 FFF 300
7 GGG 500
How can I fetch 4th row based upon a condition of amount column?
If you need a specific answer only for the above table try:
select * from *your_table_name* where amount = 300 ORDER BY id ASC LIMIT 1,1
Helpful Link about the Limit and Offset
EDIT
Explanation:
I use amount equal to 300 to keep only the rows that have this
Amount
I have order the table ascending by id to use limit and offset. With Limit 1 returned only one row and with offset 1 is skipping the first row.
select * from TABLE_NAME where amount > 100 limit 3,1
this will fetch only the 4th record
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 2 years ago.
Improve this question
i have this table called job opening and it has attributes ( company name, opening_available, opening_description, Hourly_Pay) whereby i have 4 rows of data, each with unique companies.
i have been given a query to calculate average pay of each companies
i am confused how to do it..here is my table
company name|opening_available|opening_description|Hourly_Pay
1. Facebook| 4 | Senior programmer | 4.50
2. Google | 5 | Web developer | 5.00
3. Metropol| 4 | Web Designer | 6.00
4. dailybug| 3 | Junior programmer | 7.50
You want an average, so you should use a so-called aggregation function named AVG. However, to use an aggregation function, you must separate the table data into parts, and the average shall be calculated upon each part. You can do this with a GROUP BY expression (the parts are groups). Now you want to split the data among the companies, and the splitting criterion will be the company name.
Also, in MySQL if you have column names with spaces, you should wrap them in backticks.
SELECT `company name`, AVG(Hourly_Pay) as average_pay
FROM `job opening`
GROUP BY `company name`
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 4 years ago.
Improve this question
id. goods qty
1 ABC susu ds 1
2 ABC susu gt 1
3 Kapal api moca 1
4 kapal api mix 1
how to display with sql syntax be ..
id goods qty
1 ABC susu 2
2 Kapal api 2
because there is a duplicate of ABC susu and kapal api then the amount is added.
Help me please,
I created a new id base on the grouping (#id) then I get substring of goods without the last word. I also used uppercase because I notice that the first letter of 3rd row is not the same with last row. Then did a sum of the qty.
select #id:=#id+1 as id,
substr(goods,1,length(goods)-locate(' ', reverse(goods))) as goods,
sum(qty) as qty
from yourTable
inner join (select #id:=0) s
group by ucase(substr(goods,1,length(goods)-locate(' ', reverse(goods))))
See demo here: http://sqlfiddle.com/#!9/f70b381/17
Result:
id goods qty
1 ABC susu 2
2 Kapal api 2
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 have a table as follows:
name week effort
quentin 1 1
quentin 1 2
quentin 2 1
tracy 1 1
joe 2 2
There will only be a handful of unique names so it doesn't need to be dynamic
And I would like to query it to return something like
week QuentinEffortSum TracyEffortSum JoeEffortSum
1 3 1 0
2 1 0 2
I have tried something along the lines of
SELECT SUM(Effort) AS JoeEffort, Min (Week) AS week FROM [Group$]
WHERE name = "Joe"
GROUP BY week
ORDER By week
which returned:
week JoeEffort
1 3
2 1
and now I need the other columns and imagine in involves joins but am not sure how to complete the task
Please help
Thanks
I think a PIVOT table would work, like so:
SELECT *
FROM (
SELECT
week,name,effort
FROM [Group$]
) as s
PIVOT
(
SUM(effort)
FOR [name] IN ('quentin','joe','tracy')
)AS pvt
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
I have a MySQL table of billing records. Each entry records either a payment or a charge, the amount, the date applied, and the related customer id. If there are more charges than payments, how can I get the outstanding charge records sorted by most recent, and if there are more payments than charges, the payment credits sorted by most recent? Basically, if I have 3 rows of charges amounting to $1.25 each, and 2 rows of $1.50, how can I get the 3 charge record that is the most recent, and know that there is only $0,75 left due? I hope that makes sense.
Let's say we have this table:
user_id | amount | date_applied | type
---------------------------------------
1 1.25 *datetime* -
1 1.25 *datetime* -
1 1.25 *datetime* -
1 1.50 *datetime* +
1 1.50 *datetime* +
the result should be something similar to this:
user_id | amount | date_applied | type
---------------------------------------
1 0.75 *datetime* -
I may not even be thinking about this right. I am open to other suggestions
Your question is a bit tricky. That's because there is no clear definition on what datetime to show. It seems you want to have the sum of all positive and negative values on the table per user. The problem is that when you make a total you can't have a detail of the datetimes. You will have to choose one of them, but there won't be any order in the result as there will only be one datetime per user in the result (in your example you can see for user 1 there is only one datetime).
Also, do you need the type column? Why not add it to the amount?
This is the closest I can get to your needs given the detail provided:
select user_id, sum(if(type = '+', amount, -amount)) amount from t
group by user_id
+---------+--------+
| USER_ID | AMOUNT |
+---------+--------+
| 1 | -0.75 |
+---------+--------+
Note: If you really need the datetime put a real date for each record and update the expected result with the expected date value.
SELECT
A.USERID,
B.TOTAL,
A.date_applied,
IF(B.TOTAL < 0, '-', '+')
FROM
TABLE AS A
JOIN
(
SELECT
USERID,
SUM(IF(TYPE = '+', AMOUNT, AMOUNT * -1)) AS TOTAL
FROM
TABLE
GROUP BY
USERID, AMOUNT
) AS B
ON A.USERID = B.USERID