In my table, positive numbers are stored as negatives and vice versa. This makes sense from an accounting point of view, however, I would like to make a select statement that returns the numbers reversed.
Example as stored in db:
YEAR MONTH REVENUE
-------------------------
2017 12 -12000,00
2017 11 -1500,00
2017 10 30000,00
This tells me that there was a surplus in December 2017 and a deficit in November and October.
Is there a function that does this or do I need some more advanced SQL wizardry?
Disclaimer: I understand this question might have been asked several times before. I am asking again because I am looking for a more simplified answer (if possible?)
As stated in comment multiply by -1
Select
Year,
Month,
Revenue * -1 as Revenue
From Table
Related
Please share your feedback on this problem. I need to calculate difference in 'years' and store it under a new column 'Age'.
While the formula works fine, it gives me incorrect output when I consider dates starting from 1st Jan of any year
For example: difference in years between 1st Jan 2019 and 31st Dec 2021 is 3 years - this includes end date in calculation. My result shows 2 years.
Here are the 2 date columns from which I am deriving the difference:
However, when I consider dates from 1st Jan - the result shows me one year less:
Here is the code I used to calculate difference:
UPDATE animals
SET age = abs(benchmarkdate :: date - birthdate :: date)/ 365;
Any help would be appreciated. Thank you.
I would use EXTRACT here and then take a difference of only the year components on the two dates:
UPDATE animals
SET age = EXTRACT(year FROM benchmarkdate) - EXTRACT(year FROM birthdate);
Note that you might even want to avoid doing this update, and instead just compute the age when you select. If you foresee the need to frequently do such an update, that would be a good indicator that you probably should change your approach.
At my last meeting someone asked me if it was possible to hide people who where ill since a year from a dashboard. So I'm searching for the best way to actually KNOW who has been ill for 12 months.
I am working with a table with the number of days you've been absent for every kind of absence you could have, the number of days you should have been working that month, with a row per person, department and profession each month.
So it looks something like this :
PersonID
YEAR
MONTH
DEPARTMENT
PROFESSION
Absence1
Absence2
Absence3
WORKING DAYS OF THE MONTH...
11111
2021
07
HR
ASSISTANT
0
2
0
22
11111
2021
08
HR
ASSISTANT
0
0
0
22
==> So if I'm on a row of July 2021 I need to check the lines from June 2020 to June 2021.
My guess is that I need to add a column to this table who will say (with some kind of loop maybe) "if for the last 12 months (rows) the total number of days of absence equal the number of working days of the last 12 months then "ILL FOR A YEAR OR MORE" for each person (knowing that a person can work in more than one department or more than one profession so she'll have more than one row per month).
But I really have no idea how to actually write it in a script as I usually do very basic things. I'm using SQL SERVER and have 429 207 rows in the table. I'm thinking about doing it in the whole table and not only treating this month's rows because in the dashboard we show an historic.
Your table is heavily denormalized. If you want to represent all this information in the database, I would have expected the following tables, instead of just one:
Person
Department
Illness (list of illnesses)
IllnessAbsence (join table between Person and Illness)
Either way, you can get the information you need with something like this:
I've assumed you want the whole table, so you need a window function
We need to flip the logic on its head: exclude all rows which have no non-absence in the last 12 months
SELECT
PersonID,
YEAR,
MONTH,
DEPARTMENT,
PROFESSION,
ILLNESS1,
ILLNESS2,
ILLNESS3,
[WORKING DAYS OF THE MONTH]
FROM (
SELECT *,
NotIllLast12Months = COUNT(CASE WHEN DATEFROMPARTS(YEAR + 1, MONTH, 1) >= GETDATE()
AND ILLNESS1 + ILLNESS2 + ILLNESS3 = 0 THEN 1 END)
OVER (PARTITION BY ID)
FROM HETP_ABS
) abs
WHERE NotIllLast12Months > 0;
I have two files, one with event date, the other one is the security prices I need to match the month with event month 0, -1 (previous month), and +1 (the month after event month). So e.g., one observation has event date 1/1/2010, I have the daily bond prices from 1/1/2009 to 1/20/2010, but I need to match January,2010 to December 2009 prices and February 2010. I have the following codes to do that,
PROC SQL;
CREATE TABLE test AS
select *
FROM bondprice as a, sdc as b
where b.Participant_CUSIP=a.cusip
& INTCK('MONTH',b.Alliance_Date,a.time)>=-1
& INTCK('MONTH',b.Alliance_Date,a.time)<=1 ;
QUIT;
DATA test1;
SET test;
BY deal_number CUSIP code time;
IF first.code THEN price1=price;
IF first.code THEN day1=time;
IF last.code THEN price0=price;
IF last.code THEN day0=time;
RUN;
clearly this observation should be only matched in 1-month Jan 2010 (0,0) and 2-month Dec 2009 to Jan 2010 (-1,0), because it does not have 3-month Feb 2010's prices. However, if using my codes above, it will yield as a valid obs who has 3-month price since it fell into the range of >=-1 and <=1.
So the first part of code is not accurate (the >= and <= range): it yields any prices that falls into December 2009 to Feb 2010, this is wrong because of two situations. First, if the bond prices stops in between Dec 2009 and Jan 2010, it will be counting as a valid 2-month and 3-month observations. Second, if the bond prices begins in between Jan 2010 and Feb 2010, it will be counting as a valid 3-month observation. Both of these two situation will yield invalid observations, since the first situation do not have Feb 2010 prices but still counts as 2-month and 3-month, and the second situation do not have either Dec 2009 or Jan 2010 prices, but still counts as 3-month.
Will someone help me with this? How can I modify the code so that the 3-month window and 2-month window contains the relative months?
I hope I describe the problem clearly. Please let me know if anything is unclear.
I've uploaded an example to this problem here.
https://www.dropbox.com/s/hx9ahst7nts4k0q/ex1.xlsx?dl=0
I have a database that is more or less the following:
John Smith
Jan 1 2016
Sales $80
Jan 2 2016
Sales $100
Jane Doe
Jan 1 2016
Sales $85
Jan 2 2016
Sales $110
My report is setup to break on names and on dates (b/c in actuality there are more numbers than just sales, but I'm simplifying this question).
I need to find the average for a week, rolling 4 weeks, and month for each person. I can control the date range specified in the report using a form I made so I can get them to click a button that autofills a range of time based on today (a week would be the previous 7 days, etc).
I cannot however figure out how to make it average everything under a name. Using Avg, DAvg, and some "grouping" controls have not resulted in anything useable. DAvg got me the closest but it would average EVERYONE together instead of breaking each person by their name (which is "Employee Name" in the query).
Any help or ideas would be vastly appreciated! I don't care if it is a modification to the query language or the report itself as I'm versed in VBA, SQL, and Access to an extent. I came from doing things in Excel, so I'm no Access pro, but I'm great with Excel. Unfortunately that doesn't solve my problem here haha! Oh wait, that's not funny...doh!
Thanks everyone!
It should follow this line:
Select Person, Avg(Sales), Format(yyyymm, [Date])
From YourTable
Group By Person, Format(yyyymm, [Date])
For weeks it gets a little more complicated as weeks cross New Year.
I have been given population data like this;
Year Region Population
----------------------------------
2012 District1 1000
2012 District2 1500
2012 District3 2000
Now I have to make a cube where a user can filter population in Month, Quarter and Year level. So I decided to enter data into a fact table with each and every month of the given year that means 12 records for each District with the same given number. So if a user asks for any month he will get the same count. But now the problem is if user does not filter it by Month, Quarter and Year I get the Sum of all the data that means District1 will display 12 times 1000 = 12000. How can I get 1000 for district1 at any given time? If data is in multiple years then also it should not sum them up. Is my approach wrong? Hope I am clear enough to explain the problem.
Your fact is a semi additive measure, more info at http://msdn.microsoft.com/en-us/library/ms175356.aspx