Related
I have created accouting database as per my accounting knowledge.
I want to check Is that database is correct which i design for accounting system.
I have table called Mst_AccountGroup(Asset,Liabilities,Capital/Equity,Income & Expense data to create chart of account).
The database structure give below.
CREATE TABLE [dbo].[Mst_AccountGroup](
[AccountGrp_No] [int] NULL,
[AccountGrp_Name] [nvarchar](50) NULL,
[AccountGrp_Type] [nvarchar](50) NULL,
[AccountGrp_Increase] [nvarchar](50) NULL,
[AccountGrp_Decrease] [nvarchar](50) NULL,
[AccountGrp_Sign] [nvarchar](50) NULL,
[AccountGrp_SalesSign] [nvarchar](50) NULL,
[AccountGrp_ReceiptSign] [nvarchar](50) NULL,
[AccountGrp_PurchaseSign] [nvarchar](50) NULL,
[AccountGrp_PaymentSign] [nvarchar](50) NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[Mst_AccountCategory](
[AccountCat_No] [int] NULL,
[AccountCat_Name] [nvarchar](500) NULL,
[AccountCat_AccountGrpNo] [int] NULL,
[AccountCat_Sign] [nvarchar](50) NULL,
[AccountCat_SalesSign] [nvarchar](50) NULL,
[AccountCat_ReceiptSign] [nvarchar](50) NULL,
[AccountCat_PurchaseSign] [nvarchar](50) NULL,
[AccountCat_PaymentSign] [nvarchar](50) NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[Mst_AccountSubCategory](
[AccountSubCat_No] [int] NULL,
[AccountSubCat_AccountCatNo] [int] NULL,
[AccountSubCat_Name] [nvarchar](500) NULL,
[AccountSubCat_SalesSign] [nvarchar](50) NULL,
[AccountSubCat_ReceiptSign] [nvarchar](50) NULL,
[AccountSubCat_PurchaseSign] [nvarchar](50) NULL,
[AccountSubCat_PaymentSign] [nvarchar](50) NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[Trn_Ledger](
[Ledger_No] [int] NULL,
[Ledger_Name] [nvarchar](500) NULL,
[Ledger_AccountSubCatNo] [int] NULL,
[Ledger_VATNumber] [nvarchar](250) NULL,
[Ledger_CreditDays] [numeric](18, 0) NULL,
[Ledger_OpeningBalance] [numeric](18, 3) NULL,
[Ledger_BankName] [nvarchar](500) NULL,
[Ledger_BankAC] [nvarchar](500) NULL,
[Ledger_SwiftCode] [nvarchar](500) NULL,
[Ledger_BankAddress] [nvarchar](500) NULL,
[Ledger_Countory] [nvarchar](500) NULL,
[Ledger_VATPercent] [numeric](18, 3) NULL
) ON [PRIMARY]
GO
I have only one transaction table which include Sales,Receipt and Purchase, Payment and Journal Entries. Vouchers are seprating through [AccTran_VchType] field. Note that all the transaction are double entery.
Sales,Receipt and Purchase enteries, on those voucher user cannot make mistake to put the ledger on credit or debit side because from the chart of account once set then when select particular ledger then either it will be credit or debit system will tell the user.
CREATE TABLE [dbo].[Trn_AccountTransactionList](
[AccTran_No] [int] NULL,
[AccTran_Number] [nvarchar](50) NULL,
[AccTran_Date] [date] NULL,
[AccTran_Remark] [nvarchar](500) NULL,
[AccTran_VchType] [nvarchar](50) NULL,
[AccTran_RefNo] [nvarchar](250) NULL,
[AccTran_BillDate] [date] NULL,
[AccTran_Post] [numeric](18, 0) NULL,
[AccTran_Number2] [numeric](18, 0) NULL,
[AccTran_InvoiceNo] [numeric](18, 0) NULL,
[AccTran_PoNo] [numeric](18, 0) NULL,
[AccTran_PDC] [numeric](18, 0) NULL,
[Acc_TranLedgerNo] [numeric](18, 0) NULL,
[Acc_TranAmount] [numeric](18, 3) NULL,
[Acc_CompNo] [numeric](18, 0) NULL,
[Acc_Updatedby] [nvarchar](50) NULL,
[Acc_UpdatedDatetime] [datetime] NULL,
[Acc_BankDate] [date] NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[Trn_AccountTransaction](
[AccTran_No] [int] NULL,
[AccTran_No_N] [int] NULL,
[AccTran_AccountLedgerNo] [int] NULL,
[AccTran_AmountDR] [numeric](18, 3) NULL,
[AccTran_AmountCR] [numeric](18, 3) NULL,
) ON [PRIMARY]
GO
Trial balance query.
CREATE VIEW [dbo].[View_TrialBalance]
AS
SELECT TOP (100) PERCENT AccountCat_Name, AccountGrp_Name, DR, CR, AccountGrp_Type, AccountGrp_No, Balance, AccountSubCat_No, CASE WHEN AccountGrp_No IN (1,
5) THEN Balance ELSE 0 END AS TrialDebit, CASE WHEN AccountGrp_No IN (2, 3, 4) THEN Balance ELSE 0 END AS TrialCredit, AccountSubCat_Name, Ledger_No,
Ledger_Name, AccTran_Date, Comp_No, Comp_Name, Acc_CompNo, Comp_Add, Comp_Tel, Comp_Fax, Comp_Country
FROM dbo.View_Ledger
ORDER BY AccountSubCat_No
GO
View_P&LandBalanceSheet
CREATE VIEW [dbo].[View_P&LandBalanceSheet]
AS
SELECT TOP (100) PERCENT AccountCat_Name, AccountGrp_Name, DR, CR, AccountGrp_Type, Balance, TrialDebit, TrialCredit, AccountSubCat_No,
CASE WHEN AccountGrp_Type = 'P&L' THEN TrialDebit ELSE 0 END AS InDebit, CASE WHEN AccountGrp_Type = 'P&L' THEN TrialCredit ELSE 0 END AS InCredit,
CASE WHEN AccountGrp_Type = 'BS' THEN TrialDebit ELSE 0 END AS BSDebit, CASE WHEN AccountGrp_Type = 'BS' THEN TrialCredit ELSE 0 END AS BSCredit,
AccountSubCat_Name, Ledger_No, Ledger_Name, AccTran_Date, Comp_No, Comp_Name, Acc_CompNo, Comp_Add, Comp_Tel, Comp_Fax, Comp_Country,
YEAR(AccTran_Date) AS YRS
FROM dbo.View_TrialBalance
WHERE (NOT (AccountCat_Name IS NULL))
ORDER BY AccountSubCat_No
GO
Note that all other reports are coming correctly based on above Transaction table. I want to ask the database structure is perfect?
CREATE TABLE [dbo].[TB_CUSTPARAM](
[CUSTTYPE] [varchar](10) NOT NULL,
[PARAMETER] [varchar](20) NULL,
[DATATYPE] [int] NULL,
[TEXT_PARAM] [varchar](50) NULL,
[RANGE_FROM] [int] NULL,
[RANGE_TO] [int] NULL,
[RATING] [int] NULL,
[DESCRIPTION] [varchar](50) NULL,
[TABLENAME] [varchar](50) NULL,
[COLUMNNAME] [varchar](100) NULL,
[SQLQUERY] [varchar](1000) NULL,
[SRNO] [int] NOT NULL,
[WEIGHT] [numeric](4, 0) NULL,
[RANGE_VIEW_NAME] [varchar](200) NULL
) ON [PRIMARY]
-------------------------------------------------------------------------
CREATE TABLE [dbo].[TEMP_CUSTPARAM](
[CUSTNO] [varchar](50) NOT NULL,
[RATING] [numeric](18, 0) NULL,
CONSTRAINT [PK_TEMP_CUSTPARAM] PRIMARY KEY CLUSTERED
(
[CUSTNO] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
) ON [PRIMARY]
-------------------------------------------------------------------
CREATE TABLE [dbo].[TB_CUSTPERSONAL](
[CUSTNO] [varchar](50) NOT NULL,
[SEX] [varchar](30) NULL,
[MARITALSTATUS] [varchar](30) NULL,
[INCOMESOURCE] [int] NULL,
[OCCUPATION] [varchar](30) NULL,
[EMPLOYERSNAME1] [varchar](50) NULL,
[EMPLOYERPHNO1] [varchar](15) NULL,
[EMPLOYERADDR1] [varchar](100) NULL,
[YEARSOFSERVICE1] [numeric](30, 2) NULL,
[EMPLOYERSNAME2] [varchar](50) NULL,
[EMPLOYERPHNO2] [varchar](15) NULL,
[EMPLOYERADDR2] [varchar](100) NULL,
[YEARSOFSERVICE2] [numeric](30, 2) NULL,
[TOTALYEAROFSERVICE] [numeric](30, 2) NULL,
[NOOFDEPENDANTS] [varchar](10) NULL,
[CURRANNUALINCOME] [numeric](30, 3) NULL,
[QUALIFICATION] [varchar](30) NULL,
[DESIGNATION] [varchar](30) NULL,
[VERIFICATIONTYPE] [int] NULL,
[PURPOSEOFACCOUNT] [varchar](50) NULL,
[REFEREDBY] [varchar](50) NULL,
[ANTICIPATEDACTIVITY] [varchar](50) NULL,
[NI_NUMBER] [varchar](35) NULL,
[IDENTIFICATION_MARK] [varchar](50) NULL,
[PKID] [varchar](50) NULL,
[TXNTURNOVER] [varchar](50) NULL,
[MONTHLY_INCOME] [numeric](30, 3) NULL,
[FATHER_SPOUSE_NAME] [varchar](100) NULL,
[MOBILENO] [varchar](30) NULL,
[EMPLOYERTYPE] [varchar](30) NULL,
[PROFESSIONTYPE] [varchar](30) NULL,
[BUSINESSTYPE] [varchar](30) NULL,
[AREAOFOCCUPATION] [varchar](30) NULL,
[EXPTXNPERQTR] [varchar](30) NULL,
[TYPEOF4WHEELEROWN] [varchar](30) NULL,
[NATIONALITY] [varchar](50) NULL,
[MINORYN] [varchar](1) NULL,
[SRCITIZENYN] [varchar](1) NULL,
[FORM60_61YN] [varchar](1) NULL,
[PASSPORT_NO] [varchar](50) NULL,
[IF_SELF_EMPLOYED] [varchar](50) NULL,
[IF_BUSINESS] [varchar](50) NULL,
[SOURCE_OF_INCOME] [varchar](50) NULL,
[YOU_LIVE_IN] [varchar](50) NULL,
[YOUR_RESIDENCE_IS] [varchar](50) NULL,
[DI_DATE] [datetime] NULL,
[REC_UPDATEDT] [datetime] NULL,
[DAILY_INCOME] [varchar](50) NULL,
[EMPLOYERPHNO] [varchar](50) NULL,
[EMPLOYERADDR] [varchar](50) NULL,
[YEARSOFSERVICE] [varchar](50) NULL,
[FIRSTNAME] [varchar](50) NULL,
[MIDDLENAME] [varchar](50) NULL,
[LASTNAME] [varchar](50) NULL,
[PLACEOFBIRTH] [varchar](50) NULL,
[PASSPORT_YN] [varchar](50) NULL,
[EMIRATESID_YN] [varchar](50) NULL,
[RESIVISA_YN] [varchar](50) NULL,
[SALCERTIFICATE_YN] [varchar](50) NULL,
[EMPLOYMENT_STATUS] [varchar](50) NULL,
[EMPLOYERSNAME] [varchar](50) NULL,
[E_ADDRESS2] [varchar](50) NULL,
[E_ADDRESS3] [varchar](50) NULL,
[E_COUNTRY] [varchar](50) NULL,
[YEAROFSERVICE] [varchar](50) NULL,
[DEPENDENT_CHILDREN] [varchar](50) NULL,
[SPOUSE_EMP_STATUS] [varchar](50) NULL,
[ACCOMODATION] [varchar](50) NULL,
[PROFESSION] [varchar](50) NULL,
[RESVISANO] [varchar](50) NULL,
[RESVISAISSUEPLACE] [varchar](50) NULL,
[RESVISAISSUEDATE] [varchar](50) NULL,
[RESVISAEXPIRYDATE] [varchar](50) NULL,
[EMP_CODE] [varchar](50) NULL,
[PREV_EMPLOYER] [varchar](50) NULL,
[EMP_JOIN_DATE] [varchar](50) NULL,
[ACCOUNTOPERATEDBY] [varchar](50) NULL,
[RESIDENT_STATUS] [varchar](50) NULL,
[CREDIT_CARDNO] [varchar](50) NULL,
[ISSED_BY] [varchar](50) NULL,
[EXPIRY_DATE] [varchar](50) NULL,
[SCHEME_NAME] [varchar](50) NULL,
[TENURE_SCHEME] [varchar](50) NULL,
[INSTALLAMT_SCHEME] [varchar](50) NULL,
[NOOFINSTALL_SCHEME] [varchar](50) NULL,
[MATURITYAMT_SCHEME] [varchar](50) NULL,
[MONTHLYAMT_SCHEME] [varchar](50) NULL,
[TERM_DEPOSIT] [varchar](50) NULL,
[DATE_OF_ISSUE] [varchar](50) NULL,
[DATE_OF_MATURITY] [varchar](50) NULL,
[TERMDEPO_AMOUNT] [varchar](50) NULL,
[TERMDEPO_RENEWAL] [varchar](50) NULL,
[TERMDEPO_RATE] [varchar](50) NULL,
[INTIAL_DEPOSIT] [varchar](50) NULL,
[SPECIAL_INST] [varchar](50) NULL,
[NAME_CONTACTPERSON] [varchar](50) NULL,
[NAME_CONTACTNUMBER] [varchar](50) NULL,
[MODIFIED_ON] [varchar](50) NULL,
[MODIFIED_BY] [varchar](50) NULL,
[MOTHERSMAIDENNAME] [varchar](50) NULL,
[CUSTOMER_FIRSTNAME] [varchar](50) NULL,
[CUSTOMER_LASTNAME] [varchar](50) NULL,
[CUSTOMER_MIDDLENAME] [varchar](50) NULL,
[EMPLOYERCITY] [varchar](50) NULL,
[EMPLOYERSTATE] [varchar](50) NULL,
[EMPLOYERZIP] [varchar](50) NULL,
[RESIDENTCOUNTRY] [varchar](50) NULL,
[EMPLOYERCOUNTRYCODE] [varchar](50) NULL,
[INRODU_NAME] [varchar](50) NULL,
[RISKCATEGORY] [varchar](50) NULL,
[ENTITY_GROUP_CODE] [varchar](50) NULL,
[ACCTNO_INTRO] [varchar](50) NULL,
[ENTITY_CODE] [varchar](50) NULL,
CONSTRAINT [PK_TB_CUSTPERSONAL] PRIMARY KEY CLUSTERED
(
[CUSTNO] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
) ON [PRIMARY]
----------------------------------------------------------
create
PROCEDURE PROC_CUSTRATING_INC
#WEIGHTAGE FLOAT,
#TAB_NAME VARCHAR(4000),
#COL_NAME VARCHAR(4000)
--#ENTITY_CODE VARCHAR(50),#ENTITY_GROUP_CODE VARCHAR(50)
AS
BEGIN
DECLARE #v_DAT_ DATETIME,
#v_DAT2 DATETIME,
#v_Pro_ VARCHAR(100),
#v_LAS_ DATETIME,
#v_SQL_ VARCHAR(500),
#v_COU_ FLOAT,
#v_TOT_ FLOAT,
#v_BAT_ FLOAT,
#v_SQLSTRING VARCHAR(1000)
SET NOCOUNT ON;
SET #v_DAT_=GETDATE();
SET #v_Pro_='PROC_CUSTRATING_INC';
DELETE FROM TEMP_CUSTPARAM;
SELECT #v_LAS_ = LASTRUNDT FROM TB_SCHEDULE WHERE PROCESS_ID ='2';
SET #v_SQLSTRING = 'INSERT INTO /*+ APPEND*/ TEMP_CUSTPARAM(CUSTNO, RATING)
SELECT DISTINCT Q.CUSTNO, ISNULL(((ISNULL(P.RATING,0) * '+ ISNULL(#WEIGHTAGE, '') +')/100),0) AS RATING
FROM '+ ISNULL(#TAB_NAME, '') +' Q INNER JOIN TB_CUSTPARAM P
ON Q.' + ISNULL(#COL_NAME, '') +'=P.TEXT_PARAM
AND P.TABLENAME='''+ ISNULL(#TAB_NAME, '') +''' AND P.COLUMNNAME='''+ ISNULL(#COL_NAME, '') +'''
AND TRUNC(Q.DI_DATE) >= '''+ ISNULL(#v_LAS_, '') +''' ';
PRINT 'v_SQLSTRING111=== ' + ISNULL(#v_SQL_, '');
EXECUTE (#v_SQLSTRING);
-- RETURN;
MERGE INTO TB_RATING_TEMP A
USING TEMP_CUSTPARAM E
ON (A.CUSTNO=E.CUSTNO)
WHEN MATCHED THEN
UPDATE SET
A.TEMP_RATING=ISNULL(E.RATING,0);
SET #v_SQLSTRING='UPDATE TB_RATING_TEMP A
SET
A.PARAMCOUNT=A.PARAMCOUNT + 1,
A.BRATING = NVL(A.BRATING,0) + NVL(A.TEMP_RATING,0)
WHERE
A.CUSTNO IN
(
SELECT Q.CUSTNO
FROM TB_CUSTPARAM P,'+ ISNULL(#TAB_NAME, '') +' Q
WHERE P.TEXT_PARAM=TO_CHAR(Q.'+ ISNULL(#COL_NAME, '') +')
)';
PRINT 'v_SQLSTRING222=== ' + ISNULL(#v_SQL_, '');
EXECUTE (#v_SQLSTRING);
PRINT 4;
SET #v_DAT2=GETDATE();
--PRINT 'PROCEDURE ' + ISNULL(#v_Pro_, '') + ' TAKES : ' + ISNULL(CONVERT(VARCHAR, trunc((((86400*(#v_DAT2-#v_DAT_))/60)/60)/24)), '') + ' Days ' + ISNULL(CONVERT(VARCHAR, trunc(((86400*(#v_DAT2-#v_DAT_))/60)/60)- 24*(trunc((((86400*(#v_DAT2-#v_DAT_))/60)/60)/24))), '') + ' Hrs. ' + ISNULL(CONVERT(VARCHAR, trunc((86400*(#v_DAT2-#v_DAT_))/60)- 60*(trunc(((86400*(#v_DAT2-#v_DAT_))/60)/60)) ), '') + ' Mins.' + ISNULL(CONVERT(VARCHAR, trunc(86400*(#v_DAT2-#v_DAT_))- 60*ISNULL((trunc((86400*(#v_DAT2-#v_DAT_))/60)), '') + ' Sec.'), '');
END;
Running
EXEC PROC_CUSTRATING_INC 11.0,'TB_CUSTPERSONAL','OCCUPATION'
results in the error
error converting data type varchar to float.
The problem I think is in the below statement
SET #v_SQLSTRING = 'INSERT INTO /*+ APPEND*/ TEMP_CUSTPARAM(CUSTNO, RATING)
SELECT DISTINCT Q.CUSTNO, ISNULL(((ISNULL(P.RATING,0) * '+ ISNULL(#WEIGHTAGE, '') +')/100),0) AS RATING
FROM '+ ISNULL(#TAB_NAME, '') +' Q INNER JOIN TB_CUSTPARAM P
ON Q.' + ISNULL(#COL_NAME, '') +'=P.TEXT_PARAM
AND P.TABLENAME='''+ ISNULL(#TAB_NAME, '') +''' AND P.COLUMNNAME='''+ ISNULL(#COL_NAME, '') +'''
AND TRUNC(Q.DI_DATE) >= '''+ ISNULL(#v_LAS_, '') +''' ';
The ISNULL(#WEIGHTAGE, '') should be changed to ISNULL(#WEIGHTAGE, 0.0) since #WEIGHTAGE is a float and you are trying to change a varchar '' to a float in this ISNULL statement which is causing it to fail.
select
a.COUNTY_FIPS
,COUNT(e.PROPERTY_ID) as house_count
,AVG(cast(e.AVM_FINAL_VALUE as bigint)) as avg_avm
,max(cast(e.AVM_FINAL_VALUE as bigint)) as max_avm
,min(cast(e.AVM_FINAL_VALUE as bigint)) as min_avm
from
RAW_Equity e
left join
(SELECT
SA_PROPERTY_ID, MM_FIPS_STATE_CODE, MM_FIPS_MUNI_CODE,
CASE
WHEN MM_FIPS_STATE_CODE < 10
THEN '0' + CAST(MM_FIPS_STATE_CODE as VARCHAR)
ELSE CAST(MM_FIPS_STATE_CODE as VARCHAR)
END
+ CASE
WHEN MM_FIPS_MUNI_CODE < 10
THEN '00' + CAST(MM_FIPS_MUNI_CODE as VARchar)
WHEN MM_FIPS_MUNI_CODE < 100
THEN '0' + CAST(MM_FIPS_MUNI_CODE as VARchar)
ELSE CAST(MM_FIPS_MUNI_CODE as VARchar)
END AS COUNTY_FIPS
FROM
RAW_Address) a ON a.SA_PROPERTY_ID = e.PROPERTY_ID
where
AVM_CONFIDENCE_SCORE >= 70
group by
a.COUNTY_FIPS
Is there any way I can improve the performance of this query? Schema for both the tables are shown below. I am was thinking about creating non clustered index on AVM_CONFIDENCE_SCORE but I think it will only going to increase the query time. Any help will be greatly appreciated.
RAWADDRESS table:
CREATE TABLE [dbo].[RAW_Address]
(
[SA_PROPERTY_ID] [int] NOT NULL,
[SA_SCM_ID] [int] NOT NULL,
[MM_STATE_CODE] [varchar](2) NOT NULL,
[MM_MUNI_NAME] [varchar](24) NOT NULL,
[MM_FIPS_STATE_CODE] [tinyint] NOT NULL,
[MM_FIPS_MUNI_CODE] [smallint] NOT NULL,
[MM_FIPS_COUNTY_NAME] [varchar](35) NOT NULL,
[SA_SITE_HOUSE_NBR] [varchar](20) NULL,
[SA_SITE_FRACTION] [varchar](10) NULL,
[SA_SITE_DIR] [varchar](2) NULL,
[SA_SITE_STREET_NAME] [varchar](40) NULL,
[SA_SITE_SUF] [varchar](4) NULL,
[SA_SITE_POST_DIR] [varchar](2) NULL,
[SA_SITE_UNIT_PRE] [varchar](10) NULL,
[SA_SITE_UNIT_VAL] [varchar](6) NULL,
[SA_SITE_CITY] [varchar](30) NULL,
[SA_SITE_STATE] [varchar](2) NOT NULL,
[SA_SITE_ZIP] [int] NULL,
[SA_SITE_PLUS_4] [smallint] NULL,
[SA_SITE_CRRT] [varchar](4) NULL,
[SA_MAIL_HOUSE_NBR] [varchar](20) NULL,
[SA_MAIL_FRACTION] [varchar](10) NULL,
[SA_MAIL_DIR] [varchar](2) NULL,
[SA_MAIL_STREET_NAME] [varchar](50) NULL,
[SA_MAIL_SUF] [varchar](4) NULL,
[SA_MAIL_POST_DIR] [varchar](2) NULL,
[SA_MAIL_UNIT_PRE] [varchar](10) NULL,
[SA_MAIL_UNIT_VAL] [varchar](6) NULL,
[SA_MAIL_CITY] [varchar](50) NULL,
[SA_MAIL_STATE] [varchar](2) NULL,
[SA_MAIL_ZIP] [int] NULL,
[SA_MAIL_PLUS_4] [smallint] NULL,
[SA_MAIL_CRRT] [varchar](4) NULL,
[SA_SITE_MAIL_SAME] [varchar](1) NULL
) ON [PRIMARY]
RAW Equity table:
CREATE TABLE [dbo].[RAW_Equity]
(
[PROPERTY_ID] [int] NOT NULL,
[SCM_ID] [int] NOT NULL,
[MM_STATE_CODE] [varchar](2) NOT NULL,
[MM_MUNI_NAME] [varchar](24) NOT NULL,
[MM_FIPS_STATE_CODE] [int] NOT NULL,
[MM_FIPS_MUNI_CODE] [int] NOT NULL,
[MM_FIPS_COUNTY_NAME] [varchar](35) NOT NULL,
[AVM_FINAL_VALUE] [int] NULL,
[AVM_LOW_VALUE] [int] NULL,
[AVM_HIGH_VALUE] [int] NULL,
[AVM_CONFIDENCE_SCORE] [int] NULL,
[FINAL_VALUE] [float] NULL,
[FIRST_POSITION_SR_UNIQUE_ID] [int] NULL,
[FIRST_POSITION_LOAN_DATE] [int] NULL,
[FIRST_POSITION_DOC_NBR] [varchar](20) NULL,
[FIRST_POSITION_LOAN_VAL] [int] NULL,
[FIRST_POSITION_LENDER_CODE] [int] NULL,
[FIRST_POSITION_LNDR_LAST_NAME] [varchar](50) NULL,
[FIRST_POSITION_LNDR_FIRST_NAME] [varchar](50) NULL,
[FIRST_POSITION_LENDER_TYPE] [varchar](1) NULL,
[FIRST_POSITION_LOAN_TYPE] [varchar](1) NULL,
[FIRST_POSITION_INTEREST_RATE_TYPE] [varchar](1) NULL,
[FIRST_POSITION_ESTIMATED_INTEREST_RATE] [float] NULL,
[FIRST_POSITION_LNDR_CREDIT_LINE] [varchar](1) NULL,
[FIRST_POSITION_MODELED_MORTGAGE_TYPE] [varchar](1) NULL,
[SECOND_POSITION_SR_UNIQUE_ID] [int] NULL,
[SECOND_POSITION_LOAN_DATE] [int] NULL,
[SECOND_POSITION_DOC_NBR] [varchar](20) NULL,
[SECOND_POSITION_LOAN_VAL] [int] NULL,
[SECOND_POSITION_LENDER_CODE] [int] NULL,
[SECOND_POSITION_LNDR_LAST_NAME] [varchar](50) NULL,
[SECOND_POSITION_LNDR_FIRST_NAME] [varchar](50) NULL,
[SECOND_POSITION_LENDER_TYPE] [varchar](1) NULL,
[SECOND_POSITION_LOAN_TYPE] [varchar](1) NULL,
[SECOND_POSITION_INTEREST_RATE_TYPE] [varchar](1) NULL,
[SECOND_POSITION_ESTIMATED_INTEREST_RATE] [float] NULL,
[SECOND_POSITION_LNDR_CREDIT_LINE] [varchar](1) NULL,
[SECOND_POSITION_MODELED_MORTGAGE_TYPE] [varchar](1) NULL,
[THIRD_POSITION_SR_UNIQUE_ID] [int] NULL,
[THIRD_POSITION_LOAN_DATE] [int] NULL,
[THIRD_POSITION_DOC_NBR] [varchar](20) NULL,
[THIRD_POSITION_LOAN_VAL] [int] NULL,
[THIRD_POSITION_LENDER_CODE] [int] NULL,
[THIRD_POSITION_LNDR_LAST_NAME] [varchar](50) NULL,
[THIRD_POSITION_LNDR_FIRST_NAME] [varchar](50) NULL,
[THIRD_POSITION_LENDER_TYPE] [varchar](1) NULL,
[THIRD_POSITION_LOAN_TYPE] [varchar](1) NULL,
[THIRD_POSITION_INTEREST_RATE_TYPE] [varchar](1) NULL,
[THIRD_POSITION_ESTIMATED_INTEREST_RATE] [float] NULL,
[THIRD_POSITION_LNDR_CREDIT_LINE] [varchar](1) NULL,
[THIRD_POSITION_MODELED_MORTGAGE_TYPE] [varchar](1) NULL,
[TOTAL_OUTSTANDING_LOANS] [bigint] NULL,
[LTV] [int] NULL,
[AVAILABLE_EQUITY] [int] NULL,
[LENDABLE_EQUITY] [int] NULL,
[PROCESS_ID] [int] NOT NULL,
[FILLER] [varchar](4) NULL,
CONSTRAINT [PK_RAW_Equity] PRIMARY KEY CLUSTERED
(
[PROPERTY_ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
You could try this way :-
(willing to know, how much this one, helpful to you)
Declare #result Table
(
RowId Int Identity(1,1) Primary Key
,COUNTY_FIPS Varchar(100)
,MM_FIPS_STATE_CODE Int
,MM_FIPS_MUNI_CODE Int
,house_count Int
,avg_avm Int
,max_avm Int
,min_avm Int
)
Insert Into #result(MM_FIPS_STATE_CODE,MM_FIPS_MUNI_CODE,house_count,avg_avm,max_avm,min_avm)
Select a.MM_FIPS_STATE_CODE
,a.MM_FIPS_MUNI_CODE
,Count(e.PROPERTY_ID) as house_count
,Avg(Cast(e.AVM_FINAL_VALUE as bigint)) as avg_avm
,Max(Cast(e.AVM_FINAL_VALUE as bigint)) as max_avm
,Min(Cast(e.AVM_FINAL_VALUE as bigint)) as min_avm
From RAW_Equity As e With (Nolock)
Left Join RAW_Address As a With (Nolock) On e.PROPERTY_ID = a.SA_PROPERTY_ID
Where e.AVM_CONFIDENCE_SCORE >= 70
Group by a.MM_FIPS_STATE_CODE
,a.MM_FIPS_MUNI_CODE
Update r
Set r.COUNTY_FIPS = REPLICATE('0',2-LEN(RTRIM(r.MM_FIPS_STATE_CODE))) + RTRIM(r.MM_FIPS_STATE_CODE) + REPLICATE('0',3-LEN(RTRIM(r.MM_FIPS_MUNI_CODE))) + RTRIM(r.MM_FIPS_MUNI_CODE)
From #result As r
Select r.COUNTY_FIPS
,r.house_count
,r.avg_avm
,r.max_avm
,r.min_avm
From #result As r
1st try without any index, and after that create clustered index as mentioned and try AGAIN the above same query
CREATE INDEX IX_RAW_Address_SA_PROPERTY_ID ON RAW_Address(SA_PROPERTY_ID)
I would put an index on:
Table: RAW_Equity
Columns: PROPERTY_ID, AVM_CONFIDENCE_SCORE
and
Table: RAW_Address
Columns: SA_PROPERTY_ID
Include: MM_FIPS_STATE_CODE,MM_FIPS_MUNI_CODE
Your query can be simplified, which will likely make it faster:
select
REPLICATE('0',2-LEN(RTRIM(a.MM_FIPS_STATE_CODE)))
+ RTRIM(a.MM_FIPS_STATE_CODE)
+ REPLICATE('0',3-LEN(RTRIM(a.MM_FIPS_MUNI_CODE)))
+ RTRIM(a.MM_FIPS_MUNI_CODE)
AS COUNTY_FIPS
,COUNT(e.PROPERTY_ID) as house_count
,AVG(cast(e.AVM_FINAL_VALUE as bigint)) as avg_avm
,max(cast(e.AVM_FINAL_VALUE as bigint)) as max_avm
,min(cast(e.AVM_FINAL_VALUE as bigint)) as min_avm
from
RAW_Equity e
left join RAW_Address a
ON a.SA_PROPERTY_ID = e.PROPERTY_ID
where
e.AVM_CONFIDENCE_SCORE >= 70
group by
a.MM_FIPS_STATE_CODE, a.MM_FIPS_MUNI_CODE
If RAW_Address doesn't already have a CLUSTERED INDEX with SA_PROPERTY_ID as the first key of the index, then this may help:
CREATE INDEX IX_RAW_Address_SA_PROPERTY_ID ON RAW_Address(SA_PROPERTY_ID)
INCLUDE (MM_FIPS_STATE_CODE, MM_FIPS_MUNI_CODE)
I'm trying to replay SQL Server 2014 Profiler trace that I saved to a DB table. When I open I get "Failed to open a table" error message. There is nothing in the windows logs.
I googled and this error used to happen when upgrading a SQL Server 2000 system to a 64 bit system. That doesn't apply here. I'm running on Windows Server 2012 with a fresh install of SQL Server 2014.
The trace was a TSQL_replay template. I saved it to a table using the following code. The code produced a table with the definition shown.
SELECT *
INTO myTrace
FROM ::fn_trace_gettable(N'c:\Logs\sql_trace_events.trc', default)
CREATE TABLE [dbo].[myTrace]
(
[TextData] [ntext] NULL,
[BinaryData] [image] NULL,
[DatabaseID] [int] NULL,
[TransactionID] [bigint] NULL,
[LineNumber] [int] NULL,
[NTUserName] [nvarchar](256) NULL,
[NTDomainName] [nvarchar](256) NULL,
[HostName] [nvarchar](256) NULL,
[ClientProcessID] [int] NULL,
[ApplicationName] [nvarchar](256) NULL,
[LoginName] [nvarchar](256) NULL,
[SPID] [int] NULL,
[Duration] [bigint] NULL,
[StartTime] [datetime] NULL,
[EndTime] [datetime] NULL,
[Reads] [bigint] NULL,
[Writes] [bigint] NULL,
[CPU] [int] NULL,
[Permissions] [bigint] NULL,
[Severity] [int] NULL,
[EventSubClass] [int] NULL,
[ObjectID] [int] NULL,
[Success] [int] NULL,
[IndexID] [int] NULL,
[IntegerData] [int] NULL,
[ServerName] [nvarchar](256) NULL,
[EventClass] [int] NULL,
[ObjectType] [int] NULL,
[NestLevel] [int] NULL,
[State] [int] NULL,
[Error] [int] NULL,
[Mode] [int] NULL,
[Handle] [int] NULL,
[ObjectName] [nvarchar](256) NULL,
[DatabaseName] [nvarchar](256) NULL,
[FileName] [nvarchar](256) NULL,
[OwnerName] [nvarchar](256) NULL,
[RoleName] [nvarchar](256) NULL,
[TargetUserName] [nvarchar](256) NULL,
[DBUserName] [nvarchar](256) NULL,
[LoginSid] [image] NULL,
[TargetLoginName] [nvarchar](256) NULL,
[TargetLoginSid] [image] NULL,
[ColumnPermissions] [int] NULL,
[LinkedServerName] [nvarchar](256) NULL,
[ProviderName] [nvarchar](256) NULL,
[MethodName] [nvarchar](256) NULL,
[RowCounts] [bigint] NULL,
[RequestID] [int] NULL,
[XactSequence] [bigint] NULL,
[EventSequence] [bigint] NULL,
[BigintData1] [bigint] NULL,
[BigintData2] [bigint] NULL,
[GUID] [uniqueidentifier] NULL,
[IntegerData2] [int] NULL,
[ObjectID2] [bigint] NULL,
[Type] [int] NULL,
[OwnerID] [int] NULL,
[ParentName] [nvarchar](256) NULL,
[IsSystem] [int] NULL,
[Offset] [int] NULL,
[SourceDatabaseID] [int] NULL,
[SqlHandle] [image] NULL,
[SessionLoginName] [nvarchar](256) NULL,
[PlanHandle] [image] NULL,
[GroupID] [int] NULL
)
I tried the same thing and I did not run into any issues. Have you tried with a new trace and save to a different named table?
You have to wait...the 'replay' is grayed out for about 1 minute until it fully loads the script.
Had the same issue and it turned out I was trying to open a trace recorded in Profiler 2014 with Profiler 2008 on a diffrent SQL instance in order to reply the trace. Upgrading profiler to 2014 on a replay instance solved the problem.
You have to create table of specific structure first. Try to export trace into a table from profiler and look what it created. Then just insert subset of columns into the table. Here is what I used for SQL 2012-2017:
------- Trace created with Replay template
USE [testdb]
GO
/****** Object: Table [dbo].[TraceTable] Script Date: 29-Oct-18 17:37:07 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[TraceTableSQL1]
(
[RowNumber] [int] IDENTITY ( 0 , 1 ) NOT NULL ,
[EventClass] [int] NULL ,
[BinaryData] [image] NULL ,
[DatabaseID] [int] NULL ,
[NTUserName] [nvarchar] ( 128 ) NULL ,
[NTDomainName] [nvarchar] ( 128 ) NULL ,
[HostName] [nvarchar] ( 128 ) NULL ,
[ClientProcessID] [int] NULL ,
[ApplicationName] [nvarchar] ( 128 ) NULL ,
[LoginName] [nvarchar] ( 128 ) NULL ,
[SPID] [int] NULL ,
[StartTime] [datetime] NULL ,
[EndTime] [datetime] NULL ,
[Error] [int] NULL ,
[DatabaseName] [nvarchar] ( 128 ) NULL ,
[RowCounts] [bigint] NULL ,
[RequestID] [int] NULL ,
[EventSequence] [bigint] NULL ,
[IsSystem] [int] NULL ,
[ServerName] [nvarchar] ( 128 ) NULL ,
[TextData] [ntext] NULL ,
[EventSubClass] [int] NULL ,
[Handle] [int] NULL ,
PRIMARY KEY CLUSTERED
(
[RowNumber] ASC
)
WITH ( PAD_INDEX = OFF , STATISTICS_NORECOMPUTE = OFF , IGNORE_DUP_KEY = OFF , ALLOW_ROW_LOCKS = ON , ALLOW_PAGE_LOCKS = ON ) ON [PRIMARY]
)
ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
INSERT [TraceTableSQL1]
SELECT
[EventClass] ,
[BinaryData] ,
[DatabaseID] ,
[NTUserName] ,
[NTDomainName] ,
[HostName] ,
[ClientProcessID] ,
[ApplicationName] ,
[LoginName] ,
[SPID] ,
[StartTime] ,
[EndTime] ,
[Error] ,
[DatabaseName] ,
[RowCounts] ,
[RequestID] ,
[EventSequence] ,
[IsSystem] ,
[ServerName] ,
[TextData] ,
[EventSubClass] ,
[Handle]
FROM sys.fn_trace_gettable ( N'd:\temp\profiler.trc' , DEFAULT )
When you have 2 tables table like this
CREATE TABLE #BranchType
(
[External_BranchTypeID] [uniqueidentifier] DEFAULT newsequentialid() NOT NULL,
[BranchTypeID] [smallint] identity(1,1) ,
[BranchTypeDescription] [nvarchar](20) NOT NULL,
[DateCreated] [datetime] NOT NULL,
[UserCreated] [nvarchar](20) NOT NULL,
[DateModified] [datetime] NULL,
[UserModified] [nvarchar](20) NULL,
[IsDeleted] [bit] NOT NULL,
)
CREATE TABLE BranchSubType
(
[External_BranchSubTypeID] [uniqueidentifier] DEFAULT newsequentialid() NOT NULL,
[BranchSubTypeID] [smallint] identity(1,1) ,
[BranchTypeID] [uniqueidentifier] NOT NULL,
[BranchSubTypeDescription] [nvarchar](30) NOT NULL,
[FinancialSystemTypeId] [smallint] NOT NULL,
[DateCreated] [datetime] NOT NULL,
[UserCreated] [nvarchar](20) NOT NULL,
[DateModified] [datetime] NULL,
[UserModified] [nvarchar](20) NULL,
[IsDeleted] [bit] NOT NULL,
)
How can you do an insert like the one below in SQL Server? I am trying to return the guid value
DECLARE #SQLCmd VARCHAR(max)
set #SQLCmd = 'SELECT External_BranchTypeID FROM #BranchType WHERE BranchTypeID =1'
INSERT INTO BranchSubType (BranchTypeID, BranchSubTypeDescription, BranchSubTypeId, DateCreated, UserCreated,IsDeleted)
VALUES ( exec(#SQLCmd), 'Normal',1, getdate(), 'System',0) --FROM #BranchType A WHERE A.BranchTypeID = 1
In this case you don't need to use EXEC
INSERT INTO BranchSubType
(BranchTypeID,
BranchSubTypeDescription,
BranchSubTypeId,
DateCreated,
UserCreated,
IsDeleted)
SELECT External_BranchTypeID,
'Normal',
1,
getdate(),
'System',
0
FROM #BranchType WHERE BranchTypeID =1