简体   繁体   English

从多个值中选择最高值

[英]select top value from multiple values

I have this table 我有这张桌子

trans 反式

id
user_id
amt
desc
bal
created

sample data is 样本数据是

id  user_id desc    amt     bal     created
1   1   credit  12.00   12.00   2013-07-02 00:00:00
2   1   credit  44.00   56.00   2013-07-09 00:00:00
3   1   debit   11.00   45.00   2013-07-18 00:00:00
4   2   credit  11.00   11.00   2013-07-12 01:17:08
5   2   credit  45.00   56.00   2013-07-13 01:17:08
6   2   debit   15.00   41.00   2013-07-14 01:17:08
7   3   credit  33.00   33.00   2013-07-15 01:17:52
8   3   credit  45.00   78.00   2013-07-16 01:17:52
9   3   debit   25.00   53.00   2013-07-17 01:17:52

now i need to select the current balance from each user only, so outout should be output id user_id desc amt bal created 3 1 debit 11.00 45.00 2013-07-18 00:00:00 6 2 debit 15.00 41.00 2013-07-12 01:17:08 9 3 credit 25.00 53.00 2013-07-12 01:17:52 现在我需要从每个用户中选择当前余额,因此outout应输出 id user_id desc amt bal created 3 1借记11.00 45.00 2013-07-18 00:00:00 6 2借记15.00 41.00 2013-07-12 01 :17:08 9 3学分25.00 53.00 2013-07-12 01:17:52

this is what i tried 这是我试过的

SELECT * 
FROM trans
WHERE created
IN (

SELECT MAX( created ) 
FROM trans
GROUP BY user_id
)

this gives two records for each user instead of 1 each. 这为每个用户提供了两条记录,而不是每条记录。 what am i doing wrong? 我究竟做错了什么?

The goal is the select Top amounts (highest and descending) 目标是选择最高金额(最高和最低)

it is ok if two dates and time are same, but not returning two records each 如果两个日期和时间相同,但每个都不返回两个记录,则可以

I find this simpler if you use a JOIN : 如果你使用JOIN我觉得这个更简单:

SELECT t.*
FROM trans t
   INNER JOIN (
       SELECT Max(Id) MaxId
       FROM trans
       GROUP BY User_Id
   ) t2 ON t.Id = t2.MaxId

This assumes the MAX(Id) is the max record you're looking for with each user (and is unique). 这假设MAX(Id)是您为每个用户寻找的最大记录(并且是唯一的)。 If not, you can use MAX(created) with the User_Id the same way, but it could yield multiple results per user if the time is the same. 如果没有,您可以以相同的方式将MAX(created)User_Id使用,但如果时间相同,则每个用户可以产生多个结果。 For example: 例如:

SELECT t.*
FROM trans t
   INNER JOIN (
       SELECT Max(created) MaxCreated, User_Id
       FROM trans
       GROUP BY User_Id
   ) t2 ON t.User_Id = t2.User_Id AND t.Created = t2.MaxCreated

A good solution is; 一个好的解决方案是;

SELECT user_id, SUM(IF(desc='credit',-1,1)*amt) AS balance 
FROM trans GROUP BY user_id

Your suggested table is not well designed. 您建议的表格设计不合理。 You should really not store the balance for each row, as that can introduce inconsistency in your data. 您实际上应该存储每行的余额 ,因为这可能会导致数据不一致。 What happens if the individual amt don't agree with the bal fields? 会发生什么,如果个人amt不与认同bal领域? Store just the amt fields. 只存储amt字段。 It would be simpler if you dropped the desc column and stored credits as negative. 如果您删除desc列并将信用存储为负数,则会更简单。 In that case, you just select SUM(amt) AS balance . 在这种情况下,您只需选择SUM(amt) AS balance

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM