Trouble with Select statement [closed] - sql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a following select query That runs well
SELECT
division,
division_name,
object_account,
account_description,
Sum(value) AS PreferedSpend
FROM jde_dw.blanket_rpt
WHERE division = 02
AND vendor_type = 'PV'
AND month = 07
AND year = 2013
GROUP BY division, division_name, object_account, account_description
ORDER BY division
Now I want to add a column that shows total spend it would be the same query as above but without the vendor_type condition in the where statement.
So the final query will show Division, division_name, Object_account, Account_Description, Value( as preferred ) and value as( total which includes everything)
How can I do this?

Move the condition inside the SUM() so that only matching rows contribute to the sum:
SELECT
division,
division_name,
object_account,
account_description,
SUM(CASE WHEN vendor_type = 'PV' THEN value ELSE 0 END) as PreferedSpend,
SUM(value) AS TotalSpend
FROM jde_dw.blanket_rpt
WHERE division = 02
AND month = 07
AND year = 2013
GROUP BY division, division_name, object_account, account_description
ORDER BY division

Related

How to aggregate event dates based on aggregated table? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 16 days ago.
Improve this question
I have a table that aggregates disables/reenables events in the following format:
Disabled Date
Enabled Date
Count
01/01
01/01
5
01/01
02/01
2
03/01
05/01
1
04/01
05/01
5
and want to build a report that aggregates the number of disables and reenables per day:
Date
Enables
Disables
01/01
5
7
02/01
2
0
03/01
0
1
04/01
0
5
05/01
6
0
I was able to build the following query that works for days that have at least one disable and one enable:
SELECT
disables.disable_date AS disable_date,
disables.disable_count disable_count,
enables.enable_count enable_count
FROM
(SELECT
disable_date,
sum(disable_count) disable_count
FROM table
GROUP BY 1) AS disables,
(SELECT
enable_date,
sum(disable_count) enable_count
FROM table
GROUP BY 1) AS enables
WHERE enables.enable_date = disables.disable_date;
Any suggestions how to build the complete output? I'm not sure this is the right strategy, so a JOIN could also be considered.
Thanks!
;WITH cte
AS (SELECT disabled_date AS Date,
Sum(count) AS Disables
FROM table1
GROUP BY disabled_date
UNION ALL
SELECT enabled_date AS Date,
Sum(-count) AS Disables
FROM table1
GROUP BY enabled_date)
SELECT cte.date,
COALESCE(Sum(CASE
WHEN cte.disables > 0 THEN cte.disables
END), 0) AS Enables,
COALESCE(Sum(CASE
WHEN cte.disables < 0 THEN -cte.disables
END), 0) AS Disables
FROM cte
GROUP BY cte.date
ORDER BY cte.date;
TEST
http://sqlfiddle.com/#!18/13cace/3

Stuck with database query [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 months ago.
Improve this question
If I run this query:
select date, sum(orders) as "Total orders"
from tbl
where date >=1 and date <= 4
group by date
How can I get the expected output as shown in the screenshot?
Use a left against generate_series():
select g.date,
sum(tbl.orders) as "Total orders"
from generate_series(1,4) as g(date)
left join tbl on tbl.date = g.date
group by g.date
WITH cte as (
SELECT date, orders
FROM dbo.Orders
),
cte2 as (
select top (SELECT MAX(date) FROM cte) ROW_NUMBER() over(order by a.name)
as date
from sys.all_objects a
)
SELECT cte2.date, SUM(ISNULL(orders, 0))
FROM cte2 LEFT JOIN cte ON cte2.date = cte.date
GROUP BY cte2.date

bring many years (oracle plsql) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
hi this query bring for "one year 2020" ; but, i want to bring this query.
state_name ,2020 total deaths ,2021 total deaths
One option might be
select
s.statename,
sum(case when extract(year from st.date_) = 2020 then st.death end) deaths_2020,
sum(case when extract(year from st.date_) = 2021 then st.death end) deaths_2021
from statecases1 st join state_ s on s.statecode = st.statecode
group by s.statename
order by s.statename;
Your query can't return data for 2020 and 2021 because of the WHERE clause you used (you restricted dates to year 2020).
You can use this. Although i am not 100% clear but this is what i thought you need - sum by state and year. You can also use pivot.
WITH yearly_data AS
(SELECT s.statename, to_char(st.date_, 'YYYY') AS YEAR, sum(death) death
FROM covid.statecases1 st
JOIN covid_state_ s ON s.statecode=st.statecode
GROUP BY s.statename, to_char(st.date_, 'YYYY'))
SELECT DISTINCT a.statename, b.death 2020_death,c.death 2021_death
FROM yearly_data a
LEFT JOIN yearly_data b ON a.statename=b.statename AND b.year='2020'
LEFT JOIN yearly_data c ON a.statename=c.statename AND c.year='2021'
ORDER BY 1

How to group by quarters(1 year = 4 quarters) in SQL? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Question Description:Show for each customer and product combination, the average sales quantities for 4 quarters,Q1, Q2, Q3 and Q4(in four separate columns) –Q1 being the first 3 months of the year (Jan, Feb & Mar), Q2 the next 3 months (Apr, May & Jun), and so on–ignore the YEAR component of the dates (i.e., 3/11/2001is considered the same date as 3/11/2002, etc.).Also compute the average for the “whole” year(again ignoring the YEAR component, meaning simply compute AVG) along with the total quantities(SUM) and the counts(COUNT).
table as follow:enter image description here
The sample result:enter image description here
I am a beginner on SQL and I met such a problem. It needs to group the 4 quarters just by using aggregate functions and group by clause.
with cte as (
select
customer,
product,
case
when (month < 4) then 'Q1'
when (month >= 4 AND month < 7) then 'Q2'
when (month >= 7 AND month < 10) then 'Q3'
when (month >= 10) then 'Q4'
end AS quarter,
quant
from bgg.table_sof
),
cte_full as (
select
customer,
product,
avg(quant) as average,
sum(quant) as total,
count(quant) as count
from bgg.table_sof
GROUP BY customer, product
)
select
cte_full.customer,
cte_full.product,
avg(cte1.quant) as Q1_AVG,
avg(cte2.quant) as Q2_AVG,
avg(cte3.quant) as Q3_AVG,
avg(cte4.quant) as Q4_AVG,
average,
total,
count
from cte_full
left join cte cte1 on cte_full.customer = cte1.customer and cte_full.product = cte1.product
left join cte cte2 on cte_full.customer = cte2.customer and cte_full.product = cte2.product
left join cte cte3 on cte_full.customer = cte3.customer and cte_full.product = cte3.product
left join cte cte4 on cte_full.customer = cte4.customer and cte_full.product = cte4.product
where cte1.quarter = 'Q1' OR cte2.quarter = 'Q2' OR cte3.quarter = 'Q3' OR cte4.quarter = 'Q4'
group by cte_full.customer, cte_full.product, average, total, count;
It's a tricky task for a beginner. My advice is to start with some theory and easy practice.
There is two CTE (common table expression):
the first is for defining quarters from months.
the second (cte_full) for calculating the last 3 columns in the result table.
And final select joins separately calculated avg by quarters to result.
I'm sure it isn't easy for understanding for beginner, feel free to ask.

Get rows by id or all rows if id is 0 [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a stored procedure that gets all rows in a table with a specific ID. What I want to do is get all rows from this table if the store_id is 0.
This is the code I use right now:
SELECT store_id, SUM(traffic) FROM traffic
WHERE store_id = #store_id AND year = #year
GROUP BY store_id
I want to change this query so that it returns all rows regardless of the store_id when #store_id = 0
Try the below
SELECT store_id, SUM(traffic) FROM traffic
WHERE (store_id = #store_id OR #store_id = 0) AND year = #year
GROUP BY store_id
IF #store_id = 0
BEGIN
SELECT store_id, SUM(traffic) FROM traffic
WHERE year = #year
GROUP BY store_id
END
ELSE
BEGIN
SELECT store_id, SUM(traffic) FROM traffic
WHERE store_id = #store_id AND year = #year
GROUP BY store_id
END
Test your store_id before making a request. If the store_id == 0, then make another request to get all regarding the store_id.
Try this, if #store_id may also be NULL then do the following
SELECT store_id, SUM(traffic) FROM traffic
WHERE (store_id = #store_id OR ISNULL(#store_id, 0) = 0)
AND year = #year
GROUP BY store_id