我有两列,我希望根据两者的比较基础获得输出。我的数据有点像:
Customer Id status
100 A
100 B
101 B
102 A
103 A
103 B
因此,客户可以拥有状态A或B或两者,我必须根据客户ID对状态进行细分。如果状态A和B然后返回高兴,如果只有A,则返回平均值,如果只有B返回悲伤。
答案 0 :(得分:0)
尝试以下查询,
SELECT DISTINCT Customer_Id,
(CASE WHEN COUNT(*) OVER(PARTITION BY Customer_Id) > 1 THEN 'happy'
WHEN Tstatus = 'A' THEN 'Avg'
ELSE 'Sad'END) AS New_Status
FROM @table1
GROUP BY Customer_Id,Tstatus
答案 1 :(得分:0)
如果客户ID和状态是唯一的组合 第1步:用例确定a或b
function GetLanguageData(betaid){
var temp_data = ''
$.getJSON("/api/get, function (result) {
temp_data = result
})
return temp_data;
}
function LoadJsTree(){
//alert('loadjstree')
var list_newprojects = []
var list_ongoing = []
var list_finished = []
var list_archived = []
var arraylist = []
var betaphaselist = []
var betalanglist = []
var betaprojectlist = []
var fulltree = []
var phaselist_tree = []
var language_tree = []
var en_arr = []
var tc_arr = []
var sc_arr = []
var jp_arr = []
var fr_arr = []
var gm_arr = []
var sp_arr = []
arr_lang = []
group_lang = []
var languages = ['English','Simplified Chinese','Traditional Chinese','Japanese','French','German','Spanish']
$.getJSON("/projectlanguagedetails/", function (alldata) {
console.log(alldata)
for (var bp in alldata){
betaprojectlist.push(alldata[bp])
var obj_language = alldata[bp].languages
for ( var lang in obj_language )
{
console.log('obj_language[lang]',obj_language[lang])
betalanglist.push(obj_language[lang])
var obj_details = obj_language[lang].details
for (var d in obj_details){
console.log('obj_details',obj_details[d])
//if(obj_language[lang].id == obj_details[d].beta_language){
if(obj_language[lang].language == 'English'){
var detail_display =
{
"id" : obj_details[d].id,
"langid":obj_details[d].beta_language,
"text" : obj_details[d].phase_name,
"icon" : "../static/backoffice/images/icon-file.png",
"data" : { "isPhase" : true }
}
en_arr.push(detail_display)
}
else if(obj_language[lang].language == 'Traditional Chinese'){
var detail_display =
{
"id" : obj_details[d].id,
"langid":obj_details[d].beta_language,
"text" : obj_details[d].phase_name,
"icon" : "../static/backoffice/images/icon-file.png",
"data" : { "isPhase" : true }
}
tc_arr.push(detail_display)
}
else if(obj_language[lang].language == 'Simplified Chinese'){
var detail_display =
{
"id" : obj_details[d].id,
"langid":obj_details[d].beta_language,
"text" : obj_details[d].phase_name,
"icon" : "../static/backoffice/images/icon-file.png",
"data" : { "isPhase" : true }
}
sc_arr.push(detail_display)
}
else if(obj_language[lang].language == 'Japanese'){
var detail_display =
{
"id" : obj_details[d].id,
"langid":obj_details[d].beta_language,
"text" : obj_details[d].phase_name,
"icon" : "../static/backoffice/images/icon-file.png",
"data" : { "isPhase" : true }
}
jp_arr.push(detail_display)
}
}
}
}
}).then(function(){
console.log('languages',en_arr)
console.log('betalanglist',betalanglist)
for (var lang in betalanglist){
language = betalanglist[lang].language
lang_id = betalanglist[lang].id
if(language == 'English')
{
var lang_display = {
"langid":lang_id,
"beta_id":betalanglist[lang].beta_project,
"text" : language,
"icon" : "../static/backoffice/images/icon-folder.png",
"data" : { "isLanguage" : true },
"state": { "opened" : true },
"children" :
en_arr
}
language_tree.push(lang_display)
}
else if(language == 'Traditional Chinese')
{
var lang_display = {
"langid":lang_id,
"beta_id":betalanglist[lang].beta_project,
"text" : language,
"icon" : "../static/backoffice/images/icon-folder.png",
"data" : { "isLanguage" : true },
"state": { "opened" : true },
"children" :
tc_arr
}
language_tree.push(lang_display)
}
else if(language == 'Simplified Chinese')
{
var lang_display = {
"langid":lang_id,
"beta_id":betalanglist[lang].beta_project,
"text" : language,
"icon" : "../static/backoffice/images/icon-folder.png",
"data" : { "isLanguage" : true },
"state": { "opened" : true },
"children" :
sc_arr
}
language_tree.push(lang_display)
}
else if(language == 'Japanese')
{
var lang_display = {
"langid":lang_id,
"beta_id":betalanglist[lang].beta_project,
"text" : language,
"icon" : "../static/backoffice/images/icon-folder.png",
"data" : { "isLanguage" : true },
"state": { "opened" : true },
"children" :
jp_arr
}
language_tree.push(lang_display)
}
}
}).then(function(){
console.log('betaprojectlist',betaprojectlist)
console.log('language_tree',language_tree)
for( var x in language_tree){
var beta_id = language_tree[x].beta_id
//alert(beta_id)
for (var bp in betaprojectlist){
var betaprojectid = betaprojectlist[bp].id
var betaprojectname = betaprojectlist[bp].project_name
if(betaprojectid == beta_id ){
arr_lang.push(language_tree[x])
//alert(betaprojectid)
var project_display =
{
"betaprojectid":betaprojectid,
"text" : betaprojectname,
"icon": "../static/backoffice/images/icon-briefcase.png",
"data" : { "isProject" : true },
"state": { "opened" : true },
"children" :
[
language_tree[x]
]
}
//if(project_display.beta_project_id == beta_id)
//{
fulltree.push(project_display)
//}
}
console.log('arr_lang',arr_lang)
}
}
}).then(function(){
}).then(function(){
console.log('fulltree',fulltree)
$("#jstreeBetaProjects").jstree({
"core": {
"check_callback": true,
"themes" : { "stripes" : true },
"data": [
{ "text" : "New Projects",
"state" : {"opened": true},
"data" : { "isParent" : true, "cache":false, },
"icon" : "../static/backoffice/images/icon-folder-star.png",
"children" :fulltree
}
]
},
"types" : {
"#" : {
},
"default" : {
"valid_children" : ["file"],
"icon" : "../static/backoffice/images/icon-folder.png"
},
"file" : {
"icon" : "../static/backoffice/images/icon-file.png"
}
},
"plugins": [ "contextmenu", "themes" ],
"contextmenu": {items: customMenu}
});
})
// function resfreshJSTree() {
// var acctobj = jQuery.parseJSON(localStorage['arraylist']);
// $('#jstreeBetaProjects').jstree(true).settings.core.data = acctobj;
// $('#jstreeBetaProjects').jstree(true).refresh();
// }
}
$("#btnCreateBetaTitle").click(function(){
CreateBetaProject()
});
$("#btnSaveBetaTitle").click(function(){
RenameBetaProject()
});
function CreateBetaProject(){
var projectname = $('#lblProjectTitle').val()
$.ajax({
url: '/api/get_delete_update_post',
type: 'POST',
contentType: "application/json; charset=utf-8",
dataType: "json",
data: JSON.stringify({
"project_name": projectname,
"is_deleted": false,
"created_by": "Sam Marcos Samarro",
"updated_by": "Chad Change",
}),
success: function(data) {
console.log(data)
LoadJsTree()
alert('Successfully Added!')
},
error: function(data) {
console.log(data)
alert('Failed.')
}
});
}
function RenameBetaProject(){
var projectname = $('#lblRenameBetaTitle').val()
var projectid = $("#hdnpid").text();
var anchorId = "#"+projectid + "_anchor"
$.ajax({
url: '/api/get_delete_update_post_beta,
type: 'PUT',
contentType: "application/json; charset=utf-8",
dataType: "json",
data: JSON.stringify({
"project_name": projectname,
"is_deleted": false,
"created_by": "Sam Marcos Samarro",
"updated_by": "Chad Change",
}),
success: function(data) {
// console.log(data)
// $("#jstreeBetaProjects").html(data)
// $('#jstreeBetaProjects').jstree(true).refresh();
// resfreshJSTree()
// LoadJsTree()
//window.location.reload();
//location.reload(true)
//$(anchorId).text(projectname);
$(anchorId).html('');
$(anchorId).append('<i class="jstree-icon jstree-themeicon jstree-themeicon-custom" role="presentation" style="background-image: url("../static/backoffice/images/icon-briefcase.png"); background-position: center center; background-size: auto;"></i>'+projectname)
$("#modal-rename-project").modal('hide');
//tree.jstree("refresh");
alert('Successfully Renamed!')
},
error: function(data) {
console.log(data)
alert('Failed.')
}
});
}
和步骤2将avg结果化为结果:像这样
SELECT customer id
,CASE WHEN avg(case when [status] ='A' then 0 else 2 end)
FROM [Your Table]
group by[customer id]
答案 2 :(得分:0)
我会这样做:
select customer_id,
(case when min(status) <> max(status) then 'happy'
when min(status) = 'A' then 'avg'
else 'sad'
end)
from t
where status in ('A', 'B')
group by customer_id