将最大和案例添加到数据中?

发布于 2025-01-21 06:41:51 字数 1199 浏览 0 评论 0原文

我试图在这里的代码中添加一些内容并更改此表,但是我在SQL中有点像Greenhorn,我正在努力地进行精神飞跃,以了解如何连接一些事情。

  1. 我正在尝试添加Job的分区以获取第1,2,3,4 ...(因此Job 21980将有3行)

  2. 我 想要显示每个作业的最高行。
  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.

  1. I'm trying to add a Partition by job to get row 1,2,3,4... (So job 21980 will have 3 rows)

  2. 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)

  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

Some Sample Data

I appreciate all the help in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

痕至 2025-01-28 06:41:51

我认为您难题的缺失作品是a 常见的表表达式。除非它首先在公共表表达式或子查询中,否则您不能引用窗口函数。这就是我将解决您的问题的方式。您还需要在row_number()函数中通过子句进行订单,以包括您想要的作业记录的方式。例如,您可能希望用最新的[日期上一期]作为最后一个记录或分区中的第一个记录对其进行评估。

如果您只想显示顶部作业#记录,则需要添加另一个row_number()函数(例如“ Rownumberdesc”),但随着[Job#]下降。然后,您可以在外部查询中添加一个子句,以限制Rownumberdesc = 1的位置。

with cte as (
    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] 
        ,ROW_NUMBER() OVER(PARTITION BY [Job #] ORDER BY [Date Last Issue]) AS 'RN'
        ,count(*) over(partition by [Job #]) as maxrn
      FROM [VarianceView] 
)

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] 
        ,case when rn <> maxrn then 0 else [PO$] end as [PO$]
        ,[Date Last Rcvd] 
        ,case when rn <> maxrn then 0 else [Wip Total]      end as [Wip Total] 
        ,case when rn <> maxrn then 0 else [per pc]         end as [per pc] 
        ,case when rn <> maxrn then 0 else [Standard Cost]  end as [Standard Cost]
        ,case when rn <> maxrn then 0 else [DIFF]           end as [DIFF] 
        ,case when rn <> maxrn then 0 else [% of Profit]    end as [% of Profit] 
        ,rn as [ROW NO by JOB]
        ,maxrn as [MAX of ROW NO by JOB]
        FROM cte
        Order By [Job #]

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 the row_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.

with cte as (
    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] 
        ,ROW_NUMBER() OVER(PARTITION BY [Job #] ORDER BY [Date Last Issue]) AS 'RN'
        ,count(*) over(partition by [Job #]) as maxrn
      FROM [VarianceView] 
)

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] 
        ,case when rn <> maxrn then 0 else [PO$] end as [PO$]
        ,[Date Last Rcvd] 
        ,case when rn <> maxrn then 0 else [Wip Total]      end as [Wip Total] 
        ,case when rn <> maxrn then 0 else [per pc]         end as [per pc] 
        ,case when rn <> maxrn then 0 else [Standard Cost]  end as [Standard Cost]
        ,case when rn <> maxrn then 0 else [DIFF]           end as [DIFF] 
        ,case when rn <> maxrn then 0 else [% of Profit]    end as [% of Profit] 
        ,rn as [ROW NO by JOB]
        ,maxrn as [MAX of ROW NO by JOB]
        FROM cte
        Order By [Job #]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文