그룹, 그룹 멤버 및 데일리 테이블 각각 세 개의 테이블이 있습니다.쿼리 출력에 DENSE_RANK()를 적용하려면
models.py 내가 indiviaully 각 그룹에 의해 불에 총 칼로리를 계산하고 싶었
class Group(models.Model):
name = models.CharField(max_length=255, blank=False, null=False)
group_type = models.ForeignKey('GroupType',blank=True,default=False)
class Meta:
db_table = 'groups'
class GroupMembers(models.Model):
group = models.ForeignKey('Group')
user = models.ForeignKey('User')
status = models.CharField(max_length=20)
class Meta:
db_table = 'group_members'
unique_together = ['group', 'user']
class DailyTable(models.Model):
class Meta:
managed = True
db_table = 'dailytable'
user_id = models.IntegerField(db_index=True)
calories = models.FloatField()
date = models.DateField()
: 아래 그들의 각 하나에 대한 내 모델이다. 그래서 아래의 쿼리 해고 : 아래
select groups.id,groups.name as group_display_name,
(select count(*) from group_members where group_id = groups.id and status = 'accepted' and groups.group_type_id = 1) as total_members,
(select sum(calories) from dailytable where user_id in (select user_id from group_members where group_id = groups.id and groups.group_type_id = 1) and dailytable.date='2016-02-02') as total_calories,
(select case when exists(select * from group_members where group_id = groups.id and user_id = 3 and status = 'accepted' and groups.group_type_id = 1) then cast(1 as bit) else cast(0 as bit) end) as is_member_of_group
from groups
where groups.group_type_id = 1
order by total_calories desc
nulls last;
내가 내가 구워 놓은 total_calories에 따라 그룹 순위 싶어 이제
를 얻을 출력입니다, 그래서 쿼리에서 아래의 변경 :
....
dense_rank() over(order by total_calories)
from groups
....
오류 : 열 "total_calories"나는 쿼리의 출력에 RANK을 적용 할
존재하지 않습니다. 이를 달성 할 수있는 방법이 있습니까?