more than one AVG column with diffrent conditions - sql

I have a table as follows:
id year value
1 2012 10
2 2013 7
3 2013 7
4 2014 8
5 2014 10
6 2015 6
7 2011 12
I need to write a query which gives the AVG value of the last 4 years from today. Meaning that if today is 2016 then the AVG is on 2015,2014,2013.
Basicly this could be done with 3 queries:
Select avg(value) as a
from tab
where year=2015
and
Select avg(value) as b
from tab
where year=2014
and
Select avg(value) as c
from tab
where year=2013
The results based on the given values should be:
2013 7
2014 9
2015 6
Since all of them is on the same table... How can I do that in one query (postgresql)?
it should be without a WHERE.
Something like:
Select avg(with condition) as a, avg(with condition) as b, avg(with condition) as c
from tab

You can group by year and constrict to the years you want in your where clause
select avg(value), year
from tab
where year in (2013,2014,2015)
group by year
The query above will give you 3 separate rows. If you prefer a single row then you can use conditional aggregation instead of a group by
select
avg(case when year = 2013 then value end) as avg_2013,
avg(case when year = 2014 then value end) as avg_2014,
avg(case when year = 2015 then value end) as avg_2015,
from tab
where year in (2013,2014,2015)

select
avg(case when year = date_part('year', NOW()) then value end) as avg_2016,
avg(case when year = ((date_part('year', NOW())) - 1 ) then value end) as avg_2015,
avg(case when year = ((date_part('year', NOW())) - 2 ) then value end) as avg_2014,
avg(case when year = ((date_part('year', NOW())) - 3 ) then value end) as avg_2013
from tab

Related

FInd records 1 year older/newer than dates given in the same column, for each ID#

I need to find if a customer has a subscription the previous year and the following year, and how many subscriptions were new or were canceled the following year.
Sample data:
ID
Subscription year
1
2010
1
2011
1
2019
2
2011
2
2012
3
2010
Thinking of this approach: subtracting and adding 1 to the subscription year and seeing if the customer ID has another row that corresponds (ex. if no rows for year+1, customer had canceled the next year). Hoping for something like this:
ID
Subscription year
SubscribedPreviousYear
SubscribedNextYear
1
2010
F
T
1
2011
T
F
1
2019
F
F
2
2011
F
T
2
2012
T
F
3
2010
F
F
Then counting the F's in SubscribedPreviousYear as new subscriptions (they are counted as new if customer did not have one the immediate previous year, even for existing customers) and F's in SubscribedNextYear as canceled subscriptions, to get something like this:
Year
New (# F's in SubscribedPreviousYear)
Canceled (# F's in SubscribedPreviousYear)
2010
2
1
2011
1
1
2012
0
1
2019
1
1
I had tried this code, modified from a similar MySQL question, but got 'F' for all rows.
select
t1.Id, cast(t1.year as date),
IIF((select count(*) from table t2
where t1.Id=t2.Id and
datediff(y, t2.year, t1.year)=1) <1, 'T','F')
as SubscribedPreviousYear
from table t;
I would use LEAD() and LAG() here:
SELECT id, year,
CASE WHEN LAG(year) OVER (PARTITION BY id ORDER BY year) = year - 1
THEN 'T' ELSE 'F' END AS SubscribedPreviousYear,
CASE WHEN LEAD(year) OVER (PARTITION BY id ORDER BY year) = year + 1
THEN 'T' ELSE 'F' END AS SubscribedNextYear
FROM yourTable
ORDER BY id, year;
To get the final result, we can aggregate by year:
WITH cte AS (
SELECT *,
LAG(year) OVER (PARTITION BY id ORDER BY year) AS year_lag,
LEAD(year) OVER (PARTITION BY id ORDER BY year) AS year_lead
FROM yourTable
)
SELECT year,
COUNT(CASE WHEN year != year_lag + 1 THEN 1 END) AS [New],
COUNT(CASE WHEN year != year_lead - 1 THEN 1 END) AS Cancelled
FROM cte
GROUP BY year
ORDER BY year;

How to calculate the average per day for different years

I am trying to calculate the average number of times apple with an increment of 3 are shown per day in the years of both 2018 and 2017. To do this I am trying to use setNum and exNum that has a difference of 3.
ID Year Text setNum ExNum
-------------------------------------------------
1 2018-01-21 apple 1 3
2 2017-08-03 apple 2 5
3 2018-03-02 banana 1 3
4 2018-05-22 apple 1 3
5 2018-12-12 apple 3 6
6 2017-04-13 apple 3 6
My current query to obtain this is:
SELECT
2017 = avg(case when Year BETWEEN '2017-01-01' AND '2017-12-31' then 1 else 0 end),
2018 = avg(case when Year BETWEEN '2018-01-01' AND '2018-12-31' then 1 else 0 end)
FROM
exampleTable
WHERE
Text LIKE '%apple%'
This currently outputs:
2017 2018
0 0
Note: The original table had a single text column Increment, which had values like 1-3. That is, the 1-3 represented a setNum of 1 and an ExNum of 3.
Your decision to store a numerical increment range as text is not a good one, and ideally you should be storing the two points of the increment in separate columns. That being said, we can do some string olympics to work around this:
SELECT
YEAR(Year) AS Year,
COUNT(CASE WHEN 3 BETWEEN CAST(LEFT(Increment, CHARINDEX('-', Increment)-1) AS int) AND
CAST(RIGHT(Increment, LEN(Increment) - CHARINDEX('-', Increment)) AS int)
THEN 1 END) AS apple_3_cnt
FROM exampleTable
WHERE
TEXT LIKE '%apple%'
GROUP BY
YEAR(year);
Demo
Here I am aggregating by year, and then taking a conditional count of record, for each year, where the apple increment range contains 3. To do this, I separate out the two ends of the increment range, and then convert them to integers.
Edit:
Based on your updated table, we can try a simpler query:
SELECT
YEAR(Year) AS Year,
COUNT(CASE WHEN 3 BETWEEN setNum AND ExNum THEN 1 END) AS apple_3_cnt
FROM exampleTable
WHERE
TEXT LIKE '%apple%'
GROUP BY
YEAR(year);
Try below
SELECT
avg(case when Year BETWEEN '2017-01-01' AND '2017-12-31' then setNum+ExNum end) as 2017
avg(case when Year BETWEEN '2018-01-01' AND '2018-12-31' then setNum+ExNum end) as 2018
FROM
exampleTable
WHERE
Text LIKE '%apple%'
Your query is fine. The only problem is how and to where you assign the results.
Use this syntax instead
SELECT
avg(case when Year BETWEEN '2017-01-01' AND '2017-12-31' then 1 else 0 end) as A2017,
avg(case when Year BETWEEN '2018-01-01' AND '2018-12-31' then 1 else 0 end) as A2018
FROM
exampleTable
WHERE
Text LIKE '%apple%'
Note that you can't use numbers as variable names.

Display 12 months of data from the past 5 years

I am currently creating a script that will pull 5 years of invoice data and will summarize the invoice amounts by month of that year for a specific customer. Example
Year jan feb mar
2011 800 900 700
2012 700 800 900, and so forth
I am having issues getting my output to be like this though. My current code
select MAX(cust) as customer,year(invoicedate) as y, month(invoicedate) as m, sum(amount) as summary
from #tquery
group by year(dinvoice), month(dinvoice)
having MAX(ccustno) ='WILLAMETTE'
order by y asc,m asc
select * from #tquery
gives me this. which i just need to find a way to reformat it.
customer year month amount
WILLAMETTE 2012 11 500
WILLAMETTE 2012 12 600
WILLAMETTE 2013 1 600
No need to go through a Pivot. It is only 12 columns. A conditional aggregation would be more efficient
Select Customer = cust
,Year = year(invoicedate)
,Jan = sum(case when month(invoicedate) = 1 then amount else 0 end)
,Feb = sum(case when month(invoicedate) = 2 then amount else 0 end)
...
,Dec = sum(case when month(invoicedate) =12 then amount else 0 end)
From #tquery
Group by ccustno,year(dinvoice)
Order By 1,2
You must using PIVOT to reformat rows to column
select customer
,y
,"1","2","3","4","5","6","7","8","9","10","11","12"
from (select cust as customer,year(invoicedate) as y, month(invoicedate) as m,amount
from #tquery
where ccustno ='WILLAMETTE'
)
t
pivot (sum (amount) for m in ("1","2","3","4","5","6","7","8","9","10","11","12")) p
order by y
;

How to calculate a growing or decreasing percentage between rows usign group by

Let's suposse that I have this table_1:
Year Item Qty_sold
2013 1 3
2013 2 2
2013 3 5
2014 1 2
2014 2 3
I'll perform something like this
select year , sum(Qty_sold) as Quantity
from table_1 inner join table_2 on .... inner join table_n
where Year = 2014
The final result depends mostly on the filter by year, but there are other tables involved.
But as a result I need something like this:
Year Quantity Diff_Percentage
2014 5 0.5
Because during the previous year (2013) the final quantity of items sold was 10.
Regards
You seem to want something like this:
select year, sum(Qty_sold) as Quantity,
lag(sum(qty_sold)) over (order by year) as prev_Quantity,
(1 - Quantity / lag(sum(qty_sold)) over (order by year)) as diff_percentage
from table
group by Year;
Of course, this returns the info for all years. If you really just want the year 2014 and 2013 then use conditional aggregation:
select year,
sum(case when year = 2014 then Qty_sold end) as Quantity_2014,
sum(case when year = 2013 then Qty_sold end) as Quantity_2013,
(1 - sum(case when year = 2014 then Qty_sold end)/
sum(case when year = 2013 then Qty_sold end)
) as diff_percentage
from table
where Year in (2013, 2014);
I'm sort of guessing on the formula for diff_percentage, but I think that's what you want.
As per your requirement, please try this
select t.year,qty, (1-qty/prev_qty)
from
(select year, sum(Qty_sold) as qty,
lag(sum(qty_sold)) over (order by year) as prev_qty
from tbl group by Year) t where t.year=in_year --in_year whichever year record you want.
Hope it works for you.

How to subtract result of 2 queries grouped by a field

I have a table in this form:
id year type amount
1 2015 in 10
2 2015 out 5
3 2016 in 20
4 2016 out 1
...
The followin query will give me the sum of the amount of type = 'in' grouped by year:
SELECT year, sum(amount)
FROM table
WHERE type = in
GROUP BY year
How am I going to get the following result?
year sum(in) sum(out) "in-out"
2015 10 5 5
2016 20 1 19
sum(in) is the sum of the 'amount' where type='in'.
Use a CASE statement to handle the values of type.
SELECT year,
SUM(CASE WHEN type = 'in' THEN amount ELSE 0 END) AS sum_in,
SUM(CASE WHEN type = 'out' THEN amount ELSE 0 END) AS sum_out,
SUM(CASE WHEN type = 'in' THEN amount ELSE -amount END) AS in_out
FROM table
GROUP BY year;