Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
osirisa
Constraint Violating Yak Guru
289 Posts |
Posted - 2007-10-15 : 16:03:38
|
Bye. |
|
Kristen
Test
22859 Posts |
Posted - 2007-10-15 : 22:34:01
|
"The current date starts with the first day of the previous month. Example today is October 10, 2007 I need September 01, 2007 for this query."You can get that withDATEADD(Month, DATEDIFF(Month, 0, '20071010')-1, 0)I've had a look at your code but it seems to contain a whole load of debugging stuff (outputting data for various scenarios by the looks of it), the first cursor isn't used, and so on that its hard to see the wood for the trees.Do you really need a cursor? I would have thought you could just do a set based:INSERT INTO #TP_TIES_OVerlaping_DatesSELECT ...FROM ...It might help if you explain what this Sproc needs to do plsKristen |
|
|
Kristen
Test
22859 Posts |
Posted - 2007-10-15 : 22:48:58
|
Additional thread:http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=91023 |
|
|
osirisa
Constraint Violating Yak Guru
289 Posts |
Posted - 2007-10-16 : 10:21:35
|
Good Morning Kristen:Thank You, You are the first person, that has given me a clear answer. I am going to explain in plain English What this SP is suppose to do. I have to find any station_nbr that has more than one group_code and that the current date = (DATEADD(mm,DATEDIFF(mm,0,dateadd(mm,-0-datepart(day,0),getdate())),0) The current date starts with the first day of the previous month. Example today is October 10, 2007 I need September 01, 2007 for this query. To insert the record it needs to be active meaning that the current date is in between Beg_eff_date and end_eff_date, and the end_eff_date is = ‘12/31/3000’I need to write SP(cursor) that reads line by line this table and that insert the answer: INSERT INTO #TP_TIES_OVerlaping_Dates. Example: Current_date: 09/01/2007 Group_Code Station_nbr beg_eff_date end_eff_date0000003 00371101 03/12/2006 12/31/30000000016 00371101 07/08/2007 12/31/3000The above explanation is for the first type of Error.The second Type of Error: If the Group Code ends with "W" and the End_effective_date = '12/31/3000' and the W_Beg_date is Part of the Temp Table(I to create the W_Beg_Date Column) -- is GREATER than the End_eff_date. I want to Insert this Record into the Table. I need to set sometype of Record Control to make the different between one error to another one. I thank you so much for your help.....I hope this is clear to understand for everybody. End |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-10-16 : 11:28:19
|
May if you can provide proper and accurate sample data together with expected output we can help you.There are many routines available here SQLTeam that may fit your purposes.Are you only inserting in one table selecting from another and just substract one month from date? E 12°55'05.25"N 56°04'39.16" |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-10-16 : 11:33:25
|
Is this something similar to what you want?DECLARE @Sample TABLE (FromDate DATETIME, ToDate DATETIME)INSERT @SampleSELECT '20070401', '20070701' UNION ALLSELECT '20070101', '20070301' UNION ALLSELECT '20070501', '20070901'/*1 2 3 4 5 6 7 8 9***************** |-----||---| |-------|******************/SELECT s1.FromDate, s1.ToDate, s2.FromDate, s2.ToDateFROM @Sample AS s1CROSS JOIN @Sample AS s2WHERE s1.ToDate >= s2.FromDate AND s1.FromDate <= s2.ToDate AND s1.FromDate < s2.FromDate E 12°55'05.25"N 56°04'39.16" |
|
|
osirisa
Constraint Violating Yak Guru
289 Posts |
Posted - 2007-10-16 : 11:48:27
|
<code> Group_Code ***** Station_nbr ******* Beg_eff_date***** End_Eff_date 00000005 00251 08/03/2007 12/31/3000 00000015 00251 02/01/2007 12/31/3000 00000015N 00251 02/01/2007 12/31/3000Current_date = 09/01/20007 (already have the correct SQL syntax)All of them coming from TIES_Temp.dbo.station_group----**** In the first path I don't need the record that ends with "N" to eliminate that record, I need to Select *from TIES_Temp.dbo.group_detailWhere group_type = 'NPN'. I need the two first Records both with the same station_nbr but different group_code 00000005 00251 08/03/2007 12/31/3000 00000015 00251 02/01/2007 12/31/3000 </code> |
|
|
Kristen
Test
22859 Posts |
Posted - 2007-10-17 : 23:29:42
|
Original posts:10/15/2007 : 16:03:38 Hi:Once, I have the selection criteria working for the first cursor. I need to write the Stored Procedure for the rest of cursors. UUUUFFFff, this is amazingly difficult for me. Here is what I have done so far.TIES_Temp.dbo.station_groupError Report:Criteria for Error 1:I have to find any station_nbr that has more than one group_code and that the current date = (Month-1, getdate) I am not sure if this is the right way to write the previous month from the current date. The current date starts with the first day of the previous month.Example today is October 10, 2007 I need September 01, 2007 for this query.To insert the record it needs to be active meaning that the current date is in between Beg_eff_date and end_eff_date, and the end_eff_date is = ‘12/31/3000’I need to write SP(cursor) that reads line by line this table and that insert the answer: INSERT INTO #TP_TIES_OVerlaping_Dates.Example:Current_date: 09/01/2007Group_Code Station_nbr beg_eff_date end_eff_date0000003 00371101 03/12/2006 12/31/30000000016 00371101 07/08/2007 12/31/3000This records needs to be in the report. There are other 2 Errors but if you give me a hand with this one, I can figure it out the other two.Here is the Query that I have created to get all the data that all the Selection Criteria.TIES_Temp.dbo.station_group and dbo.group_detailThe first Query is the Selection Criteria for all station_nbr records with more than one group_code within the Station_Group Table (active records).The Second query is coming from Group Detail Table where group type = ‘NPN’. This group type filter out all the group_code that ends with N or W. Reflecting only the Primary Code the group_code without any letters at the end. I need to find a way to combine both queries into one, so my end result will be all station_nbr records with more than one group_code, all active, within the Station_Group Table.1. My question is:How can I write the syntax to put the two queries together?________________________________________________________________________/* Declare Overlaping Dates 1 cursor*/DECLARE Overlaping_Duplicate_cursor INSENSITIVE CURSOR FORSelect * From(SELECT group_code,station_nbr, beg_eff_date, end_eff_dateFrom TIES_Temp.dbo.station_groupwhere(dateadd(month,-1,getdate()) Between beg_eff_date and end_eff_date))a,(Select station_nbr, count(station_nbr) as ErrorDuplicateFrom TIES_Temp.dbo.station_groupGROUP BY station_nbrHAVING (COUNT(station_nbr) > 1 )) bWhere a.station_nbr = b.station_nbrFOR READ ONLYINSERT INTO #TP_TIES_OVerlaping_Dates(group_code,station_nbr, beg_eff_date, end_eff_date,W_beg_date, W_End_date,N_Beg_date,N_End_Date,Debug)values (@groupcode,@stationnumber,@begdate, @endeffdate,@W_beg_date,@W_End_Date,@N_Beg_Date,@N_End_Date,@TEST)selectgroup_code, count(*)As ErrorDuplicatefrom TIES_Temp.dbo.station_groupgroup by group_code, station_nbr, beg_eff_date, end_eff_datehaving count(*)> 1Question 2I don’t understand what do I need this part inside the SP? What exactly this selection does? At the end I need to enter in the report only the records that have station_nbr actives for more than one group_codeHere is the Error that I am getting when I try to join both queries to give the selection criteria for the Cursor.Here is the entire Stored ProcedureCREATE PROC dbo.swn_sp_TIES_Overlap_DatesAS/ overlaping dates.************************************************************************* Modification History:**********************************************************************--*************************************************************--SET SQL STMT BASED ON SQL TYPE PASSED IN--*************************************************************//* Create a Temp Table */Create Table #TP_TIES_OVerlaping_Dates(group_code char (12),station_nbr char (20),beg_eff_date datetime,end_eff_date datetime,C02_pct float,Current_Month datetime,W_Beg_date datetime,W_End_date datetime,N_beg_date datetime,N_End_date datetime,debug varchar(255))--------------------------------First Temp Table Selection-------------------------------------------------DECLARE @groupcode char (12),@stationnumber char(20),@Error Varchar(2),@begdate datetime,@endeffdate datetime,@record_found_flag char(1),@Records int,@current_date datetime,@Count varchar(10),@TEST varchar(10)/* Set the record_found_flag */SELECT @record_found_flag = 'N'/* Declare Overlaping Dates 1 cursor*/DECLARE Overlaping_Duplicate_cursor INSENSITIVE CURSOR FORSelect * From(SELECT group_code,station_nbr, beg_eff_date, end_eff_dateFrom TIES_Temp.dbo.station_groupwhere(dateadd(month,-1,getdate()) Between beg_eff_date and end_eff_date))a,(Select station_nbr, count(station_nbr) as ErrorDuplicateFrom TIES_Temp.dbo.station_groupGROUP BY station_nbrHAVING (COUNT(station_nbr) > 1 )) bWhere a.station_nbr = b.station_nbrFOR READ ONLYINSERT INTO #TP_TIES_OVerlaping_Dates(group_code,station_nbr, beg_eff_date, end_eff_date)values (@groupcode,@stationnumber,@begdate, @endeffdate)selectgroup_code, count(*)As ErrorDuplicatfrom TIES_Temp.dbo.station_groupgroup by group_codehaving count(*)> 1DEALLOCATE Overlaping_Duplicate_cursor--------------------------------2nd Compare Temp Table against Station Group Selection-------------------------------------------------DECLARE @groupcode2 char (12),@stationnumber2 char(20),@Error2 Varchar(2),@begdate2 datetime,@endeffdate2 datetime,@current_C02 float,@record_found_flag2 char(1),@N_Beg_date datetime,@N_End_date datetime,@W_Beg_date datetime,@W_End_date datetime,@Records2 int,@current_date2 datetime,@Count2 varchar(10),@TEST2 varchar(10)/* Declare Overlaping Dates 2 cursor to read from the Temp Table */DECLARE OverlapingDates_Report_cursor INSENSITIVE CURSOR FORSELECTa.group_code, a.station_nbr,a.beg_eff_date,a.end_eff_datefrom #TP_TIES_OVerlaping_Dates a, TIES_Temp.dbo.station_group bwhere(dateadd(month,-1,getdate()) Between b.beg_eff_date and b.end_eff_date)and (a.group_code = b.group_code)FOR READ ONLYINSERT INTO #TP_TIES_OVerlaping_Dates(group_code,station_nbr, beg_eff_date, end_eff_date,W_beg_date, W_End_date,N_Beg_date,N_End_Date,Debug)values (@groupcode,@stationnumber,@begdate, @endeffdate,@W_beg_date,@W_End_Date,@N_Beg_Date,@N_End_Date,@TEST)/* Procedure Division*/OPEN OverlapingDates_Report_cursorFETCH NEXT FROM OverlapingDates_Report_cursor INTO @groupcode, @stationnumber, @begdate, @endeffdateWHILE @@FETCH_STATUS = 0BEGIN-- Begin cursor and set the @record_found_flagSELECT @record_found_flag = 'N'Set @N_Beg_date = ''Set @N_End_date = ''Set @W_Beg_date = ''Set @W_End_date = ''---- Set @groupcode = ''Set @current_date = dateadd(month,-1,getdate())---------Check for 'W' Records'---------If @groupcode like '%W'BEGINSelect @W_Beg_date = Beg_eff_date, @W_End_date= End_eff_dateFrom TIES_Temp.dbo.station_groupWhere (station_nbr = @stationnumber)and RIGHT(rtrim(group_code),1) = 'W'AND (@current_date between Beg_eff_date and end_eff_date)END---------Check for 'N' Records'---------If @groupcode like '%N'BEGINSelect @N_Beg_date = Beg_eff_date, @N_End_date= End_eff_dateFrom TIES_Temp.dbo.station_groupWhere (station_nbr = @stationnumber)and RIGHT(rtrim(@groupcode),1) = 'N'AND (@current_date between Beg_eff_date and end_eff_date)ENDIf @W_End_date = '12/31/3000'BeginSelect *From TIES_Temp.dbo.station_groupwhere @W_beg_date > end_eff_dateEndIf @N_End_date = '12/31/3000'BeginSelect *From TIES_Temp.dbo.station_groupWhere @N_Beg_date > beg_eff_dateEndINSERT INTO #TP_TIES_OVerlaping_Dates(group_code,station_nbr, beg_eff_date, end_eff_date,W_beg_date, W_End_date,N_Beg_date,N_End_Date,Debug)values (@groupcode,@stationnumber,@begdate, @endeffdate,@W_beg_date,@W_End_Date,@N_Beg_Date,@N_End_Date,@TEST)FETCH NEXT FROM OverlapingDates_Report_cursor INTO @groupcode,@stationnumber, @begdate, @endeffdate-- End Overlap Report--------ENDSelect *From #TP_TIES_OVerlaping_DatesCLOSE OverlapingDates_Report_cursor---Close Overlaping_Duplicate_cursor/* Remove cursor structures (Deallocate) */DEALLOCATE OverlapingDates_Report_cursorGOLet me know if you need anything or if you have any problems.Osiris 10/16/2007 : 10:21:35 Good Morning Kristen:Thank You, You are the first person, that has given me a clear answer. I am going to explain in plain English What this SP is suppose to do.I have to find any station_nbr that has more than one group_code and that the current date = (DATEADD(mm,DATEDIFF(mm,0,dateadd(mm,-0-datepart(day,0),getdate())),0) The current date starts with the first day of the previous month.Example today is October 10, 2007 I need September 01, 2007 for this query.To insert the record it needs to be active meaning that the current date is in between Beg_eff_date and end_eff_date, and the end_eff_date is = ‘12/31/3000’I need to write SP(cursor) that reads line by line this table and that insert the answer: INSERT INTO #TP_TIES_OVerlaping_Dates.Example:Current_date: 09/01/2007Group_Code Station_nbr beg_eff_date end_eff_date0000003 00371101 03/12/2006 12/31/30000000016 00371101 07/08/2007 12/31/3000The above explanation is for the first type of Error.The second Type of Error:If the Group Code ends with "W" and the End_effective_date = '12/31/3000' and the W_Beg_date is Part of the Temp Table(I to create the W_Beg_Date Column) -- is GREATER than the End_eff_date. I want to Insert this Record into the Table. I need to set sometype of Record Control to make the different between one error to another one.I thank you so much for your help.....I hope this is clear to understand for everybody.End10/16/2007 : 11:48:27 <code> Group_Code ***** Station_nbr ******* Beg_eff_date***** End_Eff_date00000005 00251 08/03/2007 12/31/300000000015 00251 02/01/2007 12/31/300000000015N 00251 02/01/2007 12/31/3000Current_date = 09/01/20007 (already have the correct SQL syntax)All of them coming from TIES_Temp.dbo.station_group----**** In the first path I don't need the record that ends with "N" to eliminate that record, I need to Select *from TIES_Temp.dbo.group_detailWhere group_type = 'NPN'. I need the two first Records both with the same station_nbr but different group_code00000005 00251 08/03/2007 12/31/300000000015 00251 02/01/2007 12/31/3000 </code> |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-10-18 : 01:32:23
|
What happened? E 12°55'05.25"N 56°04'39.16" |
|
|
Kristen
Test
22859 Posts |
Posted - 2007-10-18 : 02:00:29
|
OP deleted original post |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-10-18 : 02:27:49
|
Perfect! I get to bed and you filter all the bad boys out while I am asleep E 12°55'05.25"N 56°04'39.16" |
|
|
|
|
|
|
|