以下是表数据和我想要的输出的示例:
OrderNo Item Qty Regular Line
349443 AFU20451-KIT1 1 Y 1
349443 AFU20451 0 N 2
349443 HAWKE-14252 1 N 3
349443 RGPM-25H4 1 N 4
349443 AV-003-265 1 Y 5
349443 AV-A00090-KIT 1 Y 6
349443 AV-A00091 1 N 7
349443 AV-A00090 1 N 8
349443 AV-00043 1 N 9
349443 AV457/310GR/FP 2 Y 10
期望的输出:
OrderNo Item Qty
349433 AFU20451-KIT1 3
349433 AV-003-265 1
349433 AV-A00090-KIT 4
349433 AV457/310GR/FP 2
正如你所看到的,我想在每次说Y时重置总和,只包括父项(我可以绕过这个,因为我可以保持项目的顺序相同,也许可以使用行号) 。我一直试图使用Over和Partition来做到这一点,但无济于事。如果有可能,或者您需要任何进一步的信息,请告诉我。
答案 0 :(得分:1)
with cte as
(
select OrderNo,
-- only return the main item
case when Regular = 'Y' then Item end AS Item,
Qty,
-- assign a unique number to each `YNNN..` component group
-- needed for GROUP BY in next step
sum(case when Regular = 'Y' then 1 else 0 end)
over (partition by OrderNo
order by Line
rows unbounded preceding) as grp
from myTable
)
select OrderNo,
-- find the matching value for the main component
max(Item),
sum(Qty)
from cte
group by OrderNo, grp
答案 1 :(得分:0)
试试这个:
select orderno, item, sum(qty) over(partition by regular order by regular)
from your_table
group by orderno, item, regular
答案 2 :(得分:0)
目前的陈述反对第一个Codd的规则。
规则1:信息规则:关系数据中的所有信息 base在逻辑级别明确表示,并且恰好在一个中表示 方式 - 通过表中的值。
但是我相信你仍然可以创建FUNCTION / PROCEDURE并使用IF语句逐个迭代行,用于Y / N.例如。您创建新表,如果是 - 向表中添加新行,IF N - 将QTY添加到最新行。
答案 3 :(得分:0)
我会创建两个单独的表:manufacturer&部分,以获取值,这样您就不必手动堵塞每个库存,也不必关心它们落入发票清单的位置。
然后,您需要做的就是将值与零件表进行比较以获取此数据。这是更多的前期工作,但是将所有这些都保存和存储将得到回报。未来的示例查询看起来像:
SELECT OrderNo.OrderTable, Item.OrderTable, Sum(Qty.OrderTable) AS Quantity
FROM OrderTable INNER JOIN Part ON OrderTable.Item = Table.PartName
GROUP BY OrderNo.OrderTable, Item.OrderTable, Regular.OrderTable, Part.ParentID;