i have 3 tables as per below
Table Referral
id
name
ref_code
1
ANDY
AN123
2
BEN
BE34
3
CHARLES
CH44
4
DANNY
DA66
Table Application
app_id
ref_code
status
APP1
AN123
2
APP2
AN123
2
APP3
CH44
3
APP4
DA66
3
APP5
DA66
1
APP6
CH44
1
Table FINANCING
app_id
AMOUNT
APP1
1000
APP2
1500
APP3
2000
APP4
3000
APP5
4000
APP6
1000
I want to show referral summary like below table, i want to show referral when they have applciation with status where in (1,2). approve case is when status = 2
here is my sql
SELECT
referral.name,COUNT(application.ref_code) as total ,SUM(financing.amount) as total financing,
COUNT(CASE WHEN application.status=2 THEN 1 ELSE NULL END) as TOTAL CASE APPROVE
FROM referral
JOIN application
ON application.ref_code= referral.ref_code
JOIN financing
ON application.app_id= financing.app_id
where application.status IN ('1','2')
GROUP BY referral.name
from sql above, i can show table as below
NAME
TOTAL FINANCING
TOTAL ALL CASE
TOTAL CASE approved
ANDY
2500
2
2
CHARLES
2000
1
0
DANNY
3000
1
0
i have no idea how to show the last column (total financing amount for approved case only, where status=2).
here is table that i want to show
NAME
TOTAL FINANCING
TOTAL CASE
TOTAL CASE APPROVED
FINANCING approved
ANDY
2500
2
2
2500
CHARLES
2000
1
0
0
DANNY
3000
1
0
0
thank u in advance
You can sum "financing.amount" the same way you did with "TOTAL CASE APPPROVE"
SUM(
CASE WHEN application.status=2 THEN
financing.amount
ELSE 0 END
)
as TOTAL financing amount CASE APPROVE
Related
i have a table TB_Orders , i save orders in thatl ,ike below :
userid
factorid
productname
economy
vip
count
1
A-123
name1
13000
18000
3
1
A-123
name2
9000
13000
4
2
A-124
name2
2000
5000
2
For example, user 1: has registered two orders with invoice number A-123.
And user 2 has only registered one order
I want to report as follows:
userid
factorid
total economy
total vip
total count
1
A-123
21000
31000
7
2
A-124
2000
5000
2
thanks for your help
This is an aggregation query:
select userid, factorid, sum(economy), sum(vip), sum(count)
from tb_orders
group by userid, factorid;
Aggregation is a very basic part of SQL functionality. If you don't recognize it, I would suggest that you brush up on your SQL skills with a tutorial, book, or something like that.
I have a data set that repeats daily and shows sales. If a product is released on Day 1 and has between 1-5 sales AND also if on Day 2 it has between 10-50 sales, I want to classify it as "Limited Sales."
If a product is released on Day 1 and has over 1,000 sales and also if on Day 2 it has over 1,000 sales, I want to classify it as "Wide Sales."
How would I go about doing this in standard SQL?
I've tried using some workarounds using CASE WHEN, but I ultimately end up with issues because while I can classify the 1st column with an output, I can't get the 2nd column to have an output that is also based on the 1st output (e.g. Column 1 is TRUE, but Column 2 is FALSE. What I need is for Column 1 = TRUE and Column 2 = True.
Here's what a sample query would look like:
Table looks like this:
Columns: name, day_number, sales
1. Jack | 1 | 5
2. Jack | 2 | 10
3. Mary | 1 | 1250
4. Mary | 2 | 1500
SELECT name,
day_number,
sales,
CASE
WHEN day_number = 1
AND sales >= 1
AND sales <= 5
THEN "LIMITED SALES"
ELSE "WIDE SALES"
END AS status_1,
CASE
WHEN day_number = 2
AND sales >= 10
AND sales <= 50
THEN TRUE
ELSE FALSE
END AS status_2
FROM table
Unfortunately this isn't really going to get me what I want. At the end of the day, I would like to see results like:
1. Jack | 1 | 5 | LIMITED SALES
2. Jack | 2 | 10 | LIMITED SALES
3. Mary | 1 | 1250 | WIDE SALES
4. Mary | 2 | 1500 | WIDE SALES
Is this what you want?
select name,
(case when sum(case when day_number = 1 then sales end) between 1 and 5 and
sum(case when day_number = 2 then sales end) between 10 and 50
then 'Limited Sales'
when sum(case when day_number = 1 then sales end) > 1000 and
sum(case when day_number = 2 then sales end) > 1000
then 'Wide Sales'
else '???'
end) as sales_category
from t
group by name
If you want this on each of the original rows, then use window functions or a join.
With the following function and stored procedure i get a resultset with 2 columns.
What i need additional is a third column total for all open invoices inner score range
It would great for any idea.
ALTER FUNCTION dbo.OpenOrders
(
#MandantId int
)
RETURNS TABLE
AS
RETURN
SELECT SUM(Invoices.Amount - ISNULL(Payment.Amount, 0)) AS Op
FROM Invoices INNER JOIN
Orders ON Invoices.OrderId = Orders.OrderId LEFT OUTER JOIN
Payment ON Invoices.InvoiceId = Payment.InvoiceId
WHERE (Orders.MandantId = #MandantId)
GROUP BY Invoice.InvoiceId, Invoices.Amount
HAVING (SUM(Invoices.Amount - ISNULL(Payment.Amount, 0)) <> 0)
ALTER PROCEDURE dbo.GetOpRanges
#MandantId int
AS
BEGIN
SELECT * INTO #tmp_ranges
FROM
//wrong in first post -> OPDebitorByMandant(#MandantId)
OpenOrders(#MandantId)
SELECT op AS [score range], COUNT(*) AS [number of occurences]
FROM
(SELECT CASE
WHEN op BETWEEN 0 AND 50 THEN ' 0- 50'
WHEN op BETWEEN 50 AND 100 THEN ' 50-100'
WHEN op BETWEEN 100 AND 500 THEN '100-500'
ELSE '500 and >' END AS op
FROM [#tmp_ranges]) AS t
GROUP BY op
RETURN
Result:
score range number of occurences range
------------------+-------------
0- 50 23
50-100 4
100-500 4
500 and > 21
What i need additional is a third column total for all open invoices inner score range.
Target result:
score range number of occurences Total
-----------+--------------------+------
0- 50 23 1.150
50-100 4 400
100-500 4 2.000
500 and > 21 22.000
Tables:
Invoices
InvoiceId CustomerId OrderId DateOfInvoice Amount
----------+----------+-------+-------------+------
1 1 20 20160301 1000.00
2 2 22 20160501 2000.00
3 1 102 20160601 3000.00
...
Orders
OrderId MandantId CustomerId DateOfOrder Amount
-------+---------+----------+-----------+-----------
20 1 1 20160101 1000.00
22 1 2 20160101 2000.00
102 1 1 20160101 3000.00
...
Payment
PaymentId MandantId CustomerId InvoiceId OrderId DateOfPayment Amount
---------+---------+----------+---------+-------+-------------+-------------
1 1 1 1 20 20160310 1000.00
2 1 2 2 22 20160505 2000.00
3 1 1 3 102 20160610 3000.00
...
hope it's helpfull and thanks again in advance for any solution
Can this group please me brainstorm ideas to write a sql query for the condition listed below.
The item listed below are the accounts with the collection amount per each account.
Let say there are 4 people ( A, B C D) and each people should be assigned the average of total collection amount. (ie 8190/4 = 2047.5 ) or smthing around this value. ( In reality there will be hundered of people collecting thousands of collection accounts)
Accounts # Collection Amount
1 25X1 $800.00
1 25X2 $200.00
1 25X3 $700.00
1 25X4 $120.00
1 25X5 $500.00
1 25X6 $410.00
1 25X7 $800.00
1 25X8 $200.00
1 25X9 $700.00
1 25X10 $120.00
1 25X11 $500.00
1 25X12 $410.00
1 25X13 $800.00
1 25X14 $200.00
1 25X15 $700.00
1 25X16 $120.00
1 25X17 $500.00
1 25X18 $410.00
Total Collection $8,190.00
Assuming your "people" are each individual account holder:
SELECT [Accounts #]
,[Collection Amount]
,(SELECT SUM([Collection Amount])/COUNT(distinct [Accounts #]) as AverageCollection
FROM [TableName])
I have a table called users where the employee data are stored.
Also I have another table payment_details where employee's payment related data are stored
the two tables are as follows.
this table is users
sr_no emp_no username payment
1 1001 leroy <null>
2 1003 harry <null>
3 1004 Tom <null>
4 1008 Jon <null>
This table below is payment_details
sr_no name number month status date
43 Jon 1008 January paid 5/16/2012
44 Jon 1008 January balance 5/16/2012
45 Harry 1003 January paid 5/16/2012
46 Tom 1004 January paid 5/16/2012
47 leroy 1001 January paid 5/16/2012
48 Jon 1008 January paid 5/16/2012
49 Harry 1003 January paid 5/16/2012
50 Jon 1008 February balance 5/16/2012
51 leroy 1001 February paid 5/16/2012
52 Jon 1008 February paid 5/16/2012
53 Tom 1004 February balance 5/16/2012
My question here is to update "users" table payment column to "paid" when the status of his/her is all paid in payment_details table
You can either do this: http://www.sqlfiddle.com/#!3/db13f/18
update users set payment = 'paid'
from
(
select number
from payment_details
group by number
having sum(case when status = 'paid' then 1 end)
= count(*)
) as x
where x.number = users.emp_no;
Or this: http://www.sqlfiddle.com/#!3/db13f/19
update users
set payment = x.upd
from
(
select u.emp_no,
case when sum(case when d.status = 'paid' then 1 end) = count(*) then
'paid'
else
null
end as upd
from users u
left join payment_details d
on d.number = u.emp_no
group by u.emp_no
) as x
where x.emp_no = users.emp_no;
Their difference is how many rows it updates. On the second query, it updates all users, regardless if the user has a paid all status('paid') or not(null); on the first query, it updates only those who are paid.
The advantage of the second query, is when you change one of the all 'paid' status on payment_detail of a given user to 'not paid' for example, it can revert back the user's payment status to null
UPDATE order_details
SET payment= 'paid'
WHERE not EXISTS (SELECT 1
FROM payment_details
WHERE payment_details.emp_no= order_details.emp_no
AND payment_details.status <> 'paid'
)
There also simple way to get the value of particulars table into variable and update the tabe as below:
declare #bb varchar(50)
select #bb= status from payment_details where name=#name and id=#id
update uuser set payment = #bb
where name = #name and id=#id