Abhijeet
Abhijeet

Reputation: 111

How can I fetch total of Qty of Buy and Sale in month of July in single sql query

I want to find total buy and sell in single row for the specific month. Below is the table This is the table


Upvotes: 1

Views: 84

Answers (1)

DineshDB
DineshDB

Reputation: 6193

Month() function will help to group the month values.

SELECT
YEAR(TrDate) SaleYear, 
MONTH(TrDate) SaleMonth,
   SUM(CASE WHEN trType = 'B' THEN Qty END)) as TotalBuy,
   SUM(CASE WHEN trType = 'S' THEN Qty END)) as TotalSale
   FROM TableName 
   GROUP BY YEAR(TrDate), MONTH(TrDate)

Upvotes: 3

Related Questions