Sql 查询以解开具有相同值的列

Sql 查询以解开具有相同值的列,sql,Sql,我在查询中遇到一些SQL问题,可能需要一些帮助 这里我有一个查询,根据每位客户在我的商店购买的iTen数量,向我显示他们的最佳类别: SELECT Email, case when (C.Category1 > C.Category2 AND C.Category1 > C.Category3 AND C.Category1 > C.Category4 AND C.Category1 > C.Category5 AND C.Category1 > C.Categ

我在查询中遇到一些SQL问题,可能需要一些帮助

这里我有一个查询,根据每位客户在我的商店购买的iTen数量,向我显示他们的最佳类别:

SELECT Email, 
case 
when (C.Category1 > C.Category2 AND C.Category1 > C.Category3 AND C.Category1 > C.Category4 AND C.Category1 > C.Category5 AND C.Category1 > C.Category6 AND C.Category1 > C.Category7 AND C.Category1 > C. Category8 ) then 'Category1'
when (C.Category2>C.Category1 AND C.Category2 > C.Category3 AND C.Category2 > C.Category4 AND C.Category2 > C.Category5 AND C.Category2 > C.Category6 AND C.Category2 > C.Category7 AND C.Category2 > C.Category8) then 'Category2'
when (C.Category3 >C.Category1 AND C.Category3 > C.Category2 AND C.Category3 > C.Category4 AND C.Category3 > C.Category5 AND C.Category3 > C.Category6 AND C.Category3 > C.Category7 AND C.Category3 > C.Category8) then 'Category3' 
when (C.Category4 >C.Category1 AND C.Category4 > C.Category2 AND C.Category4 > C.Category3 AND C.Category4 > C.Category5 AND C.Category4 > C.Category6 AND C.Category4 > C.Category7 AND C.Category4 > C.Category8) then 'Category 4'
when (C.Category5 >C.Category1 AND C.Category5 > C.Category2 AND C.Category5 > C.Category3 AND C.Category5 > C.Category4 AND C.Category5 > C.Category6 AND C.Category5 > C.Category7 AND C.Category5 > C.Category8) then 'Category5'
when (C.Category6 >C.Category1 AND C.Category6 > C.Category2 AND C.Category6 > C.Category3 AND C.Category6 > C.Category4 AND C.Category6 > C.Category5 AND C.Category6 > C.Category7 AND C.Category6 > C.Category8) then 'Category6'
when (C.Category7 >C.Category1 AND C.Category7 > C.Category2 AND C.Category7 > C.Category3 AND C.Category7 > C.Category4 AND C.Category7 > C.Category5 AND C.Category7 > C.Category6 AND C.Category7 > C.Category8) then 'Category7' 
when (C.Category8 >C.Category1 AND C.Category8 > C.Category2 AND C.Category8 > C.Category3 AND C.Category8 > C.Category4 AND C.Category8 > C.Category5 AND C.Category8 > C.Category6 AND C.Category8 > C.Category7) then 'Category 8'     
else 'Tie'
end as BestCategory
FROM Category as C
由于我有一些类别的iTen购买量相同,我需要根据累计收入(其他表)对其中一些类别进行解列


你们能帮我找到一个解开这个查询的方法吗?

我把去掉前导逗号作为练习。(我不知道你在哪个平台上:也许,
子字符串(,2100)
?)我想这就是你想要的

select
    Email,
    /* concatenate the list of categories that match the max value */
    case when Category1 = CategoryMax then ', Category1' end +
    case when Category2 = CategoryMax then ', Category2' end +
    case when Category3 = CategoryMax then ', Category3' end +
    case when Category4 = CategoryMax then ', Category4' end +
    case when Category5 = CategoryMax then ', Category5' end +
    case when Category6 = CategoryMax then ', Category6' end +
    case when Category7 = CategoryMax then ', Category7' end +
    case when Category8 = CategoryMax then ', Category8' end as BestCategories
from 
    Category c inner join
    (
        select /* what is the largest category value for each email? */
            Email,
            min(case 
                when Category1 >= Category2 and Category1 >= Category3 and Category1 >= Category4 and Category1 >= Category5 and Category1 >= Category6 and Category1 >= Category7 and Category1 >= Category8 then Category1
                when Category2 >= Category1 and Category2 >= Category3 and Category2 >= Category4 and Category2 >= Category5 and Category2 >= Category6 and Category2 >= Category7 and Category2 >= Category8 then Category2
                when Category3 >= Category1 and Category3 >= Category2 and Category3 >= Category4 and Category3 >= Category5 and Category3 >= Category6 and Category3 >= Category7 and Category3 >= Category8 then Category3 
                when Category4 >= Category1 and Category4 >= Category2 and Category4 >= Category3 and Category4 >= Category5 and Category4 >= Category6 and Category4 >= Category7 and Category4 >= Category8 then Category 4
                when Category5 >= Category1 and Category5 >= Category2 and Category5 >= Category3 and Category5 >= Category4 and Category5 >= Category6 and Category5 >= Category7 and Category5 >= Category8 then Category5
                when Category6 >= Category1 and Category6 >= Category2 and Category6 >= Category3 and Category6 >= Category4 and Category6 >= Category5 and Category6 >= Category7 and Category6 >= Category8 then Category6
                when Category7 >= Category1 and Category7 >= Category2 and Category7 >= Category3 and Category7 >= Category4 and Category7 >= Category5 and Category7 >= Category6 and Category7 >= Category8 then Category7 
                when Category8 >= Category1 and Category8 >= Category2 and Category8 >= Category3 and Category8 >= Category4 and Category8 >= Category5 and Category8 >= Category6 and Category8 >= Category7 then Category 8     
                else null
            end) as CategoryMax /* min is just a dummy aggregate */
        from Category
        group by Email
    ) cmax
        on cmax.Email = c.Email
您可以通过连接到另一个表来扩展此功能,以断开与以下内容的联系。不过现在开始觉得很乱了

left outer join Revenue r
    on  r.Email = c.Email and
        case when Category1 = CategoryMax then 1 end +
        case when Category2 = CategoryMax then 1 end +
        case when Category3 = CategoryMax then 1 end +
        case when Category4 = CategoryMax then 1 end +
        case when Category5 = CategoryMax then 1 end +
        case when Category6 = CategoryMax then 1 end +
        case when Category7 = CategoryMax then 1 end +
        case when Category8 = CategoryMax then 1 end > 1

或者,您可能只需要添加一个相关子查询作为
else
案例:

else (
    select
        case 
            when R.Category1 > R.Category2 and R.Category1 > R.Category3 and R.Category1 > R.Category4 and R.Category1 > R.Category5 and R.Category1 > R.Category6 and R.Category1 > R.Category7 and R.Category1 > R.Category8 then 'Category1'
            when R.Category2 > R.Category1 and R.Category2 > R.Category3 and R.Category2 > R.Category4 and R.Category2 > R.Category5 and R.Category2 > R.Category6 and R.Category2 > R.Category7 and R.Category2 > R.Category8 then 'Category2'
            when R.Category3 > R.Category1 and R.Category3 > R.Category2 and R.Category3 > R.Category4 and R.Category3 > R.Category5 and R.Category3 > R.Category6 and R.Category3 > R.Category7 and R.Category3 > R.Category8 then 'Category3' 
            when R.Category4 > R.Category1 and R.Category4 > R.Category2 and R.Category4 > R.Category3 and R.Category4 > R.Category5 and R.Category4 > R.Category6 and R.Category4 > R.Category7 and R.Category4 > R.Category8 then 'Category 4'
            when R.Category5 > R.Category1 and R.Category5 > R.Category2 and R.Category5 > R.Category3 and R.Category5 > R.Category4 and R.Category5 > R.Category6 and R.Category5 > R.Category7 and R.Category5 > R.Category8 then 'Category5'
            when R.Category6 > R.Category1 and R.Category6 > R.Category2 and R.Category6 > R.Category3 and R.Category6 > R.Category4 and R.Category6 > R.Category5 and R.Category6 > R.Category7 and R.Category6 > R.Category8 then 'Category6'
            when R.Category7 > R.Category1 and R.Category7 > R.Category2 and R.Category7 > R.Category3 and R.Category7 > R.Category4 and R.Category7 > R.Category5 and R.Category7 > R.Category6 and R.Category7 > R.Category8 then 'Category7'
            when R.Category8 > R.Category1 and R.Category8 > R.Category2 and R.Category8 > R.Category3 and R.Category8 > R.Category4 and R.Category8 > R.Category5 and R.Category8 > R.Category6 and R.Category8 > R.Category7 then 'Category8'     
            else 'Tie'
         end
     from Revenue as R
     where R.Email = C.Email
)

这不会将比较仅限于第一个tie中涉及的那些类别,尽管不需要做更多的工作。

样本数据和期望的结果将有助于传达您试图做的事情。假设我想将[else'tie]替换为另一个具有与上述相同结构的表中的查找。我没有每个类别的最大值。我用购买ITEN数量最多的类别对每个用户进行分类。你的平台是什么?取消分类并处理行中的数据可能是最简单的方法。我已经尝试过了,但是,由于我在SQL Server 2005上运行,我遇到了以下错误:“当子查询不存在时,只能在选择列表中指定一个表达式。”我不认为该错误或该逻辑特定于2005,尽管
case
有时会有一些奇怪的限制,尤其是子查询。但我在选择列表中只看到一个表达式。。。
else (
    select
        case 
            when R.Category1 > R.Category2 and R.Category1 > R.Category3 and R.Category1 > R.Category4 and R.Category1 > R.Category5 and R.Category1 > R.Category6 and R.Category1 > R.Category7 and R.Category1 > R.Category8 then 'Category1'
            when R.Category2 > R.Category1 and R.Category2 > R.Category3 and R.Category2 > R.Category4 and R.Category2 > R.Category5 and R.Category2 > R.Category6 and R.Category2 > R.Category7 and R.Category2 > R.Category8 then 'Category2'
            when R.Category3 > R.Category1 and R.Category3 > R.Category2 and R.Category3 > R.Category4 and R.Category3 > R.Category5 and R.Category3 > R.Category6 and R.Category3 > R.Category7 and R.Category3 > R.Category8 then 'Category3' 
            when R.Category4 > R.Category1 and R.Category4 > R.Category2 and R.Category4 > R.Category3 and R.Category4 > R.Category5 and R.Category4 > R.Category6 and R.Category4 > R.Category7 and R.Category4 > R.Category8 then 'Category 4'
            when R.Category5 > R.Category1 and R.Category5 > R.Category2 and R.Category5 > R.Category3 and R.Category5 > R.Category4 and R.Category5 > R.Category6 and R.Category5 > R.Category7 and R.Category5 > R.Category8 then 'Category5'
            when R.Category6 > R.Category1 and R.Category6 > R.Category2 and R.Category6 > R.Category3 and R.Category6 > R.Category4 and R.Category6 > R.Category5 and R.Category6 > R.Category7 and R.Category6 > R.Category8 then 'Category6'
            when R.Category7 > R.Category1 and R.Category7 > R.Category2 and R.Category7 > R.Category3 and R.Category7 > R.Category4 and R.Category7 > R.Category5 and R.Category7 > R.Category6 and R.Category7 > R.Category8 then 'Category7'
            when R.Category8 > R.Category1 and R.Category8 > R.Category2 and R.Category8 > R.Category3 and R.Category8 > R.Category4 and R.Category8 > R.Category5 and R.Category8 > R.Category6 and R.Category8 > R.Category7 then 'Category8'     
            else 'Tie'
         end
     from Revenue as R
     where R.Email = C.Email
)