簡體   English   中英

如何在SQL中使用內部聯接獲取不重復的記錄

[英]How to get Records Without Duplication Using Inner Join in SQL

我想擺脫來自父表的重復記錄,我使用自連接。 因此,我如何擺脫父表中的重復記錄。 我的表結構看起來像這樣

ID      CategoryName    ParentID    IsActive
----+----------------+-----------+-----------
1   |   Toyota       |   NULL    |     1    
2   |   Honda        |   NULL    |     1    
3   |   Mitsubhi     |   NULL    |     1    
4   |   Toyota GLI   |     1     |     1    
5   |   Toyota XLI   |     1     |     1    
6   |   Swift        |     1     |     1    
7   |   Honda Civic  |     2     |     1    
8   |   Honda City   |     2     |     1    

我已經嘗試通過命令和命令與group進行自我連接,但是現在可以正常工作了

select a.CatogoryName,b.CatogoryName
from CategoryInfo a inner join
     CategoryInfo b
     on a.Id= b.ParentId
Group By a.CatogoryName, b.CatogoryName

結果

Category Name     Model
Honda             Honda City
Honda             Honda Civic
Toyota            Swift
Toyota            Toyota GLI
Toyota            Toyota XLI

我用這個查詢

select a.CatogoryName, b.CatogoryName
from CategoryInfo a inner join
     CategoryInfo b
     on a.Id = b.ParentId 

結果

Category Name     Model
Honda             Honda City
Honda             Honda Civic
Toyota            Swift
Toyota            Toyota GLI
Toyota            Toyota XLI

預期結果

Category Name     Model
Honda             Honda City
                  Honda Civic
Toyota            Swift
                  Toyota GLI
                  Toyota XLI

我希望輸入單個類別名稱,像這樣

“ tmp”部分-檢查查詢的表。 您不需要它,它適合其他讀者:)

CREATE TABLE #tmpCatInfo (ID int, CategoryName varchar(55), ParentID int, IsActive int)
INSERT INTO #tmpCatInfo 
SELECT * FROM (VALUES (1, 'Toyota', NULL, 1)
                     ,(2, 'Honda', NULL, 1)
                     ,(3, 'Mitsubhi', NULL, 1)
                     ,(4, 'Toyota GLI', 1, 1)
                     ,(5, 'Toyota XLI', 1, 1)
                     ,(6, 'Swift', 1, 1)
                     ,(7, 'Honda Civic', 2, 1)
                     ,(8, 'Honda City', 2, 1)) a(ID, CategoryName, ParentID, IsActive)
;

-- USE THIS PART
-- Change #tmpCatInfo on your table name

WITH CategoryInfo (ID, CategoryName, IsActive, Model)
AS
(
SELECT ci.ID            as ID
     , ci.CategoryName  as CategoryName
     , ci2.IsActive     as IsActive
     , ci2.CategoryName as Model
FROM #tmpCatInfo ci
JOIN #tmpCatInfo ci2 ON ci.ID = ci2.ParentID
)

SELECT CASE 
         WHEN row_number() over (PARTITION BY CategoryName ORDER BY (SELECT NULL)) = 1
         THEN CategoryName
       END AS 
      CatogoryName
    , Model
FROM CategoryInfo

-- USE THIS PART

就像Gordon Linoff已經告訴您的那樣:您應該在應用程序中而不是在數據庫中執行此操作。

這是您的想法會引起麻煩的示例:

// Let's create a DataTable which holds the recors you want to get from the db
DataTable dt = new DataTable();
dt.Columns.Add("Category Name");
dt.Columns.Add("Model");

// Let's put in data
dt.Rows.Add(new[] { "Honda", "Honda City" });
dt.Rows.Add(new[] { null, "Honda Civic" });
dt.Rows.Add(new[] { "Toyota", "Swift" });
dt.Rows.Add(new[] { null, "Toyota GLI" });
dt.Rows.Add(new[] { null, "Toyota XLI" });

// dt is now what you want to get from your db.

// Let's see what we got:
Console.WriteLine(dt.Columns[0].ColumnName.PadRight(20) + dt.Columns[1].ColumnName.PadRight(20)); // PadRight to make it look better..
foreach (DataRow item in dt.Rows)
{
    Console.WriteLine((item[0] + "").PadRight(20) + (item[1] + "").PadRight(20));
}

我們得到了您想要的輸出:

Category Name       Model
Honda               Honda City
                    Honda Civic
Toyota              Swift
                    Toyota GLI
                    Toyota XLII

但是您不能使用此數據。 讓我們看看如果您使用它在應用程序中執行某些操作會發生什么:

Console.WriteLine("".PadRight(50, '-')); // Horizontal line to see 1st and 2nd output better

// Now we want to play with our data, by removing the 3rd Row:
dt.Rows.RemoveAt(2); // Index 2 is the 3rd row

// Let's see what we got after removing:
Console.WriteLine(dt.Columns[0].ColumnName.PadRight(20) + dt.Columns[1].ColumnName.PadRight(20)); // PadRight to make it look better..
foreach (DataRow item in dt.Rows)
{
    Console.WriteLine((item[0] + "").PadRight(20) + (item[1] + "").PadRight(20));
}

輸出顯示本田類別中的所有豐田汽車:

Category Name       Model
Honda               Honda City
                    Honda Civic
                    Toyota GLI
                    Toyota XLI

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM