Author |
Topic |
Ubbe
Starting Member
14 Posts |
Posted - 2011-10-20 : 07:22:01
|
I need assistance please... I wish the following:ra gr xno startxdate endxdate2I 728 1471 2011-10-11 2011-10-141I 701 1471 2011-10-15 2011-10-152I 728 1471 2011-10-16 2011-10-21with this datasetxdate ra gr xno 2011-10-11 2I 728 14712011-10-12 2I 728 14712011-10-13 2I 728 14712011-10-14 2I 728 14712011-10-15 1I 701 14712011-10-16 2I 728 14712011-10-17 2I 728 14712011-10-18 2I 728 14712011-10-19 2I 728 14712011-10-20 2I 728 14712011-10-21 2I 728 1471How do you read row by row, and breaks at the right date.Best Regardsub-oOo- |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-10-20 : 08:01:08
|
no need of row by row. just useSELECT ra,gr,xno,MIN(xdate) AS startxdate,MAX(xdate) AS endxdateFROM tableGROUP BY ra,gr,xno ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
Ubbe
Starting Member
14 Posts |
Posted - 2011-10-20 : 10:19:11
|
Thanks but then the result will be like this:startxdate endxdate ra gr xno ---------- ---------- ----- ---------- ---- 2011-10-15 2011-10-29 1I 701 14712011-10-11 2011-10-31 2I 728 1471 what I want is:ra gr xno startxdate endxdate2I 728 1471 2011-10-11 2011-10-141I 701 1471 2011-10-15 2011-10-152I 728 1471 2011-10-16 2011-10-21to read row by row and break at the date,Hope you understand, I'm bad at that explanation :)/ub-oOo- |
|
|
Ubbe
Starting Member
14 Posts |
Posted - 2011-10-20 : 10:22:19
|
Sorry!I think that your proposal will be:startxdate endxdate ra gr xno ---------- ---------- ----- ---------- ---- 2011-10-15 2011-10-15 1I 701 14712011-10-11 2011-10-21 2I 728 1471-oOo- |
|
|
Ifor
Aged Yak Warrior
700 Posts |
Posted - 2011-10-20 : 12:08:25
|
In SQL2005, and above, you can use the ROW_NUMBER() windowed function.In SQL2000 there is no good way to do this. If you do not have too much data you may be able to get away with:-- *** Test Data ***CREATE TABLE #t( xdate datetime NOT NULL ,ra char(2) NOT NULL ,gr smallint NOT NULL ,xno smallint NOT NULL)INSERT INTO #tSELECT '20111011', '2I', 728, 1471UNION ALL SELECT '20111012', '2I', 728, 1471UNION ALL SELECT '20111013', '2I', 728, 1471UNION ALL SELECT '20111014', '2I', 728, 1471UNION ALL SELECT '20111015', '1I', 701, 1471UNION ALL SELECT '20111016', '2I', 728, 1471UNION ALL SELECT '20111017', '2I', 728, 1471UNION ALL SELECT '20111018', '2I', 728, 1471UNION ALL SELECT '20111019', '2I', 728, 1471UNION ALL SELECT '20111020', '2I', 728, 1471UNION ALL SELECT '20111021', '2I', 728, 1471-- *** End Test Data ***SELECT ra, gr, xno, StartDate, EndDateFROM( SELECT ra, gr, xno, grp ,MIN(xdate) AS StartDate ,MAX(xdate) AS EndDate FROM ( SELECT ra, gr, xno , YEAR(xdate) * 10000 + MONTH(xdate) * 100 + DAY(xdate) - ( SELECT COUNT(*) FROM #t T2 WHERE T2.ra = T1.ra AND T2.gr = T1.gr AND T2.xno = T1.xno AND T2.xdate <= T1.xdate ) AS grp ,xdate FROM #t T1 ) D1 GROUP BY ra, gr, xno, grp) DORDER BY StartDate |
|
|
Ubbe
Starting Member
14 Posts |
Posted - 2011-10-21 : 02:29:59
|
I have sql2000 and i have lots of data over 600 000 rows.And Start and End date must not overlap.the key: ra, gr, xno, grp.Are there any more ideas ...-oOo- |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2011-10-21 : 02:39:44
|
did you try the query Ifor posted ? KH[spoiler]Time is always against us[/spoiler] |
|
|
Ubbe
Starting Member
14 Posts |
Posted - 2011-10-21 : 03:27:56
|
Yes, and I am very grateful.But I try to get the right enddate.In this proposal, the enddate last day of the month.ex.ra gr xno StartDate EndDate2I 728 1471 2010-09-01 2010-09-302I 728 1471 2010-10-01 2010-10-312I 728 1471 2010-11-01 2010-11-302I 728 1471 2010-12-01 2010-12-23and so on...my wish is.ex.2I 728 1471 2010-09-01 2011-10-141I 701 1471 2011-10-15 2011-10-152I 728 1471 2011-10-16 2011-10-211I 701 1471 2011-10-22 2011-10-22and so on...I am sorry I did not express myself well.-oOo- |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2011-10-21 : 03:37:12
|
can you provide the sample data that Ifor's query does not give you what you want ? Please also post your expected result for the sample data KH[spoiler]Time is always against us[/spoiler] |
|
|
Ubbe
Starting Member
14 Posts |
Posted - 2011-10-21 : 08:46:09
|
OK!-- *** Test Data ***CREATE TABLE #t( xdate datetime NOT NULL ,ra char(3) NOT NULL ,gr char(3) NOT NULL ,xno char(4) NOT NULL)INSERT INTO #tSELECT '2011-09-01', '2I', '728', '1471'UNION ALL SELECT '2011-09-02', '2I', '728', '1471'UNION ALL SELECT '2011-09-03', '2I', '728', '1471'UNION ALL SELECT '2011-09-04', '2I', '728', '1471'UNION ALL SELECT '2011-09-05', '2I', '728', '1471'UNION ALL SELECT '2011-09-06', '2I', '728', '1471'UNION ALL SELECT '2011-09-07', '2I', '728', '1471'UNION ALL SELECT '2011-09-08', '2I', '728', '1471'UNION ALL SELECT '2011-09-09', '2I', '728', '1471'UNION ALL SELECT '2011-09-10', '2I', '728', '1471'UNION ALL SELECT '2011-09-11', '2I', '728', '1471'UNION ALL SELECT '2011-09-12', '2I', '728', '1471'UNION ALL SELECT '2011-09-13', '2I', '728', '1471'UNION ALL SELECT '2011-09-14', '2I', '728', '1471'UNION ALL SELECT '2011-09-15', '2I', '728', '1471'UNION ALL SELECT '2011-09-16', '2I', '728', '1471'UNION ALL SELECT '2011-09-17', '2I', '728', '1471'UNION ALL SELECT '2011-09-18', '2I', '728', '1471'UNION ALL SELECT '2011-09-19', '2I', '728', '1471'UNION ALL SELECT '2011-09-20', '2I', '728', '1471'UNION ALL SELECT '2011-09-21', '2I', '728', '1471'UNION ALL SELECT '2011-09-22', '2I', '728', '1471'UNION ALL SELECT '2011-09-23', '2I', '728', '1471'UNION ALL SELECT '2011-09-24', '2I', '728', '1471'UNION ALL SELECT '2011-09-25', '2I', '728', '1471'UNION ALL SELECT '2011-09-26', '2I', '728', '1471'UNION ALL SELECT '2011-09-27', '2I', '728', '1471'UNION ALL SELECT '2011-09-28', '2I', '728', '1471'UNION ALL SELECT '2011-09-29', '2I', '728', '1471'UNION ALL SELECT '2011-09-30', '2I', '728', '1471'UNION ALL SELECT '2011-10-01', '2I', '728', '1471'UNION ALL SELECT '2011-10-02', '2I', '728', '1471'UNION ALL SELECT '2011-10-03', '2I', '728', '1471'UNION ALL SELECT '2011-10-04', '2I', '728', '1471'UNION ALL SELECT '2011-10-05', '2I', '728', '1471'UNION ALL SELECT '2011-10-06', '2I', '728', '1471'UNION ALL SELECT '2011-10-07', '2I', '728', '1471'UNION ALL SELECT '2011-10-08', '2I', '728', '1471'UNION ALL SELECT '2011-10-09', '2I', '728', '1471'UNION ALL SELECT '2011-10-10', '2I', '728', '1471'UNION ALL SELECT '2011-10-11', '2I', '728', '1471'UNION ALL SELECT '2011-10-12', '2I', '728', '1471'UNION ALL SELECT '2011-10-13', '2I', '728', '1471'UNION ALL SELECT '2011-10-14', '2I', '728', '1471'UNION ALL SELECT '2011-10-15', '1I', '701', '1471'UNION ALL SELECT '2011-10-16', '2I', '728', '1471'UNION ALL SELECT '2011-10-17', '2I', '728', '1471'UNION ALL SELECT '2011-10-18', '2I', '728', '1471'UNION ALL SELECT '2011-10-19', '2I', '728', '1471'UNION ALL SELECT '2011-10-20', '2I', '728', '1471'UNION ALL SELECT '2011-10-21', '2I', '728', '1471'UNION ALL SELECT '2011-10-22', '1I', '701', '1471'UNION ALL SELECT '2011-10-23', '2I', '728', '1471'UNION ALL SELECT '2011-10-24', '2I', '728', '1471'UNION ALL SELECT '2011-10-25', '2I', '728', '1471'UNION ALL SELECT '2011-10-26', '2I', '728', '1471'UNION ALL SELECT '2011-10-27', '2I', '728', '1471'UNION ALL SELECT '2011-10-28', '2I', '728', '1471'UNION ALL SELECT '2011-10-29', '1I', '701', '1471'UNION ALL SELECT '2011-10-30', '2I', '728', '1471'UNION ALL SELECT '2011-10-31', '2I', '728', '1471'UNION ALL SELECT '2011-11-01', '2I', '728', '1471'-- *** End Test Data ***SELECT ra, gr, xno, StartDate, EndDateFROM( SELECT ra, gr, xno, grp ,MIN(xdate) AS StartDate ,MAX(xdate) AS EndDate FROM ( SELECT ra, gr, xno , YEAR(xdate) * 10000 + MONTH(xdate) * 100 + DAY(xdate) - ( SELECT COUNT(*) FROM #t T2 WHERE T2.ra = T1.ra AND T2.gr = T1.gr AND T2.xno = T1.xno AND T2.xdate <= T1.xdate ) AS grp ,xdate FROM #t T1 ) D1 GROUP BY ra, gr, xno, grp) DORDER BY StartDateThis is the result of the above scriptra gr xno StartDate EndDate2I 728 1471 2011-09-01 2011-09-302I 728 1471 2011-10-01 2011-10-141I 701 1471 2011-10-15 2011-10-152I 728 1471 2011-10-16 2011-10-211I 701 1471 2011-10-22 2011-10-222I 728 1471 2011-10-23 2011-10-281I 701 1471 2011-10-29 2011-10-292I 728 1471 2011-10-30 2011-10-312I 728 1471 2011-11-01 2011-11-01But my this is may wishra gr xno StartDate EndDate2I 728 1471 2011-09-01 2011-10-141I 701 1471 2011-10-15 2011-10-152I 728 1471 2011-10-16 2011-10-211I 701 1471 2011-10-22 2011-10-222I 728 1471 2011-10-23 2011-10-281I 701 1471 2011-10-29 2011-10-292I 728 1471 2011-10-30 2011-11-01I have to compress it more between start-and enddate,see the first and last line./ub-oOo- |
|
|
Ifor
Aged Yak Warrior
700 Posts |
Posted - 2011-10-21 : 09:03:59
|
To get over the month boundary problem:replaceYEAR(xdate) * 10000 + MONTH(xdate) * 100 + DAY(xdate)withDATEDIFF(day, 0, xdate) |
|
|
Ubbe
Starting Member
14 Posts |
Posted - 2011-10-21 : 09:30:47
|
OK, that's right by my example, but if I run on a larger data source.So will the following resultsLine ra gr xno StartDate EndDate1 2I 728 1471 2010-09-01 2010-12-232 2I 728 1471 2010-12-26 2010-12-303 2I 728 1471 2011-01-01 2011-03-204 2I 728 1471 2011-03-22 2011-04-175 2I 728 1471 2011-04-20 2011-04-216 2I 728 1471 2011-04-25 2011-04-307 2I 728 1471 2011-05-02 2011-05-168 2I 728 1471 2011-05-18 2011-07-039 2I 728 1471 2011-07-09 2011-07-1010 2I 728 1471 2011-07-16 2011-07-1711 2I 728 1471 2011-07-23 2011-07-2412 2I 728 1471 2011-07-30 2011-07-3113 2I 728 1471 2011-08-06 2011-08-0714 2I 728 1471 2011-08-13 2011-10-1415 1I 701 1471 2011-10-15 2011-10-1516 2I 728 1471 2011-10-16 2011-10-2117 1I 701 1471 2011-10-22 2011-10-2218 2I 728 1471 2011-10-23 2011-10-2819 1I 701 1471 2011-10-29 2011-10-29"Line" 1 to 14 can conpress as followsra gr xno StartDate EndDate2I 728 1471 2010-09-01 2011-10-14-oOo- |
|
|
Ifor
Aged Yak Warrior
700 Posts |
Posted - 2011-10-21 : 09:53:28
|
You have now introduced gaps in your dates which even a basic reading of the query would tell you will not work.Also, your sample data seems to consist mainly of ra = '2I', gr = '728' and xno = '1471'. If your real data has a similar distribution you may be best looking for a solution based on expections rather than a triangular join. If all else fails, just use a cursor. |
|
|
Ubbe
Starting Member
14 Posts |
Posted - 2011-10-21 : 10:04:12
|
I think I understand.Thanks for your help Ifor.-oOo- |
|
|
|
|
|