views:

165

answers:

2

Hello All, I have an SQL Table which consists of 1 column only

Column Name
A 
A
A
B
B
B
B
C
D
D
E

I need an SQL Code that returns the cut points. For the table above, it will return this:

Column Name
    3
    7
    8
    10
    11

3 is the end of A's and 7 is the end of B's and 8 is the end of C's and so on...

Let's see what can you come up with :=)

+13  A: 

Assuming the data can be sorted by your column, the strategy is to generate a row number for each row and do a simple group by to get your data points.

SQL Server 2000

DECLARE @YourTable TABLE (Col VARCHAR(1))
CREATE TABLE #TempTable (ID INTEGER IDENTITY(1, 1), Col VARCHAR(1))

SET IDENTITY_INSERT #TempTable OFF
INSERT INTO @YourTable (Col) VALUES ('A')
INSERT INTO @YourTable (Col) VALUES ('A')
INSERT INTO @YourTable (Col) VALUES ('A')
INSERT INTO @YourTable (Col) VALUES ('B')
INSERT INTO @YourTable (Col) VALUES ('B')
INSERT INTO @YourTable (Col) VALUES ('B')
INSERT INTO @YourTable (Col) VALUES ('B')
INSERT INTO @YourTable (Col) VALUES ('C')
INSERT INTO @YourTable (Col) VALUES ('D')
INSERT INTO @YourTable (Col) VALUES ('D')
INSERT INTO @YourTable (Col) VALUES ('E')

INSERT INTO #TempTable
SELECT      *
FROM        @YourTable
ORDER BY    Col

SELECT    MAX(ID)
FROM      #TempTable
GROUP BY  Col

DROP TABLE #TempTable

SQL Server 2005

DECLARE @Table TABLE (Col VARCHAR(1))

INSERT INTO @Table VALUES ('A')
INSERT INTO @Table VALUES ('A')
INSERT INTO @Table VALUES ('A')
INSERT INTO @Table VALUES ('B')
INSERT INTO @Table VALUES ('B')
INSERT INTO @Table VALUES ('B')
INSERT INTO @Table VALUES ('B')
INSERT INTO @Table VALUES ('C')
INSERT INTO @Table VALUES ('D')
INSERT INTO @Table VALUES ('D')
INSERT INTO @Table VALUES ('E')


SELECT  MAX(RowNumber)
FROM    (SELECT RowNumber = ROW_NUMBER() OVER (ORDER BY Col), Col
         FROM   @Table) t
GROUP BY Col
Lieven
+1 Tidy solution.
Andrew
Any solution for sql server 2000 ?
stckvrflw
@stckvrflw - I've updated the answer to include a SQL Server 2000 solution.
Lieven
Is this suppose to work when I copy paste it ? Because I get the following error: An explicit value for the identity column in table '@TempTable' can only be specified when a column list is used and IDENTITY_INSERT is ON.
stckvrflw
Try specifying the column in the insert statementsINSERT INTO @YourTable(col) VALUES ('A')
HLGEM
<g> it was but I don't have a 2000 instance running so I tested with 2005. Simply add the column name to the inserts should suffice. I've updated the solution.
Lieven
No it didn't make a change for the error.
stckvrflw
@stckvrflw - could you try the new version using #TempTable?
Lieven
didn't work, also I used a version with SET IDENTITY_INSERT #TempTable ON, nothing changes.
stckvrflw
I tried a few things, and simplified some parts of the code that doesn't give an error. Here is the error giving part.SET IDENTITY_INSERT #TempTable ONINSERT #TempTableSELECT *FROM @YourTableORDER BY ColSET IDENTITY_INSERT #TempTable OFF
stckvrflw
Hello, I solved the problem, we forget to write (Col) while inserting into the temp table. Here how it should be. INSERT INTO TempTable (Col)SELECT *FROM @YourTableORDER BY ColThanks for help Lieven :)
stckvrflw
You'r welcome. Thanks for letting us know what solved it for you.
Lieven
+2  A: 
with endings(columnname, ending) as
(
    select columnname, row_number() over (order by columnname) as 'ending'
    from theTable
)
select max(ending)
from endings
group by columnname
DyingCactus