将最大和案例添加到数据中?
我试图在这里的代码中添加一些内容并更改此表,但是我在SQL中有点像Greenhorn,我正在努力地进行精神飞跃,以了解如何连接一些事情。
我正在尝试添加Job的分区以获取第1,2,3,4 ...(因此Job 21980将有3行)
- 我 想要显示每个作业的最高行。
- 然后,我想添加作业的最大行no(因此, :当行号不是最大行号时,我希望行中的数据为列:PO $$,WIP总计,每台PC,标准成本,差异,利润的百分比
当前的SQL生成了此数据:
SELECT [Job #],
,[Date]
,[Variance Amt]
,[Job QTY]
,[OpenQty]
,[Part #]
,[Material]
,[PCS #]
,[Matrl$$]
,[Date Last Issue]
,case when substring([PurchaseOrders],len([PurchaseOrders]),1) = '|' then substring([PurchaseOrders],1,len([PurchaseOrders])-1) else [PurchaseOrders] end [PurchaseOrders]
,[PO$$]
,[Date Last Rcvd]
,[Wip Total]
,[per pc]
,[Standard Cost]
,[DIFF]
,[% of Profit]
FROM [VarianceView]
Order By [Job #]
我有点启动了步骤1和2,但这就是我所知道的。
ROW_NUMBER() OVER(PARTITION BY [Job #]
ORDER BY [Job #] DESC) AS 'RN'
,count(*) over(partition by [Job #]) as maxrn
我感谢所有的帮助!
I'm trying to add a few things to my code here and alter this table, but I'm a bit of a greenhorn in SQL and I'm struggling to make the mental leap to find out how to connect a few things.
I'm trying to add a Partition by job to get row 1,2,3,4... (So job 21980 will have 3 rows)
I then want to add MAX of ROW NO by JOB (So essentially I want to display the highest of the rows for each job. So job 21980 has 3 rows, I want to just show value 3)
I then want to say: when the row number ISN'T the maximum row number, then I want the data in the rows to be ZERO for columns: PO$$, Wip Total, per pc, Standard Cost, DIFF,% of Profit
Current SQL to generate this data:
SELECT [Job #],
,[Date]
,[Variance Amt]
,[Job QTY]
,[OpenQty]
,[Part #]
,[Material]
,[PCS #]
,[Matrl$]
,[Date Last Issue]
,case when substring([PurchaseOrders],len([PurchaseOrders]),1) = '|' then substring([PurchaseOrders],1,len([PurchaseOrders])-1) else [PurchaseOrders] end [PurchaseOrders]
,[PO$]
,[Date Last Rcvd]
,[Wip Total]
,[per pc]
,[Standard Cost]
,[DIFF]
,[% of Profit]
FROM [VarianceView]
Order By [Job #]
I kind of get how to get steps 1 and 2 started, but that's about all I know.
ROW_NUMBER() OVER(PARTITION BY [Job #]
ORDER BY [Job #] DESC) AS 'RN'
,count(*) over(partition by [Job #]) as maxrn
I appreciate all the help in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您难题的缺失作品是a 常见的表表达式。除非它首先在公共表表达式或子查询中,否则您不能引用窗口函数。这就是我将解决您的问题的方式。您还需要在
row_number()
函数中通过子句进行订单,以包括您想要的作业记录的方式。例如,您可能希望用最新的
[日期上一期]
作为最后一个记录或分区中的第一个记录对其进行评估。如果您只想显示顶部作业#记录,则需要添加另一个
row_number()
函数(例如“ Rownumberdesc”),但随着[Job#]下降。然后,您可以在外部查询中添加一个子句,以限制Rownumberdesc = 1的位置。I think the missing piece to your puzzle is a Common Table Expression. You can't reference a windowed function unless it is first in a common table expression or sub-query. Here's how I would approach your problem. You'll also want your
order by
clause in therow_number()
function to include the way you want the job records ordered. For example, you might want to have them evaluated with the newest[Date Last Issue]
as the last record or the first record in the partition.If you only want to show the top Job # record, you would need to add another
row_number()
function (e.g. "RowNumberDesc"), but with [Job #] being ordered descending. Then you could add a where clause to the outer query to limit where RowNumberDesc = 1.