0

1つまたは2つのビューに配置する必要のある、個別の小さな選択クエリがたくさんあります。これらのクエリは、最終的に結合され、提案が必要になります。ストアドプロシージャでうまく機能しないサードパーティのアドホックアプリケーションを使用しているため、一時テーブルは適切ではありません。これをどのように達成できるかについて誰かが何か提案がありますか?以下はクエリです:

select count(cmrckid)as TotalRDIs
FROM  tblCMRCK 


select count(AccountID)as TotalMerchants
FROM  tblAccounts


select count(AccountID)as TotalActiveMerchants
FROM  tblAccounts
where inactive = 0


select count(AccountID)as TotalInActiveMerchants
FROM  tblAccounts
where inactive = 1


select count(AccountID)as TotalwithRDIs
FROM  [tblAccounts] a WITH (NOLOCK) inner join
      [tblcmrck] c WITH (NOLOCK) on a.configid=c.configid and a.accountid=c.acctid


select COUNT(AccountID) as ActivewithRDIs
FROM  [tblAccounts] a WITH (NOLOCK) inner join
      [tblcmrck] c WITH (NOLOCK) on a.configid=c.configid and a.accountid=c.acctid 
      where a.Inactive = 0


select COUNT(AccountID) as ActivewithoutRDIs
FROM  [tblAccounts] a WITH (NOLOCK) 
      where a.inactive = 0 and a.AccountID not in (select AcctID from tblCMRCK)



--select 'Total Active Merchants' / 'Total Merchants' as 'PctActive'
--from #tmptable


--select 'Total InActive Merchants' / 'Total Merchants' as 'PctInActive'
--from #tmpTableExample2


--select 'Active with RDIs' / 'Total Merchants' as 'PctActivewithRDIs'
--FROM  #tmpTableExample


--select 'Active without RDIs' / 'Total Merchants' as 'PctActivewithoutRDIs'
--FROM  #tmpTableExample    
4

2 に答える 2

0

私はこのようなものをもっと試してみます:

SELECT 
    TotalMerchants, 
    TotalRDIs,
    inactive, 
    COUNT(DISTINCT AccountID) AS totalAccounts,
    COUNT(DISTINCT AccountID) / TotalMerchants AS pct,
    SUM(CASE WHEN c IS NULL THEN 0 ELSE 1 END) AS isInRDI,
    SUM(CASE WHEN c IS NULL THEN 0 ELSE 1 END)  / TotalMerchants AS pctRDI
FROM
    (SELECT 
        COUNT(DISTINCT AccountID) AS TotalMerchants
    FROM  
        tblAccounts) tm,
    (SELECT 
        COUNT(DISTINCT AcctID) AS  TotalRDIs 
    FROM 
        tblCMRCK) tr,
    tblAccounts  a LEFT OUTER JOIN
    tblCMRCK c ON
    a.configid=c.configid AND 
    a.accountid=c.acctid
GROUP BY
    TotalMerchants, 
    TotalRDIs,
    inactive
于 2012-11-15T19:22:32.127 に答える
0

これは間違いなく組み合わせることができます:

select count(AccountID)as TotalActiveMerchants
FROM  tblAccounts
where inactive = 0

select count(AccountID)as TotalInActiveMerchants
FROM  tblAccounts
where inactive = 1

の中へ:

SELECT CASE
          WHEN inactive = 0 THEN TotalActiveMerchants
          ELSE TotalInactiveMerchants
        end              AS metric,
        Count(accountid) AS value
FROM   tblaccounts
WHERE  inactive IN ( 0, 1 )
GROUP  BY inactive

次に、メトリックを1つのステートメントに単純にスタックできます。

SELECT 'TotalRDIs' AS metric,
       Count(cmrckid) AS value
FROM   tblcmrck
UNION ALL
SELECT 'TotalMerchants' AS metric,
       Count(accountid)
FROM   tblaccounts
UNION ALL
SELECT CASE
          WHEN inactive = 0 THEN TotalActiveMerchants
          ELSE TotalInactiveMerchants
        end              AS metric,
        Count(accountid) AS value
FROM   tblaccounts
WHERE  inactive IN ( 0, 1 )
GROUP  BY inactive
...

これは次のようになります。

メトリック値
TotalRDIs 0
TotalMerchants 0
TotalActiveMerchants 0
TotalInactiveMerchants 0
..。
于 2012-11-15T19:17:03.783 に答える