using COALESCE() 分組結果顯示成一條記錄

來源:互聯網
上載者:User

1 將分組結果顯示成一條記錄,並且加入分隔字元 “,”

CREATE               PROCEDURE dbo.RestaurantInfo_KindCount
AS
BEGIN TRAN
declare @Counts int
declare @ArraryCount Nvarchar(100)
declare @ArraryID Nvarchar(100)

      select @ArraryCount=''
      select @ArraryID=''
      select @ArraryCount=@ArraryCount+COALESCE(Convert(Nvarchar,Count(1)) + ',', ' '),@ArraryID=@ArraryID+COALESCE(Type + ',', ' ') from T_Ad_RestaurantInfo group by Type
      select @ArraryCount as ArraryCount,@ArraryID as ArraryID

IF @@ERROR!=0
    BEGIN
        ROLLBACK TRAN
        RAISERROR 20000 'p_soofan_Object_ProjectList: Error'
        RETURN(1)
    END
COMMIT TRAN

-----------------------------------------------

Examples from SQL Server Books online

In this example, the wages table is shown to include three columns with information about an employee's yearly wage: hourly_wage, salary, and commission. However, an employee receives only one type of pay. To determine the total amount paid to all employees, use the COALESCE function to receive only the nonnull value found in hourly_wage, salary, and commission.

SET NOCOUNT ONGOUSE masterIF EXISTS (SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES       WHERE TABLE_NAME = 'wages')   DROP TABLE wagesGOCREATE TABLE wages(   emp_id      tinyint    identity,   hourly_wage   decimal   NULL,   salary      decimal    NULL,   commission   decimal   NULL,   num_sales   tinyint   NULL)GOINSERT wages VALUES(10.00, NULL, NULL, NULL)INSERT wages VALUES(20.00, NULL, NULL, NULL)INSERT wages VALUES(30.00, NULL, NULL, NULL)INSERT wages VALUES(40.00, NULL, NULL, NULL)INSERT wages VALUES(NULL, 10000.00, NULL, NULL)INSERT wages VALUES(NULL, 20000.00, NULL, NULL)INSERT wages VALUES(NULL, 30000.00, NULL, NULL)INSERT wages VALUES(NULL, 40000.00, NULL, NULL)INSERT wages VALUES(NULL, NULL, 15000, 3)INSERT wages VALUES(NULL, NULL, 25000, 2)INSERT wages VALUES(NULL, NULL, 20000, 6)INSERT wages VALUES(NULL, NULL, 14000, 4)GOSET NOCOUNT OFFGOSELECT CAST(COALESCE(hourly_wage * 40 * 52,    salary,    commission * num_sales) AS money) AS 'Total Salary' FROM wagesGO

Here is the result set:

Total Salary ------------ 20800.000041600.000062400.000083200.000010000.000020000.000030000.000040000.000045000.000050000.0000120000.000056000.0000

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.