我使用以下代码作为查询从Excel中的sql server(对于ERP系统)返回数据。
我尝试根据Job.Job
排序,仅选择desc
(作业表)中的前1条记录。但是,我无法使用select top
或limit
函数。我还尝试了case when else
中的where
函数,但无法解决这个问题。
如果您能够了解如何修改以下代码来实现这一点,我将会很棒。
的第一行SELECT
SO_Detail.Status,
SO_Detail.Sales_Order,
SO_Detail.SO_Line,
SO_Detail.Material,
SO_Detail.Rev,
SO_Header.Customer_PO,
SO_Detail.Order_Qty,
Coalesce(Material_Location.On_Hand_Qty, 0),
SO_Detail.Promised_Date,
SO_Detail.Note_Text,
Material.Description,
Job.Status AS Status1,
Job.Job
FROM
SO_Detail
LEFT OUTER JOIN Material_Location ON SO_Detail.Material = Material_Location.Material
LEFT OUTER JOIN SO_Header ON SO_Detail.Sales_Order = SO_Header.Sales_Order
INNER JOIN Material ON Material.Material = SO_Detail.Material
INNER JOIN Job ON SO_Detail.Material = Job.Part_Number
WHERE
SO_Detail.Status = 'open'
ORDER BY
SO_Detail.Sales_Order,
SO_Detail.SO_Line,
Job.Job desc
答案 0 :(得分:0)
如果没有足够的信息,我会尝试在row_number
中使用case
函数或order by
语句:
使用row_number
函数和派生表:
select *
from
(SELECT
SO_Detail.Status,
SO_Detail.Sales_Order,
SO_Detail.SO_Line,
SO_Detail.Material,
SO_Detail.Rev,
SO_Header.Customer_PO,
SO_Detail.Order_Qty,
Coalesce(Material_Location.On_Hand_Qty, 0),
SO_Detail.Promised_Date,
SO_Detail.Note_Text,
Material.Description,
Job.Status AS Status1,
Job.Job,
row_number() over (partition by job.job order by job.job desc) rn
FROM
SO_Detail
LEFT OUTER JOIN Material_Location ON SO_Detail.Material = Material_Location.Material
LEFT OUTER JOIN SO_Header ON SO_Detail.Sales_Order = SO_Header.Sales_Order
INNER JOIN Material ON Material.Material = SO_Detail.Material
INNER JOIN Job ON SO_Detail.Material = Job.Part_Number
WHERE
SO_Detail.Status = 'open') t
where t.rn = 1
在case
和order by
中使用top 1
语句:
SELECT TOP 1
SO_Detail.Status,
SO_Detail.Sales_Order,
SO_Detail.SO_Line,
SO_Detail.Material,
SO_Detail.Rev,
SO_Header.Customer_PO,
SO_Detail.Order_Qty,
Coalesce(Material_Location.On_Hand_Qty, 0),
SO_Detail.Promised_Date,
SO_Detail.Note_Text,
Material.Description,
Job.Status AS Status1,
Job.Job
FROM
SO_Detail
LEFT OUTER JOIN Material_Location ON SO_Detail.Material = Material_Location.Material
LEFT OUTER JOIN SO_Header ON SO_Detail.Sales_Order = SO_Header.Sales_Order
INNER JOIN Material ON Material.Material = SO_Detail.Material
INNER JOIN Job ON SO_Detail.Material = Job.Part_Number
WHERE
SO_Detail.Status = 'open'
ORDER BY
SO_Detail.Sales_Order,
SO_Detail.SO_Line,
Job.Job desc,
case when job.status = 'Complete' then 1 else 2 end
如果两者都不起作用,请提供更多详细信息,例如样本数据和表格。请注意,您有SQL-Server
标记,limit
是MySQL语法。
<强>更新强>:
我回来后发现你已经更新了你的问题。这将返回EACH材料的最新结果:
select *
from
(SELECT
SO_Detail.Status,
SO_Detail.Sales_Order,
SO_Detail.SO_Line,
SO_Detail.Material,
SO_Detail.Rev,
SO_Header.Customer_PO,
SO_Detail.Order_Qty,
Coalesce(Material_Location.On_Hand_Qty, 0),
SO_Detail.Promised_Date,
SO_Detail.Note_Text,
Material.Description,
Job.Status AS Status1,
Job.Job ,
row_number() over(partition by Material.Description order by Job.Job desc) rn
FROM
SO_Detail
LEFT OUTER JOIN Material_Location ON SO_Detail.Material = Material_Location.Material
LEFT OUTER JOIN SO_Header ON SO_Detail.Sales_Order = SO_Header.Sales_Order
INNER JOIN Material ON Material.Material = SO_Detail.Material
INNER JOIN Job ON SO_Detail.Material = Job.Part_Number
WHERE
SO_Detail.Status = 'open') t --name of derived query
where t.rn = 1
-- you can then add an order statement if you need. Keep in note
-- I named the derived query t. So if you need to order by it'll need to be t.column
将来如果您发表评论并让回答者知道您已更新或有什么工作或没有,您可能会回答您的问题。此外,它只是排队等候消失在互联网领域。
答案 1 :(得分:0)
对于Job表中的select desc top 1行,你可以写一个lil select语句
SELECT
SO_Detail.Status,
SO_Detail.Sales_Order,
SO_Detail.SO_Line,
SO_Detail.Material,
SO_Detail.Rev,
SO_Header.Customer_PO,
SO_Detail.Order_Qty,
Coalesce(Material_Location.On_Hand_Qty, 0),
SO_Detail.Promised_Date,
SO_Detail.Note_Text,
Material.Description,
Job.Status AS Status1,
Job.Job
FROM
SO_Detail
LEFT OUTER JOIN Material_Location ON SO_Detail.Material = Material_Location.Material
LEFT OUTER JOIN SO_Header ON SO_Detail.Sales_Order = SO_Header.Sales_Order
INNER JOIN Material ON Material.Material = SO_Detail.Material
INNER JOIN (Select top 1 * from Job order by Job.Job desc) as TempJob ON SO_Detail.Material = TempJob.Part_Number
WHERE
SO_Detail.Status = 'open'
ORDER BY
SO_Detail.Sales_Order,
SO_Detail.SO_Line,
JobTemp.Job desc
答案 2 :(得分:0)
您可以使用此查询。
df_EVENT5.columns.sort_values().values