So, I have the following query,
WITH yearlist AS
(
SELECT (year(getdate())+3) AS years
UNION ALL
SELECT y.years - 1 AS years
FROM yearlist y
WHERE y.years - 1 >= (YEAR(GETDATE())-10)
)
SELECT
a.years as [year],
a.CountryName as country,
ISNULL(sum(b.sales), 0) as total
FROM(
SELECT
distinct years
,g.CountryName
FROM
yearlist AS A CROSS JOIN (SELECT
CountryName, salesYear, ISNULL(sum(sales), 0) as total
FROM tblSales where
salesYear BETWEEN (year(getdate())-12) AND (year(getdate()) + 3)
,sales
,salesYear) g
) a left outer join
(SELECT
CountryName, salesYear, ISNULL(sum(sales), 0) as total
FROM tblSales where
salesYear BETWEEN (year(getdate())-12) AND (year(getdate()) + 3)
group by CountryName
,salesYear, sales
) b ON a.CountryName=b.CountryName and a.years=b.salesYear
group by a.CountryName,years
order by years
I am getting the following returned:
year country Total
---------- ---------------------------------------- -------
2009 France 0.00
2009 Japan 0.00
2009 Norway 2.30
2009 Portugal 0.00
2009 South Korea 0.00
2009 Spain 0.00
2009 Sweden 0.00
2009 United Kingdom 0.00
2009 United States 0.00
2010 France 0.00
2010 Japan 0.00
2010 Norway 0.00
2010 Portugal 0.00
2010 South Korea 0.00
2010 Spain 0.00
2010 Sweden 0.00
2010 United Kingdom 0.00
2010 United States 0.00
2011 France 0.00
2011 Japan 0.00
2011 Norway 0.00
2011 Portugal 2.00
2011 South Korea 0.00
2011 Spain 0.00
2011 Sweden 0.00
2011 United Kingdom 0.00
2011 United States 0.00
2012 France 0.00
2012 Japan 0.01
2012 Norway 0.00
2012 Portugal 0.00
2012 South Korea 0.00
2012 Spain 0.00
2012 Sweden 0.00
2012 United Kingdom 0.00
2012 United States 0.00
2013 France 0.00
2013 Japan 2.00
2013 Norway 0.00
2013 Portugal 0.00
2013 South Korea 0.00
2013 Spain 0.00
2013 Sweden 0.00
2013 United Kingdom 0.00
2013 United States 0.00
I am trying to achieve a cumulative total for each country, as the years increase. But I cant seem to get it. I've tried this:
sum(sales) over (order by salesYear rows unbounded preceding) as total
But that just filled each row with the cumulative total.
The output I desire is as follows:
year country Total
---------- ---------------------------------------- -------
2009 France 0.00
2010 France 0.00
2011 France 0.00
2009 Japan 0.00
2010 Japan 0.00
2011 Japan 0.00
2009 Norway 2.30
2010 Norway 2.30
2011 Norway 2.30
2009 Portugal 0.00
2010 Portugal 0.00
2011 Portugal 2.00
2009 South Korea 0.00
2010 South Korea 0.00
2011 South Korea 0.00
2009 Spain 0.00
2010 Spain 0.00
2011 Spain 0.00
2009 Sweden 0.00
2010 Sweden 0.00
2011 Sweden 0.00
2009 United Kingdom 0.00
2010 United Kingdom 0.00
2011 United Kingdom 0.00
2009 United States 0.00
2010 United States 0.00
2011 United States 0.00
I just cant seem to get them to individually accumulate.
You most likely need a partition by clause, too:
sum(sum(sales)) over (partition by country order by salesYear rows unbounded preceding)
Related
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 7 months ago.
Improve this question
i have this table
declare #table table(year int, code int, import decimal(5,2))
insert into #table values
(2019,390107,10.00),
(2021,390107,175.00),
(2022,390107,102.00),
(2022,470101,101.00),
(2022,53015101,140.00)
i want to make a query that returns the import for each year and for each code contained in the following tables (return import = 0 where there is no record for a specific combination of year and code):
declare #years table (year int)
insert into #years values
(2018),
(2019),
(2020),
(2021),
(2022)
declare #codes table (code int)
insert into #codes values
(390107),
(470101),
(470103),
(471103),
(53010101),
(53015101)
i tried with somethig like that:
select
y.year,
c.code,
isnull(t.import,0)
from #table t
right join #years y on t.year = y.year
right join #codes c on t.code = c.code
the query does not return errors (is not a problem of using create Vs declare, nor a a problem with the tables name). but i don't get the result expected:
Expected Results
Having 6 codes and 5 years I expect 30 records (one for each combination of year and code) along with the corresponding import value from "#table" for that year/code combination (or 0 if the combination is not found)
year
code
import
2018
390107
0.00
2018
470101
0.00
2018
470103
0.00
2018
471103
0.00
2018
53010101
0.00
2018
53015101
0.00
2019
390107
10.00
2019
470101
0.00
2019
470103
0.00
2019
471103
0.00
2019
53010101
0.00
2019
53015101
0.00
2020
390107
0.00
2020
470101
0.00
2020
470103
0.00
2020
471103
0.00
2020
53010101
0.00
2020
53015101
0.00
2021
390107
175.00
2021
470101
0.00
2021
470103
0.00
2021
471103
0.00
2021
53010101
0.00
2021
53015101
0.00
2022
390107
102.00
2022
470101
101.00
2022
470103
0.00
2022
471103
0.00
2022
53010101
0.00
2022
53015101
140.00
You need to cross join the Codes and Years to get every combination and then use that in the outer join
SELECT Y.year,
C.code,
ISNULL(T.import, 0) AS import
FROM #years Y
CROSS JOIN #codes C
LEFT JOIN #table T
ON T.year = Y.year
AND T.code = C.code
I have a pandas dataframe as below -
Federation Game Medal_each_game
0 AFG Athletics 1.00
1 AFG Boxing 0.00
2 AFG Football 1.00
3 AFG Hockey 0.00
4 AFG Taekwondo 2.00
5 AFG Wrestling 0.00
6 AHO Athletics 0.00
7 AHO Boxing 3.00
8 AHO Fencing 2.00
9 AHO Football 0.00
I need to find highest medal count per 'federation' and get the 'Game'
output should be something like this
Federation Game Medal_each_game
0 AFG Taekwondo 2.00
1 AHO Boxing 3.00
Use groupby_idxmax:
>>> df.loc[df.groupby('Federation')['Medal_each_game'].idxmax()]
Federation Game Medal_each_game
4 AFG Taekwondo 2.0
7 AHO Boxing 3.0
I am wondering if what I am trying to do is possible. I believe it is using the PIVOT function in TSQL but don't have enough experience with the PIVOT function to know where to start.
Basically I'm trying to take the following # table called #tmpbudgetdata (truncated for simplicity):
Account Description BudgetAmount Period
-------------------- ---------------------------------------------------------------------------------------------------- --------------------- --------------------
4001 Mood Embedded Account 0.00 1
4001 Mood Embedded Account 0.00 2
4001 Mood Embedded Account 0.00 3
4001 Mood Embedded Account 0.00 4
4001 Mood Embedded Account 0.00 5
4001 Mood Embedded Account 0.00 6
4001 Mood Embedded Account 0.00 7
4001 Mood Embedded Account 0.00 8
4001 Mood Embedded Account 0.00 9
4001 Mood Embedded Account 0.00 10
4001 Mood Embedded Account 0.00 11
4001 Mood Embedded Account 0.00 12
4003 DBS Music 0.00 1
4003 DBS Music 0.00 2
4003 DBS Music 0.00 3
4003 DBS Music 0.00 4
4003 DBS Music 0.00 5
4003 DBS Music 0.00 6
4003 DBS Music 0.00 7
4003 DBS Music 0.00 8
4003 DBS Music 0.00 9
4003 DBS Music 0.00 10
4003 DBS Music 0.00 11
4003 DBS Music 0.00 12
4010 Sales - Software 5040.00 1
4010 Sales - Software 0.00 2
4010 Sales - Software 6280.56 3
4010 Sales - Software 6947.93 4
4010 Sales - Software 4800.00 5
4010 Sales - Software 0.00 6
4010 Sales - Software 2400.00 7
4010 Sales - Software 2550.00 8
4010 Sales - Software 4800.00 9
4010 Sales - Software 2400.00 10
4010 Sales - Software 0.00 11
4010 Sales - Software 2400.00 12
4015 New Install Revenue 0.00 1
4015 New Install Revenue 0.00 2
4015 New Install Revenue 0.00 3
4015 New Install Revenue 3844.79 4
4015 New Install Revenue 0.00 5
4015 New Install Revenue 0.00 6
4015 New Install Revenue 0.00 7
4015 New Install Revenue 0.00 8
4015 New Install Revenue 0.00 9
4015 New Install Revenue 0.00 10
4015 New Install Revenue 0.00 11
4015 New Install Revenue 0.00 12
and turning it into something like this:
Account Description Period1 Period2 Period3 Period4 Period5 Period6 Period7 Period8 Period9 Period10 Period11 Period12
------- --------------- -------- ------- -------- ------ ------- ------- -------- ------ ------- -------- -------- --------
4001 Mood Enabled... 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
4003 Dbs Music 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
4010 Sales - Software 5040.00 0.00 6280.56 6947.93 4800.00 0.00 2400.00 2550.00 4800.00 2400.00 0.00 2400.00
...etc...
Basically just grouping via the Account column (the description is the same per account) and then taking the period values and pivoting them horizontally.
I know I could do it with a cursor and loop through but wondering if this is possible with a pivot or by other means.
Thanks in advance
I simple PIVOT should do the trick
Example
Select *
From (
Select [Account]
,[Description]
,Period = concat('Period',Period)
,[BudgetAmount]
From YourTable
) src
Pivot (sum([BudgetAmount]) for Period in ( [Period1],[Period2],[Period3],[Period4],[Period5],[Period6],[Period7],[Period8],[Period9],[Period10],[Period11],[Period12] ) ) pvt
Returns
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 6 years ago.
Improve this question
I tried to avoid using VBA for this due to my lack of familiarity, but it looks like this is complex enough to require the Excel native language.
What I have is a CSV with a list of employees hours worked each day, all in separate rows. Employee name, Date, Regular Hours, and OT Hours are in separate columns. The challenge is that for employee Bob, there may be 20 rows for one day. Bob applies a slice of time to different projects all day. Then of course there are multiple days and multiple employees. What I am trying to end up with is a report that shows all of the regular and OT hours (separately) for each employee on a daily basis.
What I can't wrap my head around is how to start the compilation. I am guessing that separating each employee would be the start. Then separating each date, then adding all hours for that date.
I appreciate any assistance.
Emp# Name Date Reg OT
Emp1 Bob 1/1/2016 8.00 0.00
Emp1 Bob 1/4/2016 3.00 0.00
Emp1 Bob 1/4/2016 5.00 0.00
Emp1 Bob 1/5/2016 2.00 0.00
Emp1 Bob 1/5/2016 1.00 0.00
Emp1 Bob 1/5/2016 5.00 0.00
Emp1 Bob 1/6/2016 1.00 0.00
Emp1 Bob 1/6/2016 2.00 0.00
Emp1 Bob 1/6/2016 5.00 0.00
Emp1 Bob 1/7/2016 2.00 0.00
Emp2 Henry 1/1/2016 8.00 0.00
Emp2 Henry 1/4/2016 8.00 0.00
Emp2 Henry 1/5/2016 8.00 0.00
Emp2 Henry 1/6/2016 2.00 0.00
Emp2 Henry 1/6/2016 6.00 0.00
Emp2 Henry 1/7/2016 1.50 0.00
Emp2 Henry 1/7/2016 0.50 0.00
Emp2 Henry 1/7/2016 6.00 0.00
Emp2 Henry 1/8/2016 8.00 0.00
Emp2 Henry 1/11/2016 8.00 0.00
Emp2 Henry 1/12/2016 3.00 0.00
Emp2 Henry 1/12/2016 1.00 0.00
Emp2 Henry 1/12/2016 3.00 0.00
Emp2 Henry 1/12/2016 1.00 0.00
Emp2 Henry 1/13/2016 1.50 0.00
Your description sounds like you want to use a pivot table. They are easy to build - this example literally took me 5 minutes to build, including typing in the data.
As an illustration, you can take data that looks like this ...
and consolidate it in a way that provides a lot of flexibility in looking at it. Such as this:
or this
There are several good, simple tutorials available on building Pivot Tables. A google search turns up plenty.
I have a Table (parts) where I store when an item was requested and when it was issued. With this, I can easily compute each items turn-around-time ("TAT"). What I'd like to do is have another column ("Computed") where any overlapping request-to-issue dates are properly computed.
RecID Requested Issued TAT Computed
MD0001 11/28/2012 12/04/2012 6.00 0.00
MD0002 11/28/2012 11/28/2012 0.00 0.00
MD0003 11/28/2012 12/04/2012 6.00 0.00
MD0004 11/28/2012 11/28/2012 0.00 0.00
MD0005 11/28/2012 12/10/2012 12.00 0.00
MD0006 11/28/2012 01/21/2013 54.00 54.00
MD0007 11/28/2012 11/28/2012 0.00 0.00
MD0008 11/28/2012 12/04/2012 6.00 0.00
MD0009 01/29/2013 01/30/2013 1.00 1.00
MD0010 01/29/2013 01/30/2013 1.00 0.00
MD0011 02/05/2013 02/06/2013 1.00 1.00
MD0012 02/07/2013 03/04/2013 25.00 25.00
MD0013 03/07/2013 03/14/2013 7.00 7.00
MD0014 03/07/2013 03/08/2013 1.00 0.00
MD0015 03/13/2013 03/25/2013 12.00 11.00
MD0016 03/20/2013 03/21/2013 1.00 0.00
Totals 133.00 99.00 <- waiting for parts TAT summary
In the above, I manually filled in the ("Computed") column so that there is an example of what I'm trying to accomplish.
NOTE: Notice how MD0013 affects the computed time for MD0015 as MD0013 was "computed" first. This could have been where MD0015 was computed first, then MD0013 would be affected accordingly - the net result is there is -1 day.