I have a factor table and it has two columns: totalprice and paidprice.
I want divide the count of total price on paidprice and *100 and i want return result as percentage.
select TotalPrice/PaidPrice as h
from Factors
where TotalPrice>=PaidPrice
You can try the following:
SELECT (COUNT(PAID_PRICE) / CAST(Total_Price AS FLOAT)) * 100 AS Result
from #Table
WHERE TOTAL_PRICE >= PAID_PRICE
GROUP by TOTAL_PRICE
http://rextester.com/PKXSYV97387
TABLE:
**PAID_PRICE** **TOTAL_PRICE**
35 50
40 45
QUERY:
SELECT (SUM(PAID_PRICE)/SUM(TOTAL_PRICE))*100
FROM TABLE_NAME WITH (NOLOCK)
WHERE TOTAL_PRICE>=TOTAL_PRICE
Try this?
select
cast(
SUM(CASE WHEN PaidPrice>=TotalPrice THEN 1 ELSE 0 END) *
100 /
COUNT(FactorID)as varchar(5)
) + '%' as percentageOFcomepletfactor
from
Factors
Related
I have a table with multiple rows, each of which contains these four columns:
product
price_instore
price_pickup
price_delivery
price_ship
1
$13.00
$13.50
$14.50
$18.00.
2
$4.00
$4.00
NULL
NULL
3
$10.00
$10.00
$12.00
NULL
I'd like to have a fifth column average_price that gives the average price for a product, but does not count NULLS towards the sum price, or towards the count used to divide the average.
So average price of product 1: ($13+$13.50+$14.50+$18)/4=$14.75
Average price of product 2: ($4+$4)/2 = $4.00
Average price of product 3: ($10+$10+$12)/3 = $10.67
Is there any way to do this is SQL? I've tried subqueries such as the one below without success:
(select coalesce((PRICE_INSTORE + PRICE_PICKUP + PRICE_DELIVERY + PRICE_SHIP) / 4,
PRICE_INSTORE, PRICE_PICKUP, PRICE_DELIVERY, PRICE_SHIP)
but then I only get one price if any of them are null
select
*,
(select avg(x) from unnest(array[price_instore,price_pickup,price_delivery,price_ship]) as x) as avg_price
from
your_table;
I'm drunk, so there's probably a much better way to do this than pivoting, but I cannot see it now with the room spinning around me like it is.
with j as (
select product, to_jsonb(mytable) - 'product' as jdata
from mytable
)
select j.product, avg(e.value::numeric) as avg_price
from j
cross join lateral jsonb_each(j.jdata) as e(key, value)
where e.value is not null
group by j.product.
Try this
select coalesce (PRICE_INSTORE, 0 ) + coalesce (PRICE_PICKUP, 0) + coalesce (PRICE_DELIVERY , 0) + coalesce (PRICE_SHIP , 0) /
((case when PRICE_INSTORE is null then 0 else 1 end) + (case when PRICE_PICKUP is null then 0 else 1 end) +
(case when PRICE_DELIVERY is null then 0 else 1 end) + (case when PRICE_SHIP is null then 0 else 1 end) )
from product
One method is a lateral join:
select t.*, avg_price
from t cross join lateral
(select avg(price) as avg_price
from (values (price_instore), (price_pickup), (price_delivery), (price_ship)
) v(price)
) x
try this:
select coalesce(PRICE_INSTORE,0) + coalesce(PRICE_PICKUP,0) + coalesce(PRICE_DELIVERY,0) + coalesce(PRICE_SHIP,0)) / 4
This one is probably the easiest to digest. Performance shouldn't be too bad unless you have a very large table that is also, not indexed
with cte (product, prices) as
(select product, price_instore from t union all
select product, price_pickup from t union all
select product, price_delivery from t union all
select product, price_ship from t)
select product, avg(prices)
from cte
group by product;
You an either use the output as is, or wrap it around another CTE and use that to join on your original table to get the averages
Table Prod
Product quantity Weight
A 10 2
B 20 3
C 30 0
D 40 0
select SUM((quantity*weight)) / sum(NULLIF(quantity,0) ) As [Tot Avg]
from prod
Expected Output:
80/30 = 2.67
But above query will output
80/100 = 0.8
How to achieve the 2.67 result?
I want to sum(quantity) with not null weight column values.
Just do it like this
select SUM((quantity*weight)) / sum(coalesce(1.0*( case when weight > 0 then quantity else 0 end),0)) As [Tot Avg]
from prod
The output is:
Tot Avg
2.67
I originally misunderstood the question.
Simply use a where clause:
However, I would simplify it to:
select sum(quantity * weight) * 1.0 / nullif(sum(quantity), 0) As TotAvg
from prod
where weight <> 0;
Here is a db<>fiddle.
use this:
declare #wq int
set #wq=(select SUM((quantity*weight)) from Product)
declare #quantity int
set #quantity=(select SUM(quantity)from Product where Weight<>0 )
select CAST(#wq AS float)/CAST(#quantity AS float)
I have a table with Article_code, Date, And Quantity.
What I try is to find where for the same article the quantity had an increase or a decrease of 10 times compared to the previous date.
For instance, I have following columns
Article Date Quantity X10
1001 20-01-2015 22
1001 21-02-2016 220
For the row with quantity 220 should be marked as x in column x10.
What I tried is the following
UPDATE ph
SET [X10] = 'Px10'
FROM
[dbo].[Sales] ph
inner join [dbo].[Sales] t on (ph.[Article] = t.[Article])
WHERE
ph.[quantity] / t.[quantity] = 10
OR
t.[quantity] / ph.[quantity] = 10;
Try the following:
select *,
case when exists
(
select 1
from yourtable t2
where dateadd(day,-1,t1.[Date])=t2.[Date]
and t1.Quantity>=10*t2.Quantity
) then 'x' else '' end as 'x10'
from youttable t1
Try this.
UPDATE s
SET s.x10 = 'x'
FROM Sales s
OUTER APPLY ( SELECT TOP 1 quantity
FROM sales s2
WHERE s2.Article_code = s.Article_code
AND s2.date < s.date
ORDER BY s2.date DESC ) t
WHERE s.quantity = 10 * t.quantity;
i have two columns business_line(with values X,Y) and business_segment(values X,Y,Z same X and Y as business_line) in a table with name "Sometable". I have another column with name type_of_payment(with values A,B,C,D,E) and a final column with name transaction_value. This is what i want to do:
Sum the transactions grouped by business_line and business_segment and also find out what proportion of these payments were from A,C,E. So my output table would be something like this
(last three columns can be named anything
but they specify proportions of A,C,E)
Business_line SUM(transaction_value) A C E
and business seg.
X 100 20% 30% 50%
Y 200 11% 12% 77%
X 300 and so on
Y 170
Z 230
How do i do this??
PS : the sums of A C E need not be 100% as B and D are also present
This is standard SQL, should work on Oracle (but untested):
SELECT
business_line,
business_segment,
grand_total,
A_total * 100.0 / grand_total as A,
C_total * 100.0 / grand_total as C,
E_total * 100.0 / grand_total as E
FROM
(
SELECT
business_line,
business_segment,
SUM(transaction_value) as grand_total,
SUM(CASE WHEN payment_type = 'A' THEN transaction_value END) as A_total,
SUM(CASE WHEN payment_type = 'C' THEN transaction_value END) as C_total,
SUM(CASE WHEN payment_type = 'E' THEN transaction_value END) as E_total
FROM
SomeTable
GROUP BY
business_line,
business_segment
) as t
For Oracle 11g and above, you can use PIVOT
select *
from
(
select sometable.line, paymenttype,total, 100.0*transaction_value/total as percentage
from sometable
inner join
(select line, sum(transaction_value) as total
from sometable
group by line) total
on sometable.line = total.line
)
pivot
(
sum(percentage) for paymenttype in (a,c,e)
)
I'm not an expert in SQL by any means, and am having a hard time getting the data I need from a query. I'm working with a single table, Journal_Entry, that has a number of columns. One column is Status_ID, which is a foreign key to a Status table with three values "Green", "Yellow", and "Red". Also, a journal entry is logged against a particular User (User_ID).
I'm trying to get the number of journal entries logged for each Status, as a percentage of the total number of journal entries logged by a particular user. So far I've got the following for a Status of 1, which is green (and I know this doesn't work):
SELECT CAST((SELECT COUNT(Journal_Entry_ID)
FROM Journal_Entry
WHERE Status_ID = 1 AND User_ID = 3 /
SELECT COUNT(Journal_Entry_ID)
FROM Journal_Entry AND User_ID = 3)) AS FLOAT * 100
I need to continue the query for the other two status ID's, 2 and 3, and ideally would like to end with the selection of three columns as percentages, one for each Status: "Green_Percent", "Yellow_Percent", and "Red_Percent".
This is probably the most disjointed question I've ever asked, so I apologize for any lack of clarity. I'll be happy to clarify as necessary. Also, I'm using SQL Server 2005.
Thanks very much.
Use:
SELECT je.statusid,
COUNT(*) AS num,
(COUNT(*) / (SELECT COUNT(*)+.0
FROM JOURNAL_ENTRY) ) * 100
FROM JOURNAL_ENTRY je
GROUP BY je.statusid
Then it's a matter of formatting the precision you want:
CAST(((COUNT(*) / (SELECT COUNT(*)+.0 FROM BCCAMPUS.dbo.COURSES_RFIP)) * 100)
AS DECIMAL(4,2))
...will give two decimal places. Cast the result to INT if you don't want any decimal places.
You could use a CTE to minimize the duplication:
WITH cte AS (
SELECT je.*
FROM JOURNAL_ENTRY je
WHERE je.user_id = 3)
SELECT c.statusid,
COUNT(*) AS num,
(COUNT(*) / (SELECT COUNT(*)+.0
FROM cte) ) * 100
FROM cte c
GROUP BY c.statusid
This should work:
SELECT
user_id,
(CAST(SUM(CASE WHEN status_id = 1 THEN 1 ELSE 0 END) AS DECIMAL(6, 4))/COUNT(*)) * 100 AS pct_green,
(CAST(SUM(CASE WHEN status_id = 2 THEN 1 ELSE 0 END) AS DECIMAL(6, 4))/COUNT(*)) * 100 AS pct_yellow,
(CAST(SUM(CASE WHEN status_id = 3 THEN 1 ELSE 0 END) AS DECIMAL(6, 4))/COUNT(*)) * 100 AS pct_red
FROM
Journal_Entry
WHERE
user_id = 1
GROUP BY
user_id
If you don't need the user_id returned then you could get rid of that and the GROUP BY clause as long as you're only ever returning data for one user (or you want the aggregates for all users in the WHERE clause). If you want it for each user then you can keep the GROUP BY and simply get rid of the WHERE clause.
DECLARE #JournalEntry TABLE
( StatusID INT
);
INSERT INTO #JournalEntry (StatusID) VALUES
(1), (1),(1),(1),(1),(1),(1)
,(2), (2),(2),(2),(2),(2),(2)
,(3), (3),(3),(3),(3),(3),(3);
SELECT
CAST(SUM(CASE WHEN StatusID = 1 THEN 1 ELSE 0 END) AS DECIMAL) / CAST(COUNT(*) AS DECIMAL) Green
,CAST(SUM(CASE WHEN StatusID = 2 THEN 1 ELSE 0 END) AS DECIMAL) / CAST(COUNT(*) AS DECIMAL) Yellow
,CAST(SUM(CASE WHEN StatusID = 3 THEN 1 ELSE 0 END) AS DECIMAL) / CAST(COUNT(*) AS DECIMAL) Blue
FROM #JournalEntry;