How to separate positive and negative numbers into their own columns? - sql

I have a table with the following columns and data:
activity_dt | activity_amt
2009-01-01 | -500
2009-01-01 | 750
Can I write a query that looks at the sign of activity_amt and puts it in the credits column if it's positive, and the debits column if it's negative? (I'm using Sybase)
activity_dt | debits | credits
2009-01-01 | -500 | 750

select activity_dt,
sum(case when activity_amt < 0 then activity_amt else 0 end) as debits,
sum(case when activity_amt > 0 then activity_amt else 0 end) as credits
from the_table
group by activity_dt
order by activity_dt

I'm not sure about the exact syntax in Sybase, but you should be able to group on the date and sum up the positive and negative values:
select
activity_dt,
sum(case when activity_amt < 0 then activity_amt else 0 end) as debits,
sum(case when activity_amt >= 0 then activity_amt else 0 end) as credits
from
theTable
group by
activity_dt

I found a new answer to this problem using the DECODE function.
I hope this turns out to be useful for everyone.
select activity_dt,
sum((DECODE(activity_amt /-ABS(activity_amt), 1, activity_amt, 0))) as credits,
sum((DECODE(activity_amt /-ABS(activity_amt), -1, activity_amt, 0))) as debits
from the_table
group by activity_dt
order by activity_dt;

select (select JV_GroupsHead.GroupTitle
from JV_GroupsHead
whereJV_GroupsHead.Id=jv.GroupId) as 'GroupName'
,jv.Revenue
,jv.AccountNo
,jv.AccountNoTitle
,(case when jv.Revenue < 0 then jv.Revenue else 0 end) as 'debits'
,(case when jv.Revenue> 0 then jv.Revenue else 0 end) as 'credits'
from JVFunction1('2010-07-08','2010-08-08') as jv

Related

find percentage of sale and percentage of returns

I have a table in MS SQL DB which contains transactions of any company, there are two columns
sub_cat_code
total_amount
The total_amount contain sometimes positive and sometimes negative value. Where positive values indicates the sale and negative value indicates Returns.
Now, I need to find the percentage of sales and percentage of return using following query, in percentage of return column every value coming same. any help would be appreciable.
Select prod_subcat_code ,
(sum(total_amt)*100 /(select sum(total_amt) from transection)) as
Sale_pers,
((select sum(total_amt) from transection where total_amt<0)*100/(select
sum(total_amt) from transection)) as return_sale
from transection
group by prod_subcat_code
order by Sale_pers desc
Use a CTE which returns the total sales and total returns of the table:
with cte as (
select
sum(case when total_amount > 0 then total_amount else 0 end) total_sales,
sum(case when total_amount < 0 then total_amount else 0 end) total_returns
from transection
)
select prod_subcat_code ,
100.0 * sum(case when total_amount > 0 then total_amount else 0 end) / (select total_sales from cte) as sale_perc,
100.0 * sum(case when total_amount < 0 then total_amount else 0 end) / (select total_returns from cte) as return_perc
from transection
group by prod_subcat_code
See the demo.
If I understand correctly, you just want to divide two aggregated values:
Select prod_subcat_code,
sum(total_amt) as net_sales,
sum(case when total_amt > 0 then total_amt else 0 end) as sales,
sum(case when total_amt < 0 then total_amt else 0 end) as returns,
(sum(case when total_amt > 0 then total_amt else 0 end) /
nullif(sum(total_amt), 0)
) as sales_ratio,
(sum(case when total_amt < 0 then total_amt else 0 end) /
nullif(sum(total_amt), 0)
) as return_ratio
from transection
group by prod_subcat_code;
You may want change your query to this for desired result:
Select prod_subcat_code,
sum(case when total_amt > 0 then total_amt else 0 end) as sales,
(sum(case when total_amt > 0 then total_amt else 0 end) /
(sum(total_amt)) ) * 100 as sales_Percent,
sum(case when total_amt < 0 then total_amt else 0 end) as [returns],
(sum(case when total_amt < 0 then total_amt else 0 end) /
(sum(total_amt)) ) * 100 as Return_Percent
from transection
group by prod_subcat_code
order by sales_Percent desc
Hope it will help you,
SELECT prod_subcat_code
,(SUM(CASE WHEN total_amt > 0 THEN total_amt END)/sum(total_amt))*100 AS Sales_Percentage
,(SUM(CASE WHEN total_amt < 0 THEN total_amt END)/sum(total_amt))*100 AS Return_Percentage
FROM transection
GROUP BY prod_subcat_code;

SQL query to compare the amount

I have a Claim table that holds the claim amount. I need to write a query to compare if the amount is the same or not.
SELECT
CLAIM_NO, SUM(TRAN_AMOUNT) AS Amount
FROM
CLMTABLE
WHERE
TRTYPE = 0
GROUP BY
CLAIM_NO
UNION ALL
SELECT
CLAIM_NO, SUM(TRAN_AMOUNT) AS Amount
FROM
CLMTABLE
WHERE
TRTYPE <> 0
GROUP BY
CLAIM_NO
OUTPUT
CLAIM_NO AMOUNT
-----------------
1234567890 883.00
1234567890 883.00
1234567891 990.00
1234567891 990.00
1234567892 883.00
1234567892 893.00
The last record the amount is not the same, i want to be able to spot the claim that the amount doesn't match.
Thanks, any help will be appreciated
Please use below query to find all such transaction:
SELECT CLAIM_NO,SUM(CASE WHEN TRTYPE=0 THEN ISNULL(TRAN_AMOUNT,0) ELSE 0 END) As Amount,
SUM(CASE WHEN TRTYPE<>0 THEN ISNULL(TRAN_AMOUNT,0) ELSE 0 END) AS Amount2
FROM CLMTABLE
GROUP BY CLAIM_NO
HAVING SUM(CASE WHEN TRTYPE=0 THEN ISNULL(TRAN_AMOUNT,0) ELSE 0 END) <> SUM(CASE WHEN TRTYPE<>0 THEN ISNULL(TRAN_AMOUNT,0) ELSE 0 END)
Use a CASE inside a SUM func to retrieve the difference between amount of different types.
SELECT claim_no, SUM(CASE WHEN trtype = 0 THEN tran_amount
ELSE -1*tran_amount
END) as delta
FROM clmtable
GROUP BY claim_no

Redshift - Aggregate function calls may not have nested aggregate or window function

I am trying to build a SQL query that would count the sum of sales made based on certain values as shown below:
Given below is how my dataset is:
cust_name,sales_count,day_count
cust_a,100,3
cust_a,200,5
cust_a,150,7
cust_a,120,1
cust_a,180,10
cust_a,100,8
cust_b,20,3
cust_b,10,4
cust_b,50,6
cust_b,60,8
cust_b,15,9
I would like to get the output in the below format
cust_name,sales_count,day_count
cust_a,280,last_14
cust_a,450,last_7
cust_b,85,last_14
cust_b,80,last_7
Given below is the case statement I tried to build
select cust_name,
sum(case when day_count > 7 then count(sales_count) else 0 end) as count_14,
sum(case when day_count < 7 then count(sales_count) else 0 end) as count_7
from sales
group by cust_name;
I am using a Amazon Redshift Database.
Found a similar issue in this link (Amazon Redshift - Get week wise sales count by category) but I keep getting aggregate function calls may not have nested aggregate or window function.
Could anyone help trouble shoot this. Thanks.
From your question, you can try this query.
use SUM and CASE WHEN Expression.
select cust_name,
sum(case when day_count > 7 then sales_count else 0 end) as count_14,
sum(case when day_count < 7 then sales_count else 0 end) as count_7
from sales
group by cust_name;
EDIT:
Becasue Aggregate functions can't nest multiple times.
If you want to fix
sum(case when day_count > 7 then count(sales_count) else 0 end)
You can try to write a subquery to fix it.
SELECT cust_name,
sum(case when day_count > 7 then cnt else 0 end) as count_14,
sum(case when day_count < 7 then cnt else 0 end) as count_7
FROM (
SELECT cust_name,(case when day_count > 7 then 1
when day_count < 7 then 2
else null
end) grp,
count(sales_count) cnt
FROM sales
GROUP BY cust_name,
(case when day_count > 7 then 1
when day_count < 7 then 2
else null
end)
)t
WHERE grp is not null
GROUP BY cust_name
to produce the desired output, what you need is just
sum(case when day_count > 7 then sales_count else 0 end)
what you have in the brackets is the expression which output you redirect to the sum function that aggregates it, so for cust_a it produces the following set of values:
cust_a,100,3 -> 0 (3<=7)
cust_a,200,5 -> 0 (5<=7)
cust_a,150,7 -> 0 (7<=7)
cust_a,120,1 -> 0 (1<=7)
cust_a,180,10 -> 180 (10>7)
cust_a,100,8 -> 100 (8>7)
and then the sum is 280

How to separate the Positive and negative record from a query which is calculate the all balance sum......?

select account
, SUM(balance) as bal
from Main_report
Group BY account
How can I separate the positive and negative records from the above query?
I want to combine this query with the cases listed below.
select (case when bal < 0 then bal else 0 end) as debits,
(case when bal > 0 then bal else 0 end) as credits from Main_report
Group BY account
Just use your two cases and sum the result of those:
SELECT account
, SUM(CASE WHEN balance < 0 THEN balance ELSE 0 END) as debits
, SUM(CASE WHEN balance > 0 THEN balance ELSE 0 END) as credits
FROM Main_report
GROUP BY account

Group by year in sql

I am trying to group by year but was not able to do.I can get the column count but not year wise. this is what i tried.
select t_contract ,
sum(CASE t_contract when '18' then 1 else 0 end) as XL,
sum(CASE t_contract when '01' then 1 else 0 end) as VC,
sum(CASE t_contract when '75' then 1 else 0 end) as AN,
sum(CASE t_contract when '48' then 1 else 0 end) as CS
from icps.dbo.tickets
WHERE
t_date_time_issued >= DATEADD(year, -6, GETDATE())
GROUP BY contract
.. but i want to add year .. where i have t_date_time _issued column.
My another query is I have a column called t_zone_name and I want to sum all the rows where t_zone_anme like '%ICeland%' an i tried this:
sum(CASE t_zone_name like '%ICeland%' then 1 else 0 end) as ICELAND
but I get an error on statement like... thanks in advance.
LIKE
YEAR XL VC AN CS total
2010 50 50 50 50 200
2011 5 5 5 5 20
Try the below query:
SELECT t_contract, YEAR(t_date_time_issued) As Yr, SUM(CASE WHEN t_zone_name like '%ICeland%' THEN 1 ELSE 0 END) AS ICELAND
SUM(CASE t_contract when '18' then 1 else 0 end) as XL,
SUM(CASE t_contract when '01' then 1 else 0 end) as VC,
SUM(CASE t_contract when '75' then 1 else 0 end) as AN,
SUM(CASE t_contract when '48' then 1 else 0 end) as CS
FROM icps.dbo.tickets
WHERE YEAR(t_date_time_issued) >= (YEAR(GetDate()) - 6)
GROUP BY t_contract, YEAR(t_date_time_issued)
You might need change the order of t_contract and YEAR(t_date_time_issued) depending on which grouping you want to apply first.
As suggested by #ray I have replaced DATEPART(yyyy, t_date_time_issued) >= DATEPART(yyyy, DATEADD(year, -6, GETDATE())) with year(t_date_time_issued) >= (year(GetDate()) - 6)
If you want to group by year, in sql server, you might
GROUP BY DATEDIFF(year,t_date_time_issued, GETDATE())
In other DB engine, usually has method to get year part, or use substring to get year part from a time string.