I am using MySQL 5.6, and creating a view. It seems MySQL 5.6 view does not support subquery inside a view. I have two tables like the following:
table 1
account balance date time in out
408 100.00 2018-03-09 11:36:47 100.00 0.00
408 86.00 2018-03-09 11:54:48 0.00 14.00
408 74.00 2018-03-09 11:54:48 0.00 12.00
408 21.00 2018-03-09 11:54:48 0.00 13.00
408 11.00 2018-03-09 11:54:48 0.00 10.00
408 0.00 2018-03-09 11:54:48 0.00 11.00
408 13000000.00 2018-03-09 19:15:04 13000000.00 0.00
408 12999880.00 2018-03-10 00:51:37 0.00 120.00
408 12999640.00 2018-03-10 00:51:48 0.00 240.00
table2
account name
999 bank1
408 bank2
The view is created like this:
create view test
select table1.account, table2.name, table1.balance, table1.date from (table1 join table2) where table1.account=table2.account group by table1.account, table1.date
The problem is that the view picked out the first time, showed the first balance. But I want the max time on that day. For example, the view results are like:
408 bank1 100 2018-03-09
408 bank1 12999880.00 2018-03-10
The MySQL 5.6 does not allow me to use a subquery. So is there another way to show the correct balance like this:
408 bank1 13000000.00 2018-03-09
408 bank1 12999640.00 2018-03-10
which shows the lastest balance of the last record on that day.
Related
Maybe I am trying to do something that can't be done, as my searches don't seem to find any similar examples.
I am trying to write a query in MariaDB that lists the lineitems (detail) as well as the subtotals, totals, and grand totals.
So for instance if I have the following data:
DonationDate
UserContactID
DonationAmount
2021-05-20
613
5.00
2021-05-20
613
40.00
2021-06-22
613
6.00
2021-06-22
613
10.00
2021-06-22
777
10.00
2021-06-22
777
30.00
DonationDate
UserContactID
DonationAmount
2021-05-20
613
5.00
2021-05-20
613
40.00
sub for 613 on 2021-05-20
45.00
tot for 2021-05-20
45.00
2021-06-22
613
6.00
2021-06-22
613
10.00
Sub for 613 on 2021-06-22
16.00
2021-06-22
777
10.00
2021-06-22
777
30.00
Sub for 777 on 2021-06-22
40.00
tot for 2021-06-22
56.00
When I try to use rollups I lose the individual lines. For example, 2021-05-20 shows 45, not one line for 40 and the other for 5.
for example:
SELECT
DonationDate,UserContactID,DonationID, sum(DonationAmount)
FROM
donations
GROUP BY
DonationDate, UserContactID with Rollup`
The first two rows show up like this:
DonationDate
UserContactID
DonationAmount
2021-05-20
613
45
2021-05-20
45
Note that what we want is for that first 45 to show up as the detail items, 40 & 45, before showing up as as subtotal for those useids for that date.
Any help greatly appreciated. (P.S. The initial table with the data looks right on the preview, but I see it gets messed up when it actually posts. Sorry about that.)
How to get a record row wise date and datewise last record only display a total effort
106 Nishanthi st1234 Vitakids yutr 2018-03-19 2018-03-18 Completed 2018-03-18 10:00:00.000 2018-03-18 13:00:00.000 03:00 0.00 0.00 2018-03-19 09:57:34.750 UPS 2018-03-19 09:57:34.750 UPS
107 Nishanthi st1234 jakel we 2018-03-19 2018-03-18 Completed 2018-03-18 13:00:00.000 2018-03-18 14:00:00.000 01:00 0.00 0.00 2018-03-19 09:58:48.670 UPS 2018-03-19 09:58:48.670 UPS
108 Nishanthi st1234 Vitakids ds 2018-03-19 2018-03-18 Completed 2018-03-18 15:00:00.000 2018-03-19 18:00:00.000 03:00 7 : 0 0.00 0.00 2018-03-19 09:59:21.630 UPS 2018-03-19 09:59:21.630 UPS
S
This is what i have got in my SQL SERVER database table, where I am trying to calculate Balance Leave of an employee.
My actual data is:
EmpId EmpName EvalDate OpeningEL EnjoyedEL BalanceEL
12 CHANDRA 2014-04-01 18:30:00.000 0.95 0.00 0.95
12 CHANDRA 2014-05-01 18:30:00.000 1.30 0.00 1.30
12 CHANDRA 2014-06-01 18:30:00.000 1.20 1.00 1.20
12 CHANDRA 2014-07-01 18:30:00.000 1.25 0.00 1.25
12 CHANDRA 2014-08-01 18:30:00.000 1.25 1.00 1.25
But i need the data in below way
EmpId EmpName EvalDate OpeningEL EnjoyedEL BalanceEL
12 CHANDRA 2014-04-01 18:30:00.000 0.95 0.00 0.95
12 CHANDRA 2014-05-01 18:30:00.000 2.25 0.00 2.25
12 CHANDRA 2014-06-01 18:30:00.000 3.45 1.00 2.45
12 CHANDRA 2014-07-01 18:30:00.000 3.70 0.00 3.70
12 CHANDRA 2014-08-01 18:30:00.000 4.95 1.00 3.95
Previous BalanceELs are added with next OpeningELs.
So, how to achieve this....Please suggest something.
You can use CROSS APPLY and GROUP BY to achieve this
OpeningEL and BalanceEL from CROSS APPLY will get the sum of current and previous records for an employee.
SELECT
EL1.EmpId,
EL1.EmpName,
EL1.EvalDate,
Temp.OpeningEL,
EL1.EnjoyedEL,
Temp.BalanceEL
FROM EmployeeLeave EL1
CROSS APPLY
(
SELECT
SUM(OpeningEL) as OpeningEL,
SUM(BalanceEL) - SUM(EnjoyedEL) as BalanceEL
FROM EmployeeLeave EL2
WHERE EL2.EmpId = EL1.EmpId
AND EL2.EvalDate <= EL1.EvalDate
)Temp;
Origin Dest Date Amount 50% Due
92509 0021 2013-07-30 00:00:00.000 5.37 0.00
92509 0021 2013-07-30 00:00:00.000 5.37 0.00
92509 0021 2013-07-30 00:00:00.000 5.37 0.00
92509 0021 2013-07-31 00:00:00.000 5.37 2.69
92509 0021 2013-07-31 00:00:00.000 5.37 2.69
92509 0021 2013-07-31 00:00:00.000 5.37 2.69
92509 0021 2013-08-01 00:00:00.000 5.37 2.69
92509 0021 2013-08-01 00:00:00.000 5.37 2.69
42101 0029 2013-03-06 00:00:00.000 6.06 0.00
42101 0029 2013-03-06 00:00:00.000 6.06 0.00
42101 0029 2013-03-07 00:00:00.000 6.06 3.03
42101 0029 2013-03-07 00:00:00.000 6.06 3.03
42101 0030 2013-03-06 00:00:00.000 6.06 0.00
42101 0030 2013-03-06 00:00:00.000 6.06 0.00
42101 0030 2013-03-07 00:00:00.000 6.06 3.03
42101 0030 2013-03-07 00:00:00.000 6.06 3.03
So I have a table something similar to what i shown above. Right now, the 50% Due field is empty. I need to fill that field with values as shown above.
The 50% due field should populate values that are half of what is present in the Amount field. But, it should fill zero for the initial date (2013-07-30 00:00:00.000) and for the consecutive days it should fill half of what is present in the Amount field.
I have a lot of rows like these that needs to get updated. Also there are rows with different Origin and Destination.
I am dealing with some freight parcels. The data describes the parcels that were sent to the same destination from same origin on consecutive days. The parcels that were sent on consecutive days could have been sent together on the initial date itself. So I am trying to generate a claim for those parcels that were sent on consecutive days to the same destination from the same origin. And the 50% Due would be the claim!
I am fairly new to SQL! This seems to be very complicated for me. Please help.
If you want to update all origin/dest combinations that had an entry on the prior day then you can do this:
update t
set [50% due]=coalesce(cast(p.amount/2.0 as smallmoney),0.00)
from [table] t
left join [table] p
on t.origin=p.origin and t.dest=p.dest
and dateadd(D,-1,cast(t.[date] as date))=cast(p.[date] as date)
or use a cte and LAG in SQL 2012 and later versions:
;with previous as
(
select origin,dest,[date]
,LAG(cast([date] as date),1,cast([date] as date)) OVER (PARTITION BY origin,dest ORDER BY cast([date] as date)) as previous
from (select distinct origin, dest, [date]
from [table]) a
)
update t
set [50% Due]=case when dateadd(D,-1,cast(t.[date] as date))<>cte.previous then 0.00 else cast(t.[amount]/2.0 as smallmoney) end
from [table] t
join previous cte
on cte.origin=t.origin and cte.dest=t.dest and cte.[date]=t.[date]
If you want to update all your records based on the earliest day that the origin/dest combination occurred, then this will work in SQL Server:
;with earliest as
(
select origin,dest,min(cast([date] as date)) earliest
from [table]
group by origin,dest
)
update t
set [50% Due]=case when cast(t.[date] as date)=cte.earliest then 0.00 else cast(t.[amount]/2.0 as smallmoney) end
from [table] t
join earliest cte
on cte.origin=t.origin and cte.dest=t.dest
If you want to update all your records only based on the earliest day in the table and you don't care about the orgin/dest combination then you don't need the cte to group the earliest dates and you can simply do the compare in the update.
UPDATE t
set [50% Due]=case when cast(t.[date] as date)=(select min(cast(t.date as date))) then 0.00 else cast(t.[amount]/2.0 as smallmoney) end
(Please refer to SQLFiddle for a working example of this post)
I have a table with stock information, as follows:
sp100_id _date bullishness agreement
----------------------------------------------
1 2011-03-16 1.01 0.33
1 2011-03-17 0.85 1.28
1 2011-03-18 0.89 1.25
1 2011-03-21 1.46 1.21
1 2011-03-22 0.39 2.53
2 2011-03-16 3.07 1.27
2 2011-03-17 2.09 0.80
2 2011-03-18 0.91 0.12
2 2011-03-21 1.50 0.00
2 2011-03-22 2.62 1.10
3 2011-03-16 0.73 1.13
3 2011-03-17 1.13 1.21
3 2011-03-18 1.12 0.45
3 2011-03-21 1.00 1.01
3 2011-03-22 1.00 0.53
4 2011-03-16 0.40 1.10
4 2011-03-17 2.40 0.03
4 2011-03-18 3.16 0.10
4 2011-03-21 0.86 0.50
4 2011-03-22 1.00 0.10
I need to order the companies (sp100_id) by their averge bullishness over time into a top-3:
SELECT
sp100_id,
AVG(bullishness) as bullishness,
AVG(agreement) AS agreement
FROM stocks
WHERE _date BETWEEN '2011-03-16' AND '2011-03-22'
GROUP BY sp100_id LIMIT 3
This works fine, as the result is
SP100_ID BULLISHNESS AGREEMENT
2 2.038 0.658
4 1.564 0.366
3 0.996 0.866
Now that I have the top-3, I need the top-3 to be re-ordered by AGREEMENT, ascending:
SP100_ID BULLISHNESS AGREEMENT
4 1.564 0.366
2 2.038 0.658
3 0.996 0.866
Is this possible to do with one query? I tried the following but it didn't work. It still only orders by bullishness
SELECT
sp100_id,
AVG(bullishness) as bullishness,
AVG(agreement) AS agreement
FROM stocks
WHERE _date BETWEEN '2011-03-16' AND '2011-03-22'
GROUP BY sp100_id
ORDER BY bullishness DESC, agreement ASC LIMIT 3
So to be clear: (1) I need to find the top-3 companies with highest average bullsihness (2) this top-3 then needs to be ordered from lowest to highest agreement. Preferably with one query. Do you know how?
It's called structured query language because you can build structures in which queries (aka virtual tables) are nested inside other queries.
Take your first query, which is correct except it needs its own ORDER BY clause, and nest it in another, like so.
SELECT *
FROM (
SELECT sp100_id,
AVG(bullishness) as bullishness,
AVG(agreement) AS agreement
FROM stocks
WHERE _date BETWEEN '2011-03-16' AND '2011-03-22'
GROUP BY sp100_id
ORDER BY bullishness DESC
LIMIT 3
) subquery
ORDER BY agreement ASC
Go fiddle: http://sqlfiddle.com/#!2/c9ff0/7/0