Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
需要SQL Server游标的帮助吗_Sql_Sql Server 2008_Sql Server 2012 - Fatal编程技术网

需要SQL Server游标的帮助吗

需要SQL Server游标的帮助吗,sql,sql-server-2008,sql-server-2012,Sql,Sql Server 2008,Sql Server 2012,我有这张桌子: 我想要这个输出: 我正在使用这个代码 DECLARE @InvoiceNo Varchar(100) SET @InvoiceNo = '17-18/1003' DECLARE @srNO Varchar(100) PRINT @InvoiceNo BEGIN CREATE TABLE #tempTableNew ( SrNO Varchar(100), Description Varchar(100),

我有这张桌子:

我想要这个输出:

我正在使用这个代码

DECLARE @InvoiceNo Varchar(100)
SET @InvoiceNo = '17-18/1003'

DECLARE @srNO Varchar(100)
PRINT @InvoiceNo

BEGIN

    CREATE TABLE #tempTableNew 
    (
        SrNO Varchar(100),
        Description Varchar(100),
        Qnty Varchar(100),
        Unit Varchar(100),
        Rate Varchar(100),
        Amount Varchar(100)
    )

    DECLARE CREATFINALTABLE CURSOR FOR
        SELECT
            ROW_NUMBER() OVER(ORDER BY B.InvoiceNo ASC) AS 'SrNo'
        FROM 
            Billing B
        INNER JOIN 
            Furniture F on F.FurnitureID = B.ProductID
        WHERE
            B.InvoiceNo = @InvoiceNo  

    OPEN CREATFINALTABLE   
    FETCH NEXT FROM CREATFINALTABLE INTO @srNO

    WHILE @@FETCH_STATUS = 0   
    BEGIN   
        SELECT
            CONVERT(varchar, @srNO) AS 'SrNo',
            F.FurnitureName AS 'Description',
            CONVERT(varchar, B.Quantity) AS 'Qnty',
            CONVERT(varchar, U.UnitName) AS 'Unit',
            CONVERT(varchar, B.Rate) AS 'Rate',
            CONVERT(varchar, B.Amount) AS 'Amount' 
        INTO 
            #tempTable
        FROM
            Billing B
        INNER JOIN 
            Furniture F on F.FurnitureID = B.ProductID
        INNER JOIN 
            UnitMaster U ON U.UnitID = B.UnitID
        WHERE
            B.BillingID = @srNO

        INSERT INTO #tempTableNew
            SELECT * 
            FROM #tempTable
            WHERE SrNo = @srNO
            UNION ALL
            SELECT 
                (@srNO) AS 'srNo', 
                ('SIZE: ' + CONVERT(varchar, Length) + ' X' + 
                 CONVERT(varchar, Breadth) + ' X' + CONVERT(varchar, Height)) AS '[Description]',
                ' ' AS 'Qnty', ' ' AS 'Unit', ' ' AS 'Rate', ' ' AS 'Amount' 
            FROM Billing
            WHERE BillingID = @srNO
            UNION ALL
            SELECT 
                (@srNO) AS 'srNo', 
                'SPECS:' AS '[Description]', 
                ' ' AS 'Qnty', ' ' AS 'Unit', ' ' AS 'Rate', ' ' AS 'Amount' 
            FROM #tempTable
            WHERE SrNo = @srNO

           FETCH NEXT FROM CREATFINALTABLE INTO @srNO 

           DROP TABLE #tempTable
    END   

    SELECT * 
    INTO #formated 
    FROM #tempTableNew

    SELECT * FROM #formated

    DROP TABLE #tempTableNew
    DROP Table #formated

    CLOSE CREATFINALTABLE   
    DEALLOCATE CREATFINALTABLE
END
我得到了这个输出。请帮助我获得所需的输出。我想根据分组依据
Area of Sage
显示记录。请帮助我修改此光标,以便获得所需的输出。先谢谢你


样本数据

CREATE TABLE Product 
  ([SrNo] int, [Description] varchar(100), [Size] varchar(100)
, [Specs] varchar(100), [Qnty] int, [Unit] varchar(5)
, [Rate] int, [Amount] int, [AreaOfUsage] varchar(100)) 
; 

INSERT INTO Product 
([SrNo], [Description],[Size], [Specs], [Qnty], [Unit], [Rate], [Amount], [AreaOfUsage]) 
VALUES 
 (1, 'Bed as per design and detail', 'test iu','7.00x4.00x2.00', 1, 'P.RFT', 35000, 35000, 'Bedroom'),
 (2, 'Coffee Table as per design and detail', 'shdjh test','7.00x3.00x2.00', 1, 'P.RFT', 12500, 12500, 'Hall'),
 (3, 'Day Bed as per design in white', 'day bed test','7.00x5.00x3.00', 1, 'P.RFT', 19630, 19630, 'Bedroom') 
 ;

这种程度的数据操作不太适合SQL,通常在“表示层”中执行,但这里有一种方法,您可能会发现它很有用,根本不需要使用游标。它使用带值的交叉应用来代替,这将为每个输入行创建多行输出。“值”区域的视觉布局易于控制


修改后的答案

作为演示,可在上获得

MS SQL Server 2014架构设置

CREATE TABLE Product 
  ([SrNo] int, [Description] varchar(100), [Size] varchar(100)
, [Specs] varchar(100), [Qnty] int, [Unit] varchar(5)
, [Rate] int, [Amount] int, [AreaOfUsage] varchar(100)) 
; 

INSERT INTO Product 
([SrNo], [Description],[Size], [Specs], [Qnty], [Unit], [Rate], [Amount], [AreaOfUsage]) 
VALUES 
 (1, 'Bed as per design and detail', 'test iu','7.00x4.00x2.00', 1, 'P.RFT', 35000, 35000, 'Bedroom'),
 (2, 'Coffee Table as per design and detail', 'shdjh test','7.00x3.00x2.00', 1, 'P.RFT', 12500, 12500, 'Hall'),
 (3, 'Day Bed as per design in white', 'day bed test','7.00x5.00x3.00', 1, 'P.RFT', 19630, 19630, 'Bedroom') 
 ; 
SELECT
      CASE WHEN rn = 2 THEN concat(t.srNo, '') ELSE '' END   srNo
    , t.Description
    , CASE WHEN rn = 2 THEN concat(t.Qnty, '') ELSE '' END   Qnty
    , CASE WHEN rn = 2 THEN concat(t.Unit, '') ELSE '' END   Unit
    , CASE WHEN rn = 2 THEN concat(t.Rate, '') ELSE '' END   Rate
    , CASE WHEN rn = 2 THEN concat(t.Amount, '') ELSE '' END Amount
FROM (
      SELECT
              p.*
            , row_number() over(partition by AreaOfUsage
                                order by srNo, rn) as grporder
      FROM (
          SELECT
                AreaOfUsage
              , srNo
              , ca.rn
              , ca.Description
              , Qnty
              , Unit
              , Rate
              , Amount
          FROM Product
          CROSS APPLY (
                      VALUES
                            (1, AreaOfUsage),
                            (2, Description),
                            (3, Specs),
                            (4, Size)
                      ) ca (rn, description)
            ) p
      ) t
WHERE (
           (grporder = 1 )
        OR (grporder > 1 and AreaOfUsage <> Description)
      )  
ORDER BY AreaOfUsage, grporder
| srNo |                           Description | Qnty |  Unit |  Rate | Amount |
|------|---------------------------------------|------|-------|-------|--------|
|      |                               Bedroom |      |       |       |        |
|    1 |          Bed as per design and detail |    1 | P.RFT | 35000 |  35000 |
|      |                        7.00x4.00x2.00 |      |       |       |        |
|      |                               test iu |      |       |       |        |
|    3 |        Day Bed as per design in white |    1 | P.RFT | 19630 |  19630 |
|      |                        7.00x5.00x3.00 |      |       |       |        |
|      |                          day bed test |      |       |       |        |
|      |                                  Hall |      |       |       |        |
|    2 | Coffee Table as per design and detail |    1 | P.RFT | 12500 |  12500 |
|      |                        7.00x3.00x2.00 |      |       |       |        |
|      |                            shdjh test |      |       |       |        |
| AreaOfUsage | srNo | rn |                           Description | Qnty |  Unit |  Rate | Amount | grporder |
|-------------|------|----|---------------------------------------|------|-------|-------|--------|----------|
|     Bedroom |    1 |  1 |                               Bedroom |    1 | P.RFT | 35000 |  35000 |        1 |
|     Bedroom |    1 |  2 |          Bed as per design and detail |    1 | P.RFT | 35000 |  35000 |        2 |
|     Bedroom |    1 |  3 |                        7.00x4.00x2.00 |    1 | P.RFT | 35000 |  35000 |        3 |
|     Bedroom |    1 |  4 |                               test iu |    1 | P.RFT | 35000 |  35000 |        4 |
|     Bedroom |    3 |  1 |                               Bedroom |    1 | P.RFT | 19630 |  19630 |        5 |
|     Bedroom |    3 |  2 |        Day Bed as per design in white |    1 | P.RFT | 19630 |  19630 |        6 |
|     Bedroom |    3 |  3 |                        7.00x5.00x3.00 |    1 | P.RFT | 19630 |  19630 |        7 |
|     Bedroom |    3 |  4 |                          day bed test |    1 | P.RFT | 19630 |  19630 |        8 |
|        Hall |    2 |  1 |                                  Hall |    1 | P.RFT | 12500 |  12500 |        1 |
|        Hall |    2 |  2 | Coffee Table as per design and detail |    1 | P.RFT | 12500 |  12500 |        2 |
|        Hall |    2 |  3 |                        7.00x3.00x2.00 |    1 | P.RFT | 12500 |  12500 |        3 |
|        Hall |    2 |  4 |                            shdjh test |    1 | P.RFT | 12500 |  12500 |        4 |
CREATE TABLE Ihavethistable
    ([SrNo] int, [Description] varchar(17), [Specs] varchar(12), [Qnty] int, [Unit] varchar(5), [Rate] int, [Amount] int, [AreaOfUsage] varchar(7))
;

INSERT INTO Ihavethistable
    ([SrNo], [Description], [Specs], [Qnty], [Unit], [Rate], [Amount], [AreaOfUsage])
VALUES
    (1, 'bed as per...', 'test iu', 1, 'P.RFT', 35000, 35000, 'Bedroom'),
    (2, 'coffee table...', 'shdjh test', 1, 'P.RFT', 12500, 12500, 'Hall'),
    (3, 'day bed as per...', 'day bed test', 1, 'P.RFT', 19630, 19630, 'Bedroom')
;
select
      case when ca.rn = 2 then concat(t.srNo,'') else '' end srNo
    , ca.description
    , case when ca.rn = 2 then concat(t.Qnty,'') else '' end Qnty
    , case when ca.rn = 2 then concat(t.Unit,'') else '' end Unit
    , case when ca.rn = 2 then concat(t.Rate,'') else '' end Rate
    , case when ca.rn = 2 then concat(t.Amount,'') else '' end Amount
from Ihavethistable t
CROSS APPLY (
  VALUES
     (1, AreaOfUsage),
     (2, Description),
     (3, Specs)
  ) ca (rn, description)
order by t.srNo, ca.rn
| srNo |       description | Qnty |  Unit |  Rate | Amount |
|------|-------------------|------|-------|-------|--------|
|      |           Bedroom |      |       |       |        |
|    1 |     bed as per... |    1 | P.RFT | 35000 |  35000 |
|      |           test iu |      |       |       |        |
|      |              Hall |      |       |       |        |
|    2 |   coffee table... |    1 | P.RFT | 12500 |  12500 |
|      |        shdjh test |      |       |       |        |
|      |           Bedroom |      |       |       |        |
|    3 | day bed as per... |    1 | P.RFT | 19630 |  19630 |
|      |      day bed test |      |       |       |        |
查询1

CREATE TABLE Product 
  ([SrNo] int, [Description] varchar(100), [Size] varchar(100)
, [Specs] varchar(100), [Qnty] int, [Unit] varchar(5)
, [Rate] int, [Amount] int, [AreaOfUsage] varchar(100)) 
; 

INSERT INTO Product 
([SrNo], [Description],[Size], [Specs], [Qnty], [Unit], [Rate], [Amount], [AreaOfUsage]) 
VALUES 
 (1, 'Bed as per design and detail', 'test iu','7.00x4.00x2.00', 1, 'P.RFT', 35000, 35000, 'Bedroom'),
 (2, 'Coffee Table as per design and detail', 'shdjh test','7.00x3.00x2.00', 1, 'P.RFT', 12500, 12500, 'Hall'),
 (3, 'Day Bed as per design in white', 'day bed test','7.00x5.00x3.00', 1, 'P.RFT', 19630, 19630, 'Bedroom') 
 ; 
SELECT
      CASE WHEN rn = 2 THEN concat(t.srNo, '') ELSE '' END   srNo
    , t.Description
    , CASE WHEN rn = 2 THEN concat(t.Qnty, '') ELSE '' END   Qnty
    , CASE WHEN rn = 2 THEN concat(t.Unit, '') ELSE '' END   Unit
    , CASE WHEN rn = 2 THEN concat(t.Rate, '') ELSE '' END   Rate
    , CASE WHEN rn = 2 THEN concat(t.Amount, '') ELSE '' END Amount
FROM (
      SELECT
              p.*
            , row_number() over(partition by AreaOfUsage
                                order by srNo, rn) as grporder
      FROM (
          SELECT
                AreaOfUsage
              , srNo
              , ca.rn
              , ca.Description
              , Qnty
              , Unit
              , Rate
              , Amount
          FROM Product
          CROSS APPLY (
                      VALUES
                            (1, AreaOfUsage),
                            (2, Description),
                            (3, Specs),
                            (4, Size)
                      ) ca (rn, description)
            ) p
      ) t
WHERE (
           (grporder = 1 )
        OR (grporder > 1 and AreaOfUsage <> Description)
      )  
ORDER BY AreaOfUsage, grporder
| srNo |                           Description | Qnty |  Unit |  Rate | Amount |
|------|---------------------------------------|------|-------|-------|--------|
|      |                               Bedroom |      |       |       |        |
|    1 |          Bed as per design and detail |    1 | P.RFT | 35000 |  35000 |
|      |                        7.00x4.00x2.00 |      |       |       |        |
|      |                               test iu |      |       |       |        |
|    3 |        Day Bed as per design in white |    1 | P.RFT | 19630 |  19630 |
|      |                        7.00x5.00x3.00 |      |       |       |        |
|      |                          day bed test |      |       |       |        |
|      |                                  Hall |      |       |       |        |
|    2 | Coffee Table as per design and detail |    1 | P.RFT | 12500 |  12500 |
|      |                        7.00x3.00x2.00 |      |       |       |        |
|      |                            shdjh test |      |       |       |        |
| AreaOfUsage | srNo | rn |                           Description | Qnty |  Unit |  Rate | Amount | grporder |
|-------------|------|----|---------------------------------------|------|-------|-------|--------|----------|
|     Bedroom |    1 |  1 |                               Bedroom |    1 | P.RFT | 35000 |  35000 |        1 |
|     Bedroom |    1 |  2 |          Bed as per design and detail |    1 | P.RFT | 35000 |  35000 |        2 |
|     Bedroom |    1 |  3 |                        7.00x4.00x2.00 |    1 | P.RFT | 35000 |  35000 |        3 |
|     Bedroom |    1 |  4 |                               test iu |    1 | P.RFT | 35000 |  35000 |        4 |
|     Bedroom |    3 |  1 |                               Bedroom |    1 | P.RFT | 19630 |  19630 |        5 |
|     Bedroom |    3 |  2 |        Day Bed as per design in white |    1 | P.RFT | 19630 |  19630 |        6 |
|     Bedroom |    3 |  3 |                        7.00x5.00x3.00 |    1 | P.RFT | 19630 |  19630 |        7 |
|     Bedroom |    3 |  4 |                          day bed test |    1 | P.RFT | 19630 |  19630 |        8 |
|        Hall |    2 |  1 |                                  Hall |    1 | P.RFT | 12500 |  12500 |        1 |
|        Hall |    2 |  2 | Coffee Table as per design and detail |    1 | P.RFT | 12500 |  12500 |        2 |
|        Hall |    2 |  3 |                        7.00x3.00x2.00 |    1 | P.RFT | 12500 |  12500 |        3 |
|        Hall |    2 |  4 |                            shdjh test |    1 | P.RFT | 12500 |  12500 |        4 |
CREATE TABLE Ihavethistable
    ([SrNo] int, [Description] varchar(17), [Specs] varchar(12), [Qnty] int, [Unit] varchar(5), [Rate] int, [Amount] int, [AreaOfUsage] varchar(7))
;

INSERT INTO Ihavethistable
    ([SrNo], [Description], [Specs], [Qnty], [Unit], [Rate], [Amount], [AreaOfUsage])
VALUES
    (1, 'bed as per...', 'test iu', 1, 'P.RFT', 35000, 35000, 'Bedroom'),
    (2, 'coffee table...', 'shdjh test', 1, 'P.RFT', 12500, 12500, 'Hall'),
    (3, 'day bed as per...', 'day bed test', 1, 'P.RFT', 19630, 19630, 'Bedroom')
;
select
      case when ca.rn = 2 then concat(t.srNo,'') else '' end srNo
    , ca.description
    , case when ca.rn = 2 then concat(t.Qnty,'') else '' end Qnty
    , case when ca.rn = 2 then concat(t.Unit,'') else '' end Unit
    , case when ca.rn = 2 then concat(t.Rate,'') else '' end Rate
    , case when ca.rn = 2 then concat(t.Amount,'') else '' end Amount
from Ihavethistable t
CROSS APPLY (
  VALUES
     (1, AreaOfUsage),
     (2, Description),
     (3, Specs)
  ) ca (rn, description)
order by t.srNo, ca.rn
| srNo |       description | Qnty |  Unit |  Rate | Amount |
|------|-------------------|------|-------|-------|--------|
|      |           Bedroom |      |       |       |        |
|    1 |     bed as per... |    1 | P.RFT | 35000 |  35000 |
|      |           test iu |      |       |       |        |
|      |              Hall |      |       |       |        |
|    2 |   coffee table... |    1 | P.RFT | 12500 |  12500 |
|      |        shdjh test |      |       |       |        |
|      |           Bedroom |      |       |       |        |
|    3 | day bed as per... |    1 | P.RFT | 19630 |  19630 |
|      |      day bed test |      |       |       |        |
查询2

CREATE TABLE Product 
  ([SrNo] int, [Description] varchar(100), [Size] varchar(100)
, [Specs] varchar(100), [Qnty] int, [Unit] varchar(5)
, [Rate] int, [Amount] int, [AreaOfUsage] varchar(100)) 
; 

INSERT INTO Product 
([SrNo], [Description],[Size], [Specs], [Qnty], [Unit], [Rate], [Amount], [AreaOfUsage]) 
VALUES 
 (1, 'Bed as per design and detail', 'test iu','7.00x4.00x2.00', 1, 'P.RFT', 35000, 35000, 'Bedroom'),
 (2, 'Coffee Table as per design and detail', 'shdjh test','7.00x3.00x2.00', 1, 'P.RFT', 12500, 12500, 'Hall'),
 (3, 'Day Bed as per design in white', 'day bed test','7.00x5.00x3.00', 1, 'P.RFT', 19630, 19630, 'Bedroom') 
 ; 
SELECT
      CASE WHEN rn = 2 THEN concat(t.srNo, '') ELSE '' END   srNo
    , t.Description
    , CASE WHEN rn = 2 THEN concat(t.Qnty, '') ELSE '' END   Qnty
    , CASE WHEN rn = 2 THEN concat(t.Unit, '') ELSE '' END   Unit
    , CASE WHEN rn = 2 THEN concat(t.Rate, '') ELSE '' END   Rate
    , CASE WHEN rn = 2 THEN concat(t.Amount, '') ELSE '' END Amount
FROM (
      SELECT
              p.*
            , row_number() over(partition by AreaOfUsage
                                order by srNo, rn) as grporder
      FROM (
          SELECT
                AreaOfUsage
              , srNo
              , ca.rn
              , ca.Description
              , Qnty
              , Unit
              , Rate
              , Amount
          FROM Product
          CROSS APPLY (
                      VALUES
                            (1, AreaOfUsage),
                            (2, Description),
                            (3, Specs),
                            (4, Size)
                      ) ca (rn, description)
            ) p
      ) t
WHERE (
           (grporder = 1 )
        OR (grporder > 1 and AreaOfUsage <> Description)
      )  
ORDER BY AreaOfUsage, grporder
| srNo |                           Description | Qnty |  Unit |  Rate | Amount |
|------|---------------------------------------|------|-------|-------|--------|
|      |                               Bedroom |      |       |       |        |
|    1 |          Bed as per design and detail |    1 | P.RFT | 35000 |  35000 |
|      |                        7.00x4.00x2.00 |      |       |       |        |
|      |                               test iu |      |       |       |        |
|    3 |        Day Bed as per design in white |    1 | P.RFT | 19630 |  19630 |
|      |                        7.00x5.00x3.00 |      |       |       |        |
|      |                          day bed test |      |       |       |        |
|      |                                  Hall |      |       |       |        |
|    2 | Coffee Table as per design and detail |    1 | P.RFT | 12500 |  12500 |
|      |                        7.00x3.00x2.00 |      |       |       |        |
|      |                            shdjh test |      |       |       |        |
| AreaOfUsage | srNo | rn |                           Description | Qnty |  Unit |  Rate | Amount | grporder |
|-------------|------|----|---------------------------------------|------|-------|-------|--------|----------|
|     Bedroom |    1 |  1 |                               Bedroom |    1 | P.RFT | 35000 |  35000 |        1 |
|     Bedroom |    1 |  2 |          Bed as per design and detail |    1 | P.RFT | 35000 |  35000 |        2 |
|     Bedroom |    1 |  3 |                        7.00x4.00x2.00 |    1 | P.RFT | 35000 |  35000 |        3 |
|     Bedroom |    1 |  4 |                               test iu |    1 | P.RFT | 35000 |  35000 |        4 |
|     Bedroom |    3 |  1 |                               Bedroom |    1 | P.RFT | 19630 |  19630 |        5 |
|     Bedroom |    3 |  2 |        Day Bed as per design in white |    1 | P.RFT | 19630 |  19630 |        6 |
|     Bedroom |    3 |  3 |                        7.00x5.00x3.00 |    1 | P.RFT | 19630 |  19630 |        7 |
|     Bedroom |    3 |  4 |                          day bed test |    1 | P.RFT | 19630 |  19630 |        8 |
|        Hall |    2 |  1 |                                  Hall |    1 | P.RFT | 12500 |  12500 |        1 |
|        Hall |    2 |  2 | Coffee Table as per design and detail |    1 | P.RFT | 12500 |  12500 |        2 |
|        Hall |    2 |  3 |                        7.00x3.00x2.00 |    1 | P.RFT | 12500 |  12500 |        3 |
|        Hall |    2 |  4 |                            shdjh test |    1 | P.RFT | 12500 |  12500 |        4 |
CREATE TABLE Ihavethistable
    ([SrNo] int, [Description] varchar(17), [Specs] varchar(12), [Qnty] int, [Unit] varchar(5), [Rate] int, [Amount] int, [AreaOfUsage] varchar(7))
;

INSERT INTO Ihavethistable
    ([SrNo], [Description], [Specs], [Qnty], [Unit], [Rate], [Amount], [AreaOfUsage])
VALUES
    (1, 'bed as per...', 'test iu', 1, 'P.RFT', 35000, 35000, 'Bedroom'),
    (2, 'coffee table...', 'shdjh test', 1, 'P.RFT', 12500, 12500, 'Hall'),
    (3, 'day bed as per...', 'day bed test', 1, 'P.RFT', 19630, 19630, 'Bedroom')
;
select
      case when ca.rn = 2 then concat(t.srNo,'') else '' end srNo
    , ca.description
    , case when ca.rn = 2 then concat(t.Qnty,'') else '' end Qnty
    , case when ca.rn = 2 then concat(t.Unit,'') else '' end Unit
    , case when ca.rn = 2 then concat(t.Rate,'') else '' end Rate
    , case when ca.rn = 2 then concat(t.Amount,'') else '' end Amount
from Ihavethistable t
CROSS APPLY (
  VALUES
     (1, AreaOfUsage),
     (2, Description),
     (3, Specs)
  ) ca (rn, description)
order by t.srNo, ca.rn
| srNo |       description | Qnty |  Unit |  Rate | Amount |
|------|-------------------|------|-------|-------|--------|
|      |           Bedroom |      |       |       |        |
|    1 |     bed as per... |    1 | P.RFT | 35000 |  35000 |
|      |           test iu |      |       |       |        |
|      |              Hall |      |       |       |        |
|    2 |   coffee table... |    1 | P.RFT | 12500 |  12500 |
|      |        shdjh test |      |       |       |        |
|      |           Bedroom |      |       |       |        |
|    3 | day bed as per... |    1 | P.RFT | 19630 |  19630 |
|      |      day bed test |      |       |       |        |
显示详细信息行以帮助理解

      SELECT
              p.*
            , row_number() over(partition by AreaOfUsage
                                order by srNo, rn) as grporder
      FROM (
          SELECT
                AreaOfUsage
              , srNo
              , ca.rn
              , ca.Description
              , Qnty
              , Unit
              , Rate
              , Amount
          FROM Product
          CROSS APPLY (
                      VALUES
                            (1, AreaOfUsage),
                            (2, Description),
                            (3, Specs),
                            (4, Size)
                      ) ca (rn, description)
            ) p

CREATE TABLE Product 
  ([SrNo] int, [Description] varchar(100), [Size] varchar(100)
, [Specs] varchar(100), [Qnty] int, [Unit] varchar(5)
, [Rate] int, [Amount] int, [AreaOfUsage] varchar(100)) 
; 

INSERT INTO Product 
([SrNo], [Description],[Size], [Specs], [Qnty], [Unit], [Rate], [Amount], [AreaOfUsage]) 
VALUES 
 (1, 'Bed as per design and detail', 'test iu','7.00x4.00x2.00', 1, 'P.RFT', 35000, 35000, 'Bedroom'),
 (2, 'Coffee Table as per design and detail', 'shdjh test','7.00x3.00x2.00', 1, 'P.RFT', 12500, 12500, 'Hall'),
 (3, 'Day Bed as per design in white', 'day bed test','7.00x5.00x3.00', 1, 'P.RFT', 19630, 19630, 'Bedroom') 
 ; 
SELECT
      CASE WHEN rn = 2 THEN concat(t.srNo, '') ELSE '' END   srNo
    , t.Description
    , CASE WHEN rn = 2 THEN concat(t.Qnty, '') ELSE '' END   Qnty
    , CASE WHEN rn = 2 THEN concat(t.Unit, '') ELSE '' END   Unit
    , CASE WHEN rn = 2 THEN concat(t.Rate, '') ELSE '' END   Rate
    , CASE WHEN rn = 2 THEN concat(t.Amount, '') ELSE '' END Amount
FROM (
      SELECT
              p.*
            , row_number() over(partition by AreaOfUsage
                                order by srNo, rn) as grporder
      FROM (
          SELECT
                AreaOfUsage
              , srNo
              , ca.rn
              , ca.Description
              , Qnty
              , Unit
              , Rate
              , Amount
          FROM Product
          CROSS APPLY (
                      VALUES
                            (1, AreaOfUsage),
                            (2, Description),
                            (3, Specs),
                            (4, Size)
                      ) ca (rn, description)
            ) p
      ) t
WHERE (
           (grporder = 1 )
        OR (grporder > 1 and AreaOfUsage <> Description)
      )  
ORDER BY AreaOfUsage, grporder
| srNo |                           Description | Qnty |  Unit |  Rate | Amount |
|------|---------------------------------------|------|-------|-------|--------|
|      |                               Bedroom |      |       |       |        |
|    1 |          Bed as per design and detail |    1 | P.RFT | 35000 |  35000 |
|      |                        7.00x4.00x2.00 |      |       |       |        |
|      |                               test iu |      |       |       |        |
|    3 |        Day Bed as per design in white |    1 | P.RFT | 19630 |  19630 |
|      |                        7.00x5.00x3.00 |      |       |       |        |
|      |                          day bed test |      |       |       |        |
|      |                                  Hall |      |       |       |        |
|    2 | Coffee Table as per design and detail |    1 | P.RFT | 12500 |  12500 |
|      |                        7.00x3.00x2.00 |      |       |       |        |
|      |                            shdjh test |      |       |       |        |
| AreaOfUsage | srNo | rn |                           Description | Qnty |  Unit |  Rate | Amount | grporder |
|-------------|------|----|---------------------------------------|------|-------|-------|--------|----------|
|     Bedroom |    1 |  1 |                               Bedroom |    1 | P.RFT | 35000 |  35000 |        1 |
|     Bedroom |    1 |  2 |          Bed as per design and detail |    1 | P.RFT | 35000 |  35000 |        2 |
|     Bedroom |    1 |  3 |                        7.00x4.00x2.00 |    1 | P.RFT | 35000 |  35000 |        3 |
|     Bedroom |    1 |  4 |                               test iu |    1 | P.RFT | 35000 |  35000 |        4 |
|     Bedroom |    3 |  1 |                               Bedroom |    1 | P.RFT | 19630 |  19630 |        5 |
|     Bedroom |    3 |  2 |        Day Bed as per design in white |    1 | P.RFT | 19630 |  19630 |        6 |
|     Bedroom |    3 |  3 |                        7.00x5.00x3.00 |    1 | P.RFT | 19630 |  19630 |        7 |
|     Bedroom |    3 |  4 |                          day bed test |    1 | P.RFT | 19630 |  19630 |        8 |
|        Hall |    2 |  1 |                                  Hall |    1 | P.RFT | 12500 |  12500 |        1 |
|        Hall |    2 |  2 | Coffee Table as per design and detail |    1 | P.RFT | 12500 |  12500 |        2 |
|        Hall |    2 |  3 |                        7.00x3.00x2.00 |    1 | P.RFT | 12500 |  12500 |        3 |
|        Hall |    2 |  4 |                            shdjh test |    1 | P.RFT | 12500 |  12500 |        4 |
CREATE TABLE Ihavethistable
    ([SrNo] int, [Description] varchar(17), [Specs] varchar(12), [Qnty] int, [Unit] varchar(5), [Rate] int, [Amount] int, [AreaOfUsage] varchar(7))
;

INSERT INTO Ihavethistable
    ([SrNo], [Description], [Specs], [Qnty], [Unit], [Rate], [Amount], [AreaOfUsage])
VALUES
    (1, 'bed as per...', 'test iu', 1, 'P.RFT', 35000, 35000, 'Bedroom'),
    (2, 'coffee table...', 'shdjh test', 1, 'P.RFT', 12500, 12500, 'Hall'),
    (3, 'day bed as per...', 'day bed test', 1, 'P.RFT', 19630, 19630, 'Bedroom')
;
select
      case when ca.rn = 2 then concat(t.srNo,'') else '' end srNo
    , ca.description
    , case when ca.rn = 2 then concat(t.Qnty,'') else '' end Qnty
    , case when ca.rn = 2 then concat(t.Unit,'') else '' end Unit
    , case when ca.rn = 2 then concat(t.Rate,'') else '' end Rate
    , case when ca.rn = 2 then concat(t.Amount,'') else '' end Amount
from Ihavethistable t
CROSS APPLY (
  VALUES
     (1, AreaOfUsage),
     (2, Description),
     (3, Specs)
  ) ca (rn, description)
order by t.srNo, ca.rn
| srNo |       description | Qnty |  Unit |  Rate | Amount |
|------|-------------------|------|-------|-------|--------|
|      |           Bedroom |      |       |       |        |
|    1 |     bed as per... |    1 | P.RFT | 35000 |  35000 |
|      |           test iu |      |       |       |        |
|      |              Hall |      |       |       |        |
|    2 |   coffee table... |    1 | P.RFT | 12500 |  12500 |
|      |        shdjh test |      |       |       |        |
|      |           Bedroom |      |       |       |        |
|    3 | day bed as per... |    1 | P.RFT | 19630 |  19630 |
|      |      day bed test |      |       |       |        |

原始响应

注意,由于您没有提供示例数据,我的列并不都是varchar,因此我使用CONCAT(…,“”)将每个非varchar列转换为varchar。请参阅此处的演示:

设置

CREATE TABLE Product 
  ([SrNo] int, [Description] varchar(100), [Size] varchar(100)
, [Specs] varchar(100), [Qnty] int, [Unit] varchar(5)
, [Rate] int, [Amount] int, [AreaOfUsage] varchar(100)) 
; 

INSERT INTO Product 
([SrNo], [Description],[Size], [Specs], [Qnty], [Unit], [Rate], [Amount], [AreaOfUsage]) 
VALUES 
 (1, 'Bed as per design and detail', 'test iu','7.00x4.00x2.00', 1, 'P.RFT', 35000, 35000, 'Bedroom'),
 (2, 'Coffee Table as per design and detail', 'shdjh test','7.00x3.00x2.00', 1, 'P.RFT', 12500, 12500, 'Hall'),
 (3, 'Day Bed as per design in white', 'day bed test','7.00x5.00x3.00', 1, 'P.RFT', 19630, 19630, 'Bedroom') 
 ; 
SELECT
      CASE WHEN rn = 2 THEN concat(t.srNo, '') ELSE '' END   srNo
    , t.Description
    , CASE WHEN rn = 2 THEN concat(t.Qnty, '') ELSE '' END   Qnty
    , CASE WHEN rn = 2 THEN concat(t.Unit, '') ELSE '' END   Unit
    , CASE WHEN rn = 2 THEN concat(t.Rate, '') ELSE '' END   Rate
    , CASE WHEN rn = 2 THEN concat(t.Amount, '') ELSE '' END Amount
FROM (
      SELECT
              p.*
            , row_number() over(partition by AreaOfUsage
                                order by srNo, rn) as grporder
      FROM (
          SELECT
                AreaOfUsage
              , srNo
              , ca.rn
              , ca.Description
              , Qnty
              , Unit
              , Rate
              , Amount
          FROM Product
          CROSS APPLY (
                      VALUES
                            (1, AreaOfUsage),
                            (2, Description),
                            (3, Specs),
                            (4, Size)
                      ) ca (rn, description)
            ) p
      ) t
WHERE (
           (grporder = 1 )
        OR (grporder > 1 and AreaOfUsage <> Description)
      )  
ORDER BY AreaOfUsage, grporder
| srNo |                           Description | Qnty |  Unit |  Rate | Amount |
|------|---------------------------------------|------|-------|-------|--------|
|      |                               Bedroom |      |       |       |        |
|    1 |          Bed as per design and detail |    1 | P.RFT | 35000 |  35000 |
|      |                        7.00x4.00x2.00 |      |       |       |        |
|      |                               test iu |      |       |       |        |
|    3 |        Day Bed as per design in white |    1 | P.RFT | 19630 |  19630 |
|      |                        7.00x5.00x3.00 |      |       |       |        |
|      |                          day bed test |      |       |       |        |
|      |                                  Hall |      |       |       |        |
|    2 | Coffee Table as per design and detail |    1 | P.RFT | 12500 |  12500 |
|      |                        7.00x3.00x2.00 |      |       |       |        |
|      |                            shdjh test |      |       |       |        |
| AreaOfUsage | srNo | rn |                           Description | Qnty |  Unit |  Rate | Amount | grporder |
|-------------|------|----|---------------------------------------|------|-------|-------|--------|----------|
|     Bedroom |    1 |  1 |                               Bedroom |    1 | P.RFT | 35000 |  35000 |        1 |
|     Bedroom |    1 |  2 |          Bed as per design and detail |    1 | P.RFT | 35000 |  35000 |        2 |
|     Bedroom |    1 |  3 |                        7.00x4.00x2.00 |    1 | P.RFT | 35000 |  35000 |        3 |
|     Bedroom |    1 |  4 |                               test iu |    1 | P.RFT | 35000 |  35000 |        4 |
|     Bedroom |    3 |  1 |                               Bedroom |    1 | P.RFT | 19630 |  19630 |        5 |
|     Bedroom |    3 |  2 |        Day Bed as per design in white |    1 | P.RFT | 19630 |  19630 |        6 |
|     Bedroom |    3 |  3 |                        7.00x5.00x3.00 |    1 | P.RFT | 19630 |  19630 |        7 |
|     Bedroom |    3 |  4 |                          day bed test |    1 | P.RFT | 19630 |  19630 |        8 |
|        Hall |    2 |  1 |                                  Hall |    1 | P.RFT | 12500 |  12500 |        1 |
|        Hall |    2 |  2 | Coffee Table as per design and detail |    1 | P.RFT | 12500 |  12500 |        2 |
|        Hall |    2 |  3 |                        7.00x3.00x2.00 |    1 | P.RFT | 12500 |  12500 |        3 |
|        Hall |    2 |  4 |                            shdjh test |    1 | P.RFT | 12500 |  12500 |        4 |
CREATE TABLE Ihavethistable
    ([SrNo] int, [Description] varchar(17), [Specs] varchar(12), [Qnty] int, [Unit] varchar(5), [Rate] int, [Amount] int, [AreaOfUsage] varchar(7))
;

INSERT INTO Ihavethistable
    ([SrNo], [Description], [Specs], [Qnty], [Unit], [Rate], [Amount], [AreaOfUsage])
VALUES
    (1, 'bed as per...', 'test iu', 1, 'P.RFT', 35000, 35000, 'Bedroom'),
    (2, 'coffee table...', 'shdjh test', 1, 'P.RFT', 12500, 12500, 'Hall'),
    (3, 'day bed as per...', 'day bed test', 1, 'P.RFT', 19630, 19630, 'Bedroom')
;
select
      case when ca.rn = 2 then concat(t.srNo,'') else '' end srNo
    , ca.description
    , case when ca.rn = 2 then concat(t.Qnty,'') else '' end Qnty
    , case when ca.rn = 2 then concat(t.Unit,'') else '' end Unit
    , case when ca.rn = 2 then concat(t.Rate,'') else '' end Rate
    , case when ca.rn = 2 then concat(t.Amount,'') else '' end Amount
from Ihavethistable t
CROSS APPLY (
  VALUES
     (1, AreaOfUsage),
     (2, Description),
     (3, Specs)
  ) ca (rn, description)
order by t.srNo, ca.rn
| srNo |       description | Qnty |  Unit |  Rate | Amount |
|------|-------------------|------|-------|-------|--------|
|      |           Bedroom |      |       |       |        |
|    1 |     bed as per... |    1 | P.RFT | 35000 |  35000 |
|      |           test iu |      |       |       |        |
|      |              Hall |      |       |       |        |
|    2 |   coffee table... |    1 | P.RFT | 12500 |  12500 |
|      |        shdjh test |      |       |       |        |
|      |           Bedroom |      |       |       |        |
|    3 | day bed as per... |    1 | P.RFT | 19630 |  19630 |
|      |      day bed test |      |       |       |        |
查询

CREATE TABLE Product 
  ([SrNo] int, [Description] varchar(100), [Size] varchar(100)
, [Specs] varchar(100), [Qnty] int, [Unit] varchar(5)
, [Rate] int, [Amount] int, [AreaOfUsage] varchar(100)) 
; 

INSERT INTO Product 
([SrNo], [Description],[Size], [Specs], [Qnty], [Unit], [Rate], [Amount], [AreaOfUsage]) 
VALUES 
 (1, 'Bed as per design and detail', 'test iu','7.00x4.00x2.00', 1, 'P.RFT', 35000, 35000, 'Bedroom'),
 (2, 'Coffee Table as per design and detail', 'shdjh test','7.00x3.00x2.00', 1, 'P.RFT', 12500, 12500, 'Hall'),
 (3, 'Day Bed as per design in white', 'day bed test','7.00x5.00x3.00', 1, 'P.RFT', 19630, 19630, 'Bedroom') 
 ; 
SELECT
      CASE WHEN rn = 2 THEN concat(t.srNo, '') ELSE '' END   srNo
    , t.Description
    , CASE WHEN rn = 2 THEN concat(t.Qnty, '') ELSE '' END   Qnty
    , CASE WHEN rn = 2 THEN concat(t.Unit, '') ELSE '' END   Unit
    , CASE WHEN rn = 2 THEN concat(t.Rate, '') ELSE '' END   Rate
    , CASE WHEN rn = 2 THEN concat(t.Amount, '') ELSE '' END Amount
FROM (
      SELECT
              p.*
            , row_number() over(partition by AreaOfUsage
                                order by srNo, rn) as grporder
      FROM (
          SELECT
                AreaOfUsage
              , srNo
              , ca.rn
              , ca.Description
              , Qnty
              , Unit
              , Rate
              , Amount
          FROM Product
          CROSS APPLY (
                      VALUES
                            (1, AreaOfUsage),
                            (2, Description),
                            (3, Specs),
                            (4, Size)
                      ) ca (rn, description)
            ) p
      ) t
WHERE (
           (grporder = 1 )
        OR (grporder > 1 and AreaOfUsage <> Description)
      )  
ORDER BY AreaOfUsage, grporder
| srNo |                           Description | Qnty |  Unit |  Rate | Amount |
|------|---------------------------------------|------|-------|-------|--------|
|      |                               Bedroom |      |       |       |        |
|    1 |          Bed as per design and detail |    1 | P.RFT | 35000 |  35000 |
|      |                        7.00x4.00x2.00 |      |       |       |        |
|      |                               test iu |      |       |       |        |
|    3 |        Day Bed as per design in white |    1 | P.RFT | 19630 |  19630 |
|      |                        7.00x5.00x3.00 |      |       |       |        |
|      |                          day bed test |      |       |       |        |
|      |                                  Hall |      |       |       |        |
|    2 | Coffee Table as per design and detail |    1 | P.RFT | 12500 |  12500 |
|      |                        7.00x3.00x2.00 |      |       |       |        |
|      |                            shdjh test |      |       |       |        |
| AreaOfUsage | srNo | rn |                           Description | Qnty |  Unit |  Rate | Amount | grporder |
|-------------|------|----|---------------------------------------|------|-------|-------|--------|----------|
|     Bedroom |    1 |  1 |                               Bedroom |    1 | P.RFT | 35000 |  35000 |        1 |
|     Bedroom |    1 |  2 |          Bed as per design and detail |    1 | P.RFT | 35000 |  35000 |        2 |
|     Bedroom |    1 |  3 |                        7.00x4.00x2.00 |    1 | P.RFT | 35000 |  35000 |        3 |
|     Bedroom |    1 |  4 |                               test iu |    1 | P.RFT | 35000 |  35000 |        4 |
|     Bedroom |    3 |  1 |                               Bedroom |    1 | P.RFT | 19630 |  19630 |        5 |
|     Bedroom |    3 |  2 |        Day Bed as per design in white |    1 | P.RFT | 19630 |  19630 |        6 |
|     Bedroom |    3 |  3 |                        7.00x5.00x3.00 |    1 | P.RFT | 19630 |  19630 |        7 |
|     Bedroom |    3 |  4 |                          day bed test |    1 | P.RFT | 19630 |  19630 |        8 |
|        Hall |    2 |  1 |                                  Hall |    1 | P.RFT | 12500 |  12500 |        1 |
|        Hall |    2 |  2 | Coffee Table as per design and detail |    1 | P.RFT | 12500 |  12500 |        2 |
|        Hall |    2 |  3 |                        7.00x3.00x2.00 |    1 | P.RFT | 12500 |  12500 |        3 |
|        Hall |    2 |  4 |                            shdjh test |    1 | P.RFT | 12500 |  12500 |        4 |
CREATE TABLE Ihavethistable
    ([SrNo] int, [Description] varchar(17), [Specs] varchar(12), [Qnty] int, [Unit] varchar(5), [Rate] int, [Amount] int, [AreaOfUsage] varchar(7))
;

INSERT INTO Ihavethistable
    ([SrNo], [Description], [Specs], [Qnty], [Unit], [Rate], [Amount], [AreaOfUsage])
VALUES
    (1, 'bed as per...', 'test iu', 1, 'P.RFT', 35000, 35000, 'Bedroom'),
    (2, 'coffee table...', 'shdjh test', 1, 'P.RFT', 12500, 12500, 'Hall'),
    (3, 'day bed as per...', 'day bed test', 1, 'P.RFT', 19630, 19630, 'Bedroom')
;
select
      case when ca.rn = 2 then concat(t.srNo,'') else '' end srNo
    , ca.description
    , case when ca.rn = 2 then concat(t.Qnty,'') else '' end Qnty
    , case when ca.rn = 2 then concat(t.Unit,'') else '' end Unit
    , case when ca.rn = 2 then concat(t.Rate,'') else '' end Rate
    , case when ca.rn = 2 then concat(t.Amount,'') else '' end Amount
from Ihavethistable t
CROSS APPLY (
  VALUES
     (1, AreaOfUsage),
     (2, Description),
     (3, Specs)
  ) ca (rn, description)
order by t.srNo, ca.rn
| srNo |       description | Qnty |  Unit |  Rate | Amount |
|------|-------------------|------|-------|-------|--------|
|      |           Bedroom |      |       |       |        |
|    1 |     bed as per... |    1 | P.RFT | 35000 |  35000 |
|      |           test iu |      |       |       |        |
|      |              Hall |      |       |       |        |
|    2 |   coffee table... |    1 | P.RFT | 12500 |  12500 |
|      |        shdjh test |      |       |       |        |
|      |           Bedroom |      |       |       |        |
|    3 | day bed as per... |    1 | P.RFT | 19630 |  19630 |
|      |      day bed test |      |       |       |        |

CREATE TABLE Product 
  ([SrNo] int, [Description] varchar(100), [Size] varchar(100)
, [Specs] varchar(100), [Qnty] int, [Unit] varchar(5)
, [Rate] int, [Amount] int, [AreaOfUsage] varchar(100)) 
; 

INSERT INTO Product 
([SrNo], [Description],[Size], [Specs], [Qnty], [Unit], [Rate], [Amount], [AreaOfUsage]) 
VALUES 
 (1, 'Bed as per design and detail', 'test iu','7.00x4.00x2.00', 1, 'P.RFT', 35000, 35000, 'Bedroom'),
 (2, 'Coffee Table as per design and detail', 'shdjh test','7.00x3.00x2.00', 1, 'P.RFT', 12500, 12500, 'Hall'),
 (3, 'Day Bed as per design in white', 'day bed test','7.00x5.00x3.00', 1, 'P.RFT', 19630, 19630, 'Bedroom') 
 ; 
SELECT
      CASE WHEN rn = 2 THEN concat(t.srNo, '') ELSE '' END   srNo
    , t.Description
    , CASE WHEN rn = 2 THEN concat(t.Qnty, '') ELSE '' END   Qnty
    , CASE WHEN rn = 2 THEN concat(t.Unit, '') ELSE '' END   Unit
    , CASE WHEN rn = 2 THEN concat(t.Rate, '') ELSE '' END   Rate
    , CASE WHEN rn = 2 THEN concat(t.Amount, '') ELSE '' END Amount
FROM (
      SELECT
              p.*
            , row_number() over(partition by AreaOfUsage
                                order by srNo, rn) as grporder
      FROM (
          SELECT
                AreaOfUsage
              , srNo
              , ca.rn
              , ca.Description
              , Qnty
              , Unit
              , Rate
              , Amount
          FROM Product
          CROSS APPLY (
                      VALUES
                            (1, AreaOfUsage),
                            (2, Description),
                            (3, Specs),
                            (4, Size)
                      ) ca (rn, description)
            ) p
      ) t
WHERE (
           (grporder = 1 )
        OR (grporder > 1 and AreaOfUsage <> Description)
      )  
ORDER BY AreaOfUsage, grporder
| srNo |                           Description | Qnty |  Unit |  Rate | Amount |
|------|---------------------------------------|------|-------|-------|--------|
|      |                               Bedroom |      |       |       |        |
|    1 |          Bed as per design and detail |    1 | P.RFT | 35000 |  35000 |
|      |                        7.00x4.00x2.00 |      |       |       |        |
|      |                               test iu |      |       |       |        |
|    3 |        Day Bed as per design in white |    1 | P.RFT | 19630 |  19630 |
|      |                        7.00x5.00x3.00 |      |       |       |        |
|      |                          day bed test |      |       |       |        |
|      |                                  Hall |      |       |       |        |
|    2 | Coffee Table as per design and detail |    1 | P.RFT | 12500 |  12500 |
|      |                        7.00x3.00x2.00 |      |       |       |        |
|      |                            shdjh test |      |       |       |        |
| AreaOfUsage | srNo | rn |                           Description | Qnty |  Unit |  Rate | Amount | grporder |
|-------------|------|----|---------------------------------------|------|-------|-------|--------|----------|
|     Bedroom |    1 |  1 |                               Bedroom |    1 | P.RFT | 35000 |  35000 |        1 |
|     Bedroom |    1 |  2 |          Bed as per design and detail |    1 | P.RFT | 35000 |  35000 |        2 |
|     Bedroom |    1 |  3 |                        7.00x4.00x2.00 |    1 | P.RFT | 35000 |  35000 |        3 |
|     Bedroom |    1 |  4 |                               test iu |    1 | P.RFT | 35000 |  35000 |        4 |
|     Bedroom |    3 |  1 |                               Bedroom |    1 | P.RFT | 19630 |  19630 |        5 |
|     Bedroom |    3 |  2 |        Day Bed as per design in white |    1 | P.RFT | 19630 |  19630 |        6 |
|     Bedroom |    3 |  3 |                        7.00x5.00x3.00 |    1 | P.RFT | 19630 |  19630 |        7 |
|     Bedroom |    3 |  4 |                          day bed test |    1 | P.RFT | 19630 |  19630 |        8 |
|        Hall |    2 |  1 |                                  Hall |    1 | P.RFT | 12500 |  12500 |        1 |
|        Hall |    2 |  2 | Coffee Table as per design and detail |    1 | P.RFT | 12500 |  12500 |        2 |
|        Hall |    2 |  3 |                        7.00x3.00x2.00 |    1 | P.RFT | 12500 |  12500 |        3 |
|        Hall |    2 |  4 |                            shdjh test |    1 | P.RFT | 12500 |  12500 |        4 |
CREATE TABLE Ihavethistable
    ([SrNo] int, [Description] varchar(17), [Specs] varchar(12), [Qnty] int, [Unit] varchar(5), [Rate] int, [Amount] int, [AreaOfUsage] varchar(7))
;

INSERT INTO Ihavethistable
    ([SrNo], [Description], [Specs], [Qnty], [Unit], [Rate], [Amount], [AreaOfUsage])
VALUES
    (1, 'bed as per...', 'test iu', 1, 'P.RFT', 35000, 35000, 'Bedroom'),
    (2, 'coffee table...', 'shdjh test', 1, 'P.RFT', 12500, 12500, 'Hall'),
    (3, 'day bed as per...', 'day bed test', 1, 'P.RFT', 19630, 19630, 'Bedroom')
;
select
      case when ca.rn = 2 then concat(t.srNo,'') else '' end srNo
    , ca.description
    , case when ca.rn = 2 then concat(t.Qnty,'') else '' end Qnty
    , case when ca.rn = 2 then concat(t.Unit,'') else '' end Unit
    , case when ca.rn = 2 then concat(t.Rate,'') else '' end Rate
    , case when ca.rn = 2 then concat(t.Amount,'') else '' end Amount
from Ihavethistable t
CROSS APPLY (
  VALUES
     (1, AreaOfUsage),
     (2, Description),
     (3, Specs)
  ) ca (rn, description)
order by t.srNo, ca.rn
| srNo |       description | Qnty |  Unit |  Rate | Amount |
|------|-------------------|------|-------|-------|--------|
|      |           Bedroom |      |       |       |        |
|    1 |     bed as per... |    1 | P.RFT | 35000 |  35000 |
|      |           test iu |      |       |       |        |
|      |              Hall |      |       |       |        |
|    2 |   coffee table... |    1 | P.RFT | 12500 |  12500 |
|      |        shdjh test |      |       |       |        |
|      |           Bedroom |      |       |       |        |
|    3 | day bed as per... |    1 | P.RFT | 19630 |  19630 |
|      |      day bed test |      |       |       |        |

1。SQL不是“报表编写器”,这正是您所期望的。
通常,此级别的表示在“表示层”或“报表编写器”2中进行。请提供样本数据作为文本,我们可以重复使用(或作为插入)请不要使用数据的图像“大小”来自哪里?这不是我想要的。请查看我所需的输出。需要分组展示产品,例如,所有卧室产品一个接一个,然后所有大厅产品一个接一个,等等。这个顺序对你来说很明显,但对我来说不是。(预期结果图像中包含样本数据图像中不存在的数据。)此外,我必须创建样本数据,我不擅长转录。将来,您应该以我们可以重用的形式提供示例数据。创建表产品([SrNo]int、[Description]varchar(17)、[Size]varchar(100)、[Specs]varchar(12)、[Qnty]int、[Unit]varchar(5)、[Rate]int、[Amount]int、[AreaOfUsage]varchar(7));在产品中插入([SrNo]、[Description]、[Size]、[Specs]、[Qnty]、[Unit]、[Rate]、[Amount]、[Area of Sage])值(1、“床符合设计和详图”、“测试iu”、“7.00x4.00x2.00”、“1”、“P.RFT”、“35000”、“35000”、“35000”、“卧室”)、(2、“咖啡桌符合设计和详图”、“shdjh测试”、“7.00x3.00x2.00”、“1”、“P.RFT”、“12500”、“大厅”),(3,'白色设计的日间床','日间床测试','7.00x5.00x3.00',1,'P.RFT',19630,19630,'卧室');这是带有数据的表格。我们只需要使用此表格来获得所需的输出。