Tuesday, 26 January 2021

SQL Project

 SQL Project Code at Suryoday Small Finance Bank


---#19816 Rera Account EOD Balance

select d.boo
      ,[desc]
      ,d.title1
      ,d.cid
      ,d.bal
      ,d.as_on_dt
from profile.dbo.deposit d join profile.dbo.brnch_master b
on d.boo = b.brcd and 
   b.AS_ON_DTE = convert(date,getdate()-1)
where d.type = 2010 and 
not(PGK = 1 and BAL = 0) and 
STAT <> 4 and 
d.AS_ON_DT between dateadd(day,1,EOMONTH(getdate()-1,-1)) and convert(date,(getdate()-1));

----To monitor credit transactions in Rera Master Collection Account

use PROFILE

declare @tdate date = convert(date,getdate()-2)
SELECT t.AS_ON_DTE AS_ON
       ,ROW_NUMBER() over (order by t.cid) [Sr.No]
       ,tied [Account No]
	   ,sum(t.TAMT) Total_Credit_Bal
	  ,0.7*sum(t.TAMT) [70% of Credit Transaction]
	  ,0.3*sum(t.TAMT) [30% of Credit Transaction]
from	dbo.TXN_HIST t
		join dbo.DEPOSIT d
		on t.CID = d.CID
where
		d.TYPE = 2010
		and not(d.PGK = 1 and BAL = 0) and d.STAT <> 4
		and t.AS_ON_DTE = @tdate
		and d.AS_ON_DT = @tdate and DRCR = 1
group by 
		T.AS_ON_DTE,t.CID


------To Query GL Balance Group Loan Balance

select sum([GL Balance])
from
(
select g.CC [Branch Code]
     , b.[DESC] [Branch Name]
     , b.STATENAM [State Name],'         ' "CRL Limit"
     ,g.BAL [GL Balance]
from GL_BALANCE g join BRNCH_MASTER b on g.CC = b.BRCD
where ACN = '301010060001'
and g.AS_ON_DTE = convert(varchar,getdate()-1,106) 
and b.AS_ON_DTE = convert(varchar,getdate()-1,106)
order by [Branch Name] ) t


----Cash Retention Limit report - URC


select * from
(select g.cc
      ,b.[DESC] [Branch_Name]
      , G.ACN,isnull(g.bal,0) [glbal]
from GL_BALANCE g join BRNCH_MASTER b on g.CC = b.BRCD
and g.AS_ON_DTE = '20 Oct 2020' 
and b.AS_ON_DTE = '20 Oct 2020') t
pivot ( sum(glbal)
for acn in ([300110010001],[300410010001],[300410020001],[301010060001],[301010010001],[301010020001],[301010030001],[301010040001],[301010050001],[301110010001],[301210010001],[301020010001],[301410010001] )) pivottable

----Count of DD issued

select c.DBRANCH [Home Branch Code]
      ,c.PAYAT [Transaction done at Branch Code]
      ,c.PAYDATE
      ,c.SERNO [DD Serial No.]
      ,c.AMOUNT
      ,c.PAYEENAME [Payee Name]
      ,c.PURCHACN
from CASH_DDPO c
join (
select DBRANCH
      ,count(remtype) cremtype
from CASH_DDPO 
where STATUS = 'paid'
group by DBRANCH
having count(remtype) >= 50) t
on c.DBRANCH = t.DBRANCH
where c.STATUS = 'paid'


----Withdrawal Transaction Lien Marked Acnt


select h.cid
      ,h.tamt
      ,h.tcmt
      ,h.DRCR
      ,h.ETC
from TXN_HIST h
join DEPOSIT d on h.CID = d.CID
--select d.CID,d.hldamt from DEPOSIT d
where 
d.HLDAMT > 0
and h.DRCR = 0
and d.TYPE between 3000 and 4000
and d.AS_ON_DT = '1 Nov 2020'
--and h.AS_ON_DTE = '1 Nov 2020'
and d.BAL > 0
and not(d.PGK = 1 and BAL = 0) and d.STAT <> 4 and h.TCMT <> 'WithHold(TDS)' and h.TCMT <> 'Interest/Dividend Available Not Credited'
and ETC <> 'ITOC'


----transaction freeze query

select d.BOO [Home Branch Code],h.brcd [Transaction Done at Branch Code],h.CID [Account Number],t.[DESC],h.TAMT [Amount],h.[TJD DATE] [Transaction Date],
h.tim [Time of Transaction],case when DRCR = 0 then 'DR' else 'CR' END as [Transaction Type Desc],
case when TYPE between 1000 and 1999 then 'Savings A/c' 
     when TYPE between 2000 and 2999 then 'Current A/c'
     when TYPE between 3000 and 3999 then 'Fixed A/c'
     when TYPE between 4000 and 5000 then 'Recurring A/c'
	  end as [Account Type],
 d.TITLE1 [Account Name],h.USERID [Input Done By],
 case when h.TSO like '%MCCKRID%' then  SUBSTRING((SUBSTRING(h.TSO,CHARINDEX('MCCKRID',h.TSO),13)),9,13)
      when h.TSO not like '%MCCKRID%' then '' 
 end as [Authorization done]
 from TXN_HIST h
join
(
select r.CID, r.rflg,r.[DESC], ROW_NUMBER() over (order by CID) as [row_no] from ACCT_LEVEL_RESTRICTION r
--where r.[DESC] like '%Restricts Debit%'
WHERE r.RFLG in ('001','002','003','004','006','011','023','R001') and AS_ON_DTE = convert(date,getdate()-1)) t
on h.CID = t.CID
and t.row_no = 1
join DEPOSIT d
on h.CID = d.CID
join Suryoday.dbo.ETC_TxnList etc
on h.ETC = etc.ETC
where h.AS_ON_DTE = convert(date,getdate()-1)
and d.AS_ON_DT = convert(date,getdate()-1)
and etc.FeesCharges in ('',NULL) and len(etc.FeesCharges) = 0
and COMPONENT in ('',NULL) and len(COMPONENT)=0
and h.ETC not in ('ITID','ITIS','INIS','IIAS','IIPS','INID')
and h.DRCR <> 1
and not(d.PGK = 1 and BAL = 0) and d.STAT <> 4
UNION ALL
select d.BOO [Home Branch Code],h.brcd [Transaction Done at Branch Code],h.CID [Account Number],t.[DESC] [Description],h.TAMT [Amount],h.[TJD DATE] [Transaction Date],
h.tim [Time of Transaction],case when DRCR = 0 then 'DR' else 'CR' END as [Transaction Type Desc],
case when TYPE between 1000 and 1999 then 'Savings A/c' 
      when TYPE between 2000 and 2999 then 'Current A/c'
	  when TYPE between 3000 and 3999 then 'Fixed A/c'
	  when TYPE between 4000 and 5000 then 'Recurring A/c'
	  end as [Account Type],
 d.TITLE1 [Account Name],h.USERID [Input Done By],
 case when h.TSO like '%MCCKRID%' then  substring((SUBSTRING(h.TSO,CHARINDEX('MCCKRID',h.TSO),13)),9,13)
      when h.TSO not like '%MCCKRID%' then '' 
 end as [Authorization done]
 from TXN_HIST h
join
(
select r.CID, r.rflg,r.[DESC], ROW_NUMBER() over (order by CID) as row_no from ACCT_LEVEL_RESTRICTION r
--where r.[DESC] like '%Restricts Debit%'
WHERE r.RFLG in ('008','009') and AS_ON_DTE = CONVERT(date,GETDATE()-1)) t
on h.CID = t.CID
and t.row_no = 1
join DEPOSIT d
on h.CID = d.CID
join Suryoday.dbo.ETC_TxnList etc
on h.ETC = etc.ETC
where h.AS_ON_DTE = convert(date,getdate()-1)
and d.AS_ON_DT = convert(date,getdate()-1)
and etc.FeesCharges in ('',NULL) and len(etc.FeesCharges) = 0
and COMPONENT in ('',NULL) and len(COMPONENT)=0
and h.ETC not in ('ITID','ITIS','INIS','IIAS','IIPS','INID')
and not(d.PGK = 1 and BAL = 0) and d.STAT <> 4


-----#31018 Video KYC Account Opening_updated

use PROFILE
declare @tdate date = convert(date,getdate()-1)
select 
	 d.CID Account_No
	,c.ACN Customer_Id
        ,d.BOO BranchCode
	,b.[DESC] [BranchName]
	,c.FNAME First_Name
	,c.LNM Last_Name
        ,c.ZMPH Mobile_No
	,c.EMAIL Email_Id
	,c.oit Identification_Type
	,format(d.ODT,'dd-MMM-yyyy') Account_Opening_Date
	,p.[DES] [Variant of Account Opened]
	,format(d.ZACTDAT1, 'dd-MMM-yyyy') Account_Activation_Date
from dbo.CUSTOMER_MASTER c
join dbo.DEPOSIT d
on c.ACN = d.ACN
join dbo.PRODCTL p
on d.TYPE = p.TYPE
join UTBLBRCD b
on b.BRCD = d.BOO
where 
d.AS_ON_DT = @tdate
and c.CIF_STAT = 1 and d.ACCT_OPN_DTE = @tdate
--and c.acn <> '140000001'
and c.OIT in (31,36)
and not(d.PGK = 1 and d.BAL = 0) and d.STAT <> 4 and d.[TYPE] < 2000
--and d.type in ('1008','1005','2003','2001','1004','2006','1003')

------FD not closed but Fund got transferred


select d.CID,d.MDT,d.BAL
from DEPOSIT d
join TXN_HIST t
on d.CID = t.CID
where 
d.mdt <> ''
and convert(date,convert(date,d.MDT,105),23) < convert(date,getdate(),103)
and d.TYPE between 3000 and 4000
and d.STAT <> 4
and d.pgk = '0'
and d.BAL <= 0
and d.AS_ON_DT = convert(varchar,GETDATE()-1,106)
--and d.AS_ON_DT < convert(varchar,GETDATE()-1,106) and t.AS_ON_DTE < convert(varchar,GETDATE()-1,106)


Sunday, 16 August 2020

VBA Project Code-Merchandising Report

Case Study: VLookup data from Multiple Workbooks, and Apply Calculations in Master Workbook to update the Master File

Number Of Files: 4
Recon_Rough(Master File)
1) Merchandise Fullfilment Report
2) Merchandise Redemption Report
3) Orders Export File

Description:
Update Recon_Rough(Master File) from 
1)Merchandise Fullfilment Report.xlsx
2) Merchandise Redemption Report.xlsx
3)Orders Export File


Sub Merchandise_12_09_2019()
Application.ScreenUpdating = False
Dim Recon_Template As Workbook
Dim wbk As Workbook
Dim x As Variant
Dim lrow As Integer
lrow = Cells(Rows.Count, 1).End(xlUp).Row
Set wbk = Workbooks.Open("D:\Recong\Merchandise_Fulfillment_12-09-2019.xlsx")
Workbooks("Recon_Rough.xlsx").Worksheets("Sheet1"). _
Range("Q2:Q" & lrow).Formula = "=VLOOKUP($D2,'D:\Recong\[" & "Merchandise_Fulfillment_12-09-2019.xlsx" & "]" & "Combined" & "'!$B:$K,10,0)"
Workbooks("Recon_Rough.xlsx").Worksheets("Sheet1"). _
Range("R2:R" & lrow).Formula = "=VLOOKUP($D2,'D:\Recong\[" & "Merchandise_Fulfillment_12-09-2019.xlsx" & "]" & "Combined" & "'!$B:$X,23,0)"
ActiveSheet.Range("R:R").Select
Selection.NumberFormat = "dd-mmm-yy"
Workbooks("Recon_Rough.xlsx").Worksheets("Sheet1"). _
Range("S2:S" & lrow).Formula = "=VLOOKUP($D2,'D:\Recong\[" & "Merchandise_Fulfillment_12-09-2019.xlsx" & "]" & "Combined" & "'!$B:$AC,28,0)"
Workbooks("Recon_Rough.xlsx").Worksheets("Sheet1"). _
Range("T2:T" & lrow).Formula = "=VLOOKUP($D2,'D:\Recong\[" & "Merchandise_Fulfillment_12-09-2019.xlsx" & "]" & "Combined" & "'!$B:$AI,34,0)"
Workbooks("Recon_Rough.xlsx").Worksheets("Sheet1"). _
Range("U2:U" & lrow).Formula = "=VLOOKUP($D2,'D:\Recong\[" & "Merchandise_Fulfillment_12-09-2019.xlsx" & "]" & "Combined" & "'!$B:$N,13,0)"
Workbooks("Recon_Rough.xlsx").Worksheets("Sheet1"). _
Range("V2:V" & lrow).Formula = "=VLOOKUP($D2,'D:\Recong\[" & "Merchandise_Fulfillment_12-09-2019.xlsx" & "]" & "Combined" & "'!$B:$R,17,0)"
Workbooks("Recon_Rough.xlsx").Worksheets("Sheet1"). _
Range("W2:W" & lrow).Formula = "=VLOOKUP($D2,'D:\Recong\[" & "Merchandise_Fulfillment_12-09-2019.xlsx" & "]" & "Combined" & "'!$B:$S,18,0)"
Workbooks("Recon_Rough.xlsx").Worksheets("Sheet1"). _
Range("X2:X" & lrow).Formula = "=$U2*$V2"
Workbooks("Recon_Rough.xlsx").Worksheets("Sheet1"). _
Range("Z2:Z" & lrow).Formula = "=VLOOKUP($D2,'D:\Recong\[" & "Merchandise_Fulfillment_12-09-2019.xlsx" & "]" & "Combined" & "'!$B:$W,22,0)"
Workbooks("Recon_Rough.xlsx").Worksheets("Sheet1"). _
Range("AA2:AA" & lrow).Formula = "=VLOOKUP($D2,'D:\Recong\[" & "Merchandise_Fulfillment_12-09-2019.xlsx" & "]" & "Combined" & "'!$B:$U,20,0)"
Workbooks("Recon_Rough.xlsx").Worksheets("Sheet1"). _
Range("AB2:AB" & lrow).Formula = "=VLOOKUP($D2,'D:\Recong\[" & "Merchandise_Fulfillment_12-09-2019.xlsx" & "]" & "Combined" & "'!$B:$Q,16,0)"
Workbooks("Recon_Rough.xlsx").Worksheets("Sheet1"). _
Range("AC2:AC" & lrow).Formula = "=VLOOKUP($D2,'D:\Recong\[" & "Merchandise_Fulfillment_12-09-2019.xlsx" & "]" & "Combined" & "'!$B:$T,19,0)"
Workbooks("Recon_Rough.xlsx").Worksheets("Sheet1"). _
Range("AD2:AD" & lrow).Formula = "=$AB2*0.25"
Workbooks("Recon_Rough.xlsx").Worksheets("Sheet1"). _
Range("AE2:AE" & lrow).Formula = "=$AC2-$AD2"
Workbooks("Recon_Rough.xlsx").Worksheets("Sheet1"). _
Range("AF2:AF" & lrow).Formula = "=$AA2+$AD2"
Workbooks("Recon_Rough.xlsx").Worksheets("Sheet1"). _
Range("AG2:AG" & lrow).Formula = "=$AF2-$Z2"
Workbooks("Recon_Rough.xlsx").Worksheets("Sheet1"). _
Range("AH2:AH" & lrow).Formula = "=AND(AF2>=X2,Y2=Z2)"
Workbooks("Recon_Rough.xlsx").Worksheets("Sheet1"). _
Range("AI2:AI" & lrow).Formula = "=Y2>X2"
Workbooks("Recon_Rough.xlsx").Worksheets("Sheet1"). _
Range("AM2:AM" & lrow).Formula = "=VLOOKUP($D2,'D:\Recong\[" & "Merchandise_Fulfillment_12-09-2019.xlsx" & "]" & "Combined" & "'!$B:$AR,43,0)"
Workbooks("Recon_Rough.xlsx").Worksheets("Sheet1").Cells.Copy
Workbooks("Recon_Rough.xlsx").Worksheets("Sheet1").Cells.PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
Workbooks("Merchandise_Fulfillment_12-09-2019.xlsx").Close SaveChanges:=False
Application.ScreenUpdating = True
Call Merchandise_Redemption
End Sub

-------------------------------------------------------------
Sub Merchandise_Redemption()
Application.ScreenUpdating = False
Dim wbk As Workbook
Dim lrow1 As Long
Dim Rng1 As Range
lrow1 = Cells(Rows.Count, 1).End(xlUp).Row - 1
Set wbk = Workbooks.Open("D:\Recong\Merchandise_Redemption_Report.xlsx")
Workbooks("Recon_Rough.xlsx").Worksheets("Sheet1").Activate
Worksheets("Sheet1").Range("A1").AutoFilter Field:=17, Criteria1:="#N/A", Operator:=xlOr, Criteria2:="="
With ActiveSheet.AutoFilter.Range
Set Rng1 = Range("R" & .Offset(1, 0).SpecialCells(xlCellTypeVisible)(1).Row)
.Range(Rng1.Address & ":R" & lrow1).Formula = "=VLOOKUP($D:$D,'D:\Recong\[" & "Merchandise_Redemption_Report.xlsx" & "]" & "Merchandise_Redemption_Report" & "'!$A:$AF,8,0)"
.Range(Rng1.Address & ":R" & lrow1).Offset(0, 2).Formula = "=VLOOKUP($D:$D,'D:\Recong\[" & "Merchandise_Redemption_Report.xlsx" & "]" & "Merchandise_Redemption_Report" & "'!$A:$AF,17,0)"
.Range(Rng1.Address & ":R" & lrow1).Offset(0, 3).Formula = "=VLOOKUP($D:$D,'D:\Recong\[" & "Merchandise_Redemption_Report.xlsx" & "]" & "Merchandise_Redemption_Report" & "'!$A:$AF,19,0)"
.Range(Rng1.Address & ":R" & lrow1).Offset(0, 4).Formula = "=VLOOKUP($D:$D,'D:\Recong\[" & "Merchandise_Redemption_Report.xlsx" & "]" & "Merchandise_Redemption_Report" & "'!$A:$AF,20,0)"
.Range(Rng1.Address & ":R" & lrow1).Offset(0, 5).Formula = "=SUMPRODUCT(VLOOKUP($D:$D,'D:\Recong\[" & "Merchandise_Redemption_Report.xlsx" & "]" & "Merchandise_Redemption_Report" & "'!$A:$AF,{21,22},0))"
.Range(Rng1.Address & ":R" & lrow1).Offset(0, 7).Formula = "=VLOOKUP($D:$D,'D:\Recong\[" & "Merchandise_Redemption_Report.xlsx" & "]" & "Merchandise_Redemption_Report" & "'!$A:$AF,28,0)"
.Range(Rng1.Address & ":R" & lrow1).Offset(0, 8).Formula = "=VLOOKUP($D:$D,'D:\Recong\[" & "Merchandise_Redemption_Report.xlsx" & "]" & "Merchandise_Redemption_Report" & "'!$A:$AF,28,0)"
.Range(Rng1.Address & ":R" & lrow1).Offset(0, 9).Formula = "=VLOOKUP($D:$D,'D:\Recong\[" & "Merchandise_Redemption_Report.xlsx" & "]" & "Merchandise_Redemption_Report" & "'!$A:$AF,15,0)"
.Range(Rng1.Address & ":R" & lrow1).Offset(0, 10).Formula = "=VLOOKUP($D:$D,'D:\Recong\[" & "Merchandise_Redemption_Report.xlsx" & "]" & "Merchandise_Redemption_Report" & "'!$A:$AF,14,0)"
.Range(Rng1.Address & ":R" & lrow1).Offset(0, 12).Formula = "=$AB:$AB * 0.25"
.Range(Rng1.Address & ":R" & lrow1).Offset(0, 13).Formula = "=$AC:$AC-$AD:$AD"
.Range(Rng1.Address & ":R" & lrow1).Offset(0, 14).Formula = "=$AA:$AA+$AD:$AD"
.Range(Rng1.Address & ":R" & lrow1).Offset(0, 15).Formula = "=$AF:$AF-$Z:$Z"
.Range(Rng1.Address & ":R" & lrow1).Offset(0, 16).Formula = "=AND($AF:$AF>=$X:$X,$Y:$Y=$Z:$Z)"
.Range(Rng1.Address & ":R" & lrow1).Offset(0, 17).Formula = "=$Y:$Y>$Z:$Z"
.Range(Rng1.Address & ":R" & lrow1).Offset(0, 21).Formula = "=VLOOKUP($D:$D,'D:\Recong\[" & "Merchandise_Redemption_Report.xlsx" & "]" & "Merchandise_Redemption_Report" & "'!$A:$AF,10,0)"
End With
Workbooks("Recon_Rough.xlsx").Worksheets("Sheet1").ShowAllData
Workbooks("Recon_Rough.xlsx").Worksheets("Sheet1").Cells.Copy
Workbooks("Recon_Rough.xlsx").Worksheets("Sheet1").Cells.PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
Workbooks("Merchandise_Redemption_Report.xlsx").Close SaveChanges:=False
Application.ScreenUpdating = True
Call Recon_Template2
End Sub

--------------------------------------------------------------------------------------------------------------------------

Application.ScreenUpdating = False
Dim wbk As Workbook
Dim lrow2 As Long
Dim Rng1 As Range
lrow2 = Cells(Rows.Count, 1).End(xlUp).Row
Set wbk = Workbooks.Open("D:\Recong\orders_export_13_9_2019_12_0.xlsx")
Workbooks("Recon_Rough.xlsx").Worksheets("Sheet1").Activate
Worksheets("Sheet1").Range("A1").AutoFilter Field:=17, Criteria1:="#N/A", Operator:=xlOr, Criteria2:="="
With ActiveSheet.AutoFilter.Range
Set Rng1 = Range("Q" & .Offset(1, 0).SpecialCells(xlCellTypeVisible)(1).Row)
.Range(Rng1.Address & ":Q" & lrow2).Formula = "=VLOOKUP($D:$D,'D:\Recong\[" & "orders_export_13_9_2019_12_0" & "]" & "data" & "'!$F:$AV,18,0)"
.Range(Rng1.Address & ":Q" & lrow2).Offset(0, 2).Formula = "=VLOOKUP($D:$D,'D:\Recong\[" & "orders_export_13_9_2019_12_0" & "]" & "data" & "'!$F:$AV,37,0)"
End With
Workbooks("Recon_Rough.xlsx").Worksheets("Sheet1").ShowAllData
Workbooks("Recon_Rough.xlsx").Worksheets("Sheet1").Cells.Copy
Workbooks("Recon_Rough.xlsx").Worksheets("Sheet1").Cells.PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
Workbooks("orders_export_13_9_2019_12_0.xlsx").Close SaveChanges:=False
Application.ScreenUpdating = True