| Author |
Topic |
|
alanhuro
Starting Member
34 Posts |
Posted - 2008-07-16 : 09:32:42
|
| 1. How can you add another column for the end date for the same category. ExampleDate-------------Cat2008-07-01-------A2008-07-03-------B2008-07-07-------C2008-07-10-------A2008-07-11-------B2008-07-13-------A The result I want return isCat--------Begin Date------------End DateA----------2008-07-01------------2008-07-10A----------2008-07-10------------2008-07-13A----------2008-07-13------------Null2. My second question is what query to return the column name within a table. Take an example above the result should returnDate and cat. Since there are 2 columns in that example table?Thanks a bunch for your help.Alan |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2008-07-16 : 09:39:13
|
are you using SQL Server 2005 ? KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-07-16 : 09:49:44
|
quote: Originally posted by alanhuro 1. How can you add another column for the end date for the same category. ExampleDate-------------Cat2008-07-01-------A2008-07-03-------B2008-07-07-------C2008-07-10-------A2008-07-11-------B2008-07-13-------A The result I want return isCat--------Begin Date------------End DateA----------2008-07-01------------2008-07-10A----------2008-07-10------------2008-07-13A----------2008-07-13------------Null2. My second question is what query to return the column name within a table. Take an example above the result should returnDate and cat. Since there are 2 columns in that example table?Thanks a bunch for your help.Alan
may be thisSELECT IDENTITY(int,1,1) AS Seq, Date,Cat INTO #TempFROM YourTableORDER BY Cat,DateSELECT Cat,[Begin Date],[End Date]FROM(SELECT t1.Cat,t1.Date AS [Begin Date],t2.Date AS [End Date]FROM #Temp t1INNER JOIN #Temp t2ON t2.Seq=t1.Seq + 1AND t2.Cat=t1.CatUNION ALLSELECT Cat,Date,NULLFROM #Temp tINNER JOIN (SELECT Cat,MAX(Date) AS MaxDate FROM #Temp GROUP BY Cat)tmpON tmp.Cat=t.CatAND tmp.MaxDate=t.Date)rORDER BY r.Cat |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-07-16 : 09:51:31
|
2. SELECT * FROM INFORMATION_SCHEMA.COLUMNS E 12°55'05.25"N 56°04'39.16" |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-07-16 : 10:07:59
|
It should be since it is part of ANSI standard. E 12°55'05.25"N 56°04'39.16" |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-07-16 : 10:13:56
|
quote: Originally posted by khtan is INFORMATION_SCHEMA.COLUMNS available in Oracle also ? alanhuro is using Oracle.See http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=100617 KH[spoiler]Time is always against us[/spoiler]
Yes available in mysql, postgreSQL as well MadhivananFailing to plan is Planning to fail |
 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2008-07-16 : 12:09:11
|
Here are a couple other ways to query the data you want:DECLARE -- Setup@Yak TABLE (Date DATETIME, Cat CHAR(1))INSERT @YakSELECT '20080701', 'A'UNION ALL SELECT '20080703', 'B'UNION ALL SELECT '20080707', 'C'UNION ALL SELECT '20080710', 'A'UNION ALL SELECT '20080711', 'B'UNION ALL SELECT '20080713', 'A'-- Using correlated sub-querySELECT Cat, Date AS StartDate, (SELECT MIN(Date) FROM @Yak WHERE Date > A.Date and Cat = A.Cat) AS EndDateFROM @Yak AS AWHERE A.Cat = 'A'ORDER BY A.Date-- Using ROW_NUMBERSELECT A.Cat, A.Date AS StartDate, B.Date AS EndDateFROM ( SELECT Cat, Date, ROW_NUMBER() OVER (PARTITION BY Cat ORDER BY Date) AS RowNum FROM @Yak ) AS ALEFT OUTER JOIN ( SELECT Cat, Date, ROW_NUMBER() OVER (PARTITION BY Cat ORDER BY Date) AS RowNum FROM @Yak ) AS B ON A.RowNum + 1 = B.RowNum AND A.Cat = B.CatWHERE A.Cat = 'A'ORDER BY A.Date |
 |
|
|
alanhuro
Starting Member
34 Posts |
Posted - 2008-07-16 : 12:14:31
|
| That is quick. Thank for your help you guy. I still have a problem. - Khtan you are right I try to run the query on the Oracle db. I though the query is going to be the same.- visakh16. I tried your script and the error I got is "ORA-00904: "IDENTITY": invalid identifier". Is there any other way? I just have a read access to the db so I can not create any table or made any change to the DB. I try to avoid doing that too. I will shade my head if I try to.Peso - I try your script and got an error "ORA-00942: table or view does not exist" maybe I miss something.Thanks |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-07-16 : 12:17:22
|
quote: Originally posted by Lamprey Here are a couple other ways to query the data you want:DECLARE -- Setup@Yak TABLE (Date DATETIME, Cat CHAR(1))INSERT @YakSELECT '20080701', 'A'UNION ALL SELECT '20080703', 'B'UNION ALL SELECT '20080707', 'C'UNION ALL SELECT '20080710', 'A'UNION ALL SELECT '20080711', 'B'UNION ALL SELECT '20080713', 'A'-- Using correlated sub-querySELECT Cat, Date AS StartDate, (SELECT MIN(Date) FROM @Yak WHERE Date > A.Date and Cat = A.Cat) AS EndDateFROM @Yak AS AWHERE A.Cat = 'A'ORDER BY A.Date-- Using ROW_NUMBERSELECT A.Cat, A.Date AS StartDate, B.Date AS EndDateFROM ( SELECT Cat, Date, ROW_NUMBER() OVER (PARTITION BY Cat ORDER BY Date) AS RowNum FROM @Yak ) AS ALEFT OUTER JOIN ( SELECT Cat, Date, ROW_NUMBER() OVER (PARTITION BY Cat ORDER BY Date) AS RowNum FROM @Yak ) AS B ON A.RowNum + 1 = B.RowNum AND A.Cat = B.CatWHERE A.Cat = 'A'ORDER BY A.Date
Not sure if OP uses SQL 2005. thats why i gave 2000 based solution |
 |
|
|
alanhuro
Starting Member
34 Posts |
Posted - 2008-07-16 : 12:30:23
|
Lamprey.I have to say you are a truly genius. It works like charm on the first tried. I can not recall how many time you save me on the SQL query problem. Thank again for your help.Maybe you should write a book just for advanced query to cover all the senarios. . Let me know I will need one.thanks |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-07-16 : 12:52:16
|
quote: Originally posted by alanhuro That is quick. Thank for your help you guy. I still have a problem. - Khtan you are right I try to run the query on the Oracle db. I though the query is going to be the same.- visakh16. I tried your script and the error I got is "ORA-00904: "IDENTITY": invalid identifier". Is there any other way? I just have a read access to the db so I can not create any table or made any change to the DB. I try to avoid doing that too. I will shade my head if I try to.Peso - I try your script and got an error "ORA-00942: table or view does not exist" maybe I miss something.Thanks
i never realised you were using ORACLE. But this is a MS SQL Server forum and all solutions we provide are guaranteed to work only in sql server. You could get ORACLE specific solutions by posting in oracle forums. |
 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2008-07-16 : 12:55:19
|
quote: Originally posted by visakh16Not sure if OP uses SQL 2005. thats why i gave 2000 based solution 
Ahh, I guess I don't even think about that unless I see strange syntax. :) EDIT: My brain is not workign too well. I was quoting you, but I was talking about weather the OP used Oracle or not.. <sigh>quote: Originally posted by alanhuro Lamprey.I have to say you are a truly genius. It works like charm on the first tried. I can not recall how many time you save me on the SQL query problem. Thank again for your help.Maybe you should write a book just for advanced query to cover all the senarios. . Let me know I will need one.thanks
You are welcome. I think there are plenty of books on queries. However, I'm tempted to write one on data warehousing to show how it should be done. ;) |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-07-16 : 13:57:01
|
quote: Originally posted by Lamprey
quote: Originally posted by visakh16Not sure if OP uses SQL 2005. thats why i gave 2000 based solution 
Ahh, I guess I don't even think about that unless I see strange syntax. :) EDIT: My brain is not workign too well. I was quoting you, but I was talking about weather the OP used Oracle or not.. <sigh>quote: Originally posted by alanhuro Lamprey.I have to say you are a truly genius. It works like charm on the first tried. I can not recall how many time you save me on the SQL query problem. Thank again for your help.Maybe you should write a book just for advanced query to cover all the senarios. . Let me know I will need one.thanks
You are welcome. I think there are plenty of books on queries. However, I'm tempted to write one on data warehousing to show how it should be done. ;) No problem. Happens to me at times as well |
 |
|
|
alanhuro
Starting Member
34 Posts |
Posted - 2008-07-30 : 15:41:09
|
Greeting to AllI thought I had it but it came back to be with more problem. Here is the situation. From the example above let say I haveDate-------------Cat2008-07-01-------A2008-07-03-------A2008-07-07-------A2008-07-10-------B2008-07-11-------B2008-07-13-------A 2008-07-15-------A 2008-07-18-------A 2008-07-19-------CThe result I want is the StartDate and Endate for selected Cat. Whatever date in the middle need to be filtered out. Result should be like this assume I select cat AStart Date_______EndDate2008-07-01_______2008-07-072008-07-13_______2008-07-18as you can see anyday in between are removedI made a change the query of Visakh16 below but It does not work. Please take a lookDECLARE -- Setup@Yak TABLE (Date DATETIME, Cat CHAR(1))INSERT @YakSELECT '20080701', 'A'UNION ALL SELECT '20080703', 'A'UNION ALL SELECT '20080707', 'A'UNION ALL SELECT '20080710', 'B'UNION ALL SELECT '20080711', 'B'UNION ALL SELECT '20080713', 'A'UNION ALL SELECT '20080715', 'A'UNION ALL SELECT '20080718', 'A'UNION ALL SELECT '20080719', 'C'select * from @YAK-- Using correlated sub-querySELECT Cat, min(Date) AS StartDate, (SELECT MIN(Date) FROM @Yak WHERE Date > min(Date) and Cat <> 'A') AS EndDateFROM @Yak AS AWHERE A.Cat = 'A'group by min(Date)ORDER BY A.Date I got an error Server: Msg 144, Level 15, State 1, Line 27Cannot use an aggregate or a subquery in an expression used for the group by list of a GROUP BY clause.How can I fix this.Thanks |
 |
|
|
Dallr
Yak Posting Veteran
87 Posts |
Posted - 2008-07-30 : 21:26:14
|
Try the following:DECLARE -- Setup@T TABLE (Date DATETIME, Cat CHAR(1))INSERT @TSELECT '20080701', 'A'UNION ALL SELECT '20080703', 'A'UNION ALL SELECT '20080707', 'A'UNION ALL SELECT '20080710', 'B'UNION ALL SELECT '20080711', 'B'UNION ALL SELECT '20080713', 'A'UNION ALL SELECT '20080715', 'A'UNION ALL SELECT '20080718', 'A'UNION ALL SELECT '20080719', 'C'UNION ALL SELECT '20080722', 'A'UNION ALL SELECT '20080724', 'A'SELECT MIN(T3.Date) AS MinDate ,MAX(T3.DATE) AS MaxDate , T3.catFROM (SELECT (t.Date), Cat ,(SELECT Count(*) FROM @t t2 WHERE t2.date<=t.date AND T2.cat <>'a') AS Counter FROM @t T) T3GROUP BY T3.CAT, CounterHAVING T3.Cat ='a' |
 |
|
|
alanhuro
Starting Member
34 Posts |
Posted - 2008-07-31 : 08:47:07
|
| Dallr very good trick. Thanks a lot. |
 |
|
|
alanhuro
Starting Member
34 Posts |
Posted - 2008-07-31 : 09:23:51
|
| Dallr.I wonder if you know any other ways. This way is working but it take a long time for a large db.I wonder if we somehow find a min time for each cat. The next min time of the next cat will be the end time of prev cat.If don't know if it would be fast that way. so the result will beCat______________Begin Date_______________endateA________________2008-07-03_______________2008-07-10 min day of BA________________2008-07-13_______________2008-07-19 min day of CThank |
 |
|
|
|