dbTalk Databases Forums  

Help to write query to combine two recordsets into one

comp.database.ms-sqlserver comp.database.ms-sqlserver


Discuss Help to write query to combine two recordsets into one in the comp.database.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
AMD Desktop
 
Posts: n/a

Default Help to write query to combine two recordsets into one - 09-13-2004 , 02:31 PM






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



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.