在 postgresql table 中查找利润较高的用户
Find user with higher profits in a postgresql table
我的数据库中有一个table结构是这样的
sell_id(int) | car_id(int) | worth(int) | date(date) | selled(boolean)
卖错了'sold'。
我的table是这样的
------------------------------
50 |50 |2405 | "2012-07-16" | false
51 |51 |9300 | "2014-10-07" | false
52 |52 |5963 | "2014-11-01" | false
53 |53 |2036 | "2014-09-19" | false
54 |54 |4770 | "2014-01-26" | false
55 |55 |11915| "2010-08-30" | true
总之,sell_id是这个table的主键,也是"mother"table的外键。 car_id 也指另一个 table。价值是卖家出售或购买汽车的金额。 Date 就是时间,而 selled 是一个布尔值,用于指定卖家是出售还是购买了汽车。如果是真的,那他确实卖了。不然他买了。
现在我想找到从 selling/buying 中获得最大价值的用户。这意味着他一定是那个卖掉的人-bought.But我想不出正确的查询。
此查询给出了仅销售的值。
select sell_id, sum(worth), selled
from sellers
where(selled=true)
group by sell_id, selled
例如,一些期望的结果是:
sell_id | total_profits(max)
---------------------------
51 | 2000
其中 total_profits 将是最大价值(其中 selled=true)-worth(其中 selled=false)
任何想法都会很有帮助。谢谢:)
如果我对你的问题理解正确,你可以单独计算,然后加入最后的计算。第一个版本给你三个查询。第一个将卖出放入 table,第二个将买入放入,第三个将它们连接在一起,让您能够进行数学运算。
select sell_id, sum(worth), selled
into #sell
from sellers
where(selled=true)
group by sell_id, selled
select sell_id, sum(worth), selled
into #buy
from sellers
where(selled=false)
group by sell_id, selled
select a.sell_id, sum(a.worth-b.worth) as 'Total'
from #sell a
join #buy b on a.sell_id = b.sell_id
group by a.sell_id
如果直接联接排除了预期的记录,您可能需要调整联接。
正如您所注意到的,临时 tables 不适合您,也许可以试试这个。
select sell_id, sum(sum(worth)-b.worth), selled
from sellers a
join (select sell_id, sum(worth) as 'worth' from sellers where(selled=false)
group by sell_id) b on a.sell_id = b.sell_id
where(selled=true)
group by sell_id, selled
我不在可以充分测试的地方,可能需要稍微调整一下,但这就是概念。您正在对联接中的买入进行计算,然后从概念上从卖出总和中减去它们,而不考虑方法。
我终于找到了我自己问题的答案。正确的查询是:
select sell_id, (sum(worth) - (select sum(worth)
from sellers where selled=false group by sell_id
order by sum limit 1)) as final from sellers
where selled=true
group by sell_id order by final desc limit 1
我的数据库中有一个table结构是这样的
sell_id(int) | car_id(int) | worth(int) | date(date) | selled(boolean)
卖错了'sold'。
我的table是这样的
------------------------------
50 |50 |2405 | "2012-07-16" | false
51 |51 |9300 | "2014-10-07" | false
52 |52 |5963 | "2014-11-01" | false
53 |53 |2036 | "2014-09-19" | false
54 |54 |4770 | "2014-01-26" | false
55 |55 |11915| "2010-08-30" | true
总之,sell_id是这个table的主键,也是"mother"table的外键。 car_id 也指另一个 table。价值是卖家出售或购买汽车的金额。 Date 就是时间,而 selled 是一个布尔值,用于指定卖家是出售还是购买了汽车。如果是真的,那他确实卖了。不然他买了。
现在我想找到从 selling/buying 中获得最大价值的用户。这意味着他一定是那个卖掉的人-bought.But我想不出正确的查询。
此查询给出了仅销售的值。
select sell_id, sum(worth), selled
from sellers
where(selled=true)
group by sell_id, selled
例如,一些期望的结果是:
sell_id | total_profits(max)
---------------------------
51 | 2000
其中 total_profits 将是最大价值(其中 selled=true)-worth(其中 selled=false)
任何想法都会很有帮助。谢谢:)
如果我对你的问题理解正确,你可以单独计算,然后加入最后的计算。第一个版本给你三个查询。第一个将卖出放入 table,第二个将买入放入,第三个将它们连接在一起,让您能够进行数学运算。
select sell_id, sum(worth), selled
into #sell
from sellers
where(selled=true)
group by sell_id, selled
select sell_id, sum(worth), selled
into #buy
from sellers
where(selled=false)
group by sell_id, selled
select a.sell_id, sum(a.worth-b.worth) as 'Total'
from #sell a
join #buy b on a.sell_id = b.sell_id
group by a.sell_id
如果直接联接排除了预期的记录,您可能需要调整联接。 正如您所注意到的,临时 tables 不适合您,也许可以试试这个。
select sell_id, sum(sum(worth)-b.worth), selled
from sellers a
join (select sell_id, sum(worth) as 'worth' from sellers where(selled=false)
group by sell_id) b on a.sell_id = b.sell_id
where(selled=true)
group by sell_id, selled
我不在可以充分测试的地方,可能需要稍微调整一下,但这就是概念。您正在对联接中的买入进行计算,然后从概念上从卖出总和中减去它们,而不考虑方法。
我终于找到了我自己问题的答案。正确的查询是:
select sell_id, (sum(worth) - (select sum(worth)
from sellers where selled=false group by sell_id
order by sum limit 1)) as final from sellers
where selled=true
group by sell_id order by final desc limit 1