MySql Result Set Combine Columns - mysql

I have this result set from my query:
OrderId CustomerId ProducerId CustomerPayment ProducerPayment
1 1 3 10 5
1 1 4 10 5
1 2 3 10 5
1 2 4 10 5
I need to return this result into this:
OrderId UserId Payment
1 1 20
1 2 20
1 3 10
1 4 10
Just combining the CustomerId and ProducerId into UserId. Same with the Payment Columns.
Is there any way to achieve this with using just a simple select and group by? I'm avoiding temp tables, calling multiple same queries and like for optimization. I hope this is possible.
Thanks a lot

SELECT
OrderId,
CustomerID AS UserId,
SUM (CustomerPayment) As Payment
FROM orders
UNION ALL
SELECT
OrderId,
ProducerId AS UserId,
SUM (ProducerPayment) As Payment
FROM orders

Try something like this:
select
OrderId,
CustomerId,
sum(CustomerPayment) Payment,
group_concat(OrderId separator ',') listOrders /* list all OrderID's from the user and separates these with a , */
from your_table
group by CustomerId
Dont know how you query looks like atm?

Related

Count ID in sql query

I have the following data,
id emp_id csa_taken
1 100 2
2 100 2
3 100 0
4 100 2
5 101 2
6 101 2
7 101 0
8 101 0
I expect a result with count where csa_taken=2 for individual employee.
expected result:
emp_id count_csa_taken
100 3
101 2
I have tried the following query with a failed attempt.
Select count(employee_id) From $employeeCSA where csa_taken=2
Please suggest as I am new to sql.
If I understand you correctly you like to count all employees with a cas_taken of two. As there are multiple entries for the csa_taken for one employee you need to group them.
E.g.:
SELECT COUNT(*) FROM $employeeCSA WHERE csa_taken = 2 GROUP_BY employee_id
Please note that COUNT(*) counts the rows (not the fields).
You also need group by. Try like:
Select count(employee_id),emp_id From $employeeCSA where csa_taken=2
group by emp_id
If i understand correctly, then you can try this:
SELECT emp_id,COUNT(emp_id) from dbo.Sample WHERE csa_token = 2 GROUP BY emp_id

Sum of values from 2 columns from 2 tables AND sorting by a column

I have 2 tables:
Table "credits":
id | amount | type
1 8 1
2 7 2
3 2 1
4 1 1
5 5 3
6 4 2
and
Table "debits":
id | amount
1 3
1 2
3 2
4 1
5 3
5 1
I need to get the sum of all "id's" balances (credit-debit) and grouping it by "type". So far I have this:
SELECT id, SUM(amount) as balance,
FROM
(
SELECT id, amount FROM credits
UNION ALL
SELECT id, -amount FROM debits
)
unified_table
GROUP BY id
But it just gives me the "id's" balances:
id | balance
1 3
2 7
3 0
4 0
5 1
6 4
Ideally, I need something like this:
type | balance
1 3
2 11
3 1
I tried to add the "type" column in the first "select" of the union, and then group by "type". But not working I think because table "debits" dont have column "type". How can I accomplish this? Thank you for your help
I think this would do it:
SELECT c.type, sum(c.amount - IFNULL(d.amount,0))
FROM credits c LEFT OUTER JOIN (SELECT id, sum(amount) FROM debits GROUP BY id) d
ON c.id=d.id
GROUP BY c.type
The idea is to group the debits table first, and then join it with the credits table, which will result in a table that you can group by type
Try this:
SELECT Type, Sum(Amount)
FROM (
SELECT C.Amount - ISNULL(D.Amount, 0) AS Amount, C.Type
FROM Credits C
LEFT JOIN (SELECT Id, Sum(Amount)
FROM Debits
GROUP BY ID) D ON C.Id = D.Id
) A
GROUP BY A.Type
Here is my solution:
SELECT
credits.`type`,
credits.`amount` - IFNULL(t_debit.`d_amount`, 0) AS balance
FROM
credits,
(SELECT id, SUM(amount) AS d_amount FROM debits GROUP BY id)t_debit
WHERE
credits.`id` = t_debit.`id`
GROUP BY
credits.`type`;
First I select sum of amounts from debits table group by id and after I did another select query on the credits table where credit id match to debit id. I don't use UNION operator because the id's column in debits table is an foreign key.

mysql get the columns sum and also get the distinct values at a time

I have my data base like this
id project_id client_id price
1 1 1 200
2 2 1 123
3 2 1 100
4 1 1 87
5 1 1 143
6 1 1 100
7 3 3 123
8 3 3 99
9 4 3 86
10 4 3 43
11 4 3 145
12 4 3 155
Now here I want that it will sum the price columns with the same client_id.
For that I just made my query like this
Select `project_id`, SUM(`price`) FROM `table-name` GROUP BY `client_id`
This one is doing sum the price but I am getting only two project_id in the result. I want the result should be all the distinct project for the client id and the price will be summed for the group clients.
So can someone tell me how to do this? Any help and suggestions will be really appreciable. Thanks
You should not have "bare" column in a group by query that are not in the group by statement.
If you want the list of projects, you can get them in a list like this:
SELECT client_id, GROUP_CONCAT(project_id), SUM(price)
FROM table-name
GROUP BY client_id;
you only have two client that why you are getting only two record , you can group by two column,
Select `project_id`, SUM(`price`) FROM `table-name` GROUP BY `client_id`, `project_id`

Get concatenated list of id's for same product

I have the following table:
create table Customer (
custid integer not null,
prodid integer not null,
primary key(custid,prodid)
);
the values in this table are as follows:
Custid | Prodid
1 10
1 11
2 10
2 11
3 10
3 11
4 11
I want to get all those pairs of customers that bought the same products.
My final output should be concantenated list as follows:
Custid
1,2
2,3
1,3
1,4
2,4
3,4
I have written a query as follows:
SELECT custid, group_concat(prodid,',')
FROM
(SELECT UNIQUE custid FROM Customer)
group by custid;
However this gives me:
1 10,11
2 10,11
3 10,11
4 11
How do I get the pairs of custid's from this? How to write it in subqueries?
Can someone help me in this.
Something like this should work, but note that I don't have MySQL access today so it's untested:
SELECT DISTINCT CONCAT(a.custid, ',', b.custid)
FROM Customer a
JOIN Customer b ON a.prodid = b.prodid
WHERE a.custid < b.custid

MYSQL select maximum sum of purchases

I have the following Table named Order
Orders Table
___________________________________________________
orderiD | userId | OrderType | Order_Date | Amount
________|________|___________|____________|________
1 1 0 12/12/2009 1
2 1 1 13/12/2009 2
3 1 1 14/12/2009 3
4 2 0 12/12/2009 4
5 2 1 16/12/2009 2
6 1 0 14/12/2009 5
7 2 1 17/12/2009 4
8 2 0 10/12/2010 2
___________________________________________________
I need to create query which returns user id with maximum SUM of purchases.
I tried the following
Select MAX(GRP.sumAmmount), o.userId join
(Select SUM(o.Amount) as sum_ammount, o.userId as UID from Orders GROUP BY(o.userID)) as GRP on o.userId=GRP.UID GROUP BY(GRP.UID)
But I believe I'm missing something.
Can you help?
If I understand your question correctly, you want to return the UserID which has the maximum SUM (total) of purchases. So the above records will result:
UserID Total Amount
2 12
And the simpliest solution would be:
SELECT UserID, SUM(AMOUNT) as TotalAmount
FROM Orders
GROUP BY UserID
ORDER BY TotalAmount DESC
LIMIT 1
I'm ready to edit this if I'm wrong. :-) (PS: Please add your desired result)
Thanks.
UPDATE 1
Derived from you query:
Select MAX(SUM(o.Amount)) as sum_ammount,
o.userId as UID
FROM Orders o
GROUP BY o.userID
ORDER BY sum_ammount DESC
LIMIT 1
See below, its working.
SELECT userId, sum(Amount) as 'Total'
FROM Orders
GROUP BY userId
ORDER BY Total DESC
LIMIT 1
Output is
+++++++++++++++
userId + Total
+++++++++++++++
2 + 12
+++++++++++++++
I also tried after adding new row as
insert into Orders values (9,1,0,'2010-12-10 12:12:12',10)
Output is
+++++++++++++++
userId + Total
+++++++++++++++
1 + 21
+++++++++++++++
I prefer to have this solution
SELECT UserID, SUM(AMOUNT) as TotalAmount
FROM Orders
GROUP BY UserID
having SUM(AMOUNT) = (select sum(amount) as col1
from orders
group by userid
order by col1 desc
limit 1
)
This sql will show all users who have max purchases
Just try to change amount of orderid 1 to be 2 , then this sql will show both.