即使其他所有人均未返回数据,也显示第一列

时间:2019-06-19 15:25:03

标签: sql

即使其余数据列中没有值,我也需要显示数据的第一列。

我已经尝试了多种功能来实现我的结果,但是没有取得任何进展。

SELECT 
  Distinct dbo.ae_i_inv_e.cycle_code AS Section,
  Sum(dbo.ae_i_loc_e.qty) As Quantity_On_Hand,
  Sum(dbo.ae_i_inv_w.whse_cost) AS Average_Cost,
  SUM (dbo.ae_i_loc_e.qty * dbo.ae_i_inv_w.whse_cost) AS Cost_On_Hand,
  Count (dbo.ae_i_inv_e.part) AS Items


  FROM
(  dbo.ae_i_inv_e
  Left Outer JOIN dbo.ae_i_loc_e ON (dbo.ae_i_inv_e.multitenant_id = 
dbo.ae_i_loc_e.multitenant_id)
  AND (dbo.ae_i_inv_e.part = dbo.ae_i_loc_e.part))
  Left Outer JOIN dbo.ae_i_inv_w ON (dbo.ae_i_loc_e.multitenant_id = 
dbo.ae_i_inv_w.multitenant_id)
  AND (dbo.ae_i_inv_w.multitenant_id = dbo.ae_i_inv_e.multitenant_id)
  AND (dbo.ae_i_loc_e.part = dbo.ae_i_inv_w.part)
  AND (dbo.ae_i_inv_w.part = dbo.ae_i_inv_e.part)

WHERE dbo.ae_i_inv_e.multitenant_id = '2824' AND
    dbo.ae_i_inv_e.active = 'N'

GROUP BY
  dbo.ae_i_inv_e.cycle_code

ORDER BY
  dbo.ae_i_inv_e.cycle_code

实际结果 部分数量_手均成本_手品

预期结果

Section  Quantity_on_hand  Average_cost  Cost_on_hand   Items
A           0                 0               0           0
B           0                 0               0           0
C           0                 0               0           0
D           0                 0               0           0
E           0                 0               0           0

2 个答案:

答案 0 :(得分:0)

查询将返回数据中的所有循环代码。如果您有设置列表,则将其包括在查询中:

SELECT v.Section,
       SUM(l.qty) As Quantity_On_Hand,
       SUM(w.whse_cost) AS Average_Cost,
       SUM(l.qty * w.whse_cost) AS Cost_On_Hand,
       COUNT(e.part) AS Items
FROM (VALUES ('A'), ('B'), ('C'), ('D'), ('E')) v(section) LEFT JOIN
     dbo.ae_i_inv_e e
     ON e.cycle_code = v.section LEFT JOIN
     dbo.ae_i_loc_e l
     ON e.multitenant_id = l.multitenant_id AND
        e.part = l.part LEFT JOIN
     dbo.ae_i_inv_w w
     ON l.multitenant_id = w.multitenant_id AND
        w.multitenant_id = e.multitenant_id AND
        l.part = w.part AND
        w.part = e.part
WHERE e.multitenant_id = '2824' AND
      e.active = 'N'
GROUP BY v.section
ORDER BY v.section;

注意:

  • 表别名使查询更易于编写和阅读。 (希望我能正确更换它们。)
  • 第一个表具有所需的部分。
  • 您的JOIN条件似乎是多余的,但我没有更改。
  • 如果值是数字,则
  • '2824'应该应该是2824(不带引号)。

答案 1 :(得分:0)

(tagsChange)='someFunction($event)'

这解决了我遇到的问题。