【问题标题】:Sql query result duplicatesql查询结果重复
【发布时间】:2020-08-28 09:20:06
【问题描述】:

大家好,首先我有这样的表 t1

ID  cid     enter     sort
===================
1             20
2      1                5 
3.     1.               6
4             10    
5      4                 2
6.             30

我需要得到这样的结果

ID  enter    sort     stock
===============================
1    20       11        9
4    10        2          8
6.    30.      0.        30

什么意思 获取 cid 为空的所有 id 获取输入编号 然后获取所有cid,其中cid =id取排序号的总和 然后最后stock = enter - sort

enter image description here

enter image description here

enter image description here

like mu db 结果一定是这样的

ID  enter    sort     stock
===============================
18    1P       6        4

我的查询

 SELECT        p_stock.id, p_stock.code, p_stock.quant_entr, SUM(p_stock_1.quant_sort) AS Expr1
FROM            (p_stock INNER JOIN
                         p_stock p_stock_1 ON p_stock.id = p_stock_1.cid)
HAVING        (p_stock.id = 18) AND (p_stock.code = 27)

感谢您的帮助

【问题讨论】:

  • 你的行有什么关系?您是否假设匹配行是具有下一个更高 id 的行?
  • 是否可以添加一个示例表来显示 stock 列的来源?通常,您必须将两个表连接在一起,其中一个表将有一个包含指针(称为外键)的列,该指针应与另一个表的主键匹配。
  • 我添加了一些照片以供理解

标签: sql vb.net ms-access


【解决方案1】:

我觉得你可以用join:

select t1.id, t1.enter, t2.sort, (t1.enter - t2.sort) as stock
from t as t1 join
     t as t2
     on t2.id = t1.id + 1
where t1.id in (1, 3)   -- or perhaps all odd numbers

【讨论】:

    猜你喜欢
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 2011-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多