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
Related
I have two tables that I need to merge.
Table 1 is :
ID
Product code
Spend
1
101
100
1
102
200
1
103
300
2
201
400
3
301
500
3
302
600
Table 2 has
ID
Product code
Spend
Product tenure
1
101
100
20
1
102
200
30
3
302
600
40
I want to merge these such that only ID's present in table 2 are retained from table 1. Table 2 does not contain all the product codes for each ID, but I want my final table to have it.
Output must have
ID
Product code
Spend
Product tenure
1
101
100
20
1
102
200
30
1
103
300
3
301
500
3
302
600
40
Any help on this would be appreciated. I tried left join on ID, but it produces many duplicates.
SELECT *,
(
SELECT `product_tenure`
FROM `second_table`
WHERE `second_table`.`id` = `first_table`.`id`
AND `first_table`.`product_code` = `second_table`.`product_code`
) product_tenure
FROM `first_table`
WHERE `id` IN (SELECT DISTINCT `id` FROM `second_table`)
Explaination:
Select id from second table, which wanted to keep from first table.
Because the product_tenure only in second_table select them with combined id and product_code
Result:
Test this:
SELECT *
FROM t1
LEFT JOIN t2 AS t21 USING (ID, `Product code`)
WHERE EXISTS ( SELECT NULL
FROM t2 AS t22
WHERE t1.ID = t22.ID )
PS. The query will return 2 Spent columns (they're present in each table, and nothing prevents their values to be not equal...) - so replace an asterisk with definite columns list.
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 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
I have 2 tables. The first one is refreshed daily.(This table has more that 10 columns but 2 of those is relevant) I would like to make a daily statistics from vid (which is a unique id ) and population. New vid ID-s can appear and disappear every day. For example :
1st day :
vid population
123 456
124 567
345 1024
2nd day :
vid population
123 470
124 520
344 100
The second table will be the statistics, and i would like the following result :
|--------------------1st day data
| |----------- 2nd day data
vid stat0819 stat0820
123 456 470
124 567 520
344 0 100
345 1024 0
Is it possible with one SQL query? I think, the UNION is the key, but I can't figure it out how.
Is this maybe what you are looking for ?
select tbl1.vid, tbl1.population as tbl1_population, tbl2.population as tbl2_population
from tbl1
left join tbl2 on tbl2.vid = tbl1.vid
union
select tbl2.vid, tbl1.population as tbl1_population, tbl2.population as tbl2_population
from tbl1
right join tbl2 on tbl2.vid = tbl1.vid;
I'm trying to make a crosstab query (with access tables), But I got lost writing the inner joins statements.
My end result suppose to be the "QueryResult".
Table1 holds the fund information,
Table2 are the type of data the funds have
Table3 is a conversion from the codes of the data to the type data in table2, and Table4 holds the data.
Table1
FundID FundName
1 Fund1
2 Fund2
3 Fund3
4 Fund4
5 Fund5
6 Fund6
7 Fund7
Table2
TypeID TypeName
1 Balance
2 Yield
3 Fees
4 Deposits
5 Withdraws
Table3
CodeID TypeID
KT111 1
KT112 2
KT113 3
KT115 3
KT116 4
KT117 4
KT118 5
KT119 5
Table 4
CodeID FundID DataVal
KT111 1 1000
KT116 2 40
KT118 3 30
KT119 3 30
KT118 2 10
KT119 2 50
KT111 2 3000
KT111 3 2000
KT112 1 1.5
KT112 2 1.0
KT112 3 0.5
P.S: Table4 holds much rows then shown here with codes which I do not need.
QueryResult
FundID Balance Yield Fees Deposits Withdraws
1 1,000 1.5 555 40 60
2 3,000 1.0 155 20 60
3 2,000 0.5 255 70 60
What is the right statement to get the query result? (I got lost on the inner joins...)
Is there also a way to sum some of the data, and show the value (without summing) of other data from table4?
Thanks!
while I am not exactly sure of all of the requirements you have, this might get you started:
TRANSFORM Sum(d.dataval) as DataValue
SELECT d.fundid
FROM
((Data d
INNER JOIN fund f ON d.fundid = f.fundid)
INNER JOIN code c ON d.codeid = c.codeid)
INNER JOIN type t on c.typeid = t.typeid
GROUP BY d.fundid
PIVOT T.Typename
Results:
fundid Balance Deposits Withdraws Yield
1 1000 1.5
2 3000 40 60 1
3 2000 60 0.5