How to solve this Query? - Bandi

Posted by Jayakumars under Sql Server on 6/22/2015 | Points: 10 | Views : 317 | Status : [Member] [MVP] | Replies : 1
How to solve this Query?

CREATE TABLE tft
(
Id INT PRIMARY KEY IDENTITY(1,1),
Ename VARCHAR(20),
Etot DECIMAL(18,2),
ebaltot DECIMAL(18,2),
estat INT
)


INSERT INTO tft VALUES('AA1',100,10,1)
INSERT INTO tft VALUES('AA2',200,2500,2)
INSERT INTO tft VALUES('AA3',300,750,2)
INSERT INTO tft VALUES('AA4',480,0,1)


-- i tried here but not working this
SELECT * FROM tft
SELECT Id,Ename,SUM(Etot),SUM(ebaltot),estat FROM tft WHERE estat IN(1,2)
GROUP BY Id,Ename,Etot,ebaltot,estat

-- I need Myoutput like this
--Output - 1
Id EName Etot EbalTot estat
1 AA1,AA4 490 10 1
2 AA2,AA3 500 3250 2


--Output - 2
Id Etot EbalTot estat Id Etot EbalTot estat
1 490 10 1 2 500 3250 2

Mark as Answer if its helpful to you


Responses

Posted by: Bandi on: 6/22/2015 [Member] [MVP] Platinum | Points: 25

Up
0
Down
SELECT distinct STUFF((select  ','+Ename FROM TFT WHERE ESTAT=E.ESTAT FOR XML PATH('')),1,1,'') Ename
,SUM(Etot) OVER(PARTITION BY estat) Etot
,SUM(ebaltot) OVER(PARTITION BY estat) ebaltot
FROM tft E


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Jayakumars, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response