0

我有一堆单独的小选择查询,我需要将它们放入一个视图或两个视图中,最终可能会加入并需要建议。我正在使用第三方临时应用程序,它不能很好地处理存储过程,因此临时表不适合。有人对如何实现这一点有任何建议吗?以下是查询:

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

然后,您可以简单地将指标堆叠到一个语句中:

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

这看起来像:

度量值
总 RDI 0
总商户 0
TotalActiveMerchants 0
不活跃商家总数 0
...
于 2012-11-15T19:17:03.783 回答