I have to combine two tables with different sets of columns by a 'salesperson' column.
The problem with the query I've got so far is that some salespeople names are duplicated, and some from the right table are missing.
Transactions table
salesperson, Profit, Units
John 100 1
John 50 1
Carl 200 2
Matt 300 3
Connections table
salesperson, Amount
Carl 100
Lynda 200
Lucy 300
Combined table
salesperson, (Amount+Profit), Units(sum)
Carl 300 2
John 150 2
Matt 300 3
Lynda 200 0
Lucy 300 0
This is what I've got so far
SELECT t.salesperson, SUM(t.profit) + SUM(c.amount), SUM(t.units)
FROM transactions AS t
FULL OUTER JOIN connections as c ON t.salesperson = c.salesperson
GROUP BY t.salesperson
ORDER BY t.salesperson ASC
Any help would be greatly appreciated.
SELECT salesperson, SUM(total), SUM(Units)
FROM
(
SELECT salesperson, Amount as total, Units
FROM Transactions
UNION ALL
SELECT salesperson, Profit as total, 0 as Units
FROM Connections
) T
GROUP BY salesperson
Related
Please help me with writing a query for the following condition. I have a table which I have listed below
ID Wt1 Wt1_Type Wt2 Wt2_Type Wt3 Wt3_Type Wt4 Wt4_Type
--------------------------------------------------------------
1 200 1 220 1 300 2 400 3
2 100 4 150 3 100 5 120 1
3 100 3 110 1 200 5 100 4
I want a query to sum all the the weights (wt1, wt2, wt3, wt4) grouped on the weight type (wt1_type, wt2_type, wt3_type, wt4_type).
The output should look like
Wt_type Total
1 650
2 300
3 650
4 200
5 300
Can someone please help me draft a mysql query to get this result ?
Thanks
You can try below - using union all and subquery
select Wt_Type,sum(Wt) as total from
(
select Wt1_Type as Wt_Type,Wt1 as Wt from tablename
union all
select Wt2_Type ,Wt2 from tablename
union all
select Wt3_Type ,Wt3 from tablename
union all
select Wt4_Type ,Wt4 from tablename
)A group by Wt_Type
Rather than giving the answer by #fa06, which should work for you, I am going to suggest using a better table design. Here is how you should be storing your data:
ID Type Wt
-------------
1 1 200
2 4 100
3 3 100
4 1 220
5 3 150
6 1 110
7 2 300
8 5 100
9 5 200
10 3 400
11 1 120
12 4 100
Note that there is a single column which stores the type and a single column for that type's weight. Now your expected output just requires a very simple query:
SELECT Type, SUM(Wt) AS Total
FROM yourTableUpdated
GROUP BY Type;
Databases are really good at performing operations across rows, much less so across columns.
Use this it should be work
select Wt_Type,sum(Wt) as total from ( select Wt1_Type as Wt_Type,Wt1 as Wt from tablename union all select Wt2_Type ,Wt2 from tablename union all select Wt3_Type ,Wt3 from tablename union all select Wt4_Type ,Wt4 from tablename )A group by Wt_Type
A company rewards one of its employees every day. The data is maintained in a table called REWARDS. As shown in the sample data below, we have one row per day in the table with the following structure:
(Sorry for the poor table formatting)
REWARD_DATE EMP_ID REWARD_AMT
1-Jan-15 101 400
2-Jan-15 102 300
3-Jan-15 101 700
4-Jan-15 102 500
5-Jan-15 103 100
Can you write a query to report the running totals as the following?
REWARD_DATE #EMP TOT_REWARD_AMT
1-Jan-15 1 400
2-Jan-15 2 700
3-Jan-15 2 1400
4-Jan-15 2 1900
5-Jan-15 3 2000
You could do this (slightly simplified now):
SELECT b.rdate, COUNT(distinct a.eid) empcnt, SUM(a.amnt) total
FROM tbl a
INNER JOIN tbl b ON b.rdate>=a.rdate
GROUP BY b.rdate
as demonstrated here: http://sqlfiddle.com/#!9/f6871/2
I'm having a Two Tables one is Sales and another one is BrandMaster
My Requirement : How to Sum a single Column based on grouping in MySQL
The Structure and Data of Sales Table:
SNo BID Amount
-----------------------------------
101 1 200
102 2 500
103 5 800
104 8 250
105 1 200
106 2 500
107 5 800
108 8 250
The Structure and Data of BrandMaster Table:
BID BrandName
-------------------------
1 Prod#1
2 Prod#2
3 Prod#3
4 Prod#4
5 Prod#5
6 Prod#6
7 Prod#7
8 Prod#8
My Expected Output:
BID SumAmount
-------------------------
1 400
2 500
3 0
4 0
5 0
6 1600
7 0
8 500
The Sales Table contains the BrandID 'BID' and the Sales Amount with Sales ID 'SNo'. I need the Sum of Sales Amount for each Product. Kindly assist me.
You can try this
SqlFiddle Demo
SELECT
BrandMaster.BID,
IFNULL(SUM(Sales.Amount),0) AS SumAmount
FROM BrandMaster
LEFT JOIN Sales ON ( BrandMaster.BID=Sales.BID )
GROUP BY
BrandMaster.BID
ORDER BY
BrandMaster.BID,
Sales.SNo
Here you go!
SELECT db.sales.bid AS "BID",
<AGGREGATE>(db.sales.amount) AS "SumAmount"
FROM db.sales
GROUP BY db.sales.bid
ORDER BY db.sales.bid;
Edit: db is your database, and sales is your, "Sales," table.
Second Edit: replace <AGGREGATE> with something from here.
I have to do some reporting, involving various tables, and having couple of SUMs, COUNTs, etc and everything is OK. But the last thing I have to resolve is SUM by another which is not in the grouped columns.
I'll give you an example (stripped down from what I have) so you can understand the tongue-twister in the previous paragraph.
Suppose I have a query with a couple of joins that get me this result, or a temporary table, or whatever:
(this is a trimmed down version, in the original I have much more columns and groupbys)
APP_ID CAT_ID CAT_DESCRIP APP_START APP_END DETAIL_ID DET_QTY DETAIL_PRICE
1 1 Categ One 900 960 1 10 150.00
1 1 Categ One 900 960 2 8 20.00
1 1 Categ One 900 960 3 12 30.00
1 1 Categ One 900 960 4 5 100.00
2 2 Categ Two 600 720 5 12 150.00
2 2 Categ Two 600 720 6 10 50.00
3 2 Categ Two 1200 1260 7 5 20.00
I need to get something like this: (the bolded column is the important)
SELECT
CAT_ID,
CAT_DESCRIP,
SUM(DET_QTY) as TotalQTY,
SUM(DETAIL_PRICE) as TotalPrice,
COUNT(DISTINCT APP_ID) as CountOfApps,
(GET THE SUM OF (APP_END - APP_START) ONLY ONE TIME BY APP_ID INTO THIS CATEG) as TimeInMinutesByCategory
FROM
MyTable
GROUP BY
CAT_ID
And the result has to give me this:
CAT_ID CAT_DESCRIP TotalQTY TotalPrice CountOfApps TimeInMinutesByCategory
1 Categ One 35 300.00 1 60
2 Categ Two 27 220.00 2 180
Thanks for your help!
I think this will do the job... or if not, a little tweaking on the sytnax for max(app_start) - max(app_end) should do the job
The idea is, summarize the data in a subquery by app_id and cat_id. Select the max value of start and end, grouped by app_id and cat_id. Since there will only be one value per each distinct pair of app_id and cat_id, we're essentially just deduping.
Then, join the subquery to the main query and summarize by category id.
SELECT
a.CAT_ID,
a.CAT_DESCRIP,
SUM(a.DET_QTY) as TotalQTY,
SUM(a.DETAIL_PRICE) as TotalPrice,
COUNT(DISTINCT a.APP_ID) as CountOfApps,
SUM(b.TimeInMinutesByCategory) AS TimeInMinutesByCategory
FROM
MyTable AS a
INNER JOIN (
SELECT APP_ID, CAT_ID, max(app_start) - max(app_end) AS TimeInMinutesByCategory
FROM MyTable
GROUP BY APP_ID, CAT_ID) AS b
ON a.cat_id = b.cat_id
AND a.app_id = b.app_id
GROUP BY
a.CAT_ID
I've got this table:
id | payment_id | quantity | unit cost
1 633 1 750
2 633 1 750
3 632 2 750
4 632 2 750
What I need is :
payemnt_id | total
633 1500
632 3000
You can also think of this as Countries and then you are trying to find the total for each Country. I was sure there were some tutorials like this but could not find by STFW.
You can simply put the formula (expression) inside SUM:
SELECT payment_id, SUM(`unit cost` * quantity) AS total
FROM myTable
GROUP BY payment_id
SELECT
payment_id,
SUM(subtotal) AS total
FROM(
SELECT
payment_id,
(unit_cost * quantity) AS subtotal
) AS t1
GROUP BY
payment_id
SELECT payemnt_id, SUM(`unit cost` * quantity) as total
FROM Table1
GROUP BY payemnt_id
SELECT COALESCE (SUM(cost_field * quantity_field),0) AS tot FROM Table_Name
I Think This One Is Good, Why ?
Because If Their is No Result It Will Give You Zero