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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Query question

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. Example

Date-------------Cat
2008-07-01-------A
2008-07-03-------B
2008-07-07-------C
2008-07-10-------A
2008-07-11-------B
2008-07-13-------A

The result I want return is

Cat--------Begin Date------------End Date
A----------2008-07-01------------2008-07-10
A----------2008-07-10------------2008-07-13
A----------2008-07-13------------Null


2. My second question is what query to return the column name within a table. Take an example above the result should return

Date 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]

Go to Top of Page

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. Example

Date-------------Cat
2008-07-01-------A
2008-07-03-------B
2008-07-07-------C
2008-07-10-------A
2008-07-11-------B
2008-07-13-------A

The result I want return is

Cat--------Begin Date------------End Date
A----------2008-07-01------------2008-07-10
A----------2008-07-10------------2008-07-13
A----------2008-07-13------------Null


2. My second question is what query to return the column name within a table. Take an example above the result should return

Date and cat. Since there are 2 columns in that example table?

Thanks a bunch for your help.

Alan





may be this


SELECT IDENTITY(int,1,1) AS Seq,
Date,Cat INTO #Temp
FROM YourTable
ORDER BY Cat,Date


SELECT Cat,[Begin Date],[End Date]
FROM
(
SELECT t1.Cat,t1.Date AS [Begin Date],t2.Date AS [End Date]
FROM #Temp t1
INNER JOIN #Temp t2
ON t2.Seq=t1.Seq + 1
AND t2.Cat=t1.Cat

UNION ALL

SELECT Cat,Date,NULL
FROM #Temp t
INNER JOIN (SELECT Cat,MAX(Date) AS MaxDate
FROM #Temp
GROUP BY Cat)tmp
ON tmp.Cat=t.Cat
AND tmp.MaxDate=t.Date)r

ORDER BY r.Cat
Go to Top of Page

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"
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2008-07-16 : 09:54:41
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]

Go to Top of Page

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"
Go to Top of Page

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

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

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 @Yak
SELECT '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-query
SELECT
Cat,
Date AS StartDate,
(SELECT MIN(Date) FROM @Yak WHERE Date > A.Date and Cat = A.Cat) AS EndDate
FROM
@Yak AS A
WHERE
A.Cat = 'A'
ORDER BY
A.Date

-- Using ROW_NUMBER
SELECT
A.Cat,
A.Date AS StartDate,
B.Date AS EndDate
FROM
(
SELECT
Cat,
Date,
ROW_NUMBER() OVER (PARTITION BY Cat ORDER BY Date) AS RowNum
FROM
@Yak
) AS A
LEFT 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.Cat
WHERE
A.Cat = 'A'
ORDER BY
A.Date
Go to Top of Page

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






Go to Top of Page

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 @Yak
SELECT '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-query
SELECT
Cat,
Date AS StartDate,
(SELECT MIN(Date) FROM @Yak WHERE Date > A.Date and Cat = A.Cat) AS EndDate
FROM
@Yak AS A
WHERE
A.Cat = 'A'
ORDER BY
A.Date

-- Using ROW_NUMBER
SELECT
A.Cat,
A.Date AS StartDate,
B.Date AS EndDate
FROM
(
SELECT
Cat,
Date,
ROW_NUMBER() OVER (PARTITION BY Cat ORDER BY Date) AS RowNum
FROM
@Yak
) AS A
LEFT 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.Cat
WHERE
A.Cat = 'A'
ORDER BY
A.Date



Not sure if OP uses SQL 2005. thats why i gave 2000 based solution
Go to Top of Page

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

Go to Top of Page

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.
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2008-07-16 : 12:55:19
quote:
Originally posted by visakh16
Not 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. ;)
Go to Top of Page

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 visakh16
Not 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
Go to Top of Page

alanhuro
Starting Member

34 Posts

Posted - 2008-07-30 : 15:41:09
Greeting to All

I thought I had it but it came back to be with more problem. Here is the situation. From the example above let say I have

Date-------------Cat
2008-07-01-------A
2008-07-03-------A
2008-07-07-------A
2008-07-10-------B
2008-07-11-------B
2008-07-13-------A
2008-07-15-------A
2008-07-18-------A
2008-07-19-------C

The 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 A

Start Date_______EndDate
2008-07-01_______2008-07-07
2008-07-13_______2008-07-18

as you can see anyday in between are removed

I made a change the query of Visakh16 below but It does not work. Please take a look



DECLARE
-- Setup
@Yak TABLE (Date DATETIME, Cat CHAR(1))

INSERT @Yak
SELECT '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-query
SELECT
Cat, min(Date) AS StartDate,
(SELECT MIN(Date) FROM @Yak WHERE Date > min(Date) and Cat <> 'A') AS EndDate
FROM
@Yak AS A
WHERE
A.Cat = 'A'
group by min(Date)
ORDER BY A.Date



I got an error

Server: Msg 144, Level 15, State 1, Line 27
Cannot 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




Go to Top of Page

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 @T
SELECT '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.cat
FROM
(SELECT (t.Date), Cat
,(SELECT Count(*) FROM @t t2 WHERE t2.date<=t.date AND T2.cat <>'a') AS Counter
FROM @t T) T3
GROUP BY T3.CAT, Counter
HAVING T3.Cat ='a'
Go to Top of Page

alanhuro
Starting Member

34 Posts

Posted - 2008-07-31 : 08:47:07
Dallr very good trick. Thanks a lot.
Go to Top of Page

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 be

Cat______________Begin Date_______________endate
A________________2008-07-03_______________2008-07-10 min day of B
A________________2008-07-13_______________2008-07-19 min day of C

Thank
Go to Top of Page
   

- Advertisement -