I have a table in sql whose name is Trans which contain 4 columns
CustomerID int
TransDate datetime
TransType char(1)
Amount float
CustomerID can contain duplicate ID's but record contain different TransDate or TransType or Amount. TransType contain 'c' for credit and 'd' for debit.
I want to select CustomerId, year, CreditAmount and DebitAmount where CreditAmount is amount where TransType is 'c' and DebitAmount is amount where TransType is 'd'. Year is calculated from TransDate which contain date and time.
I think this is what you are looking for:
select customerid,
datepart(yyyy, transdate) as yr,
sum(case when transtype = 'c' then amount else 0 end) as creditamount,
sum(case when transtype = 'd' then amount else 0 end) as debitamount
from trans
group by customerid,
datepart(yyyy, transdate)
This does assume you want the total credits, for each year, and total debits, for each year, for each customer.
I want to select CustomerId, year, CreditAmount and DebitAmount where
CreditAmount is amount where TransType is 'c' and DreditAmount is
amount where TransType is 'd'. Year is calculated from TransDate which
contain date and time.
I am not sure that I understand you correctly, but you can check:
SELECT
CustomerId
,[Year] = YEAR(TransDate)
,[CreditAmount] = Amount
,[DebitAmount] = (SELECT Amount
FROM Trans t1
WHERE t1.CustomerId = t.CustomerId
AND t1.TransDate = t.TransDate
AND t1.TransType = 'd')
FROM Trans t
WHERE t.TransType = 'c';
I assumed that CustomerId/TranDate/TransType are unique.
Related
I want to find total buy and sell in single row for the specific month.
Below is the table
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)
I have no prior SQL knowledge. I am trying to get sum of all debitTransactions for different transactionType for each FolioNumber
I have the following code but it gives sum of all the debit transactions for each transaction type for each folio.
But all I want is sum of all gst(106) for each folio in one column, sum of all levy(105) for each folio in second column and sum of all roomRevenue(100) for each folio in third column.
---non working code
SELECT FolioNumber,
(Select SUM(debitTransactions) FROM HISTTRN WHERE transactionType = 106) as gst,
(Select SUM(debitTransactions) FROM HISTTRN WHERE transactionType = 105) as levy,
(Select SUM(debitTransactions) FROM HISTTRN WHERE transactionType = 100) as roomRevenue
FROM
HISTTRN
GROUP BY FolioNumber
ORDER BY FolioNumber
Following code does work if I only want one transactionType but I want 3 different transaction types
---working code
SELECT FolioNumber,
SUM(debitTransactions) as gst
FROM HISTTRN
WHERE transactionType = 106
GROUP BY FolioNumber
ORDER BY FolioNumber
Is there a possible way of doing that? Thanks
SELECT FolioNumber,
SUM(case when transactionType = 106 then debitTransactions else 0 end) as gst,
SUM(case when transactionType = 105 then debitTransactions else 0 end) as levy,
SUM(case when transactionType = 100 then debitTransactions else 0 end) as roomRevenue
FROM HISTTRN
GROUP BY FolioNumber
ORDER BY FolioNumber
I am working with a single warehouse table with columns like "2015 Fee", "2015 Revenue", "2016 Fee", "2016 Revenue", etc. I need to split these out into "Revenue", "Fee", and "Billed Year" in order to do some analysis. Many of the records have fee and revenue in multiple years.
The CASE statement I tried only pulls in the first year, but I need it to pull in all years.
Here's my case statements:
(CASE when 2015_revenue IS NOT NULL then 2015_revenue
when 2016_revenue_$ IS NOT NULL then 2016_revenue
END) as revenue,
(CASE when 2015_fee IS NOT NULL then 2015_fee
when 2016_fee IS NOT NULL then 2016_fee
END) as fee,
(CASE when 2015_revenue IS NOT NULL then '2015'
when 2015_fee IS NOT NULL then '2015'
when 2016_revenue IS NOT NULL then '2016'
when 2016_fee IS NOT NULL then '2016'
end) as bill_year
Any ideas?
It's not a CASE statement, it's creating a temp table with UNIONs and joining to that:
SELECT p1.[bunch of fields from table]
amts.fee
amts.revenue
amts.bill_year
FROM table p1
JOIN (SELECT id, 2015_revenue as revenue, 2015_fee as fee, '2015' as bill_year FROM table
UNION
SELECT id, 2016_revenue as revenue, 2016_fee as fee, '2016' as bill_year FROM table
U) amts on amts.id = p1.id;
I have a table 'transactions(txDate, amount, txType) where txType can "credit" or "debit".
I need to get an opening and closing balance when returning transactions between two dates.
The first row of the results should be the Opening Balance, then a list of all tx between the dates and the last row to be a Closing balance
Getting the list isn't a train smash but for the balances, I currently have the following
SELECT SUM(amount) AS [Opening Balance]
FROM
(
SELECT SUM([Amount]) amount
FROM [dbo].[Transaction]
WHERE [txDate] <= #startDate
AND [txType] = 'credit'
UNION ALL
SELECT 0 - SUM([Amount]) amount
FROM [dbo].[Transaction]
WHERE [TransactionDate] <= #startDate
AND [txType] = 'debit'
) Transactions
this gives a very big amount than what it should be.
And for the Closing balance, I have no idea how to go about it
You could use CASE in SUM
select sum(case when txType = 'credit' and transactionDate <= #startDate
then amount end) -
sum(case when txType = 'debit' and transactionDate <= #startDate
then amount end)[Opening Balance],
sum(case when txType = 'credit' and transactionDate <= #endDate
then amount end) -
sum(case when txType = 'debit' and transactionDate <= #endDate
then amount end)[Closing Balance]
from transaction
In this table we see different transaction in same date some of Transaction Dr and some transaction Cr. I need Cr - Dr as Transaction Amount per date.
Please see screenshot 1st Table
Result will be-
Result
you could use a case when and group by
select date, sum(case when drcr = 'CR' then amount
when drcr = 'DR' then -amount
else 0 end)
from my_table
group by date
as #scaisEdge but you need a sum.
select date, sum(case when drcr = 'CR' then amuont else (-1 * amount) end) as tran_amt
from my_table
group by date
This should be clear, but the way it works is to add credits and substract debits to the total for each date.