Corrected: Need help with a query
From: AMD Desktop (makblair_at_yahoo.com)
Date: 09/13/04
- Next message: Andrea Montanari: "Re: how to set sql2000/ado(vb) to notify when recordset changed (insert,update)"
- Previous message: Tibor Karaszi: "Re: Get and update only Time part from 'DateTime' data type field"
- In reply to: AMD Desktop: "Need help with a query"
- Messages sorted by: [ date ] [ thread ]
Date: Mon, 13 Sep 2004 12:07:45 -0400
Here is the table:
CREATE TABLE [EmployeeOBenefitApproved] (
[emp_obenefit_id] [int] IDENTITY (1, 1) NOT NULL ,
[empplan_id] [int] NOT NULL ,
[obenefit_id] [int] NOT NULL ,
[cost_per_month] [float] NOT NULL CONSTRAINT
[DF_EmployeeOBenefitApproved_cost_per_month] DEFAULT (0),
[coverage_start_date] [datetime] NOT NULL ,
[coverage_end_date] [datetime] NOT NULL
) ON [PRIMARY]
GO
Here is the query:
----------------------------------------------------------------------------
-------------------------
SELECT emp_obenefit_id, empplan_id, obenefit_id, billing_cost_per_month,
e.coverage_start_date, e.coverage_end_date
FROM employeeobenefitapproved e
WHERE e.empplan_id = 4757
AND e.emp_obenefit_id IN
(
SELECT TOP 2 emp_obenefit_id
from employeeobenefitapproved
where empplan_id = e.empplan_id
AND obenefit_id = e.obenefit_id
ORDER BY coverage_end_date DESC, emp_obenefit_id DESC
)
ORDER BY e.obenefit_id DESC, e.coverage_end_date DESC, e.emp_obenefit_id
DESC
----------------------------------------------------------------------------
-------------------------
Here is the result of the query:
emp_obenefit_id empplan_id obenefit_id cost_per_month
coverage_start_date coverage_end_date
--------------- ----------- ----------- ------------------------------------
----------------- ------------------------------------------------------
31731 4757 84 0.0
2004-09-01 2004-12-31
26635 4757 84 0.0
2004-04-06 2004-08-31
31730 4757 83 6.2
2004-09-01 2004-12-31
31121 4757 83 0.0
2004-04-06 2004-08-31
31729 4757 82 0.0
2004-09-01 2004-12-31
31120 4757 82 0.0
2004-04-06 2004-08-31
----------------------------------------------------------------------------
-------------------------
I need to construct a query that will get me one recordset for each
obenefit_id where cost_per_month of previous (between 2004-04-06 and
2004-08-31) period will not be equal to current period (between 2004-09-01
and 2004-12-31), it they are equal this means that no change has been done
to the costs so I do not need it
The result of this query has to look like this:
empplan_id obenefit_id cost_per_month_prev cost_per_month_current
--------------- ----------- ----------- --------------------------------- --
--------------
4757 83 0.00
6.2
Only obenefit_id = 83 must be displayed because as it is seen in the query
results values for 2004-04-06 and 2004-08-31 value for
cost_per_month was 0 and for 2004-09-01 and 2004-12-31 is 6.2 (value
changed)
Thank you for your help
- Next message: Andrea Montanari: "Re: how to set sql2000/ado(vb) to notify when recordset changed (insert,update)"
- Previous message: Tibor Karaszi: "Re: Get and update only Time part from 'DateTime' data type field"
- In reply to: AMD Desktop: "Need help with a query"
- Messages sorted by: [ date ] [ thread ]