Is there a way to optimize this query and get the same results?
-
I have a query which I am running accross several DB's to get information on several of our store's oulet. The query Below provided the info I need but I am not sure if it is the best way to do it and how it will be affected when we have millions of record in our DB. Or can I make it into a single query so when executed in C# I do do get two table in my dataset. Any comment or suggestion is appreciated.
Select 'My Business' AS BusName, sum(PassVer.PassedVerifier) AS 'Passed Verifiers', Sum(PassVer.CashFunded)AS 'Cash Funded', SUM(PassVer.OtherFunding) AS 'Other Funding', Sum(PassVer.CompletedApplications) AS 'Completed Applications', SUM(PassVer.CompleteFundedCash) AS 'Complete - Funded Cash', SUM(Passver.CompleteNotFundedCash) AS 'Complete-Not Cash', SUM(PassVer.Total) AS 'Total Applications', cast((Sum(PassVer.PassedVerifier) * 100.00) / sum(PassVer.CompletedApplications) AS Float) AS '% Completed and verified', cast((SUM(PassVer.CompletedApplications)* 100.00) / Sum(PassVer.Total) AS Float) AS '% of Completed from Total' FROM ( --Selects Applications which Passed Verifier Select 1 AS 'PassedVerifier', 0 As 'CashFunded', 0 As 'OtherFunding', 0 AS 'CompletedApplications', 0 AS 'CompleteFundedCash', 0 AS 'CompleteNotFundedCash', 0 AS 'Total' FROM dbo.BK_Account A LEFT JOIN dbo.BK_AccountQueue Q ON A.Guid=Q.AccountGuid where Q.QueueId= 'Pass Validation' UNION ALL --Selects Applications which passed the verifier and are funded by Cash. SELECT 0 AS 'PassedVerifier', 1 AS 'CashFunded', 0 As 'OtherFunding', 0 AS 'CompletedApplications', 0 AS 'CompleteFundedCash', 0 AS 'CompleteNotFundedCash', 0 AS 'Total' FROM dbo.BK_Account A LEFT JOIN dbo.BK_AccountQueue Q ON A.Guid=Q.AccountGuid where A.fundingMethodId = 'Cash' and Q.QueueId= 'Pass Validation' UNION ALL --Selects Applications which passed the verifier and are funded by methods other than Cash. SELECT 0 AS 'PassedVerifier', 0 AS 'CashFunded', 1 As 'OtherFunding', 0 AS 'CompletedApplications',0 AS 'CompleteFundedCash', 0 AS 'CompleteNotFundedCash', 0 AS 'Total' FROM dbo.BK_Account A LEFT JOIN dbo.BK_AccountQueue Q ON A.Guid=Q.AccountGuid where A.fundingMethodId != 'Cash' and Q.QueueId= 'Pass Validation' UNION ALL --Selects Applications which are completed. SELECT 0 AS 'PassedVerifier', 0 AS 'CashFunded', 0 As 'OtherFunding', 1 AS 'CompletedApplications', 0 AS 'CompleteFundedCash', 0 AS 'CompleteNotFundedCash', 0 AS 'Total' FROM dbo.BK_Account A LEFT JOIN dbo.BK_AccountQueue Q ON A.Guid=Q.AccountGuid where Q.Queue
-
I have a query which I am running accross several DB's to get information on several of our store's oulet. The query Below provided the info I need but I am not sure if it is the best way to do it and how it will be affected when we have millions of record in our DB. Or can I make it into a single query so when executed in C# I do do get two table in my dataset. Any comment or suggestion is appreciated.
Select 'My Business' AS BusName, sum(PassVer.PassedVerifier) AS 'Passed Verifiers', Sum(PassVer.CashFunded)AS 'Cash Funded', SUM(PassVer.OtherFunding) AS 'Other Funding', Sum(PassVer.CompletedApplications) AS 'Completed Applications', SUM(PassVer.CompleteFundedCash) AS 'Complete - Funded Cash', SUM(Passver.CompleteNotFundedCash) AS 'Complete-Not Cash', SUM(PassVer.Total) AS 'Total Applications', cast((Sum(PassVer.PassedVerifier) * 100.00) / sum(PassVer.CompletedApplications) AS Float) AS '% Completed and verified', cast((SUM(PassVer.CompletedApplications)* 100.00) / Sum(PassVer.Total) AS Float) AS '% of Completed from Total' FROM ( --Selects Applications which Passed Verifier Select 1 AS 'PassedVerifier', 0 As 'CashFunded', 0 As 'OtherFunding', 0 AS 'CompletedApplications', 0 AS 'CompleteFundedCash', 0 AS 'CompleteNotFundedCash', 0 AS 'Total' FROM dbo.BK_Account A LEFT JOIN dbo.BK_AccountQueue Q ON A.Guid=Q.AccountGuid where Q.QueueId= 'Pass Validation' UNION ALL --Selects Applications which passed the verifier and are funded by Cash. SELECT 0 AS 'PassedVerifier', 1 AS 'CashFunded', 0 As 'OtherFunding', 0 AS 'CompletedApplications', 0 AS 'CompleteFundedCash', 0 AS 'CompleteNotFundedCash', 0 AS 'Total' FROM dbo.BK_Account A LEFT JOIN dbo.BK_AccountQueue Q ON A.Guid=Q.AccountGuid where A.fundingMethodId = 'Cash' and Q.QueueId= 'Pass Validation' UNION ALL --Selects Applications which passed the verifier and are funded by methods other than Cash. SELECT 0 AS 'PassedVerifier', 0 AS 'CashFunded', 1 As 'OtherFunding', 0 AS 'CompletedApplications',0 AS 'CompleteFundedCash', 0 AS 'CompleteNotFundedCash', 0 AS 'Total' FROM dbo.BK_Account A LEFT JOIN dbo.BK_AccountQueue Q ON A.Guid=Q.AccountGuid where A.fundingMethodId != 'Cash' and Q.QueueId= 'Pass Validation' UNION ALL --Selects Applications which are completed. SELECT 0 AS 'PassedVerifier', 0 AS 'CashFunded', 0 As 'OtherFunding', 1 AS 'CompletedApplications', 0 AS 'CompleteFundedCash', 0 AS 'CompleteNotFundedCash', 0 AS 'Total' FROM dbo.BK_Account A LEFT JOIN dbo.BK_AccountQueue Q ON A.Guid=Q.AccountGuid where Q.Queue
Try using
case
statements instead?...
CASE WHEN Q.QueueId= 'Pass Validation' THEN 1 ELSE 0 END AS 'PassedVerifier',
CASE WHEN A.fundingMethodId = 'Cash' and Q.QueueId= 'Pass Validation' THEN 1 ELSE 0 END AS 'CashFunded',
...