RollUp ,CUBE Operator


CREATE TABLE InventoryInfo
(
SL_No INT IDENTITY PRIMARY KEY,
InventoryMasterName char(50),
StateId CHAR(10) FOREIGN KEY REFERENCES StateInfo(StateId),
InventoryDate DATE,
InventoryAmount MONEY
)

INSERT INTO InventoryInfo VALUES
('Nisha Singh' , 'R0001' , '01/02/2015',5000),

('Nisha Singh' , 'R0001' , '02/02/2015',3500),

('Nisha Singh' , 'R0001' , '03/02/2015',4000),

('Prince' , 'R0001' , '01/02/2015',5340),

('Prince' , 'R0001' , '02/02/2015',4300),

('Prince' , 'R0001' , '03/02/2015',5000),

('Prince' , 'R0002' , '01/02/2015',5980),

('Prince' , 'R0003' , '01/02/2015',8790),

('Prince' , 'R0004' , '01/02/2015',5000),

('Nisha Singh' , 'R0002' , '01/02/2015',9800),

('Nisha Singh' , 'R0003' , '01/02/2015',8760),

('Nisha Singh' , 'R0004' , '01/02/2015',34230),

('Nisha Singh' , 'R0004' , '02/02/2015',15000)




--Using RollUp Operator



SELECT InventoryMasterName , StateId , grouping_id( InventoryMasterName , StateId ) as 'g n',SUM(InventoryAmount) FROM InventoryInfo
GROUP BY ROLLUP(InventoryMasterName , StateId)

SELECT InventoryMasterName , StateId , InventoryDate,SUM(InventoryAmount) FROM InventoryInfo
GROUP BY ROLLUP(InventoryMasterName , StateId , InventoryDate)

--Using CUBE Operator


SELECT InventoryMasterName , StateId , grouping_id( InventoryMasterName , StateId ) as 'g n',SUM(InventoryAmount) FROM InventoryInfo
GROUP BY CUBE(InventoryMasterName , StateId)

SELECT InventoryMasterName , StateId , SUM(InventoryAmount) FROM InventoryInfo
GROUP BY CUBE(InventoryMasterName , StateId , InventoryDate)

SELECT InventoryMasterName , StateId ,InventoryDate,grouping_id( InventoryMasterName , StateId , InventoryDate) as 'group number', SUM(InventoryAmount) FROM InventoryInfo
GROUP BY CUBE(InventoryMasterName , StateId , InventoryDate)

order by 'group number',InventoryMasterName , StateId , InventoryDate

RollUp ,CUBE Operator Reviewed by Rupesh on 20:57 Rating: 5

No comments:

All Rights Reserved by Technology from Developers Eye © 2014 - 2015
Powered By Blogger, Designed by Aadics
Disclaimers:: The information provided within this blogsite is for general informational purposes only. While we try to keep the information up-to-date and correct, there are no representations or warranties, express or implied, about the completeness, accuracy, reliability, suitability or availability with respect to the information, products, services, or related graphics contained in this blogsite for any purpose.The author does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause.

Contact Form

Name

Email *

Message *

Powered by Blogger.