我正在使用SQL Azure。我试图将一组结果分组两次,以便用户名字段是唯一的。在我的结果中,相同的行多次出现,并且具有不同的事务计数值。
这是我的代码:
select settings.UserName, SUM(TransactionCount) as TransactionCount, settings.SettingValue as Sku, CASE
WHEN settings.SettingValue = 'cccmicro' AND TransactionCount > 1800 THEN 'True'
WHEN settings.SettingValue = 'cccsub1' AND TransactionCount > 6000 THEN 'True'
WHEN settings.SettingValue = 'cccsub2' AND TransactionCount > 18000 THEN 'True'
WHEN settings.SettingValue = 'cccsub3' AND TransactionCount > 30000 THEN 'True'
ELSE 'False'
END As [Over]
from jma_qb_trans_count trans
Inner Join jma_settings settings on settings.UserName = trans.UserName
where settings.SettingName = 'QB:JMAWebStoreSku' and settings.SettingValue != ''
Group by settings.UserName, settings.SettingValue, settings.SettingName, TransactionCount
Order By settings.UserName
这是我的数据:
以下是我所寻求的:
用户名:james.dick TransactionCount:8 Sku:cloudcartconnectorlicense 结束:错误
答案 0 :(得分:1)
请尝试以下代码。我认为它会像你问的那样奏效。
select
settings.UserName,
CASE
WHEN settings.SettingValue = 'cccmicro' AND TransactionCount > 1800 THEN 'True'
WHEN settings.SettingValue = 'cccsub1' AND TransactionCount > 6000 THEN 'True'
WHEN settings.SettingValue = 'cccsub2' AND TransactionCount > 18000 THEN 'True'
WHEN settings.SettingValue = 'cccsub3' AND TransactionCount > 30000 THEN 'True'
ELSE 'False'
END As [Over],
SUM(TransactionCount) as TransactionCount,
settings.SettingValue as Sku
from jma_qb_trans_count trans
Inner Join jma_settings settings on settings.UserName = trans.UserName
where settings.SettingName = 'QB:JMAWebStoreSku' and settings.SettingValue != ''
Group by 1,2
答案 1 :(得分:0)
您的分组将分割结果放入选择部分,您会看到 为什么查询无法正常工作。
Group by settings.UserName, settings.SettingValue, settings.SettingName, TransactionCount