组计数从类似case语句派生的值

时间:2013-12-09 17:12:14

标签: sql count group-by sql-like

我有一张表格,格式为

propertyvaluestrings

userSignedwww.website.com.brxxx /销售/卖

www.website.comuser /卖

www.website.com.co.uk \暂停营业税

我需要做的是按照下面的sql的网站列进行分组,以获得每个网站的总计

SELECT top 1000
      CASE
            WHEN  propertyvaluestrings LIKE '%www.website.co.uk%' THEN 'UK'
            WHEN  propertyvaluestrings LIKE '%www.website.us%' THEN 'USA'
            WHEN  propertyvaluestrings LIKE '%www.website.com.br%' THEN 'Brazil'
            WHEN  propertyvaluestrings LIKE '%www.website.co.id%' THEN 'indonesia'
            WHEN  propertyvaluestrings LIKE '%www.website.com%' THEN 'Global'
       ELSE 'XXXXXXXXXXXXXXXX----Unknown'
       END as Site
    ,
    PropertyValueStrings

FROM profiles

1 个答案:

答案 0 :(得分:1)

您的意思是计算每个国家/地区的PropertyValueStrings数量吗?如果是这样,这应该有效:

SELECT Site, COUNT(*) AS Count
FROM (
    SELECT (CASE
                WHEN  propertyvaluestrings LIKE '%www.website.co.uk%' THEN 'UK'
                WHEN  propertyvaluestrings LIKE '%www.website.us%' THEN 'USA'
                WHEN  propertyvaluestrings LIKE '%www.website.com.br%' THEN 'Brazil'
                WHEN  propertyvaluestrings LIKE '%www.website.co.id%' THEN 'indonesia'
                WHEN  propertyvaluestrings LIKE '%www.website.com%' THEN 'Global'
           ELSE 'XXXXXXXXXXXXXXXX----Unknown'
           END) AS [Site]
    ,
    propertyvaluestrings 
    FROM Profiles
) X
GROUP BY Site