2012-02-17 6 views
0

私はMS-SQLを使用していますが、マスターテーブルの行と詳細テーブルの関連する行をフェッチするクエリを作成しようとしています。今、私が望むことは、のみがマスターテーブルから関連するフィールドを取得し、詳細テーブルからその最初の行を空白にしなければならないということです。詳細テーブルに関連する行がある場合は、別々の行に表示されます。私は次のクエリを使用しようとしていますが、望む結果が得られていません。マスターテーブルからすべての行を取得し、行をMS-SQLのディテールテーブルから関連付けるにはどうすればよいですか?

SELECT 
     DISTINCT 
     ProductMaster.ProductMasterID, ProductMaster.ProductName, 
     ProductMaster.SubCategoryID, SubCategory.SubCategoryName, 
     ProductBrandAndType.ProductBranAndTypeID, ProductBrandAndType.ProductType, 
     ProductBrandAndType.Brand, ProductMaster.ProductDesc, 
     ProductMaster.ReOrderLevel 
FROM 
    ProductBrandAndType 
RIGHT OUTER JOIN 
    Inward 
ON ProductBrandAndType.ProductBranAndTypeID = Inward.ProductBrandAndTypeID 
RIGHT OUTER JOIN 
    ProductMaster 
ON Inward.ProductID = ProductMaster.ProductMasterID 
LEFT OUTER JOIN 
    SubCategory 
ON ProductMaster.SubCategoryID = SubCategory.SubCategoryID 
ORDER BY 
     ProductMaster.ProductName, 
     ProductBrandAndType.ProductType, 
     ProductBrandAndType.Brand; 

誰でもこのことを教えてもらえますか?

よろしく

Sikandar

+1

これは事実上プレゼンテーションであり、非常によく対処されています。 SQLよりもレポートビルダ/フォーマッタです。 –

答えて

1

次のクエリが実行されました。

SELECT  dbo.ProductMaster.ProductMasterID, dbo.ProductMaster.ProductName, dbo.ProductMaster.SubCategoryID, dbo.ProductMaster.ProductDesc, 
         dbo.ProductMaster.ReOrderLevel, null as ProductBrandAndTypeID, 
         null AS Type, null as Brand 
FROM   dbo.ProductBrandAndType RIGHT OUTER JOIN 
         dbo.Inward ON dbo.ProductBrandAndType.ProductBranAndTypeID = dbo.Inward.ProductBrandAndTypeID RIGHT OUTER JOIN 
         dbo.ProductMaster ON dbo.Inward.ProductID = dbo.ProductMaster.ProductMasterID LEFT OUTER JOIN 
         dbo.SubCategory ON dbo.ProductMaster.SubCategoryID = dbo.SubCategory.SubCategoryID 
UNION 
SELECT  dbo.ProductMaster.ProductMasterID, dbo.ProductMaster.ProductName, dbo.ProductMaster.SubCategoryID, dbo.ProductMaster.ProductDesc, 
         dbo.ProductMaster.ReOrderLevel, dbo.ProductBrandAndType.ProductBranAndTypeID, 
         dbo.ProductBrandAndType.ProductType, dbo.ProductBrandAndType.Brand 
FROM   dbo.ProductBrandAndType RIGHT OUTER JOIN 
         dbo.Inward ON dbo.ProductBrandAndType.ProductBranAndTypeID = dbo.Inward.ProductBrandAndTypeID RIGHT OUTER JOIN 
         dbo.ProductMaster ON dbo.Inward.ProductID = dbo.ProductMaster.ProductMasterID LEFT OUTER JOIN 
         dbo.SubCategory ON dbo.ProductMaster.SubCategoryID = dbo.SubCategory.SubCategoryID      
ORDER BY ProductName; 
0

もしあなただけの何ProductMasterからの最初の行。そうすれば、次のようなことができます。

;WITH CTE 
(
    SELECT 
     ROW_NUMBER() OVER(ORDER BY ProductMaster.ProductMasterID) AS RowNbr, 
     ProductMaster.ProductName, 
     ProductMaster.SubCategoryID, 
     ProductMaster.ProductDesc, 
     ProductMaster.ReOrderLevel 
    FROM 
     ProductMaster 
) 
SELECT 
     ProductMaster.ProductMasterID, 
     ProductMaster.ProductName, 
     ProductMaster.SubCategoryID, 
     SubCategory.SubCategoryName, 
     ProductBrandAndType.ProductBranAndTypeID, 
     ProductBrandAndType.ProductType, 
     ProductBrandAndType.Brand, 
     ProductMaster.ProductDesc, 
     ProductMaster.ReOrderLevel 
FROM 
    ProductBrandAndType 
RIGHT OUTER JOIN Inward 
    ON ProductBrandAndType.ProductBranAndTypeID = Inward.ProductBrandAndTypeID 
RIGHT OUTER JOIN CTE AS ProductMaster 
    ON Inward.ProductID = ProductMaster.ProductMasterID 
    AND RowNbr=1 
LEFT OUTER JOIN SubCategory 
    ON ProductMaster.SubCategoryID = SubCategory.SubCategoryID 
ORDER BY 
    ProductMaster.ProductName, 
    ProductBrandAndType.ProductType, 
    ProductBrandAndType.Brand; 
+0

ごめんなさい!私は組合のクエリを使用しました。 –