T-SQL: Rows to Columns - sql

I am a SQL beginner, so can anyone please help me with this?
I have a table like this
YearMonth | Customer | Currency | BusinessType | Amount
04-2020 | 123 | EUR | Budget | 500
04-2020 | 123 | EUR | Forecast | 300
04-2020 | 123 | EUR | Sales | 700
And now I need it like:
YearMonth | Customer | Currency | Amount Budget | Amount Forecast | Amount Sales
04-2020 | 123 | EUR | 500 | 300 | 700
Is something like this possible?
Thanks in advance for your help!

Use conditional aggregation:
select yearmonth, customer, currency,
sum(case when businesstype = 'Budget' then amount end) as budget,
sum(case when businesstype = 'Forecast' then amount end) as forecast,
sum(case when businesstype = 'Sales' then amount end) as sales
from t
group by yearmonth, customer, currency;

You can do aggregation :
select yearmonth, customer, currency,
sum(case when businesstype = 'budget' then amount else 0 end),
sum(case when businesstype = 'Forecast' then amount else 0 end),
sum(case when businesstype = 'Sales' then amount else 0 end)
from table t
group by yearmonth, customer, currency;

Also, you may want to add an "Other" to capture any new Business Types:
select yearmonth, customer, currency,
sum(case when businesstype = 'Budget' then amount end) as budget,
sum(case when businesstype = 'Forecast' then amount end) as forecast,
sum(case when businesstype = 'Sales' then amount end) as sales,
sum(case when businesstype not in ('Budget', 'Forecast', 'Sales') then amount end) as other
from t
group by yearmonth, customer, currency;

This type of requirements are usually resolved using T-SQL PIVOT relational operation (https://learn.microsoft.com/en-us/sql/t-sql/queries/from-using-pivot-and-unpivot?view=sql-server-ver15). For the above example I used the following query to achieve this:
-- Create a temporary table to store the data.
IF (OBJECT_ID('tempdb..##temp2') IS NOT NULL) DROP TABLE ##temp2
CREATE TABLE ##temp2 (Id int IDENTITY (1,1) PRIMARY KEY, YearMonth varchar(100), Customer int, Currency varchar(100), BusinessType varchar(100), Amount int)
INSERT ##temp2
VALUES
('04-2020', 123,'EUR','Sales', 700),
('04-2020', 123,'EUR','Budget', 500),
('04-2020', 123,'EUR','Forecast', 300)
-- Using PIVOT allows you to move rows into columns
SELECT pivot_table.YearMonth,
pivot_table.Customer,
pivot_table.Currency,
[Amount Forecast] = pivot_table.Forecast,
[Amount Budget] = pivot_table.Budget,
[Amount Sales] = pivot_table.Sales
FROM
(
SELECT YearMonth,
Customer,
Currency,
BusinessType,
Amount
FROM ##temp2
) t
PIVOT
(
SUM(Amount)
FOR BusinessType IN ([Sales], [Budget], [Forecast])
) AS pivot_table;

Related

SQL query for multiple where clause

I have a table named tbl_transactions with columns
Category (string)
for_month (string)
Vehicle_No (string)
amount (float)
As shown below in the
.
Is it possible to get the total amount for each month and the result should be category wise something like
You can use the conditional aggregation as follows:
SELECT CATEGORY,
SUM(CASE WHEN FOR_MONTH = 'April' THEN AMOUNT END) AS APRIL_AMOUNT,
SUM(CASE WHEN FOR_MONTH = 'May' THEN AMOUNT END) AS MAY_AMOUNT,
SUM(CASE WHEN FOR_MONTH = 'June' THEN AMOUNT END) AS JUNE_AMOUNT
FROM YOUR_tABLE T
WHERE FOR_MONTH IN ('April', 'May', 'June')
GROUP BY CATEGORY
Using PIVOT
DECLARE #t TABLE(Cat VARCHAR(MAX), Mon VARCHAR(MAX),Amt INT)
INSERT INTO #T VALUES('Registration','April',850),
('Registration','April',450),
('Registration','April',295)
SELECT Cat, [April] AS [Total Amt in Apr],
[May] AS [Total Amt in May],
[June] AS [Total Amt in June]FROM
(
SELECT
Cat,
Mon,
Amt
FROM
#T
) t
PIVOT(
SUM(Amt)
FOR Mon IN (
[April],
[May],
[June]
)
) AS pivot_table;
Results:
Cat Total Amt in Apr Total Amt in May Total Amt in June
Registration 1595 NULL NULL

Multiple SELECT Statement using One Table of SQL

Hope u all fine.
Actually I'm working on a Project and need to calculate daily TOTAL_SALE and TOTAL_CASH and TOTAL_CREDIT ....
Following is my Query but my Query Gives Overall SUM of Payments in TOTAL_CASH and in TOTAL_CREDIT as it has to be like this ..
+------------+------------+--------------+
| TOTAL_SALE | TOTAL_CASH | TOTAL_CREDIT |
+------------+------------+--------------+
| 1000 | 250 | 750 |
+------------+------------+--------------+
Following is my SQL Query :-
SELECT
sss.creation_date , SUM(sss.payment_due) as 'Total Sale',
(SELECT SUM(s.payment_due) FROM sale s WHERE s.payment_method LIKE '%Cash_%' AND s.shop='10'
AND s.DateNum >='20181201000000' AND s.DateNum <='20181231235959' GROUP BY s.creation_date)as 'Total Cash' ,
(SELECT SUM(ss.payment_due) FROM sale ss WHERE ss.payment_method LIKE '%cCredit_%' AND ss.shop='10'
AND ss.DateNum >='20181201000000' AND ss.DateNum <='20181231235959' GROUP BY ss.creation_date)as 'Total Credit' ,
SUM(discount) as 'Total Discount' , SUM(number_of_item_purchased) as 'Total Sold'
FROM Sale sss
WHERE sss.shop='10' AND sss.DateNum >='20181201000000' AND sss.DateNum <='20181231235959'
GROUP BY sss.creation_date
You can try below using conditional aggregation
SELECT
SUM(payment_due) as 'Total Sale',
SUM(case when payment_method LIKE '%Cash_%' then s.payment_due end) as 'Total Cash' ,
SUM(case when payment_method LIKE '%cCredit_%' then payment_due end) as 'Total Credit',
SUM(discount) as 'Total Discount' ,
SUM(number_of_item_purchased) as 'Total Sold'
FROM Sale
WHERE shop='10' AND DateNum >='20181201000000' AND DateNum <='20181231235959'

SQL PIVOT without aggregate columns

create table Product_Price
(
id int,
dt date,
SellerName varchar(20),
Product varchar(10),
ShippingTime varchar(20),
Price money
)
insert into Product_Price values (1, '2012-01-16','Sears','AA','2 days',32)
insert into Product_Price values (2, '2012-01-16','Amazon', 'AA','4 days', 40)
insert into Product_Price values (3, '2012-01-16','eBay','AA','1 days', 27)
insert into Product_Price values (4, '2012-01-16','Walmart','AA','Same day', 28)
insert into Product_Price values (5, '2012-01-16','Target', 'AA','3-4 days', 29)
insert into Product_Price values (6, '2012-01-16','Flipcart','AA',NULL, 30)
select *
from
(select dt, product, SellerName, sum(price) as price
from product_price group by dt, product, SellerName) t1
pivot (sum(price) for SellerName in ([amazon],[ebay]))as bob
)
I want 2 more columns in output (One is AmazonShippinTime another is eBayshippintime). How can I get these? Fiddle : http://sqlfiddle.com/#!3/2210d/1
Since you need to pivot on two columns and use different aggregates on both columns, I would use aggregate functions with a CASE expression to get the result:
select
dt,
product,
sum(case when SellerName = 'amazon' then price else 0 end) AmazonPrice,
max(case when SellerName = 'amazon' then ShippingTime end) AmazonShippingTime,
sum(case when SellerName = 'ebay' then price else 0 end) ebayPrice,
max(case when SellerName = 'ebay' then ShippingTime end) ebayShippingTime
from product_price
group by dt, product;
See SQL Fiddle with Demo. This gives a result:
| DT | PRODUCT | AMAZONPRICE | AMAZONSHIPPINGTIME | EBAYPRICE | EBAYSHIPPINGTIME |
|------------|---------|-------------|--------------------|-----------|------------------|
| 2012-01-16 | AA | 40 | 4 days | 27 | 1 days |

Calculation of balance after each transaction

I have table like this:
cust_id acc_no trans_id trans_type amount
1111 1001 10 credit 2000.0
1111 1001 11 credit 1000.0
1111 1001 12 debit 1000.0
2222 1002 13 credit 2000.0
2222 1002 14 debit 1000.0
I want a Hive query or sql query for every transaction done by a customer the balance should be calculated so.
I want output as follows:
cust_id acc_no trans_id trans_type amount balance
1111.0 1001.0 10.0 credit 2000.0 2000.0
1111.0 1001.0 11.0 credit 1000.0 3000.0
1111.0 1001.0 12.0 debit 1000.0 2000.0
2222.0 1002.0 13.0 credit 2000.0 2000.0
2222.0 1002.0 14.0 debit 1000.0 1000.0
I've tried
SELECT *
FROM (SELECT cust_id,
acc_no,
trans_id,
trans_type,
amount,
CASE
WHEN Trim(trans_type) = 'credit' THEN ball =
Trim(bal) + Trim(amt)
ELSE ball = Trim(bal) - Trim(amt)
end
FROM ban) l;
This query will do the trick :
SELECT t1.cust_id,t1.acc_no,t1.trans_id,t1.trans_type,t1.amount,
sum(t2.amount*case when t2.trans_type = 'credit' then 1
else -1 end) as balance
FROM Table1 t1
INNER JOIN Table1 t2 ON t1.cust_id = t2.cust_id AND
t1.acc_no = t2.acc_no AND
t1.trans_id >= t2.trans_id
GROUP BY t1.cust_id,t1.acc_no,t1.trans_id,t1.trans_type,t1.amount
See SQLFIDDLE : http://www.sqlfiddle.com/#!2/3b5d8/15/0
EDIT :
SQL Fiddle
MySQL 5.5.32 Schema Setup:
CREATE TABLE Table1
(`cust_id` int, `acc_no` int, `trans_id` int,
`trans_type` varchar(6), `amount` int)
;
INSERT INTO Table1
(`cust_id`, `acc_no`, `trans_id`, `trans_type`, `amount`)
VALUES
(1111, 1001, 10, 'credit', 2000.0),
(1111, 1001, 11, 'credit', 1000.0),
(1111, 1001, 12, 'debit', 1000.0),
(2222, 1002, 13, 'credit', 2000.0),
(2222, 1002, 14, 'debit', 1000.0)
;
Query 1:
SELECT t1.cust_id,t1.acc_no,t1.trans_id,t1.trans_type,t1.amount,
sum(t2.amount*case when t2.trans_type = 'credit' then 1
else -1 end) as balance
FROM Table1 t1
INNER JOIN Table1 t2 ON t1.cust_id = t2.cust_id AND
t1.acc_no = t2.acc_no AND
t1.trans_id >= t2.trans_id
GROUP BY t1.cust_id,t1.acc_no,t1.trans_id,t1.trans_type,t1.amount
Results:
| CUST_ID | ACC_NO | TRANS_ID | TRANS_TYPE | AMOUNT | BALANCE |
|---------|--------|----------|------------|--------|---------|
| 1111 | 1001 | 10 | credit | 2000 | 2000 |
| 1111 | 1001 | 11 | credit | 1000 | 3000 |
| 1111 | 1001 | 12 | debit | 1000 | 2000 |
| 2222 | 1002 | 13 | credit | 2000 | 2000 |
| 2222 | 1002 | 14 | debit | 1000 | 1000 |
A simple solution is to quantify each transaction (- or +) based on trans_type and then get cumulative sum using window function .
SELECT cust_id,
acc_no,
trans_id,
trans_type,
amount,
Sum (real_amount)
OVER (ORDER BY cust_id) AS balance
FROM (SELECT cust_id,
acc_no,
trans_id,
trans_type,
amount,
( CASE trans_type
WHEN 'credit' THEN amount
WHEN 'debit' THEN amount *- 1
END ) AS real_amount
FROM test) t
You could do this easily through a View, calculating this directly on the table is possible but leads to performance and scalability issues (the database will slow down as the table grows). By using a View the calculation is performed as-needed; if you index the view you can keep the balances up to date without impacting the performance of the transaction table.
If you really insist on it being in the transaction table itself you could possibly use a calculated column which runs a user-defined function to determine the current balance. However this will depend largey on the specific SQL backend you're using.
Here's a basic SELECT Statement which calculates the current balance by Account:
select
acc_no,
sum(case trans_type
when 'credit' then amount
when 'debit' then amount * -1
end) as Amount
from Transactions
group by acc_no
You can use window function:
select cust_id,
acc_no, trans_id, trans_type, amount,
sum(pre_balance) over (partition by cust_id order by trans_id) as balance
from
(select cust_id, acc_no, trans_id, trans_type,
amount,
amount as pre_balance from test
where trans_type = 'credit'
union
select cust_id, acc_no, trans_id, trans_type,
amount, -amount as pre_balance from
test where trans_type = 'debit'
order by trans_id) as sub;
with current_balances as (
SELECT
id,
user_id,
SUM(amount) OVER (PARTITION BY user_id ORDER BY created ASC) as current_balance
FROM payments_transaction pt
ORDER BY created DESC
)
SELECT
pt.id,
amount,
pt.user_id,
cb.current_balance as running_balance
FROM
payments_transaction pt
INNER JOIN
current_balances cb
ON pt.id = cb.id
ORDER BY created DESC
LIMIT 10;
This will work very efficiently for big returns, and won't break on filtering or limiting. Please note that if you select only for one user or a subset of them, provide user_id filter in both current_balances cte, and the main select to omit whole table scan.
Table (Transaction)
-
"id" "amount" "is_credit"
1 10000 1
2 2000 0
3 5000 1
Query :
SELECT *
FROM (
SELECT id, amount, SUM(CASE When is_credit=1 Then amount Else -amount End) OVER (ORDER BY id) AS balance
FROM `Transaction`
GROUP BY id, amount
)
ORDER BY id ;
Output :
"id" "amount" "is_credit" "balance"
1 10000 1 10000
2 2000 0 8000
3 5000 1 13000

Combine data from a table to one row T-SQL

I have a table in #SQL server 2008 that has transaction data. The table looks like this. I would like to have this in a sql statement.
TransactionId|TransactionDate|TransactionType|Amount|Balance|UserId
The transaction type can be one of four types, Deposit, Withdrawals, Profit and Stake. I give an example how it can look like in the transaction table. The balance is the Sum of amount column.
TransactionId|TransactionDate|TransactionType|Amount|Balance|UserId
1| 2013-03-25| Deposit| 150| 150| 1
2| 2013-03-27| Stake| -20| 130| 1
3| 2013-03-28| Profit | 1500| 1630| 1
4 | 2013-03-29| Withdrawals| -700| 930| 1
5| 2013-03-29| Stake | -230 | 700 | 1
6| 2013-04-04| Stake| -150 | 550| 1
7| 2013-04-06| Stake | -150 | 400| 1
What I want now is to get a select statement that gives me all data grouped by week. The result should look like this.
Week|Deposit|Withdrawals|Stake|Profit|Balance|Year
13 | 150| -700 | -250 | 1500 | 700 | 2013
14 | 0 | 0 | -300| 0 | 400 | 2013
I have also problem with the weeks... I live in Europe an my first day in a week is monday. I have a solution for that but around the end of a year I get sometimes week 54 but there are only 52 weeks in a year...
I hope someone can help me out.
This is what I have so far.
SELECT transactionid,
transactiondate,
transactiontype,
amount,
(SELECT Sum(amount)
FROM transactions AS trans_
WHERE trans_.transactiondate <= trans.transactiondate
AND userid = 1) AS Balance,
userid,
Datepart(week, transactiondate) AS Week,
Datepart(year, transactiondate) AS Year
FROM transactions trans
WHERE userid = 1
ORDER BY transactiondate DESC,
transactionid DESC
Here's sample data and my query on sql-fiddle: http://www.sqlfiddle.com/#!3/79d65/92/0
In order to transform the data from the rows into columns, you will want to use the PIVOT function.
You did not specify what balance value you want to return but based on the final result, it looks like you want the final balance to be the value associated with the last transaction date for each day. If that is not correct, then please clarify what the logic should be.
In order to get the result you will want to use the DATEPART and YEAR functions. These will allow grouping by both the week and year values.
The following query should get the result that you want:
select week,
coalesce(Deposit, 0) Deposit,
coalesce(Withdrawals, 0) Withdrawals,
coalesce(Stake, 0) Stake,
coalesce(Profit, 0) Profit,
Balance,
Year
from
(
select datepart(week, t1.transactiondate) week,
t1.transactiontype,
t2.balance,
t1.amount,
year(t1.transactiondate) year
from transactions t1
cross apply
(
select top 1 balance
from transactions t2
where datepart(week, t1.transactiondate) = datepart(week, t2.transactiondate)
and year(t1.transactiondate) = year(t2.transactiondate)
and t1.userid = t2.userid
order by TransactionId desc
) t2
) d
pivot
(
sum(amount)
for transactiontype in (Deposit, Withdrawals, Stake, Profit)
) piv;
See SQL Fiddle with Demo. The result is:
| WEEK | DEPOSIT | WITHDRAWALS | STAKE | PROFIT | BALANCE | YEAR |
------------------------------------------------------------------
| 13 | 150 | -700 | -250 | 1500 | 700 | 2013 |
| 14 | 0 | 0 | -300 | 0 | 400 | 2013 |
As a side note, you stated that your start of the week is Monday, you might have to use the DATEFIRST function to set the first day of the week.
Another option, without using PIVOT, but rather with few CASEs
WITH CTE AS
(
SELECT
TransactionId
,TransactionDate
,DATEPART(WEEK, TransactionDate) AS Week
,CASE WHEN TransactionType='Deposit' THEN Amount ELSE 0 END AS Deposit
,CASE WHEN TransactionType='Stake' THEN Amount ELSE 0 END AS Stake
,CASE WHEN TransactionType='Profit' THEN Amount ELSE 0 END AS Profit
,CASE WHEN TransactionType='Withdrawals' THEN Amount ELSE 0 END AS Withdrawals
,Balance
,DATEPART(YEAR, TransactionDate) AS Year
FROM dbo.Transactions
)
SELECT
Week, SUM(Deposit) AS Deposit, SUM(Withdrawals) AS Withdrawals, SUM(Stake) AS Stake, SUM(Profit) AS Profit,
(SELECT Balance FROM CTE i WHERE i.TransactionID = MAX(o.TransactionID)) AS BAlance, Year
FROM CTE o
GROUP BY Week, Year
SQLFiddle Demo
http://www.sqlfiddle.com/#!3/79d65/89
;WITH cte AS
(
SELECT datepart(ww, transactiondate) wk,
sum(CASE WHEN TransactionType = 'Deposit' THEN Amount ELSE 0 END) AS D,
sum(CASE WHEN TransactionType = 'Withdrawals' THEN Amount ELSE 0 END) AS W,
sum(CASE WHEN TransactionType = 'Profit' THEN Amount ELSE 0 END) AS P,
sum(CASE WHEN TransactionType = 'Stake' THEN Amount ELSE 0 END) AS S,
sum(
CASE WHEN TransactionType = 'Deposit' THEN Amount ELSE 0 END +
CASE WHEN TransactionType = 'Withdrawals' THEN Amount ELSE 0 END +
CASE WHEN TransactionType = 'Profit' THEN Amount ELSE 0 END +
CASE WHEN TransactionType = 'Stake' THEN Amount ELSE 0 END +
CASE WHEN TransactionType = 'Balance' THEN Amount ELSE 0 END) AS wkTotal
FROM transactions
GROUP BY datepart(ww, transactiondate)),
cte1 AS
(
SELECT *, row_number() over (ORDER BY wk) AS rowNum
FROM cte)
SELECT wk, d, w, p, s, wktotal
+ coalesce((SELECT top 1 wktotal FROM cte1 x WHERE x.rownum < m.rownum ), 0) AS RunningBalance
FROM cte1 m