TSQL将所有数据添加到第一行,然后添加一半数据

时间:2018-05-16 21:35:52

标签: sql tsql sql-server-2016

嘿所有我有一张桌子,当我运行查询时,我得到这样的东西:

ID |The GUID                 |Quantity |Maint Part Number |Ship Group    |Date Received
-----------------------------------------------------------------------------------------
2  |54219-8974-8702-852-5425 |50       |54VRT             |ShipG105      |06/08/2018
3  |68v3f-5kjd-46ee-586-5988 |10       |M6eR5w            |ShipG001      |10/19/2010
4  |ErR20-bvmd-0001-bGT-0O0O |100      |MRE101            |ShipG99       |01/01/2011

现在我想要做的是:

ID |The GUID                 |Quantity |Maint Part Number |Ship Group    |Date Received
-----------------------------------------------------------------------------------------
2  |54219-8974-8702-852-5425 |50       |54VRT             |ShipG105      |06/08/2018
3  |                         |         |M6eR5w            |ShipG001      |10/19/2010
4  |                         |         |MRE101            |ShipG99       |01/01/2011

所有数据都出现在第一个记录行中,而以下行只有 inv _... 的adition数据。

我从多极INNER JOIN 表格以及 LEFT JOIN 表中获取所有这些数据。

查询示例:

SELECT
   BL.ID                AS 'ID',
   BL.guid              AS 'The GUID',
   BL.qty               AS 'Quantity',
   I.inv_maintPartNum   AS 'Maint Part Number',
   I.inv_ShipGrp        AS 'Ship Group',
   I.inv_DateRec        AS 'Date Received' 
FROM 
   BizLine              AS BL 
INNER JOIN inventory    AS I 
   ON BL.ID = I.ID
..........

我该怎么做?

更新

如果我有更多具有不同ID的记录,如下所示:

ID |The GUID                 |Quantity |Maint Part Number |Ship Group    |Date Received
-----------------------------------------------------------------------------------------
2  |54219-8974-8702-852-5425 |50       |54VRT             |ShipG105      |06/08/2018
2  |68v3f-5kjd-46ee-586-5988 |10       |M6eR5w            |ShipG001      |10/19/2010
2  |ErR20-bvmd-0001-bGT-0O0O |100      |MRE101            |ShipG99       |01/01/2011
3  |68rGw-3567-0001-123-fG34 |5        |GRD2859           |ShipG1        |11/11/2012
3  |5879R-376T-FDSW-RT7-23FG |1005     |Sv22              |ShipG87       |04/19/2017

现在使用来自@SQLChao的查询,它看起来像这样:

2  |54219-8974-8702-852-5425 |50       |54VRT             |ShipG105      |06/08/2018
2  |                         |         |M6eR5w            |ShipG001      |10/19/2010
2  |                         |         |MRE101            |ShipG99       |01/01/2011
3  |                         |         |GRD2859           |ShipG1        |11/11/2012
3  |                         |         |Sv22              |ShipG87       |04/19/2017

现在看起来应该是这样的:

2  |54219-8974-8702-852-5425 |50       |54VRT             |ShipG105      |06/08/2018
2  |                         |         |M6eR5w            |ShipG001      |10/19/2010
2  |                         |         |MRE101            |ShipG99       |01/01/2011
3  |68rGw-3567-0001-123-fG34 |5        |GRD2859           |ShipG1        |11/11/2012
3  |                         |         |Sv22              |ShipG87       |04/19/2017

请注意 ID 3 如何与第一个 ID 2 一样启动所有数据,然后仅显示其他数据。

2 个答案:

答案 0 :(得分:1)

您可以使用ROW_NUMBER(),如果= 1,则返回数据。否则请留空。我不确定您是如何订购数据的,因此我使用了BL.ID。如果需要,您可以将其更改为其他内容。

SELECT
   BL.ID                AS 'ID',
   CASE WHEN ROW_NUMBER() OVER (PARTITION BY BL.ID ORDER BY BL.ID ASC) = 1 THEN BL.guid ELSE '' END AS 'The GUID',
   CASE WHEN ROW_NUMBER() OVER (PARTITION By BL.ID ORDER BY BL.ID ASC) = 1 THEN BL.qty ELSE '' END AS 'Quantity',
   I.inv_maintPartNum   AS 'Maint Part Number',
   I.inv_ShipGrp        AS 'Ship Group',
   I.inv_DateRec        AS 'Date Received' 
FROM 
   BizLine              AS BL 
INNER JOIN inventory    AS I 
   ON BL.ID = I.ID
..........

答案 1 :(得分:1)

试试这个

;With myData as
(
SELECT
   BL.ID                AS 'ID',
   BL.guid              AS 'The GUID',
   BL.qty               AS 'Quantity',
   I.inv_maintPartNum   AS 'Maint Part Number',
   I.inv_ShipGrp        AS 'Ship Group',
   I.inv_DateRec        AS 'Date Received',
   ROW_NUMBER() Over (Partition By BL.ID Order By BL.ID) RowNo

FROM 
   BizLine              AS BL 
INNER JOIN inventory    AS I 
   ON BL.ID = I.ID

)
Select ID, IIF(RowNo = 1, [The GUID], ' ')  [The GUID], IIF(RowNo = 1, [Quantity], ' ') Quantity, [Maint Part Number], [Ship Group], [Date Received]
From myData

您可能需要修改窗口功能以识别您想要完整数据的记录。