2016-09-14 76 views
-2

第[count]我有3個表:
CustomerTypes表
Customers表(具有外鍵CustomerType)。一個客戶只能有一個客戶類型。
CustomersCollection表(包含許多customerIds)查詢一欄,顯示[最大計數]

查詢的主要SELECT將位於CustomerTypes上。我會選擇兩列:CustomerTypeName和CountInCollection

我的查詢中的CustomerCount列需要顯示類似以下內容:
[總#的[在CustomerType是在客戶收集客戶的總#] CustomerType中的客戶]

如何獲得作爲集合一部分的CustomerType的適當客戶數?

實施例:

  • customer1表,顧客2,和Customer3都是CustomerTypeA的。
  • CustomerCollection1擁有客戶Customer1和Customer2。 CustomerTypeA記錄的CountInCollection列應顯示「2的3」。

enter image description here

這是我如何我能得到單獨的查詢每個計數:

-- Total customers in customer collection of customer type 
SELECT COUNT(c.Id) 
FROM Customer c 
INNER JOIN CustomerCollection cc ON c.Id = cc.CustomerId 
WHERE cc.CollectionId = 1019 AND c.CustomerTypeId=1000 

-- Total customers in customer type 
SELECT COUNT(Id) FROM 
Customer WHERE CustomerTypeId=1000 
+0

的樣本數據而表格形式的理想結果將真正幫助傳達wha你想要做什麼。 –

+0

@TabAlleman我不是在說我需要什麼。我在問如何去選擇sql server中的「count count」列。這篇文章肯定可以幫助其他人,因爲我的例子很好解釋。 –

+0

我不同意你的例子很好解釋。相反,它缺乏足夠的細節讓任何人提供答案。我懷疑這個代碼並不複雜,但沒有任何細節可以與它一起工作,任何人都會猜測你在這裏做什麼。我已經多次閱讀過這個問題,但目前尚不清楚。這將是一個很好的開始。 http://spaghettidba.com/2015/04/24/how-to-post-a-t-sql-question-on-a-public-forum/ –

回答

1

由於您使用SQL 2008,我將採取Common Table Expressions,又名熱膨脹係數的優勢,裝配數據。

首先,我們需要一些測試數據。注意:我已經拋出了一些「異常值」,以便您可以看到這種邏輯在哪裏可以咬你。

DECLARE @CustomerTypes TABLE 
    (
    CustomerTypeID INT, 
    [Customer Type] VARCHAR(100) 
    ) 

INSERT INTO @CustomerTypes 
    SELECT 1, 'TypeA' 
    UNION SELECT 2, 'TypeB' 
    UNION SELECT 3, 'TypeC' --NOTE: An outlier (not in customers-collection) 
    UNION SELECT 4, 'TypeD' --NOTE: An outlier (not in customers) 

DECLARE @Customers TABLE 
    (
    CustomerID INT, 
    CustomerTypeID INT 
    ) 

INSERT INTO @Customers 
    SELECT 1, 1 
    UNION SELECT 2, 1 
    UNION SELECT 3, 1 
    UNION SELECT 4, 2 
    UNION SELECT 5, 2 
    UNION SELECT 6, 2 
    UNION SELECT 7, 3 

DECLARE @CustomersCollection TABLE 
    (
    CollectionID INT IDENTITY(1,1), 
    CustomerID INT 
    ) 

INSERT INTO @CustomersCollection 
    (CustomerID) 
    SELECT TOP 2 --INSERT 2 of 3 
     CustomerID FROM @Customers WHERE CustomerTypeID = 1 --TypeA 

INSERT INTO @CustomersCollection 
    (CustomerID) 
    SELECT TOP 1 --INSERT 1 of 3 
     CustomerID FROM @Customers WHERE CustomerTypeID = 2 --TypeB 

其次,組裝CTE數據,並生成輸出

;WITH CTE_COUNT_TYPE(CustomerTypeID, TypeCount) 
AS 
(
    SELECT CustomerTypeID, COUNT(1) 
    FROM @Customers 
    GROUP BY CustomerTypeID 
) 
--SELECT * FROM CTE_COUNT_TYPE --DEBUG 
, 
CTE_COUNT_COLLECTION(CustomerTypeID, CollectionCount) 
AS 
(
    SELECT CustomerTypeID, COUNT(1) 
    FROM @CustomersCollection CC 
     INNER JOIN @Customers C 
      ON CC.CustomerID = C.CustomerID 
    GROUP BY CustomerTypeID 
) 
--SELECT * FROM CTE_COUNT_COLLECTION --DEBUG 
SELECT [Customer Type], 
    --CONVERT is necessary to combine INT data type (i.e. Count) and VARCHAR data type (i.e. 'as') 
    CONVERT(VARCHAR(100), COALESCE(CCC.CollectionCount, 0)) + 
     ' of ' + 
     CONVERT(VARCHAR(100), COALESCE(CCT.TypeCount, 0)) As [Count in Collection] 

    FROM @CustomerTypes CT 
    LEFT OUTER JOIN @Customers C --Left outer join assists in outliers 
     ON CT.CustomerTypeID = C.CustomerTypeID 
    LEFT OUTER JOIN CTE_COUNT_TYPE CCT --Left outer join assists in outliers 
     ON CCT.CustomerTypeID = CT.CustomerTypeID 
    LEFT OUTER JOIN CTE_COUNT_COLLECTION CCC --Left outer join assists in outliers 
     ON CCC.CustomerTypeID = CT.CustomerTypeID 
    GROUP BY CT.[Customer Type] 
     , CCC.CollectionCount 
     , CCT.TypeCount 
+0

非常感謝你的深度響應。我現在正在閱讀它。你能否看到我的更新後的底部的2個查詢,以確保你完全理解我的問題。我很抱歉,我沒有在那裏開始的那些查詢。 –

+0

沒問題。至於你的兩個查詢,爲什麼你在where子句中包含「cc.CollectionId = 1019」? – SqlOnly

+0

我的頁面上的主鍵是CustomerCollectionId,它作爲參數傳遞給查詢。正如您已經知道的那樣:客戶是被添加到客戶收藏的東西。但是在個別CustomerCollection的頁面上,我想列出不同的CustomerType,並在Count Column中顯示CustomerType中有多少Customer實際上是CustomerCollection的一部分。 CustomerCollection是一個包含兩個鍵CollectionId和CustomerId的連接表。換句話說,對於CollectionId 1019,我將根據添加到CustomerCollection的Customers列出不同的CustomerTypes。 –

1

希望如此,我得將以下問題

select 
    ct.CustomerTypeName as [Customer Type], 
    convert(varchar(30),count(cc.CollectionId)) + ' of ' + convert(varchar(30), count(c.CustomerId)) as [Count in Collection] 
from 
    @Customer c 
    inner join @CustomerType ct on ct.CustomerTypeId = c.CustomerTypeId 
    left join @CustomerCollection cc on cc.CustomerId = c.CustomerId 
group by 
    CustomerTypeName 

數據腳本 -

declare @customerType table (CustomerTypeId int, CustomerTypeName varchar(100)) 
insert into @customerType (CustomerTypeId, CustomerTypeName) 
select 30, 'TypeA' 
union 
select 40, 'TypeB' 

declare @customer table (CustomerId int, CustomerTypeId int) 
insert into @customer (CustomerId, CustomerTypeId) 
select 1, 30 
union 
select 2, 30 
union 
select 3, 30 
union 
select 4, 40 
union 
select 5, 40 
union 
select 6, 40 

declare @customercollection table (CollectionId int, CustomerId int) 
insert into @customercollection (CollectionId, CustomerId) 
select 100, 1 
union 
select 200, 2 
union 
select 300, 5