Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Is there a way to optimize this query and get the same results?

Is there a way to optimize this query and get the same results?

Scheduled Pinned Locked Moved Database
databasecsharpdata-structuresbusinessquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Skanless
    wrote on last edited by
    #1

    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
    
    P 1 Reply Last reply
    0
    • S Skanless

      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
      
      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      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',
      ...

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups