查询以解除具有相同值的列

时间:2015-09-29 19:18:13

标签: sql

我在查询中遇到一些SQL问题,我可能会使用一些帮助。

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

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

由于我购买了一些含有相同数量的itens的类别,我需要根据累计收入(其他表格)解开部分类别

Email   BestCategory
aaaaa@aaa.aa    Category 1
bbbbb@bbb.bb    Category 2
ccccc@ccc.cc    Tie
ddddd@ddd.dd    Category 3
eeeee@eee.ee    Category 6

你们能帮我找到解开这个问题的方法吗?

2 个答案:

答案 0 :(得分:0)

我将摆脱领先的逗号作为练习。 (我不知道你在哪个平台:也许,substring(<expr>, 2, 100)?)我认为这就是你想要的。

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

答案 1 :(得分:0)

或者您可能只需要将相关子查询添加为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
)

这不会将比较仅限于第一次领带中涉及的那些类别,尽管没有多少工作。

相关问题