How to implement a stored procedure for my case? - sql

I will implement a SSRS summary data report with following data records from one table.
ID Part Type Value
------------------------------------------------------------
1 Payroll State Tax 2010
1 Payroll City Tax 500
1 Payroll Medical 300
2 Payroll State Tax 2000
2 Payroll City Tax 400
3 Payroll FICA 200
1 Refund State Tax -500
1 Refund Medical -100
3 Payroll FICA 200
1 Refund State Tax -500
1 Refund Medical -100
How do I implement a stored procedure to produce following result by summing same Type values for each Part so it is easy to build the SSRS report? Thanks a lot!
Type Payroll Refund Total
State Tax 4010 -500 3600
City Tax 900 0 900
FICA 400 0 400
Medical 300 -100 200

You can use a CTE/subquery with conditional aggregation as
CREATE PROCEDURE YourProcName
AS
SET NOCOUNT ON;
WITH CTE AS
(
SELECT Type,
SUM(CASE WHEN Value > 0 THEN Value ELSE 0 END) payroll,
SUM(CASE WHEN Value < 0 THEN Value ELSE 0 END) refund
FROM YourTable
GROUP BY Type
)
SELECT CTE.*,
CTE.payroll + CTE.refund Total
FROM CTE;
GO;

You can use conditional aggregation:
select type,
sum(case when part = 'Payroll' then value else 0 end) as payroll,
sum(case when part = 'Refund' then value else 0 end) as refund
from t
group by type;
Why would you want a stored procedure when a simple query does what you want? I would strongly recommend a view or table-valued function instead.

Related

How to convert values in a field to different fields in Google Big Query?

I have two distinct values in a field and some aggregates based on that, how would I convert these values into two different fields and then have the aggregates accordingly.
What I have:
user_id status amount
101 deposit 100
101 credit 300
101 deposit 700
102 deposit 1000
102 credit 200
102 credit 500
What I want:
user_id credit deposit
101 300 800
102 700 1000
Could anyone please help me on this.
SELECT user_id,
SUM(CASE WHEN status='credit' THEN amount ELSE 0 END) credit,
SUM(CASE WHEN status='deposit' THEN amount ELSE 0 END) deposit
FROM src_table
GROUP BY user_id

SQL - Case when product exists, fill up its corresponding value to all rows within the partition

I have the something like the following monthly data set.
I have a Product, company ID, Date, and Quantity. A company (denoted by Company ID) can buy multiple products. I want to create a new column that will have the quantity of Product 'C' if the company bought in the month at each line item. If Product 'C' is not bought, then return 0.
Product Company_ID Date Quantity Desired_Calculated_Column
A 1 5/1/2019 100 300
B 1 5/1/2019 200 300
C 1 5/1/2019 300 300
A 2 6/1/2019 150 125
B 2 6/1/2019 250 125
C 2 6/1/2019 125 125
A 3 7/1/2019 175 0
B 3 7/1/2019 275 0
I have been trying to partition the data based on Product and Company ID. I have been trying to leverage the LAST_VALUE but haven't been successful.
LAST_VALUE(quantity) OVER (PARTITION BY Date, Company_ID
ORDER BY product_group
) AS Desired_Calculated_Column
You don't want last_value(). You can use conditional aggregation, assuming that 'C' occurs once per group:
MAX(CASE WHEN product_group = 'C' THEN quantity ELSE 0 END) OVER
(PARTITION BY Date, Company_ID) AS C_quantity

How to calculate and update subtotal in SQL Server table

I have a SQL Server table in following format: Type column is to indicate whether row is Subtotal or line level value.
If type is 1, then it is line level cost, and if type is 2, then it is subtotal and if type is 3, then it is grand total.
Category Subcategory Option1 Option2 Option3 Option4 Type
----------------------------------------------------------------------------
Insurance Insurance Cost 10 20 30 40 1
Insurance Insurance Tax 10 20 30 40 1
Insurance Subtotal 0 0 0 0 2
Finance Finance Cost 10 20 30 40 1
Finance Finance Tax 10 20 30 40 1
Finance Subtotal 0 0 0 0 2
GrandTotal GrandTotal 0 0 0 0 3
----------------------------------------------------------------------------
I want to update rows with subtotal with line level subtotal of respective category and grand total with line level total
Category Subcategory Option1 Option2 Option3 Option4 Type
----------------------------------------------------------------------------
Insurance Insurance Cost 10 20 30 40 1
Insurance Insurance Tax 10 20 30 40 1
Insurance Subtotal 20 40 60 80 2
Finance Finance Cost 10 20 30 40 1
Finance Finance Tax 10 20 30 40 1
Finance Subtotal 20 40 60 80 2
GrandTotal GrandTotal 40 80 120 160 3
----------------------------------------------------------------------------
How can I calculate and update these rows?
Awaiting for your reply.
Thanks.
I think cube extension for group by best suites for your case. Consider :
select distinct max(Category) as Category, Subcategory,
sum(Option1) as Option1
sum(Option2) as Option2,
sum(Option3) as Option3,
sum(Option4) as Option4
from t
where type = 1
group by cube(Category,Subcategory)
order by Category, Subcategory desc;
Rextester Demo "This is for Option1 column only, expandable for other options"
P.S. it's illogical to store a computable data in a table, that could already exist as basic data in that table as #Dougie pointed out.
You are trying to do an update, so that is a bit tricky. I think this will do the trick:
update t
set option1 = (case t.subcategory
when 'Subtotal' then subtotal.option1
when 'GrandTotal' then grandtotal.option1
else option1
end),
option2 = (case t.subcategory
when 'Subtotal' then subtotal.option2
when 'GrandTotal' then grandtotal.option2
else option2
end),
option3 = (case t.subcategory
when 'Subtotal' then subtotal.option3
when 'GrandTotal' then grandtotal.option3
else option3
end),
option4 = (case t.subcategory
when 'Subtotal' then subtotal.option4
when 'GrandTotal' then grandtotal.option4
else option4
end)
from t cross apply
(select category, sum(option1) as option1, sum(option2) as option2,
sum(option3) as option3)
from t t2
where t2.category = t.category
) subtotal cross join
(select category, sum(option1) as option1, sum(option2) as option2,
sum(option3) as option3)
from t t2
) grandtotal
where t.subcategory in ('Subtotal', 'GrandTotal');

query that is splitting a column into two

Hello I have an ID column and an amount column at the moment.
A value is represented as a Debit if the amount is positive. A credit if the amount is negative. I'm wondering how can I "Split" my amount column.
Select * from Test.dbo.Accounts
Produces
ID | Amount
1 | 500
2 | -600
So Item 1 is a Debit, Item two is a credit. I want to query the Database so that it displays as followed
ID | Debit | Credit
1 | 500 | null
2 | null |-600
You can use a case statement to find which column the amount belongs in:
SELECT id ,
CASE WHEN amount >= 0 THEN amount
ELSE NULL
END AS debit ,
CASE WHEN amount < 0 THEN amount
ELSE NULL
END AS credit
FROM Test.dbo.Accounts
I assumed 0 should go in debits but that'd be your call.
Select ID, Amount as Debit, null as Credit
From Account
Where Amount >= 0
Union All
Select ID, null as Debit, Amount as Credit
From Account
Where Amount < 0

create column in for each different value in the table

Let us say that I have a table structured like this(using SQL server):
empID INT
payment INT
Now, each employee only gets paid either 50.00 or 100.00. There are two employees earning 50.00 and three earning 100.00.
How would I do a select statement so that the result set was like this:
50.00 100
----- -----
2 3
Where 50.00 and 100.00 are the column headers, and the number below are the actual values. I know that I can do
SELECT payment, COUNT(*)
FROM Student
GROUP BY payment
But that returns the payment in its own column. I want each different payment value in its own column.
Here's how:
select sum(case when payment = 50.00 then 1 else 0 end) as num050,
sum(case when payment = 100.00 then 1 else 0 end) as num100
But, with floating point numbers, you should never do equal comparisons. It is better to do something like:
sum(case when abs(payment - 50) < 0.001 then 1 else 0 end)
or something like that.