Evan Cutler
asked on
Performing Comparisons in Star Schema by Year and Department
Greetings,
I have this Schema
I am required to have a SUM of the AMT field in the PAY_FACT table by department and Earning Code.
Department Titles: PAY_FACT.ORGN_CODE = dept_ref.TITLE
Earning Codes: PAY_FACT.EARN_CODE = earn_ref.CODE
This sum is to be between two dates. In addition, I need five other AMT fields for the Years Prior.
The output I am trying to achieve is this:
My biggest problem is getting the YR1 through YR5.
They are the dates equal back 1 year, 2 years, 3years, 4 years, and 5 years.
NULLS need to be converted to 0's
How do I go about this?
Thanks
I have this Schema
CREATE TABLE `PAY_FACT` (
`PAYNO` int(11) default NULL,
`ORGN_CODE` varchar(6) default NULL,
`POSN` varchar(6) default NULL,
`EARN_CODE` varchar(3) default NULL,
`HRS` double default NULL,
`RATE` double default NULL,
`AMT` double default NULL,
`date_id` bigint(20) default NULL,
KEY `EARN` (`EARN_CODE`),
KEY `POSN` (`POSN`),
KEY `ORGN` (`ORGN_CODE`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
CREATE TABLE `dept_ref` (
`CODE` varchar(6) default NULL,
`TITLE` varchar(35) default NULL,
KEY `CODE` (`CODE`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
CREATE TABLE `earn_ref` (
`CODE` varchar(3) default NULL,
`SHORT_DESC` varchar(8) default NULL,
`LONG_DESC` varchar(30) default NULL,
KEY `CODE` (`CODE`),
KEY `SHORT` (`CODE` , `SHORT_DESC`),
KEY `LONG` (`CODE` , `LONG_DESC`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
CREATE TABLE `date_ref` (
`date_id` bigint(20) NOT NULL,
`date` date NOT NULL,
`timestamp` bigint(20) NOT NULL,
`weekend` char(10) NOT NULL default 'Weekday',
`day_of_week` char(10) NOT NULL,
`month` int(11) NOT NULL,
`month_name` char(10) NOT NULL,
`month_day` int(11) NOT NULL,
`year` int(11) NOT NULL,
`FY` int(11) NOT NULL,
`FQ` int(11) NOT NULL,
`week_starting_monday` char(2) NOT NULL,
PRIMARY KEY (`date_id`),
UNIQUE KEY `date` (`date`),
KEY `year_week` (`year`,`week_starting_monday`),
KEY `month_day` (`month`,`month_day`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
I am required to have a SUM of the AMT field in the PAY_FACT table by department and Earning Code.
Department Titles: PAY_FACT.ORGN_CODE = dept_ref.TITLE
Earning Codes: PAY_FACT.EARN_CODE = earn_ref.CODE
This sum is to be between two dates. In addition, I need five other AMT fields for the Years Prior.
The output I am trying to achieve is this:
DEPT EARN_TYPE CFY_AMT YR1_AMT YR2_AMT YR3_AMT YR4_AMT YR5_AMT
Supply BiWeekly_PAY 5000 4500 4200 3800 4500 2600
Admin Overtime_PAY 1200 1140 1254 1000 454 1560
My biggest problem is getting the YR1 through YR5.
They are the dates equal back 1 year, 2 years, 3years, 4 years, and 5 years.
NULLS need to be converted to 0's
How do I go about this?
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you so much Paul...
This is very enlightening.
Especially about the between thing.
Evan
This is very enlightening.
Especially about the between thing.
Evan
But, you can use either the ISNULL or COALESCE() function.
As for your years, maybe using a GROUP BY command.
Personally, define fields with NOT NULL and assign a base value (i.e. 0 in your case). Saves a lot of hassle.
Bob