我有一个查询在哪里我需要从表中选择所有不同的ID,还需要只选择min(date),这样我就可以得到第一个插入的记录而不是所有日期的ID。
基本上这就是我要找的 -
表1 ||表2-
ID || ID日期
1 || 1 11/11/2010
1 || 1 10/11/2010
3 || 3 12/01/2010
4 || 4 01/01/2010
4 || 4 02/01/2010
所以我需要从表2中获取所有记录(table1.ID = table2.ID),其中包含最小日期和该ID
这里的结果将是
1 10/11/2010
3 12/01/2010
4 01/01/2010
这是我的查询
select u.firstName,u.lastName ,count(*) as theCount
from tbl_appts_change_log c,tbl_appts a, tbl_users u
where c.appt_id=a.ID
and c.user_id=u.userID
and c.appt_id in ( select c.appt_id,min(c.date) from tbl_appts_change_log c, tbl_appts a
where c.appt_id=a.ID
and a.satellite_id='160' GROUP BY c.appt_id)
group by u.firstName,u.lastName
order by count(*) desc,u.firstName,u.lastName
答案 0 :(得分:1)
也许:
select u.firstName,u.lastName ,count(*) as theCount
from tbl_appts_change_log c
INNER JOIN tbl_appts a on c.appt_id=a.ID
INNER JOIN tbl_users u on c.user_id=u.userID
INNER JOIN ( select c.appt_id,min(c.date) as LastDate
from tbl_appts_change_log c
INNER JOIN tbl_appts a on c.appt_id=a.ID
Where a.satellite_id='160' GROUP BY c.appt_id) d
on c.appt_id = d.appt_id and c.date = d.LastDate
group by u.firstName,u.lastName
order by count(*) desc,u.firstName,u.lastName
答案 1 :(得分:1)
我忽略了你的代码,因为它似乎与你在问题中所描述的情况完全不同,即表1和表2。如果您想要的只是每个ID的最短日期,那么
SELECT T1.ID, MIN(T2.Date)
FROM Table1 T1
JOIN Table2 T2 ON T1.ID = T2.ID
GROUP BY T1.ID
但我猜你真正想要的是这样的东西?
Table 1 || Table 2-
ID || ID Date Desc
1 || 1 11/11/2010 AAA
1 || 1 10/11/2010 BBB
3 || 3 12/01/2010 CCC
4 || 4 01/01/2010 DDD
4 || 4 02/01/2010 EEE
预期结果
1 10/11/2010 BBB
3 12/01/2010 CCC
4 01/01/2010 DDD
这比仅仅进行分组稍微复杂一些,有两种方法可以解决它。您可以尝试两者并查看哪一个表现最佳
方法1:使用行号
;WITH ResultCTE AS
(
SELECT T2.ID, T2.Date, T2.Desc,
RowNumber = ROW_NUMBER() OVER(PARTITION BY T2.ID ORDER BY T2.Date ASC)
FROM Table1 T1
JOIN Table2 T2 ON T1.ID = T2.ID
)
SELECT ID, Date, Desc
FROM ResultCTE
WHERE RowNumber = 1
方法2:嵌套查询
;WITH ResultCTE AS
(
SELECT T2.ID, MIN(T2.Date) AS Date
FROM Table1 T1
JOIN Table2 T2 ON T1.ID = T2.ID
GROUP BY T2.ID
)
SELECT T.ID, T.Date, T.Desc
FROM Table2 T
JOIN ResultCTE R
ON R.ID = T.ID AND R.Date = T.Date
答案 2 :(得分:0)
SELECT u.firstName,
u.lastName ,
COUNT(*) AS theCount
FROM tbl_appts_change_log c,
tbl_appts a,
tbl_users u
WHERE c.appt_id=a.ID
AND c.user_id =u.userID
AND a.satellite_id = '160'
AND c.date = (SELECT MIN(ci.date)
FROM tbl_appts_change_log ci,
tbl_appts ai
WHERE ci.appt_id = ai.ID
AND ci.appt_id = c.appt_id
AND ai.satellite_id= a.satellite_id
)
GROUP BY u.firstName,
u.lastName
ORDER BY COUNT(*) DESC,
u.firstName,
u.lastName
答案 3 :(得分:0)
select u.firstName,u.lastName,count(*) as theCount,cm.MinDate
from (
select c.appt_id,min(c.date) as MinDate
from tbl_appts_change_log c, tbl_appts a
where c.appt_id=a.ID
and a.satellite_id='160'
GROUP BY c.appt_id
) cm
inner join tbl_appts_change_log c on cm.appt_id = c.appt_id and cm.MinDate = c.date
inner join tbl_appts a on c.appt_id=a.ID
inner join tbl_users u on c.user_id=u.userID
group by u.firstName,u.lastName
order by count(*) desc,u.firstName,u.lastName
答案 4 :(得分:0)
如果您要寻找的是-
表1 ||表2-
ID ||身份证日期
1 || 1 11/11/2010
1 || 1 10/11/2010
3 || 3 12/01/2010
4 || 4 01/01/2010
4 || 4 02/01/2010
尝试使用此:
def search(request):
date = request.GET.get('date')
queryset = List.objects.filter(date=date)
data = serializers.serialize('json', queryset)
return HttpResponse(data, content_type='application/json')
def export_to_csv(request):
data = search(request)
# But that does not bring the search data
print(data)
# <HttpResponse status_code=200, "application/json">
答案 5 :(得分:-1)
这就是你要追求的吗?
select u.firstName,u.lastName ,count(*) as theCount
from tbl_appts_change_log c,tbl_appts a, tbl_users u
where c.appt_id=a.ID
and c.user_id=u.userID
and a.satellite_id = '160'
and c.date = (
select min(date)
from tbl_appts_change_log c, tbl_appts a
where c.appt_id = a.id
and a.satellite_id = '160'
)
group by u.firstName,u.lastName
order by count(*) desc,u.firstName,u.lastName