我有一个包含大约100列和30000行的表。 看起来像这样:
site_id cell_id sector_id value1 value2
1 1 1 70 23
1 2 1 40 20
1 3 1 67 35
1 5 2 42 60
1 6 2 65 30
1 7 2 62 62
2 11 1 67 11
2 12 1 45 22
2 13 1 65 15
对于一个site_id的相同sector_id,如果value1&gt; = 65,那么value2 <25的相同扇区中的任何cell_id将被归类为“LOW_LOAD_CELL”。 期望的输出将是:
site_id cell_id sector_id value1 value2 cell_status
1 1 1 70 23 LOW_LOAD_CELL
1 2 1 40 20 LOW_LOAD_CELL
1 3 1 67 35
1 5 2 42 60
1 6 2 65 30
1 7 2 62 62
2 11 1 67 11 LOW_LOAD_CELL
2 12 1 45 22 LOW_LOAD_CELL
2 13 1 65 15 LOW_LOAD_CELL
...
老实说,我不知道如何在SQL中使用。我已经尝试过WHEN CASE,但是当我需要为value2写条件时,我就堆积了。
答案 0 :(得分:8)
尝试:
[AttributeUsage(AttributeTargets.Class)]
public class UserMigrationAttribute : ActionFilterAttribute
{
public ApplicationSignInManager SignInManager(ActionExecutingContext filterContext)
{
return filterContext.HttpContext.GetOwinContext().Get<ApplicationSignInManager>();
}
public UserManager UserManager(ActionExecutingContext filterContext)
{
return filterContext.HttpContext.GetOwinContext().GetUserManager<UserManager>();
}
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
CreateMigrateCurrentUser(filterContext);
base.OnActionExecuting(filterContext);
}
private static readonly object LockThis = new object();
private void CreateMigrateCurrentUser(ActionExecutingContext filterContext)
{
lock (LockThis)
{
var signInManager = SignInManager(filterContext);
var userManager = UserManager(filterContext);
var sessionTrackId = GetSessionTrackId(filterContext);
if (!filterContext.HttpContext.Request.IsAuthenticated)
{
if (!string.IsNullOrEmpty(sessionTrackId))
{
var username = string.Format("{0}@mydomain.com", sessionTrackId);
var user = userManager.FindByName(username);
if (user == null)
{
user = new User() {UserName = username, Email = username};
var result = userManager.Create(user);
userManager.AddToRole(user.Id, StringResources.AnonymousVisitorsGroup);
}
signInManager.SignIn(user, true, true);
}
}
else
{
if (!string.IsNullOrEmpty(sessionTrackId))
{
var username = string.Format("{0}@mydomain.com", sessionTrackId);
var user = userManager.FindByName(username);
if (user != null)
{
if (!HttpContext.Current.User.IsInRole(StringResources.AnonymousVisitorsGroup))
{
var targetUserId = HttpContext.Current.User.Identity.GetUserId<int>();
var service = new Service();
service.Users.MigrateUser(user.Id, targetUserId);
}
}
}
}
}
}
private string GetSessionTrackId(ActionExecutingContext filterContext)
{
var retVal = string.Empty;
if (filterContext.HttpContext.Request.Cookies["stid"] != null)
{
retVal = filterContext.HttpContext.Request.Cookies["stid"].Value;
}
return retVal;
}
}
答案 1 :(得分:7)
我认为你真正想要的逻辑是:
select t.*,
(case when max(value1) over (partition by site_it, sector_id) >= 65 and
value2 < 25
then 'LOW_LOAD_CELL'
end) as cell_status
from t ;
这符合您的数据 - 如果扇区/站点组合的任何行的value1
为65或更高,则当该value2
小于25时,该单元格是低负载单元格。
答案 2 :(得分:0)
你可以试试这个:
select t.*, case when t2.cnt > 0 and value2 < 25 then 'LOW_LOAD_CELL' end cell_status
from mytable as t left join
( select site_id, sector_id, count(*) cnt from mytable where
value1 >= 65 group by site_id, sector_id ) as t2
on t.site_id = t2.site_id and t.sector_id = t2.sector_id