У меня есть представление SQL, которое возвращает меньше результатов, чем выбор внутри него, и я не знаю почему?View возвращает разные результаты из своего 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
Просьба указать запрос на создание вида .. А также оператор select в представлении. Ваш вопрос непонятен. –
Укажите запрос на просмотр и выбор запроса – Mansoor
Могу ли я предположить, что в элементе есть левое соединение, но просмотр регулярного внутреннего соединения. – jarlh