如何从具有多值字段的同一表中查找具有不同where条件的多个记录的计数

时间:2012-01-17 21:50:23

标签: sql ms-access

我今天早些时候问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

将显示红色存在的所有记录

我知道这是非常糟糕的设计,但我继承了这个并且必须对数据运行查询。

3 个答案:

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

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

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