how to select data with syntax sql [closed] - mysql

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

Related

SQL queries for calculating shipping charges based on subtotal amount [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 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

How to identify entries with all completed tasks? [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 5 years ago.
Improve this question
Let's say i have a table Task_status like below:
id TaskId SubTaskId status
1 1 1 Complete
2 1 2 Complete
3 1 3 Complete
4 2 4 InProgress
5 2 5 Complete
I want to find all the taskId whose all child tasks are Complete. How can I write that query?
Use group by and having to check if the number of rows per task equal the number of rows with Complete status.
select taskId
from tbl
group by taskId
having count(*) = sum(status='Complete')

SQL Query Joins [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 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

SQL Sum of column based on other Column [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 7 years ago.
Improve this question
Unfortunately My tables will be poor and I have no idea how to fix that. Essentially I have this table and I want to sum all the values on the right based on one value on the left. This is sample data but it will be 400+ records in the database. So if the numbers on the left are IDs for customers, and the numbers on the right are products purchased, how do I get a summary of products purchased per customer?
1 4
1 2
2 3
3 2
2 4
1 1
3 2
1 3
The final result would need to look like this:
1 10
2 7
3 4
SELECT
user_table.userID,
SUM(user_table.prodcut_purchase)
FROM
user_table
GROUP BY
user_table.userID

How to sum all visits from mySQL and show it for each id like a group? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
My mySQL look like this
id visits
3 0
3 0
3 1
4 1
4 1
4 0
2 1
How can I have something more like group for all my entries id :
id visits
3 1
4 2
2 1
Use this query:
SELECT ID, SUM(visits) as visit FROM YOURTABLE GROUP BY ID;
SQLFIDDEL is here.
Use Group By Clause
SELECT ID, SUM(Visits) FROM YOURTABLE GROUP BY ID;