创建视图以显示来自众多select语句的字段

时间:2012-11-15 18:56:06

标签: sql views

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

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    

2 个答案:

答案 0 :(得分: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
...

这看起来像是:

metric                  value
TotalRDIs               0
TotalMerchants          0
TotalActiveMerchants    0
TotalInactiveMerchants  0
...

答案 1 :(得分: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