问题在于,WO:W142930 Leg 0重复多次,这造成了太多混乱。 我可以使用excel中的数据透视表进行清理,但是我宁愿学习如何在SQL中进行操作。 我不知道是否有可能,但我想在每条腿的顶部看到总的“ ACT_RUN_HRS”。
我编写的代码:
select
w.base_id as WO_ID
, w.sub_id as LEG
, w.desired_qty as WO_QTY
, OPR.ACT_RUN_HRS
, OPR.ACT_SETUP_HRS
, cast ((OPR.ACT_RUN_HRS + OPR.ACT_SETUP_HRS) AS decimal(15,4)) AS TOTAL_RUN_HOURS
, cast(w.close_date as date) AS CLOSE_DATE
, cast(cast( wb.bits as varbinary(100)) as varchar(100)) as SPEC
from work_order w
left join workorder_binary wb on
w.type = wb.workorder_type and
w.base_id = wb.workorder_base_id and
w.lot_id = wb.workorder_lot_id and
w.split_id = wb.workorder_split_id and
w.sub_id = wb.workorder_sub_id
left join operation opr
ON w.type = opr.workorder_type and
w.base_id = opr.workorder_base_id and
w.lot_id = opr.workorder_lot_id and
w.split_id = opr.workorder_split_id and
w.sub_id = opr.workorder_sub_id
left join PART p0 on p0.id = w.PART_ID and '0' = w.SUB_ID
left join CUSTOMER c on c.id = p0.user_1
WHERE year(w.CLOSE_DATE) >= 2017
and year(w.CLOSE_DATE) <= 2018
and w.STATUS='C'
and w.TYPE = 'W'
and w.BASE_ID LIKE 'W%'
and w.BASE_ID = 'w142930'
Order by w.base_id, w.sub_id
谢谢
答案 0 :(得分:1)
我更新了答案,将每个字段(w.base_id
,w.sub_id
,w.desired_qty
和wb.bits
)替换为强制转换为varchar(100),并且我修改了GROUP BY子句以包含相同的强制类型转换。让我知道这是否适合您。
我将cast(wb.bits as varchar(100))
替换为cast(cast(wb.bits as varbinary(100)) as varchar(100))
如@DeathMagnetic所指出的,ORDER BY
列必须出现在SELECT
或GROUP BY
子句中
您正在寻找SUM()
和GROUP BY
。类似于以下内容的东西应该可以工作(但是,由于我没有使用您的架构设置数据库,因此我没有对此进行测试):
SELECT
cast(w.base_id as varchar(100)) as WO_ID,
cast(w.sub_id as varchar(100)) as LEG,
cast(w.desired_qty as varchar(100)) as WO_QTY,
SUM(OPR.ACT_RUN_HRS),
SUM(OPR.ACT_SETUP_HRS),
SUM(cast ((OPR.ACT_RUN_HRS + OPR.ACT_SETUP_HRS) AS decimal(15,4))) AS
TOTAL_RUN_HOURS
cast(w.close_date as date) AS CLOSE_DATE,
cast(cast(wb.bits as varbinary(100)) as varchar(100)) as SPEC
FROM
work_order w
LEFT JOIN
workorder_binary wb ON
w.type = wb.workorder_type and
w.base_id = wb.workorder_base_id and
w.lot_id = wb.workorder_lot_id and
w.split_id = wb.workorder_split_id and
w.sub_id = wb.workorder_sub_id
LEFT JOIN
operation opr ON
w.type = opr.workorder_type and
w.base_id = opr.workorder_base_id and
w.lot_id = opr.workorder_lot_id and
w.split_id = opr.workorder_split_id and
w.sub_id = opr.workorder_sub_id
LEFT JOIN
PART p0 ON
p0.id = w.PART_ID and
'0' = w.SUB_ID
LEFT JOIN
CUSTOMER c ON
c.id = p0.user_1
WHERE
year(w.CLOSE_DATE) >= 2017
and year(w.CLOSE_DATE) <= 2018
and w.STATUS = 'C'
and w.TYPE = 'W'
and w.BASE_ID LIKE 'W%'
and w.BASE_ID = 'w142930'
GROUP BY
cast(w.base_id as varchar(100)),
cast(w.sub_id as varchar(100)),
cast(w.desired_qty as varchar(100)),
cast(w.close_date as date),
cast(cast(wb.bits as varbinary(100)) as varchar(100))
ORDER BY
cast(w.base_id as varchar(100)), cast(w.sub_id as varchar(100))
通常:
COUNT
,SUM
,AVERAGE
等)时,返回的每个单列必须 是汇总或属于{ {1}}子句。假设您仅按GROUP BY
和w.base_id
分组,并且对于相同的w.sub_id
和w.desired_qty
,您对w.base_id
有两个不同的值-您会选择哪个想回来吗?因此,您可能需要按w.sub_id
分组或使用一些汇总函数来告诉它如何选择(w.desired_qty
,MAX
等)MIN
子句在WHERE
子句之前执行,因此它只会对您的GROUP BY
返回的行求和/求平均/聚合。如果您想根据这些汇总信息进一步过滤(例如,返回所有带有WHERE
的人),则应使用SUM(OPR.ACT_RUN_HOURS) > 10
子句(其功能与{ {1}}子句,但在HAVING
之后而不是之前运行)答案 1 :(得分:0)
您要与GROUP BY
子句一起使用所谓的聚合函数。它的发音听起来很有用。我确定您可以在Google上搜索更多信息以了解它们,但是对于您的特定问题,您可以SUM
所需的值,并按要求和的值分组。像这样:
select
w.base_id as WO_ID
, w.sub_id as LEG
, w.desired_qty as WO_QTY
, SUM(OPR.ACT_RUN_HRS) AS SUM_ACT_RUN_HRS
, SUM(OPR.ACT_SETUP_HRS) AS SUM_ACT_SETUP_HRS
, SUM(cast ((OPR.ACT_RUN_HRS + OPR.ACT_SETUP_HRS) AS decimal(15,4))) AS
TOTAL_RUN_HOURS
, cast(w.close_date as date) AS CLOSE_DATE
, cast(cast( wb.bits as varbinary(100)) as varchar(100)) as SPEC
from work_order w
left join workorder_binary wb on
w.type = wb.workorder_type and
w.base_id = wb.workorder_base_id and
w.lot_id = wb.workorder_lot_id and
w.split_id = wb.workorder_split_id and
w.sub_id = wb.workorder_sub_id
left join operation opr
ON w.type = opr.workorder_type and
w.base_id = opr.workorder_base_id and
w.lot_id = opr.workorder_lot_id and
w.split_id = opr.workorder_split_id and
w.sub_id = opr.workorder_sub_id
left join PART p0 on p0.id = w.PART_ID and '0' = w.SUB_ID
left join CUSTOMER c on c.id = p0.user_1
WHERE year(w.CLOSE_DATE) >= 2017
and year(w.CLOSE_DATE) <= 2018
and w.STATUS='C'
and w.TYPE = 'W'
and w.BASE_ID LIKE 'W%'
and w.BASE_ID = 'w142930'
GROUP BY w.base_id, w.sub_id, w.desired_qty, w.close_Date, wb.bits
Order by w.base_id, w.sub_id
应该做你想做的。您可能需要在分组依据列中包含来自选择项的转换,以获得确切的结果,但我认为并非如此。