如何根据数据仅显示表的几列

时间:2017-08-10 16:45:02

标签: sql sql-server

考虑下表

Name partno.  sch_date    WO#   owed panels
aa   1234     08/22/2017  121   22    26
aa   1234     08/22/2017  222   22    27
aa   1234     08/22/2017  242   22    27
aa   1234     08/29/2017  152   20    24
aa   1234     08/29/2017  167   20    24
aa   1234     08/29/2017  202   20    26`

是否有可能以这样的方式显示数据:当面板数量大于欠款时,我不会显示其他部分。在同一天安排(sch_date)。

预期结果

Name partno.  sch_date    WO#   owed panels
aa   1234     08/22/2017  121   22    26
aa   1234     08/29/2017  152   20    24

2 个答案:

答案 0 :(得分:1)

交叉申请可能会有所帮助。 (请注意,你可以看到为什么我在早期评论中询问订单,因为表格中的记录顺序无法保证!我们需要知道你想要的记录评估!日期是不够的(除非它有时间组成不显示不同!)

关于Rextester的工作示例:http://rextester.com/CAUK18185

做出了许多假设:

  • 当拥有时>您不需要查看记录的面板。
  • 你想看到欠款时最低的WO#是<面板并禁止所有其他记录,包括所欠的>面板。
  • 如果没有日期,名称和partno的记录,则欠下<面板,你想看不到任何记录。

如果这些假设不正确,请提供更好的样本集和预期结果,以测试这些类型的情况并解释您希望发生的事情。

SELECT Distinct B.*
FROM tblName Z
CROSS APPLY (SELECT TOP 1 A.* 
                      FROM tblName A 
                      WHERE A.owed < A.panels
                        and Z.Name = A.Name
                        and Z.[partno.] = a.[partno.]
                        and Z.sch_date = a.sch_date
                      ORDER by A.name, A.[partno.], A.sch_date, A.[wo#]) B

对于A中的每个记录,运行一个查询,当所欠的&lt;时,返回名称的最低wo#,partno和sch_date。面板。

<强> 更新: 我在评论中看到你想保留记录,如果欠了&gt;面板......如果它首先遇到......但是如果没有先遇到它会怎样?

http://rextester.com/NXS51018

--First we get all the records w/ a owed < panels per group and assign the earliest row (that having the lowest WO) a RN of 1.  then we return that set.
cte as (
Select A.*, row_number() over (Partition by Name, [partno.],  sch_date ORDER BY [WO#]) RN 
from tblName A 
where owed < panels)

Select * from cte where RN =1

UNION ALL
--We then union in the records where owed >=panels and their WO# < the wo# from the CTE.

SELECT Z.*, 0 as rn FROM tblName Z where owed >=panels
 and exists (Select * from cte 
             where Z.name = CTE.name 
               and Z.[partno.] = cte.[partno.] 
               and Z.sch_date = cte.sch_date
               and CTE.[WO#] > Z.[WO#])  --Now this line may not be needed, depending on if you want all or just some of the WO#'s when owed >=panels.
ORDER BY name, [partno.], Sch_date, [Wo#]

最后一次评论更新后:

cte as (
Select A.*, row_number() over (Partition by Name, [partno.],  sch_date ORDER BY [WO#]) RN 
from tblName A 
where owed < panels),

cte2 as (Select * from cte where RN =1
UNION ALL
SELECT Z.*, 0 as rn FROM tblName Z where owed >=panels
 and exists (Select * from cte 
             where Z.name = CTE.name 
               and Z.[partno.] = cte.[partno.] 
               and Z.sch_date = cte.sch_date
               and CTE.[WO#] > Z.[WO#]))

Select * into SOQ#45619304  from  CTE2;  --This line creates the table based on the 2nd cte results.

Select * from SOQ#45619304;

答案 1 :(得分:0)

你可以试试这个 -

SELECT Name, partno., sch_date, WO#, owed, panels
FROM YOUR_TABLE
WHERE panels < owed
UNION ALL
SELECT Name, partno., sch_date, MIN(WO#), owed, MIN(panels)
FROM YOUR_TABLE
WHERE panels > owed
GROUP BY Name, partno., sch_date, owed
ORDER BY Name