テストデータの作成。
CREATE TABLE #TableName (Code int, percentage int, nCollects int, nDels int, [day] varchar(3))
INSERT INTO #TableName (Code, percentage, nCollects, nDels, [day])
VALUES
('101','100','4','4','SUN')
,('102','100','4','4','SUN')
,('104','100','6','6','MON')
,('101','50','4','8','TUE')
,('102','50','4','8','TUE')
,('104','100','2','2','TUE')
,('105','50','1','2','TUE')
,('106','100','2','2','TUE')
,('106','50','2','4','WED')
あなたが望む答えを与えるT-SQL。
SELECT
a.Code
,ISNULL(SUM(CASE WHEN a.day = 'SUN' THEN a.nCollects END),0) SUN_nCollects
,ISNULL(SUM(CASE WHEN a.day = 'SUN' THEN a.nDels END),0) SUN_nDels
,CONVERT(int,(ISNULL(SUM(CASE WHEN a.day = 'SUN' THEN a.nCollects END),0)/ISNULL(SUM(CASE WHEN a.day = 'SUN' THEN a.nDels END),1))*100) [SUN%]
,ISNULL(SUM(CASE WHEN a.day = 'MON' THEN a.nCollects END),0) MON_nCollects
,ISNULL(SUM(CASE WHEN a.day = 'MON' THEN a.nDels END),0) MON_nDels
,CONVERT(int,(ISNULL(SUM(CASE WHEN a.day = 'MON' THEN a.nCollects END),0)/ISNULL(SUM(CASE WHEN a.day = 'MON' THEN a.nDels END),1))*100) [MON%]
,ISNULL(SUM(CASE WHEN a.day = 'TUE' THEN a.nCollects END),0) TUE_nCollects
,ISNULL(SUM(CASE WHEN a.day = 'TUE' THEN a.nDels END),0) TUE_nDels
,CONVERT(int,(ISNULL(SUM(CASE WHEN a.day = 'TUE' THEN a.nCollects END),0)/ISNULL(SUM(CASE WHEN a.day = 'TUE' THEN a.nDels END),1))*100) [TUE%]
,ISNULL(SUM(CASE WHEN a.day = 'WED' THEN a.nCollects END),0) WED_nCollects
,ISNULL(SUM(CASE WHEN a.day = 'WED' THEN a.nDels END),0) WED_nDels
,CONVERT(int,(ISNULL(SUM(CASE WHEN a.day = 'WED' THEN a.nCollects END),0)/ISNULL(SUM(CASE WHEN a.day = 'WED' THEN a.nDels END),1))*100) [WED%]
FROM #TableName a
GROUP BY a.Code
ロジックを変換して別の列に変換する必要がある場合は、入力してください。 – piyushj