The table A looks like
number
amount
date
100
122.3
30-JUN-21
100
152.3
31-DEC-21
100
122.3
20-JUN-21
100
122.3
30-JUN-22
122
132.3
30-JUN-21
122
142.3
31-DEC-21
142
132.3
20-JUN-21
142
152.3
30-JUN-22
and the output of query should look like..
number
amount
date
100
244.6
20-JUN-21
122
132.3
30-JUN-21
142
132.3
20-JUN-21
The agenda is to sum the amount by taking minimum of date group by number and if some other date exists in same minimum month that also has to be included.Can anyone help me with the query?
select sum(amount),min(date) from A group by number;
Write a subquery that gets the minimum date for each number. Then join that with the table to get all the rows in the same month and number, and sum the amounts.
SELECT t1.number, SUM(t1.amount) AS amount, t2.date
FROM A AS t1
JOIN (
SELECT number, MIN(date) AS date
FROM A
GROUP BY number
) AS t2 ON t1.number = t2.number AND YEAR(t1.date) = YEAR(t2.date) AND MONTH(t1.date) = MONTH(t2.date)
GROUP BY t1.number
DEMO
Related
I have the following table and I just want to figure out the revenue difference by day. Like day 1 revenue and day 2=day 1- day2 so on
Date CustomerID Quantity Price Revenue
2020-1-1 C1 4 10 40
2020-1-2 C2 7 20 140
2020-1-3 C3 8 50 400
2020-1-4 C4 5 90 450
2020-1-5 C5 8 60 480
2020-1-6 C6 9 100 900
The expected result is
Date Revenue_Difference
2020-1-1 40
2020-1-2 100 i.e. 140-40
2020-1-3 260 i.e. 400-140
2020-1-4 50 i.e. 450-400
If you are using MySQL 8+, then you may use LAG here:
SELECT
Date,
Revenue - LAG(Revenue, 1, 0) OVER (ORDER BY Date) AS Revenue_Difference
FROM yourTable
ORDER BY Date;
On earlier versions of MySQL, you may use a correlated subquery in place of LAG:
SELECT
Date,
Revenue - (SELECT t2.Revenue FROM yourTable t2
WHERE t2.Date < t1.Date
ORDER BY t2.Date DESC LIMIT 1) AS Revenue_Difference
FROM yourTable t1
ORDER BY Date;
Let's say I have two tables, one storing accounts, one storing transactions :
id acct_name opening_bal
1 checking 1029.99
2 savings 2002.19
...
And
id date amount from_acct to_acct
...
99 2018-01-21 12.15 1 2
100 2018-01-21 9.99 4 1
101 2018-01-23 10.01 5 2
...
For example, row 99 in the transactions table is saying that 12.15 was transfered from checking to savings on 21 Jan 2018.
I would like to write a query that returns all accounts together with their balances on a given date (like today or 12 Oct 2018, etc), something like this :
acct balance
checking 1599.21
savings 2221.99
...
How would I write such a query?
Edit: Here's a solution, which is close enough to what I want (it just has an additional id column). You can replace CURDATE() with an arbitrary date to get the corresponding table of balances on that date.
SELECT id, acct_name, opening_bal+amt-amt2 as balance FROM accounts
INNER JOIN
(SELECT to_acct, sum(amount) AS amt
FROM transactions
WHERE date <= CURDATE()
GROUP BY to_acct) as T2
ON accounts.id=T2.to_acct
INNER JOIN
(SELECT from_acct, sum(amount) AS amt2
FROM transactions
WHERE date <= CURDATE()
GROUP BY from_acct) as T3
ON T2.to_acct = T3.from_acct
;
Something like this. If you'd provided more data, answer could be more precise. Table1 is your first table, Table2 is the second.
select acct_name as acct,
opening_bal - t1.put_money + t2.get_money as balance
from Table1
left join (select from_acct, ifnull(sum(amount),0) as put_money from Table2 group by from_acct) t1
on t1.from_acct = Table1.id
left join (select to_acct, ifnull(sum(amount),0) as get_money from Table2 group by to_acct) t2
on t2.to_acct = Table1.id;
I have two tables. One with amounts in quarterly increments, the other with amounts in annual increments. I want to join the two and sum FEES and COUNTS without double counting.
Table 1
CLM_NUM Year Quarter FEES
1234 2016 1 100
1234 2016 2 100
1234 2016 3 100
1234 2016 4 100
Table 2
CLM_NUM Year COUNT
1234 2016 10
Desired Result:
CLM_NUM Year FEES COUNT
1234 2016 400 10
However, my query causes the counts to be counted four times resulting in 40, not 10.
How do I resolve this?
SELECT Table1.CLM_NUM, Table1.YEAR, Sum(Table1.FEES) AS SumOfFEES,
Sum(Table2.COUNT) AS SumOfCOUNT
FROM Table1 INNER JOIN Table2 ON (Table1.CLM_NUM = Table2.CLM_NUM) AND (Table1.YEAR = Table2.YEAR)
GROUP BY Table1.CLM_NUM, Table1.YEAR;
You can use a sub-query. The SUM in the sub-query means it would work even if table 2 wasn't yearly (i.e. there were more rows per CLM_NUM/Year key).
SELECT
t1.clm_num,
t1.year,
sum(t1.fees),
(SELECT
sum(t2.count)
FROM
Table2 t2
WHERE t1.CLM_NUM = t2.CLM_NUM
AND t1.Year = t2.Year
GROUP BY
t2.CLM_NUM, t2.Year
) AS SumOfCount
FROM
Table1 t1
GROUP BY
t1.CLM_NUM, t1.Year
if access doesnt have max, use first
http://sqlfiddle.com/#!18/7ec1d/1
select t1.clm_num, t1.year,sum(t1.fees),max(t2.count)
from Table1 t1 inner join
Table2 t2 on t2.clm_num = t1.clm_num and (t1.YEAR = t2.YEAR)
GROUP BY t1.CLM_NUM, t1.YEAR
I have two tables
t1
id Name Total
1 Alex 100
2 Bob 100
1 Alex 100
t2
id Amount
1 2
1 3
1 4
2 12
2 13
I need to get sum of Total and Amount.
**select Name, sum(Total) as Total, sum(Amount) as Amount,day
from t1,t2
Where t1.id=t2.id
group by Name**
Result:
Alex 600 18
Bob 200 25
Incorrect sum of Amount!
**select Name, sum(distinct Total) as Total, sum(Amount) as Amount,day
from t1,t2
Where t1.id=t2.id
group by Name**
Result:
Alex100 18
Bob 100 25
Incorrect sum of Amount.
MySql use distinct by value, i need distinct by id
corect result that need be is
Alex 200 18
Bob 100 25
How to get to this result?
select t1.Name,
sum(t1.Total) as Total,
sum(t2.Amount) as Amount,
day
from t1
left join
(
select id, sum(Amount) as Amount
from t2
group by id
) t2 on t1.id = t2.id
group by t1.Name
Table 1:
Date PlacementID CampaignID Impressions
04/01/2014 100 10 1000
04/01/2014 101 10 1500
04/01/2014 100 11 500
Table 2:
Date PlacementID CampaignID Cost
04/01/2014 100 10 5000
04/01/2014 101 10 6000
04/01/2014 100 11 7000
04/01/2014 103 10 8000
When I have joined this table using Full Join and Left Join statement, I am not able to get uncommon record which is last row in table2 that display PlacementID 103 and campaignID 10 and Cost 8000. However I have searched all raw data and file but this missing records are not common between two sources. However, I want to include this records in final table. How can I do that? This two table are two different source and I have got results only common records.
Moreover, when I found out that missing value is exact value that are required in final figure so want to include every thing. I am including my SQL script below:
SELECT A.palcementid,
A.campaignid,
A.date,
Sum(A.impressions) AS Impressions,
Sum(CASE
WHEN C.placement_count > 1 THEN ( B.cost / C.placement_count )
ELSE B.cost
END) AS Cost
FROM table1 A
FULL JOIN table2 B
ON A.placementid = B.placementid
AND A.campaignid = B.campaignid
AND A.date = B.date
LEFT JOIN (SELECT Count(A.placementid) AS Placement_Count,
placementid. campaignid,
date
FROM table1
GROUP BY placementid,
campaignid,
date) c
ON A.placementid = C.placementid
AND A.campaignid = C.campaignid
AND A.date = C.date
GROUP BY A.placementid,
A.campaignid,
A.date
I am dividing Cost by placement because in source the cost was allocated for one placement only and one time so I have to divide those because in actual table the same Placementid repeat more than 1 times on same date.
As you didn't provide any expected output I guessing here but if the result you want is this:
PlacementID CampaignID Date Impressions Cost
----------- ----------- ----------------------- ----------- -----------
100 10 2014-04-01 02:00:00.000 1000 5000
100 11 2014-04-01 02:00:00.000 500 7000
101 10 2014-04-01 02:00:00.000 1500 6000
103 10 2014-04-01 02:00:00.000 NULL 8000
Then the following query should do it:
SELECT COALESCE(A.PlacementID,b.placementid) AS PlacementID,
COALESCE(A.campaignid, b.campaignid) AS CampaignID,
COALESCE(A.date, b.date) AS [Date],
SUM(A.impressions) AS Impressions,
SUM(CASE
WHEN C.placement_count > 1 THEN ( B.cost / C.placement_count )
ELSE B.cost
END ) AS Cost
FROM table1 A
FULL JOIN table2 B
ON A.[PlacementID] = B.placementid
AND A.campaignid = B.campaignid
AND A.date = B.date
LEFT JOIN (SELECT COUNT(PlacementID) AS Placement_Count,
placementid, campaignid,
date
FROM table1
GROUP BY placementid,
campaignid,
date) c
ON A.[PlacementID] = C.placementid
AND A.campaignid = C.campaignid
AND A.date = C.date
GROUP BY COALESCE(A.PlacementID, B.PlacementID),
COALESCE(A.campaignid, b.campaignid),
COALESCE(A.date, b.date)
Sample SQL Fiddle