2015-05-29 3 views
7

मेरे मालिक ने मुझे एक ही टेबल दिया है।माता-पिता को बच्चे को कई टेबल पर अभिभावक

 
Related_Items_Table 

Item  | Accessory 
--------------------- 
TV   | Antennae 
TV   | Power Cord 
TV   | Remote 
Laptop  | Power Cord 
Laptop  | Carrying Case 
Camera  | Carrying Case 
Camera  | Lens 
iPod  | Headphones 

मेरे मालिक जो परिणामों के लिए चाहता है उसका वर्णन करने का सबसे अच्छा तरीका प्रक्रिया के माध्यम से चलना है।

  1. उपयोगकर्ता टीवी के लिए खोज करता है।

  2. टीवी पाया जाता है और टीवी के लिए सहायक उपकरण एंटीना, पावर कॉर्ड & रिमोट हैं।

  3. सहायक उपकरण एंटीना, पावर कॉर्ड & रिमोट अब अन्य संबंधित वस्तुओं को खोजने के लिए उपयोग किया जाता है। पावर कॉर्ड लैपटॉप के लिए भी एक सहायक है। एंटीना & रिमोट किसी अन्य आइटम के लिए सहायक नहीं हैं।

  4. आइटम लैपटॉप अब उस आइटम के सामान को खोजने के लिए उपयोग किया जाता है, जो पावर कॉर्ड & कैरीइंग केस हैं।

  5. सहायक उपकरण पावर कॉर्ड & कैरीइंग केस अब अन्य संबंधित वस्तुओं को खोजने के लिए उपयोग किया जाता है। पावर कॉर्ड को कोई नई वस्तु नहीं मिलती है (हम पहले से ही पावर कॉर्ड टीवी & लैपटॉप से ​​जुड़े हैं)। कैरीइंग केस भी कैमरा के लिए एक सहायक है।

  6. आइटम कैमरा अब उस आइटम के सामान को खोजने के लिए उपयोग किया जाता है, जो केस & लेंस ले जा रहे हैं।

  7. सहायक उपकरण कैरीइंग केस & लेंस अब अन्य संबंधित आइटम ढूंढने के लिए उपयोग किए जाते हैं। केस लेना & लेंस को कोई नई वस्तु नहीं मिलती है (हम पहले से ही जानते हैं कि कैरीइंग केस लैपटॉप से ​​जुड़ा हुआ है)।

  8. खोज श्रृंखला जारी रखने के लिए कोई भी नया आइटम नहीं मिला है। अंतिम सूची लौटा।

 
Final List 

Item  | Accessory 
--------------------- 
TV   | Antennae 
TV   | Power Cord 
TV   | Remote 
Laptop  | Power Cord 
Laptop  | Carrying Case 
Camera  | Carrying Case 
Camera  | Lens 

सबसे अच्छा तरीका है इस समस्या को संभालने के लिए क्या है? मुझे यकीन नहीं है कि इसके लिए सही शब्दावली क्या होगी, इसलिए शायद मैं इसे अपनी खोजों में चूक गया। किसी भी सलाह की सराहना की है।

+2

लूप के बिना हल नहीं किया जा सकता है। यह अनंत रिकर्सन है। –

उत्तर

0

मैं इस pesudocode की तरह कुछ करना होगा:

insert into Final_List 
all the records that match the item in Related_Items_Table 

WHILE 1=1 
BEGIN 
    Insert into Final List 
    select NextLevel.* 
    from Related_Items_Table 
    join Related_Items_Table NextLevel 
    on Related_Items_Table.Accessory = NextLevel.Item 
    where the nextlevel.item and nextlevel.accesory not already in Final List 
    if @@Rowcount = 0 
     break 
END 
0

ब्रायन Pressler के स्यूडोकोड बहुत करीब है, मिलती है के साथ समस्याओं के एक जोड़े को बचाने के लिए।

-- The sample data from the problem. 
declare @SearchString varchar(32) = 'TV'; 
declare @RelatedItemsTable table 
(
    [Item] varchar(32), 
    [Accessory] varchar(32) 
); 
insert @RelatedItemsTable values 
    ('TV', 'Antennae'), 
    ('TV', 'Power Cord'), 
    ('TV', 'Remote'), 
    ('Laptop', 'Power Cord'), 
    ('Laptop', 'Carrying Case'), 
    ('Camera', 'Carrying Case'), 
    ('Camera', 'Lens'), 
    ('iPod', 'Headphones'); 

-- This table will hold your results. 
declare @SearchResults table 
(
    [Item] varchar(32), 
    [Accessory] varchar(32) 
); 

-- Base case: look for any item or accessory that matches the search string. 
-- I'm not sure whether you want to search items only or accessories also; 
-- adjust as needed. 
insert @SearchResults 
select * 
from 
    @RelatedItemsTable 
where 
    [Item] like @SearchString or 
    [Accessory] like @SearchString; 

while @@rowcount > 0 
begin 
    -- The recursive case: look for new records where... 
    insert @SearchResults 
    select 
     [New].[Item], 
     [New].[Accessory] 
    from 
     @RelatedItemsTable [New] 
     inner join @SearchResults [Old] on 
      -- ... the new record is an item using the same kind of accessory as 
      -- an existing item, or... 
      [New].[Accessory] = [Old].[Accessory] or 

      -- ... the new record is an accessory for the same kind of item as an 
      -- existing accessory, and... 
      [New].[Item] = [Old].[Item] 
    where 
     -- ... this record doesn't yet appear in the result set. 
     not exists 
     (
      select 1 
      from 
       @SearchResults [Existing] 
      where 
       [Existing].[Accessory] = [New].[Accessory] and 
       [Existing].[Item] = [New].[Item] 
     ); 
end; 

select * from @SearchResults; 

एसक्यूएल सर्वर पुनरावर्ती प्रश्नों-recursive CTE -लेकिन मैं उन में से एक का उपयोग कर, क्योंकि मैं NOT EXISTS को लागू नहीं कर सकता है इस उदाहरण को लागू करने में सक्षम नहीं था के लिए एक तंत्र है: यहाँ मैं इस लगता है कि क्या सोचते है उपरोक्त क्वेरी का हिस्सा।

0

मैं सीटीई के साथ ऐसा नहीं कर सका जैसा मैंने कहा था।कारण है कि यहां समझाया गया है: Prevent recursive CTE visiting nodes multiple times

तो यहाँ एक पुराने फैशन रास्ता

DECLARE @MyTable TABLE(Item NVARCHAR(50), Accessory NVARCHAR(50)) 
DECLARE @Result TABLE(Item NVARCHAR(50), Accessory NVARCHAR(50), LinkedItem NVARCHAR(50), Done int) 

INSERT INTO @MyTable 
VALUES 
('TV', 'Antennae'), 
('TV', 'Power Cord'), 
('TV', 'Remote'), 
('Laptop', 'Power Cord'), 
('Laptop', 'Carrying Case'), 
('Camera', 'Carrying Case'), 
('Camera', 'Lens') 


DECLARE @NbIteration INT = 0 

INSERT INTO @Result 
SELECT t.Item, 
     t.Accessory, 
     LinkedItem.Item, 
     @NbIteration     
FROM @MyTable AS t 
LEFT JOIN @MyTable AS LinkedItem ON t.Accessory = LinkedItem.Accessory 
WHERE t.Item = 'TV' 


WHILE(@@ROWCOUNT > 0) 
BEGIN 
    SELECT @NbIteration = @NbIteration + 1 

    INSERT INTO @Result 
    SELECT t.Item, 
      t.Accessory, 
      LinkedItem.Item, 
      @NbIteration   
    FROM @Result AS r 
    INNER JOIN @MyTable AS t ON r.LinkedItem = t.Item 
    LEFT JOIN @MyTable AS LinkedItem ON t.Accessory = LinkedItem.Accessory 
    WHERE r.Done = @NbIteration - 1 
    AND NOT EXISTS(SELECT TOP 1 1 FROM @Result AS Sub WHERE t.Item = Sub.Item) --don't go back to records already done 

END 

SELECT DISTINCT Item, Accessory 
FROM @Result 
0

आप पाशन आप प्राप्त करना चाहते हैं के लिए एक गोटो कथन का उपयोग के खिलाफ नहीं हैं, तो है, यहाँ एक समाधान है:

DECLARE @search VARCHAR(50) = 'TV' 

--Get initial resultset 
DECLARE @table TABLE (item VARCHAR(50), accessory VARCHAR(50)) 
INSERT INTO @table 
SELECT 
    items.* 
FROM 
    items 
WHERE 
    item LIKE @search 

--declare the variables used for checking if we have any new results 
DECLARE @intCount INT = (SELECT COUNT(*) FROM @table) 
DECLARE @intNewCount INT = (SELECT COUNT(*) FROM @table) 

--The infamous GOTO label 
START: 
    --Store the count of items 
    SET @intCount = (SELECT COUNT(*) FROM @table) 

    --Insert any matching rows for accessory = accessory, excluding ones already added 
    INSERT INTO @table 
     (item, accessory) 
    SELECT 
     item, accessory 
    FROM 
     items 
    WHERE 
     accessory IN (SELECT accessory FROM @table) 
    AND NOT EXISTS(SELECT TOP 1 1 FROM @table t WHERE t.item = items.item AND t.accessory = items.accessory) 

    --Now Insert any matching rows for item = item, excluding ones already added 
    INSERT INTO @table 
     (item, accessory) 
    SELECT 
     item, accessory 
    FROM 
     items 
    WHERE 
     item IN (SELECT item FROM @table) 
    AND NOT EXISTS(SELECT TOP 1 1 FROM @table t WHERE t.item = items.item AND t.accessory = items.accessory) 

    --Set the new count 
    SET @intNewCount = (SELECT COUNT(*) FROM @table) 
--Check if there's been any added during this iteration, if there are, repeat! 
IF @intCount <> @intNewCount GOTO START; 

--Finished 
SELECT * FROM @table 

मैं देख रहा हूँ अन्य उत्तर से ज्यादातर एक समय पाश है, सोचा था कि मैं सिर्फ यह मिश्रण होता अप :) SQL2008

में परीक्षण किया गया
1

यह अपनी मेज presen तरह लग रहा है एक अप्रत्यक्ष ग्राफ टीएस और आपको खोजे गए आइटम उपयोगकर्ता से शुरू होने वाले इस ग्राफ को पार करने की आवश्यकता है।

breadth-first search (BFS) algorithm का उपयोग करने पर विचार करें।

प्रत्येक विज़िट नोड आपको आवश्यक परिणामस्वरूप सूची है।

+0

हालांकि यह लिंक प्रश्न का उत्तर दे सकता है, लेकिन यहां उत्तर के आवश्यक हिस्सों को शामिल करना बेहतर है और संदर्भ के लिए लिंक प्रदान करना बेहतर है। लिंक किए गए पृष्ठ में परिवर्तन होने पर लिंक-केवल उत्तर अमान्य हो सकते हैं। - [समीक्षा से] (/ समीक्षा/कम गुणवत्ता वाली पोस्ट/13703724) – techspider

+0

@techspider: एक प्रसिद्ध एल्गोरिदम का नाम देने वाला एक उत्तर, इसके पूर्ण विवरण के लिए एक लिंक, और कुछ नोट्स क्यों और कैसे है लागू "लिंक-केवल" कोई संभावित अर्थ नहीं है। –

संबंधित मुद्दे