2012-01-17 12 views
1

我今天早些時候問過一個question,但我有一個後續問題,增加了多值字段的複雜性。如何從多值字段中查找具有不同條件的多條記錄的條數

給定如下表:

ID lightness | darkness  | color 
------|-------------|--------------|--------- 
1  |10   | 20   | green, blue, yellow 
2  |10   | 08   | green, purple, orange 
3  |10   | 10   | black, magenta, orange 
4  |20   | 05   | green, creame 
5  |10   | 20   | red, purple 
6  |10   | 16   | red, white 
7  |33   | 20   | brown, red 
8  |10   | 10   | green, blue 

我想搞清楚:

  • 的記錄計數,其中顏色有記錄亮度10
  • 計數,其中顏色有暗20

因此最終輸出將是:

Color | lightness | darkness | Total 
---------|-------------|------------|--------- 
green | 4   | 1   | 5 
red  | 2   | 2   | 4 
Total | 6   | 3   | 9 

group by將失去其價值,結果將不正確。該.value可用於對多值字段,所以我可以做到以下幾點: 例如:

select * from colortable where color.value = 2 

將展示綠色存在

select * from colortable where color.value = 3 

將展示紅色存在

所有記錄的所有記錄

據我所知,這是非常糟糕的設計,但我繼承了這一點,必須對數據運行查詢。

+2

你有任何改變設計的能力?我向你保證,除了像這樣離開桌面的任何「空間節省」之外,讓自己更加容易頭疼。此外,「總計」行最好留給您的報告程序,_不適用於SQL引擎本身 – 2012-01-17 22:05:46

+0

我確定沒有添加總計行。關於改變設計,我可以但也會涉及改變人們使用的應用程序的前端。因此,要回答您的問題,是的,我有權訪問並打算更改它,但想要從現有設計中查詢並提取結果。 – Anthony 2012-01-17 23:24:02

回答

2

既然你有一個多字段值列,你最好的辦法是讓一個新表,並拋出所有已知的顏色在那張桌子裏。所以你的新表看起來像

ID | cid | color 
---|-----|------- 
1 | 2 | green 
2 | 3 | red 

現在你已經有了一些加入!

SELECT p.color, 
     Sum(IIf(lightness=10,1,0)) as lightness, 
     Sum(IIf(darkness=20,1,0)) as darkness, 
     lightness+darkness AS Total 
FROM colortable c inner join predefinedcolors p on p.id = c.color.value 
WHERE c.color.value in (2,3) 
GROUP BY c.color, p.conditionid.value 
0

如果有一組已知的顏色,則需要建立一個「KnownColors」表。

SELECT ColourTable.ID, KnownColour, ColourTable.Lightness, ColourTable.Darkness, 
ColourTable.Colour, ColourTable.Lightness, ColourTable.Darkness 
FROM ColourTable, knownColours WHERE (((ColourTable.Colour) Like "*" 
& [KnownColour] & "*") AND ((ColourTable.Lightness)=10) 
AND ((ColourTable.Darkness)=20)); 

會給你一個行對每個顏色,其中亮度爲10,黑暗是20

0

你可以從下面的表格使用讓您的結果,


-- ** Function for creating column from colors ** 

CREATE FUNCTION [dbo].[Split](@String varchar(8000), @Delimiter char(1))  
returns @temptable TABLE (items varchar(8000))  
as  
begin  
    declare @idx int  
    declare @slice varchar(8000)  

    select @idx = 1  
     if len(@String)0) 
      insert into @temptable(Items) values(RTRIM(LTRIM(@slice)))  

     set @String = right(@String,len(@String) - @idx)  
     if len(@String) = 0 break  
    end 
return  
end 
GO 

-- ** Create view to get all colors in one column ** 

Create view [dbo].[vColors] 
as 
select distinct items from split(
(SELECT SUBSTRING(
(SELECT ',' + color 
FROM colortable 
ORDER BY color 
FOR XML PATH('')),2,200000) AS CSV_Color),',') 

GO 

-- ** And Finally get the result from this query ** 

select items,sum(lightness)lightness,sum(darkness)darkness 
from colortable c inner join vcolors v on c.color like '%'+v.items+'%' 
group by items 

-- ** output is ** 

items lightness darkness 
------------------------------- 
black  10  10 
blue  20  30 
brown  33  20 
creame  20  5 
green  50  43 
magenta  10  10 
orange  20  18 
purple  20  28 
red   53  56 
white  10  16 
yellow  10  20 

+0

是的,這是T-SQL .. – Kishor 2012-01-19 06:45:26

+1

T-SQL不能與MS Access一起工作,我很想投票下來,但也許你沒有意識到SQL是一個通用標記?有很多不同類型的SQL,這裏需要的特定類型是MS Access中使用的Jet/ACE SQL。 – Fionnuala 2012-01-19 12:28:44

相關問題