SQL - separate results of same column into different cloumns using multiple where statements - sql

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

Related

how to sum all columns with same id

Hi i have the following results:
i need to sum up all the items that have cashamount and same Payment code = 9
i have tried this query:
SELECT
CASE
WHEN StoreID = 1 THEN 'CWM'
WHEN StoreID = 2 THEN 'CWD' END as accountcode,
DocEntry,
PaymentCode,
case when PaymentCode <> 1 then paymentamount end as OtherPaymentAmount,
sum(case when PaymentCode = 1 then paymentamount end) as CashAmount,
tenders.sapcreditcard AS sapcreditcard,
--paymentamount,
-- sum (case when PaymentCode >= 1 then paymentamount else NULL end) as Total,
FileName, BPA_ProcessStatus, ERP_PaymentProcessed
FROM [Plu].[dbo].[payments_header] LEFT JOIN tenders ON payments_header.PaymentCode = tenders.postenderid
WHERE BPA_ProcessStatus='N' and ERP_PaymentProcessed='N'
group by PaymentCode, paymentamount, docentry, storeid,sapcreditcard, FileName, BPA_ProcessStatus,ERP_PaymentProcessed, cashamount
what im missing?
The GROUP BY clause lists all the columns you want to use to create separate groups. Yours is as follows...
GROUP BY
PaymentCode,
paymentamount,
docentry,
storeid,
sapcreditcard,
FileName,
BPA_ProcessStatus,
ERP_PaymentProcessed,
cashamount
Any time any of these are different, you'll get a separate row.
This means that your sum(case when PaymentCode = 1 then paymentamount end) ends up making very little sense.
Your GROUP BY says you want each different payment amount on a different row
Your SELECT says you want to aggregate multiple paymounts amounts
My best guess is that you want this...
SELECT
CASE
WHEN StoreID = 1 THEN 'CWM'
WHEN StoreID = 2 THEN 'CWD'
END
AS accountcode,
DocEntry,
PaymentCode,
SUM(CASE WHEN PaymentCode <> 1 THEN paymentamount END) AS OtherPaymentAmount,
SUM(CASE WHEN PaymentCode = 1 THEN paymentamount END) AS CashAmount,
tenders.sapcreditcard,
FileName,
BPA_ProcessStatus,
ERP_PaymentProcessed
FROM
[Plu].[dbo].[payments_header]
LEFT JOIN
tenders
ON payments_header.PaymentCode = tenders.postenderid
WHERE
BPA_ProcessStatus='N'
AND ERP_PaymentProcessed='N'
GROUP BY
CASE
WHEN StoreID = 1 THEN 'CWM'
WHEN StoreID = 2 THEN 'CWD'
END,
DocEntry,
PaymentCode,
tenders.sapcreditcard,
FileName,
BPA_ProcessStatus,
ERP_PaymentProcessed
Added SUM() around the OtherPaymentAmount calculations, to match CashAmount
Changed the GROUP BY to match the non-aggregated columns in the SELECT
NOTE: In all the places where you specify a column name, your should always qualify it with the source table's name.

SQL - Dividing aggregated fields, very new to SQL

I have list of line items from invoices with a field that indicates if a line was delivered or picked up. I need to find a percentage of delivered items from the total number of lines.
SALES_NBR | Total | Deliveryrate
1 = Delivered 0 = picked up from FULFILLMENT.
SELECT SALES_NBR,
COUNT (ITEMS) as Total,
SUM (case when FULFILLMENT = '1' then 1 else 0 end) as delivered,
(SELECT delivered/total) as Deliveryrate
FROM Invoice_table
WHERE STORE IN '0123'
And SALE_DATE >='2020-02-01'
And SALE_DATE <='2020-02-07'
Group By SALES_NBR, Deliveryrate;
My query executes but never finishes for some reason. Is there any easier way to do this? Fulfillment field does not contain any NULL values.
Any help would be appreciated.
I need to find a percentage of delivered items from the total number of lines.
The simplest method is to use avg():
select SALES_NBR,
avg(fulfillment) as delivered_ratio
from Invoice_table
where STORE = '0123' and
SALE_DATE >='2020-02-01' and
SALE_DATE <='2020-02-07'
group by SALES_NBR;
I'm not sure if the group by sales_nbr is needed.
If you want to get a "nice" query, you can use subqueries like this:
select
qry.*,
qry.delivered/qry.total as Deliveryrate
from (
select
SALES_NBR,
count(ITEMS) as Total,
sum(case when FULFILLMENT = '1' then 1 else 0 end) as delivered
from Invoice_table
where STORE IN '0123'
and SALE_DATE >='2020-02-01'
and SALE_DATE <='2020-02-07'
group by SALES_NBR
) qry;
But I think this one, even being ugglier, could perform faster:
select
SALES_NBR,
count(ITEMS) as Total,
sum(case when FULFILLMENT = '1' then 1 else 0 end) as delivered,
sum(case when FULFILLMENT = '1' then 1 else 0 end)/count(ITEMS) as Deliveryrate
from Invoice_table
where STORE IN '0123'
and SALE_DATE >='2020-02-01'
and SALE_DATE <='2020-02-07'
group by SALES_NBR

SQL select grouping and subtract

i have table named source table with data like this :
And i want to do query that subtract row with status plus and minus to be like this group by product name :
How to do that in SQL query? thanks!
Group by the product and then use a conditional SUM()
select product,
sum(case when status = 'plus' then total else 0 end) -
sum(case when status = 'minus' then total else 0 end) as total,
sum(case when status = 'plus' then amount else 0 end) -
sum(case when status = 'minus' then amount else 0 end) as amount
from your_table
group by product
There is another method using join, which works for the particular data you have provided (which has one "plus" and one "minus" row per product):
select tplus.product, (tplus.total - tminus.total) as total,
(tplus.amount - tminus.amount) as amount
from t tplus join
t tminus
on tplus.product = tminus.product and
tplus.status = 'plus' and
tplus.status = 'minus';
Both this and the aggregation query work well for the data you have provided. In other words, there are multiple ways to solve this problem (each has its strengths).
you can query as below:
select product , sum (case when [status] = 'minus' then -Total else Total end) as Total
, sum (case when [status] = 'minus' then -Amount else Amount end) as SumAmount
from yourproduct
group by product

how can i set the alias of column`s sum value based on another column in same table in sql?

I have a table of Accounts, having columns:
Acc_Id, Transaction_TypeId, Amount
I want to get result as When Transaction_TypeId = 1 then Sum of Amount as 'Total Advance Payments'.
Else when Transaction_typeId = 2 then Sum of Amount as 'Total Reciepts'
Here is my SQL query:
SELECT SUM(Amount) AS 'Sum' , Transaction_TypeId INTO #temp1 FROM AccountDetailTable WHERE Account_MasterId = 1 GROUP BY Transaction_TypeId
SELECT Sum as 'Total Advance' from #temp1 WHERE #temp1.Transaction_TypeId = 1;
SELECT Sum as 'Total Cash Receipts' FROM #temp1 WHERE #temp1.Transaction_TypeId = 2;
DROP TABLE #temp1;
but this query returns me two different result sets. How can i get the values in same result sets?
Use a CASE expression:
SELECT SUM(CASE WHEN Transaction_TypeId = 1 THEN somecolumn END) as [Total Advance],
SUM(CASE WHEN Transaction_TypeId = 2 THEN somecolumn END) as [Total Cash Receipts]
FROM #temp1;
You should use CASE EXPRESSION like this:
SELECT
sum(case when #temp1.Transaction_TypeId = 1 then amount else 0 end) as 'Total Advance',
sum(case when #temp1.Transaction_TypeId = 2 then amount else 0 end) as 'Total Cash Receipts'
FROM #temp1

Issues with MIN & MAX when using Case statement

I am trying to generate a summary report using various aggregate functions: MIN, MAX, SUM, etc. The issue I have is when I try to get a MIN and MAX of a field when I am also using the case statement. I am unable to get the MIN value of a field when I am using the case statement. I can best explain it with sample data and the sql statement:
Fields: AccountNumber, Symbol, TradeDate, TransactionType, Price, Quantity, Amount
Table: Trades
AccountNumber, Symbol, TradeDate, TransactionType, Price, Quantity, Amount
123,"XYZ",1/2/2011,"Buy",15,100,1500
123,"XYZ",1/2/2011,"Buy",10,50,500
123,"XYZ",1/2/2011,"Sell",20,100,2000
456,"ABC",1/3/2011,"Buy",10,20,200
456,"ABC",1/3/2011,"Buy",15,30,450
789,"DEF",1/4/2011,"Sell",30,100,3000
Query:
SELECT AccountNumber,
Symbol,
SUM(case when TransactionType = "Buy" then 1 else 0) as TotalBuys,
SUM(case when TransactionType = "Sell" then 1 else 0) as TotalSells,
MIN(case when TransactionType = "Buy" then Price else 0) as MinBuy,
MAX(case when TransactionType = "Buy" then Price else 0) as MaxBuy,
MIN(case when TransactionType = "Sell" then Price else 0) as MinSell,
MAX(case when TransactionType = "Sell" then Price else 0) as MaxSell,
MIN(Price) as MinPrice,
MAX(Price) as MaxPrice
FROM Trades
Group By AccountNumber, Symbol
What I am expecting is the following results:
AccountNumber, Symbol, TotalBuys, TotalSells, MinBuy, MaxBuy, MinSell, MaxSell, MinPrice, MaxPrice
123,"XYZ",2,1,10,15,20,20,10,20
456,"ABC",2,0,10,15,0,0,10,15
789,"DEF",0,1,0,0,30,30,30,30
However, I am getting the following results:
AccountNumber, Symbol, TotalBuys, TotalSells, MinBuy, MaxBuy, MinSell, MaxSell, MinPrice, MaxPrice
123,"XYZ",2,1,**0**,15,**0**,20,**0**,20
456,"ABC",2,0,10,15,0,0,10,15
789,"DEF",0,1,0,0,30,30,30,30
When there are two different TransactionTypes for each grouping, the Min fields (MinBuy,MinSell, and MinPrice) are coming out as 0 as opposed to what is expected. What am I doing wrong on the sql statement? Is there another way to get the desired results?
Min between 0 and a positive number is 0, you should change:
MIN(case when TransactionType = "Buy" then Price else 0)
by
MIN(case when TransactionType = "Buy" then Price else Null)
Null don't compute in an aggregation function.
Thats all.
Edited 6 years later:
As P5Coder says, it is enough without else clause, also I guess the end is mandatory on some database brands. Here it is:
MIN(case when TransactionType = "Buy" then Price end)