Calculation to be made while updating a field - mysql

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

Related

mysql 5.6 order first then group by

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.

Cumulative Adding in SQL SERVER 2008

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;

Date conversion using SSIS

I am trying to load excel data into a table using SSIS.
In my excel, there is one column 'TRANSACTION DATE'.
TRANSACTION_DATE IN EXCEL TRANSACTION_DATE IN TABLE
01/12/2014 2014-01-12 00:00:00.000
01/12/2014 2014-01-12 00:00:00.000
02/12/2014 2014-02-12 00:00:00.000
05/12/2014 2014-05-12 00:00:00.000
05/12/2014 2014-05-12 00:00:00.000
13/12/2014 2014-12-13 00:00:00.000
16/12/2014 2014-12-16 00:00:00.000
16/12/2014 2014-12-16 00:00:00.000
19/12/2014 2014-12-19 00:00:00.000
20/12/2014 2014-12-20 00:00:00.000
22/12/2014 2014-12-22 00:00:00.000
26/12/2014 2014-12-26 00:00:00.000
29/12/2014 2014-12-29 00:00:00.000
31/12/2014 2014-12-31 00:00:00.000
31/12/2014 2014-12-31 00:00:00.000
Problem:
If you observe first 5 rows in the table values, the syntax is quite different with other values of the table.it should be (2014-12-01,2014-12-02 etc..). Because of this issue, if I fire a query to sum the values of of another column where DATEPART(MM,TRANSACTION_DATE)=12, The top 5 values are getting excluded.
Table Structure :
TRANSACTION_DATE DATETIME
SSIS DATA CONVERSION :
TRANSACTION_DATE DT_DBDATE
What's happening is the first 5 records are being parsed in the American format (MM/DD/YYYY), and the rest of the records are being parsed as DD/MM/YYYY.
Check the datetime format of the cells in Excel to make sure the Locale setting is consistent for all the dates.

How to get any three rows of particular date in SQL Server

I am stucking in one sql query.
My table format is the below:
Acct# AdmitDate DOS ChargeAmount
12366 2011-12-07 00:00:00.000 2011-12-15 00:00:00.000 25.00
12366 2011-12-07 00:00:00.000 2011-12-16 00:00:00.000 30.00
12366 2011-12-07 00:00:00.000 2011-12-17 00:00:00.000 55.00
12366 2011-12-07 00:00:00.000 2011-12-18 00:00:00.000 48.00
12366 2011-12-07 00:00:00.000 2011-12-19 00:00:00.000 25.00
12366 2012-01-08 00:00:00.000 2012-01-08 00:00:00.000 58.00
12366 2012-01-08 00:00:00.000 2012-01-09 00:00:00.000 46.00
12366 2012-01-08 00:00:00.000 2012-01-10 00:00:00.000 90.00
12366 2012-01-08 00:00:00.000 2012-01-11 00:00:00.000 52.00
12366 2012-01-08 00:00:00.000 2012-01-12 00:00:00.000 95.00
12366 2012-01-08 00:00:00.000 2012-01-13 00:00:00.000 53.20
I want only top 3 DOS by one Admit Date and sum of their ChargeAmount. All the other DOS should be neglact. I have no idea how to do this. I want the below desired output.
Acct# AdmitDate ChargeAmount
12366 2011-12-07 00:00:00.000 128.00
12366 2012-01-08 00:00:00.000 200.00
only 2011-12-17 to 2011-12-19 for Admit Date 2011-12-07 need to be sum and do the same for other Admit date.
If you have any sql query please share with me.
Thanks in advance
Try:
SELECT TOP 3 Acct, AdmitDate, SUM(ChargeAmount)
FROM YourTable
GROUP BY Acct, AdmitDate
Edit
SELECT Acct, AdmitDate, SUM(ChargeAmount)
, RANK() OVER (PARTITION BY AdmitDate ORDER BY AdmitDate DESC)
FROM YourTable
GROUP BY Acct, AdmitDate
Select
Acct#,
AdmitDate,
sum(ChargeAmount) ChargeAmount
from
(Select
Acct#,
AdmitDate,
row_number() over(partition by AdmitDate order by DOS desc) RowNo,
ChargeAmount
from TableName
where Acct# = #AcctNumber
) as t
where (RowNo = 1 or RowNo = 2 or RowNo = 3)
group by Acct#, AdmitDate
I have found the query. This query is working fine for me. Thanks for your efforts.

JOIN unrelated tables by t1.date (between t2.startdate and t2.enddate)

A SQL question, probably not the most difficult.
I'm making a view from a bunch of related table join on ID's -> easy. Now there is one table that hasn't got a key relationship with all the others. (BatchDates)
`ALTER VIEW [ECSUB].[FCT_Ext_Collection]
AS
SELECT sh.id AS idSubmissionHistory, dh.id, dd.id AS Description, sch.id AS idScoringHistory, sh.CreationDate, sh.UpdateDate, bd.id AS BatchDateID
FROM ECSUB.SubmissionHistory AS sh INNER JOIN EC.DocumentHistory AS dh ON sh.id = dh.idSubmissionHistory
LEFT OUTER JOIN ECSM.ScoringHistory AS sch ON sh.idScoringHistory = sch.id
LEFT OUTER JOIN EC.DocumentDescriptions AS dd ON dd.id = dh.Description
LEFT OUTER JOIN ECSUB.AddressBilling AS ab ON sh.id = ab.id
LEFT OUTER JOIN ECSUB.AddressPremise AS ap ON sh.id = ap.id
CROSS JOIN EC.BatchDates AS bd --ON sh.documentdate between .......
GO`
Well, my main table 'documentHistory' contains a document date, I have to define in which batch this falls.
Each batch has an ID and startdate. A batch is always one month long.
This will make it much more easy to understand, the data from the BatchDates table:
id month startdate
1 2010-12-01 00:00:00.000 2010-12-01 00:00:00.000
1 2011-01-01 00:00:00.000 2010-12-01 00:00:00.000
1 2011-02-01 00:00:00.000 2010-12-01 00:00:00.000
2 2011-03-01 00:00:00.000 2011-03-01 00:00:00.000
2 2011-04-01 00:00:00.000 2011-03-01 00:00:00.000
2 2011-05-01 00:00:00.000 2011-03-01 00:00:00.000
3 2011-06-01 00:00:00.000 2011-06-01 00:00:00.000
3 2011-07-01 00:00:00.000 2011-06-01 00:00:00.000
3 2011-08-01 00:00:00.000 2011-06-01 00:00:00.000
4 2011-09-01 00:00:00.000 2011-09-01 00:00:00.000
4 2011-10-01 00:00:00.000 2011-09-01 00:00:00.000
4 2011-11-01 00:00:00.000 2011-09-01 00:00:00.000
5 2011-12-01 00:00:00.000 2011-12-01 00:00:00.000
5 2012-01-01 00:00:00.000 2011-12-01 00:00:00.000
5 2012-02-01 00:00:00.000 2011-12-01 00:00:00.000
6 2012-03-01 00:00:00.000 2012-03-01 00:00:00.000
6 2012-04-01 00:00:00.000 2012-03-01 00:00:00.000
6 2012-05-01 00:00:00.000 2012-03-01 00:00:00.000
7 2012-06-01 00:00:00.000 2012-06-01 00:00:00.000
7 2012-07-01 00:00:00.000 2012-06-01 00:00:00.000
7 2012-08-01 00:00:00.000 2012-06-01 00:00:00.000
8 2012-09-01 00:00:00.000 2012-09-01 00:00:00.000
8 2012-10-01 00:00:00.000 2012-09-01 00:00:00.000
8 2012-11-01 00:00:00.000 2012-09-01 00:00:00.000
9 2012-12-01 00:00:00.000 2012-12-01 00:00:00.000
9 2013-01-01 00:00:00.000 2012-12-01 00:00:00.000
9 2013-02-01 00:00:00.000 2012-12-01 00:00:00.000
10 2013-03-01 00:00:00.000 2013-03-01 00:00:00.000
10 2013-04-01 00:00:00.000 2013-03-01 00:00:00.000
10 2013-05-01 00:00:00.000 2013-03-01 00:00:00.000
etc...........
So I need to fetch the batchID based on the documentdate, therefore we use the currentMonth of the column startdate.
Thus: ...JOIN BatchDates where documentDate is in startDate.month (there is no between here)
I don't even know if I need a join, cross join, union, etc...
Thanks in advance!
L
join BatchDates
on datepart(yyyy,[document date]) = datepart(yyyy,[startDate])
and datepart(mm,[document date]) = datepart(mm,[startDate])