SQL: different aggregate values for different condition - sql

I have such a difficult problem from my knowledge.
I need to write a sole query that perform this action:
write a query that for the day Monday extract the sum of computer
sold, and for the rest of the days just the sum of Laptop.
For example I have this table
Day LaptopSold DesktopSold Total
Monday 2 2 4
Tuesday 2 3 5
Monday 1 1 2
Wednesday 2 2 4
Tuesday 1 4 5
The result should be this:
Day QtySold
Monday 6
Tuesday 3
wed 2
I can achieve the goal just writing two separate queries with the Group By for the Day field, but in one query for me is impossible!!!
Could you help me, please!!!
Thanks in advance
Lu

You can select the additive field with a CASE:
SELECT DAY,
SUM(CASE DAY
WHEN 'MONDAY' THEN TOTAL
ELSE LAPTOPSOLD
END) AS QtySold
FROM TBL
GROUP BY DAY

Related

Query to find SUM based on week

I have a table like date , sales , region
date
Sales
Region
11/02/2021
20
1
12/02/2021
23
1
13/02/2021
30
2
14/02/2021
50
2
15/02/2021
10
3
16/02/2021
10
3
How to extract sum of sales per region based on weeks (Week starting from Monday to Sunday)
You need to select the week before grouping.
This should work for you:
SELECT DATEPART(week, date) AS Week,
FROM table
GROUP BY DATEPART(week, RegistrationDate);

SQL count recurring values per week

I've asked the same question for pandas: link
And now I'm struggling to do the same thing with Big Query SQL. This is what I'm trying to achieve:
I have a Table containing dates and ids that are grouped by weeks
items_per_week:
date id
2022-02-07 1
3
5
4
2022-02-14 2
1
3
2022-02-21 9
10
1
...
...
2022-05-16 ....
I want to count for each week how much of the id's are repeating from previous week
For example the desired output for the Table would be:
date count
2022-02-07 0
2022-02-14 2 # because id 1 and 3 are present in previous week
2022-02-21 1 # because id 1 is present in previous week
...
I tried grouping the id and counting for each id how many are repeating for each date but it didn't work out as planned.
Try doing self-join and counting the results:
SELECT t1.date
,COUNT(t2.id) as count
FROM Table t1
LEFT JOIN Table t2
ON t1.date = DATE_SUB(t2.date, INTERVAL 7 DAY) -- finding previous week
AND t1.id = t2.id -- identifying matching ids
GROUP BY 1
Couple assumptions here:
id is unique per week (i.e you can't have duplicates in a week of 2022-02-07)
date is iterated over 7 days period (i.e you have one date per week)

Counting from monday to friday consecutively bigquery sql

I'm trying to find a result with two conditions in google bigquery.
Employees who worked from Monday through Friday consecutively will get an additional pay of 8 hour amount of wage.
Condition above is valid for workers who worked more than 15 hours (15 hrs <) per week.
id
date
hours
abc123
2020-01-05
12
abc123
2020-01-06
5
abc123
2020-01-07
14
abc123
2020-01-08
7
abc123
2020-01-09
6
abc123
2020-01-10
12
Thanks in advance.
Assuming you have one row per employee per day, then you can handle this by using window functions. The focus will be on Mondays, but the idea is to count the hours and days for a given row and the four days following.
So, to get the Mondays where a given id matches the conditions and is eligible for a bonus:
select id, date
from (select t.*,
count(*) over (partition by id
order by unix_date(date)
range between current row and 4 following
) as day_count,
sum(hours) over (partition by id
order by unix_date(date)
range between current row and 4 following
) as hours_count
from t
) t
where extract(dayofweek from date) = 2 and
day_count = 5 and
hours_count >= 15;

Using sum function with a condition based on a returned value

I have a set of given month with a number of hours related to each of it
DATE HOURS
8/1/2013 3
9/1/2013 8
10/1/2013 2
11/1/2013 4
12/1/2013 1
I need to return the sum of hours for everything that is in the past including current month, in the example below, starting in august, sum would be august only. For september, I'd need august + september
DATE HOURS SUM
8/1/2013 3 3
9/1/2013 8 11
10/1/2013 2 13
11/1/2013 4 17
12/1/2013 1 18
I am not sure how to proceed, since the date condition is different for each line.
If anyone can help on this, it'd be greatly appreciated
You can do this in most SQL dialects using a correlated subquery (or a non-equijoin, but I find the subquery cleaner):
select date, hours,
(select sum(t2.hours)
from t t2
where t2.date <= t.date
) as cum
from t;
Many SQL engines also support the cumulative sum function, which would typically look like this:
select date, hours sum(hours) over (order by date) as cum
from t

How to aggregate 7 days in SQL

I was trying to aggregate a 7 days data for FY13 (starts on 10/1/2012 and ends on 9/30/2013) in SQL Server but so far no luck yet. Could someone please take a look. Below is my example data.
DATE BREAD MILK
10/1/12 1 3
10/2/12 2 4
10/3/12 2 3
10/4/12 0 4
10/5/12 4 0
10/6/12 2 1
10/7/12 1 3
10/8/12 2 4
10/9/12 2 3
10/10/12 0 4
10/11/12 4 0
10/12/12 2 1
10/13/12 2 1
So, my desired output would be like:
DATE BREAD MILK
10/1/12 1 3
10/2/12 2 4
10/3/12 2 3
10/4/12 0 4
10/5/12 4 0
10/6/12 2 1
Total 11 15
10/7/12 1 3
10/8/12 2 4
10/9/12 2 3
10/10/12 0 4
10/11/12 4 0
10/12/12 2 1
10/13/12 2 1
Total 13 16
--------through 9/30/2013
Please note, since FY13 starts on 10/1/2012 and ends on 9/30/2012, the first week of FY13 is 6 days instead of 7 days.
I am using SQL server 2008.
You could add a new computed column for the date values to group them by week and sum the other columns, something like this:
SELECT DATEPART(ww, DATEADD(d,-2,[DATE])) AS WEEK_NO,
SUM(Bread) AS Bread_Total, SUM(Milk) as Milk_Total
FROM YOUR_TABLE
GROUP BY DATEPART(ww, DATEADD(d,-2,[DATE]))
Note: I used DATEADD and subtracted 2 days to set the first day of the week to Monday based on your dates. You can modify this if required.
Use option with GROUP BY ROLLUP operator
SELECT CASE WHEN DATE IS NULL THEN 'Total' ELSE CONVERT(nvarchar(10), DATE, 101) END AS DATE,
SUM(BREAD) AS BREAD, SUM(MILK) AS MILK
FROM dbo.test54
GROUP BY ROLLUP(DATE),(DATENAME(week, DATE))
Demo on SQLFiddle
Result:
DATE BREAD MILK
10/01/2012 1 3
10/02/2012 2 4
10/03/2012 2 3
10/04/2012 0 4
10/05/2012 4 0
10/06/2012 2 1
Total 11 15
10/07/2012 1 3
10/08/2012 4 7
10/10/2012 0 4
10/11/2012 4 0
10/12/2012 2 1
10/13/2012 2 1
Total 13 16
You are looking for a rollup. In this case, you will need at least one more column to group by to do your rollup on, the easiest way to do that is to add a computed column that groups them into weeks by date.
Take a lookg at: Summarizing Data Using ROLLUP
Here is the general idea of how it could be done:
You need a derived column for each row to determine which fiscal week that record belongs to. In general you could subtract that record's date from 10/1, get the number of days that have elapsed, divide by 7, and floor the result.
Then you can GROUP BY that derived column and use the SUM aggregate function.
The biggest wrinkle is that 6 day week you start with. You may have to add some logic to make sure that the weeks start on Sunday or whatever day you use but this should get you started.
The WITH ROLLUP suggestions above can help; you'll need to save the data and transform it as you need.
The biggest thing you'll need to be able to do is identify your weeks properly. If you don't have those loaded into tables already so you can identify them, you can build them on the fly. Here's one way to do that:
CREATE TABLE #fy (fyear int, fstart datetime, fend datetime);
CREATE TABLE #fylist(fyyear int, fydate DATETIME, fyweek int);
INSERT INTO #fy
SELECT 2012, '2011-10-01', '2012-09-30'
UNION ALL
SELECT 2013, '2012-10-01', '2013-09-30';
INSERT INTO #fylist
( fyyear, fydate )
SELECT fyear, DATEADD(DAY, Number, DATEADD(DAY, -1, fy.fstart)) AS fydate
FROM Common.NUMBERS
CROSS APPLY (SELECT * FROM #fy WHERE fyear = 2013) fy
WHERE fy.fend >= DATEADD(DAY, Number, DATEADD(DAY, -1, fy.fstart));
WITH weekcalc AS
(
SELECT DISTINCT DATEPART(YEAR, fydate) yr, DATEPART(week, fydate) dt
FROM #fylist
),
ridcalc AS
(
SELECT
ROW_NUMBER() OVER (ORDER BY yr, dt) AS rid, yr, dt
FROM weekcalc
)
UPDATE #fylist
SET fyweek = rid
FROM #fylist
JOIN ridcalc
ON DATEPART(YEAR, fydate) = yr
AND DATEPART(week, fydate) = dt;
SELECT list.fyyear, list.fyweek, p.[date], COUNT(bread) AS Bread, COUNT(Milk) AS Milk
FROM products p
JOIN #fylist list
ON p.[date] = list.fydate
GROUP BY list.fyyear, list.fyweek, p.[date] WITH ROLLUP;
The Common.Numbers reference above is a simple numbers table that I use for this sort of thing (goes from 1 to 1M). You could also build that on the fly as needed.