Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL Select query

Posted on 2014-04-30
3
Medium Priority
?
154 Views
Last Modified: 2014-04-30
I have a table such as the table below.   The BegMTypw identifies where a particular feature begins, EndMType identifies where a particular feature ends.  CasingID is the foreignkey to the Feature.  
ID      Coordinate      BegMType      EndMType      CasingID
1      0                      Casing                                             1
2      5                  
3      7                  
4      8                  
5      9                  
6      10                                               Casing                       1
7      20                  
8      30                  
9      35                  
10      50                       Casing                                             1
11      51                  
12      62                  
13      100                                               Casing                       1


My output should look like

BegM    EndM     CasingID
0            10            1
50           100          1

The coordinates will always be entered in order.  Any ideas on how I can get the output above?  I can't do min and max because the CasingID is not unique, this would yeild

BegM   EndM   CasingID
0             100        1

This result would be incorrect, I need the two records for beg and end.
0
Comment
Question by:yanci1179
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 5

Accepted Solution

by:
dannygonzalez09 earned 2000 total points
ID: 40032386
Something like this?
SELECT * INTO #Test
FROM
(
SELECT Id = 1,CoOrdinate = 0, BegMType = 'Casing',EndMtype = '',CasingId = 1
UNION
SELECT Id = 2,CoOrdinate = 5, BegMType = '',EndMtype = '',CasingId = ''
UNION
SELECT Id = 3,CoOrdinate = 7, BegMType = '',EndMtype = '',CasingId = ''
UNION
SELECT Id = 4,CoOrdinate = 10, BegMType = '',EndMtype = 'Casing',CasingId = 1
UNION
SELECT Id = 5,CoOrdinate = 50, BegMType = 'Casing',EndMtype = '',CasingId = 1
UNION
SELECT Id = 6,CoOrdinate = 100, BegMType = '' ,EndMtype = 'Casing',CasingId = 1

)x

With BegCTE AS
(
SELECT CoOrdinate,CasingId,RANK() OVER (PARTITION BY BegMType ORDER BY Id) BegRnk FROM #Test
WHERE LEN(BegMType) > 1
)
,EndCTE AS
(
SELECT CoOrdinate,CasingId,RANK() OVER (PARTITION BY EndMType ORDER BY Id) EndRnk FROM #Test
WHERE LEN(EndMType) > 1
)

SELECT B.CoOrdinate,E.CoOrdinate,B.CasingId FROM BegCTE B
LEFT JOIN EndCTE E
ON B.BegRnk = E.EndRnk and B.CasingId = E.CasingId

Open in new window

0
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 40032387
you could look at the rank() function.

http://technet.microsoft.com/en-us/library/ms176102.aspx

it's a CTE with a join

something like


;with cte as
(select *, rank() over (Partition By CasingID order by coordinate) myRank
  from table
  where isnull(BegMType, '') != ''  or isnull(EndMType, '') != ''
)  


select  B.Coordinate, E.Coordinate, B.CasingId
from cte B
join cte E on B.CasingID = E.CasingID and E.MyRank = B.MyRank +1 and isnull(B.BegMType, '') != ''
0
 

Author Closing Comment

by:yanci1179
ID: 40033216
Thanks!!
0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question