2016-11-21 11 views
0

SQLビューでは、選択した結果よりも少ない結果しか返しません。理由はわかりません。ビューは、SELECTと異なる結果を返します。

SQLビューコード:

SELECT cs.customer_id, cs.store_id, cs.join_date, c.name, c.email, c.phone, cs.points, 
     COALESCE (aph.added_point, 0) AS added_point, 
     COALESCE (aph.spended_cash, 0) AS spended_cash 
FROM dbo.customer_store AS cs 
INNER JOIN dbo.customer AS c 
    ON cs.customer_id = c.id 
LEFT OUTER JOIN dbo.added_points_history AS aph 
    ON cs.customer_id = aph.customer_id AND cs.store_id = aph.store_id 

上記のクエリは、26行を返しますが、私はビューから選択するとき、それはわずか7行を返します!

ビューを作成したコード:

--Set the options to support indexed views. 
SET NUMERIC_ROUNDABORT OFF; 
SET ANSI_PADDING, ANSI_WARNINGS, CONCAT_NULL_YIELDS_NULL, ARITHABORT, QUOTED_IDENTIFIER, ANSI_NULLS ON; 
GO 
--Create view with schemabinding. 
IF OBJECT_ID ('dbo.customers_store_with_points', 'view') IS NOT NULL 
DROP VIEW dbo.customers_store_with_points ; 
GO 
CREATE VIEW dbo.customers_store_with_points 
WITH SCHEMABINDING 
AS 
SELECT cs.customer_id, cs.store_id, cs.join_date, c.name, c.email, c.phone, cs.points, 
    COALESCE (aph.added_point, 0) AS added_point, 
    COALESCE (aph.spended_cash, 0) AS spended_cash 
FROM dbo.customer_store AS cs 
INNER JOIN dbo.customer AS c 
ON cs.customer_id = c.id 
LEFT OUTER JOIN dbo.added_points_history AS aph 
ON cs.customer_id = aph.customer_id AND cs.store_id = aph.store_id 
GO 
--Create an index on the view. 
CREATE UNIQUE CLUSTERED INDEX IDX_customerId_storeId 
ON dbo.customers_store_with_points (customer_id, store_id); 
GO 

ビューからの結果を得るための選択なステートメント:同一のデータソースとの

SELECT TOP 1000 * 
FROM [dbo].[customers_store_with_points] 
order by store_id 
+1

create viewのクエリを指定してください。また、Viewのselect文も指定してください。あなたの質問は明確ではありません。 –

+0

viewとselectステートメントの両方のクエリを指定します – Mansoor

+1

selectには左結合がありますが、ビューには通常の内部結合があります。 – jarlh

答えて

1

同一のクエリは同じ結果を返します。
異なるデータベースを照会しているか、照会が異なっています。

+0

いいえ、彼らは同じです!私は質問を更新しました。 –

+0

私は尋ねたとおりに確認しましたか? –

+0

私が言っていることは、Viewの内部コードを使用して外部でテストした結果、View自体とは異なる行が生成されたということです。 –

関連する問題