sql [sql]表值函数示例2

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql [sql]表值函数示例2相关的知识,希望对你有一定的参考价值。

-- Mutfak Alışverişi
SELECT 
	[FT].[Date] 'Date',
	[P].[ProductName] 'Product',
	[FT].[SIGNUPS_COUNT] 'Sign-Ups Count',
	[FT].[SELF_REFS_COUNT] 'Self Refs Count',
	[FT].[WPP_SHARES_COUNT] 'WPP Shares Count',
	[FT].[WPP_VIEWS_COUNT] 'WPP Views Count',
	([FT].[TOTAL_REFS] - [FT].[SELF_REFS_COUNT]) AS 'External Refs Count',
	([FT].[SELF_REFS_COUNT] + [FT].[WPP_VIEWS_COUNT]) AS 'Total PP Views',
	[FT].[TOTAL_REFS] 'Total Refs',
	[FT].[SALES_COUNT] 'Total Sales Count'
FROM 
	[dbo].[func_Performance_Report](13, '%2017-01-30%') AS [FT]
	INNER JOIN [PRODUCTS] [P] ON [P].[ID] = [FT].[ProductID]
USE [linaprod]
GO
/****** Object:  UserDefinedFunction [dbo].[func_Performance_Report]    Script Date: 4/20/2017 8:11:35 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [dbo].[func_Performance_Report]
(
	@ProductID INT,
	@Date NVARCHAR(20)--,
	--@GroupID INT
)
RETURNS TABLE
AS
RETURN
(
	SELECT 
		REPLACE(@Date, '%', '') [Date],
		@ProductID [ProductID],
		[T1].[TOTAL_REFS], 
		[T2].[SELF_REFS_COUNT],
		[T3].[WPP_SHARES_COUNT],
		[T4].[WPP_VIEWS_COUNT],
		[T5].[SIGNUPS_COUNT],
		[T6].[SALES_COUNT]
	FROM
	(
		SELECT 
			COUNT([R].[ID]) AS 'TOTAL_REFS'
		FROM 
			REFERRALS [R]
			INNER JOIN [PRESENTERS] [P] ON 
				[P].[ID] = [R].[PRESENTERID]
		WHERE 
			[R].[INSERTDT] LIKE @Date
			AND [R].[PRODUCTID] = @ProductID
			--AND [P].[GROUPID] = @GroupID
	) AS T1,
	(
		SELECT 
			COUNT([R].[ID]) AS 'SELF_REFS_COUNT'
		FROM 
			REFERRALS [R]
			INNER JOIN [PRESENTERS] [P] ON 
				[P].[PHONE] = [R].[PHONE]
				AND [P].[ID] = [R].[PRESENTERID]
		WHERE 
			[R].[INSERTDT] LIKE @Date 
			AND [R].[PRODUCTID] = @ProductID
			--AND [P].[GROUPID] = @GroupID
	) AS T2,
	(
		SELECT 
			COUNT([SL].[ID]) AS 'WPP_SHARES_COUNT'
		FROM 
			[SHARELOG] [SL]
			INNER JOIN [PRESENTERS] [P] ON 
				[P].[ID] = [SL].[PRESENTERID]
		WHERE 
			[SL].[INSERTDT] LIKE @Date
			AND [SL].[PRODUCTID] = @ProductID
			--AND [P].[GROUPID] = @GroupID
	) AS T3,
	(
		SELECT 
			COUNT([VL].[ID]) AS 'WPP_VIEWS_COUNT'
		FROM 
			[VIEWLOG] [VL]
			INNER JOIN [PRESENTERS] [P] ON 
				[P].[ID] = [VL].[PRESENTERID]
		WHERE 
			[VL].[INSERTDT] LIKE @Date
			AND [VL].[PRODUCTID] = @ProductID
			--AND [P].[GROUPID] = @GroupID
	) AS T4,
	(
		SELECT 
			COUNT([P].[ID]) AS 'SIGNUPS_COUNT'
		FROM 
			[PRESENTERS] [P]
		WHERE 
			[P].[INSERTDT] LIKE @Date
			--AND [P].[GROUPID] = @GroupID
	) AS T5,
	(
		SELECT 
			COUNT([R].[ID]) AS 'SALES_COUNT'
		FROM 
			[REFERRALS] [R]
			INNER JOIN [PRESENTERS] [P] ON 
				[P].[ID] = [R].[PRESENTERID]
		WHERE 
			[R].[ProcessStatusID] = 4
			AND [R].[INSERTDT] LIKE @Date
			AND [R].[PRODUCTID] = @ProductID
			--AND [P].[GROUPID] = @GroupID
	) AS T6
)

以上是关于sql [sql]表值函数示例2的主要内容,如果未能解决你的问题,请参考以下文章

sql2005中 表值函数是啥

SQL Server用户定义的函数(UDF)使用详解

使用 nhibernate t-sql 表值用户定义函数

SQL Server存储过程中使用表值作为输入参数示例

表值参数

sql2005中 表值函数是啥