आप जबकि एक अल्पविराम से अलग सूची में फोन नंबर घूर्णन डेटा को इकट्ठा करने के लिए एक CTE उपयोग कर सकते हैं। यह कुशल नहीं हो सकता है, लेकिन यह एक आसान चाल है।
AdventureWorks2008R2 पर निम्न चलता है, हालांकि आपको व्यक्ति में कुछ अतिरिक्त डेटा भरने की आवश्यकता होगी। व्यक्तिगत फ़ोन नंबर एक व्यक्ति/संख्या प्रकार के लिए एकाधिक फ़ोन नंबर बनाने के लिए।
; with PersonsWithTelephoneNumbersCTE (
BusinessEntityId, FirstName, MiddleName, LastName,
PhoneNumberTypeId, PhoneNumber, PhoneNumbers, Elements)
as (
-- Base case: Just the person identifications with all possible phone types.
select BusinessEntityID, FirstName, MiddleName, LastName, PhoneNumberTypeId,
cast('' as NVarChar(25)), cast('' as VarChar(MAX)), 0
from Person.Person as PP cross join
Person.PhoneNumberType as PNT
union all
-- Add a telephone number.
select CTE.BusinessEntityId, CTE.FirstName, CTE.MiddleName, CTE.LastName,
PNT.PhoneNumberTypeID, PN.PhoneNumber,
cast(CTE.PhoneNumbers + ', ' + PN.PhoneNumber as VarChar(MAX)), CTE.Elements + 1
from PersonsWithTelephoneNumbersCTE as CTE inner join
Person.Person as PP on PP.BusinessEntityID = CTE.BusinessEntityId inner join
Person.PhoneNumberType as PNT on PNT.PhoneNumberTypeID = CTE.PhoneNumberTypeId inner join
Person.PersonPhone as PN on PN.BusinessEntityID = CTE.BusinessEntityId and PN.PhoneNumberTypeID = PNT.PhoneNumberTypeID
where PN.PhoneNumber > CTE.PhoneNumber
)
-- Get the person and the longest list of phone numbers for each person/phone type.
select LastName, FirstName, MiddleName,
(select Name from Person.PhoneNumberType where PhoneNumberTypeID = Edna.PhoneNumberTypeID) as PhoneNumberType,
substring(PhoneNumbers, 3, len(PhoneNumbers) - 2) as PhoneNumbers from (
select BusinessEntityID, FirstName, MiddleName, LastName, PhoneNumberTypeId, PhoneNumbers,
rank() over (partition by BusinessEntityId, PhoneNumberTypeId order by Elements desc) as Ranking
from PersonsWithTelephoneNumbersCTE
) as Edna
where Ranking = 1 and PhoneNumbers <> ''
order by LastName, FirstName, MiddleName, PhoneNumberType
मुझे समझ में नहीं आता कि आप क्या पूछ रहे हैं। क्या आप इसे फिर से ले सकते हैं? –
विषय से बाहर: आप जो पूछ रहे हैं वह संभव है। सबसे पहले, क्या यह एक .NET अनुप्रयोग द्वारा उपभोग किया जा रहा है? मैं पूछता हूँ, क्योंकि यह अधिक स्वच्छ होगा LINQ –
@Neil Fenwick में सुर हाँ यह सही है क्या करना है। क्या आप कृपया बताएं कि मैं LINQ का उपयोग करके यह कैसे करूं? की – Shahin