2011-11-21 55 views
0

我遇到一個問題,即我有限的SQL知識使我無法理解。T-SQL分組信息集

第一問題:

我有我需要運行報表上的數據庫,它包含了用戶權利的配置。該報告需要顯示這些配置的明確列表並對每個配置進行計數。

所以在我的DB行看起來是這樣的:

USER_ID SALE_ITEM_ID SALE_ITEM_NAME PRODUCT_NAME CURRENT_LINK_NUM PRICE_SHEET_ID 
37715  547    CultFREE CultPlus   0    561 

上面的線是用戶配置的一行,對每一位用戶ID有可能是這些線路的1-5。所以配置的定義是多行數據共享一個共同的用戶ID與可變屬性。實例,其中> 1具有該配置並且具有該配置的實例的計數。

希望這個很清楚嗎?

任何想法?!?!

我已經嘗試了各種各樣的小組和工會,也分組設置功能無濟於事。

如果有人能給我一些指點,這將是非常偉大的!

+0

所以... u需要應用到行的不同列表全部用戶 ? ...不,這是不對的 – War

+0

我想我需要創建設置ID,然後在設置ID上分組,所以當用戶有產品x,y和z和其他屬性xyz時,我將分配一個設置ID,找到另一個用戶該組數據將被賦予相同的ID。然後我可以將這些ID分組,並且我有我想要的組? – Yoda

+1

@Yoda請多放一些行,你試圖實現的結果是什麼 –

回答

0

哎喲疼......

確定這樣的問題:

  1. 行代表一個可配置的線
  2. 用戶可以鏈接到超過1行配置的
  3. 配置行分組在一起形成配置集
  4. 我們想弄清楚所有的獨特配置集
  5. 我們想知道用戶正在使用它們。

解決方案(其有點混亂,但這個想法是有,複製並粘貼到SQL Management Studio中)...

-- ok so i imported the data to a table named SampleData ... 
-- 1. import the data 
-- 2. add a new column 
-- 3. select all the values of the config in to the new column (Configuration_id) 
--UPDATE [dbo].[SampleData] 
--SET [Configuration_ID] = SALE_ITEM_ID + SALE_ITEM_NAME + [PRODUCT_NAME] + [CURRENT_LINK_NUM] + [PRICE_SHEET_ID] + [Configuration_ID] 

-- 4. i then selected just the distinct values of those and found 6 distinct Configuration_id's 
--SELECT DISTINCT [Configuration_ID] FROM [dbo].[SampleData] 

-- 5. to make them a bit easier to read and work with i gave them int values instead 
-- for me it was easy to do this manually but you might wanna do some trickery here to autonumber them or something 
-- basic idea is to run the step 4 statement but select into a new table then add a new primary key column and set identity spec on it 
-- that will generate u a bunch of incremental numbers for your config id's so u can then do something like ... 
--UPDATE [dbo].[SampleData] sd 
--SET Configuration_ID = (SELECT ID FROM TempConfigTable WHERE Config_ID = sd.Configuration_ID) 

-- at this point you have all your existing rows with a unique ident for the values combined in each row. 
-- so for example in my dataset i have several rows where only the user_id has changed but all look like this ... 
--SALE_ITEM_ID SALE_ITEM_NAME PRODUCT_NAME CURRENT_LINK_NUM PRICE_SHEET_ID Configuration_ID 
--54101 TravelFREE TravelPlus 0 56101 1 

-- now you have a config id you can start to work on building sets up ... 
-- each user is now matched with 1 or more config id 
-- 6. we use a CTE (common table expression) to link the possibles (keeps the join small) ... 
--WITH Temp (ConfigID) 
--AS 
--(
-- SELECT DISTINCT SD.Configuration_Id --SD2.Configuration_Id, SD3.Configuration_Id, SD4.Configuration_Id, SD5.Configuration_Id, 
-- FROM [dbo].[SampleData] SD 
--) 
-- this extracts all the possible combinations using the CTE 
-- on the basis of what you told me, max rows per user is 6, in the result set i have i only have 5 distinct configs 
-- meaning i gain nothing by doing a 6th join. 
-- cross joins basically give you every combination of unique values from the 2 tables but we joined back on the same table 
-- so its every possible combination of Temp + Temp (ConfigID + ConfigID) ... per cross join so with 5 joins its every combination of 
-- Temp + Temp + Temp + Temp + Temp .. good job temp only has 1 column with 5 values in it 
-- 7. uncomment both this and the CTE above ... need to use them together 
--SELECT DISTINCT T.ConfigID C1, T2.ConfigID C2, T3.ConfigID C3, T4.ConfigID C4, T5.ConfigID C5 
--INTO [SETS] 
--FROM Temp T 
--CROSS JOIN Temp T2 
--CROSS JOIN Temp T3 
--CROSS JOIN Temp T4 
--CROSS JOIN Temp T5 

-- notice the INTO clause ... this dumps me out a new [SETS] table in my db 
-- if i go add a primary key to this and set its ident spec i now have unique set id's 
-- for each row in the table. 
--SELECT * 
--FROM [dbo].[SETS] 

-- now here's where it gets interesting ... row 1 defines a set as being config id 1 and nothing else 
-- row 2 defines set 2 as being config 1 and config 2 and nothing else ... and so on ... 
-- the problem here of course is that 1,2,1,1,1 is technically the same set as 1,1,1,2,1 from our point of view 
-- ok lets assign a set to each userid ... 
-- 8. first we pull the distinct id's out ... 
--SELECT DISTINCT USER_ID usr, null SetID 
--INTO UserSets 
--FROM SampleData 

-- now we need to do bit a of operating on these that's a bit much for a single update or select so ... 
-- 9. process findings in a loop 
DECLARE @currentUser int 
DECLARE @set int 
-- while theres a userid not linked to a set 
WHILE EXISTS(@currentUser = SELECT TOP 1 usr FROM UserSets WHERE SetId IS NULL) 
BEGIN 
    -- figure out a set to link it to 
    SET @set = (
     SELECT TOP 1 ID 
     FROM [SETS] 
     -- shouldn't really do this ... basically need to refactor in to a table variable then compare to that 
     -- that way the table lookup on ur main data is only 1 per User_id 
     WHERE C1 IN (SELECT DISTINCT Configuration_id FROM SampleData WHERE USER_ID = @currentUser) 
     AND C2 IN (SELECT DISTINCT Configuration_id FROM SampleData WHERE USER_ID = @currentUser) 
     AND C3 IN (SELECT DISTINCT Configuration_id FROM SampleData WHERE USER_ID = @currentUser) 
     AND C4 IN (SELECT DISTINCT Configuration_id FROM SampleData WHERE USER_ID = @currentUser) 
     AND C5 IN (SELECT DISTINCT Configuration_id FROM SampleData WHERE USER_ID = @currentUser) 
    ) 
    -- hopefully that worked 
    IF(@set IS NOT NULL) 
    BEGIN 
     -- tell the usersets table 
     UPDATE UserSets SET SetId = @set WHERE usr = @currentUser 
     set @set = null 
    END 
    ELSE -- something went wrong ... set to 0 to prevent endless loop but any userid linked to set 0 is a problem u need to look at 
     UPDATE UserSets SET SetId = 0 WHERE usr = @currentUser 
    -- and round we go again ... until we are done 
END 
+0

代表很值得,謝謝隊友! – Yoda

0
SELECT 
USER_ID, 
SALE_ITEM_ID, ETC..., 
COUNT(*) WhateverYouWantToNameCount 

FROM TableNAme 
GROUP BY USER_ID 
+0

我希望我沒有錯過你的問題中的一些微妙之處,因爲你說你已經嘗試了一組。 – Maess

+0

這會給我一個用戶配置中的屬性數,我需要按每個用戶的配置進行分組,給我一組配置和每個配置的實例數。謝謝 – Yoda

+0

好吧,那麼請更新您的問題,以更具體地說明您想要分組的內容,我無法理解您所發佈的內容。您是否詢問除user_id以外的列的唯一組合出現多少次? – Maess