我有一个存储过程,在这个过程中我会在用户所需的几个月之间获得数据。
Q1。我想显示Membername而不是memberID。如何从成员主表中获取成员名称?
Q2。其次我想添加这些行(求和)。像每行存储一个int值。我希望相同的行具有相同的成员ID,并为每个类型显示一行。 例如,如果3个月内访问者有三个条目,我不希望三个单独的行显示1,1,1而不是我想要一个值为3的行
USE [MTS]
GO
/****** Object: StoredProcedure [dbo].[sp_MTS_MemberTracking_generateReport] Script Date: 07-04-2015 14:48:52 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- sp_MTS_MemberTracking_generateReport '01/01/2015','03/01/2015'
-- =============================================
ALTER PROCEDURE [dbo].[sp_MTS_MemberTracking_generateReport]
-- Add the parameters for the stored procedure here
@monthYearFrom datetime , @monthYearTo datetime
AS
select MemberId,TrackingId,MonthYear, AttendanceInFormalMixer,AttendanceInFbb,GoodKarmaCredit,CupOfCofee,Business,Training,Visitors
from [dbo].[MTS_MemberTracking] MtsTracking
where MtsTracking.MonthYear >= @monthYearFrom and MtsTracking.MonthYear <= @monthYearTo
答案 0 :(得分:1)
查看代码和网页后。你需要这样的东西。
ALTER PROCEDURE [dbo].[sp_MTS_MemberTracking_generateReport]
-- Add the parameters for the stored procedure here
@monthYearFrom datetime , @monthYearTo datetime
AS
select max(mtsmem.Name) Name,MtsTracking.MemberId,--TrackingId,MonthYear,
sum(Convert(int,AttendanceInFormalMixer))*10 AttendanceInFormalMixer,
sum(Convert(int,AttendanceInFbb))*10 AttendanceInFbb,
sum(CASE WHEN GoodKarmaCredit <> 0 THEN 10 ELSE 0 END) GoodKarmaCredit,
sum(CASE WHEN CupOfCofee <2 THEN CupOfCofee*5 ELSE 10 END) CupOfCofee,
sum(CASE WHEN Convert(int,Business)<>0 THEN 10 ELSE 0 END) Business,
sum(CASE WHEN Training<>0 THEN 10 ELSE 0 END) Training,
sum(CASE WHEN Visitors<>0 THEN 10 ELSE 0 END) Visitors,
convert(decimal(10,2),(sum(Convert(int,AttendanceInFormalMixer))*10 +
sum(Convert(int,AttendanceInFbb))*10 +
sum(CASE WHEN GoodKarmaCredit <> 0 THEN 10 ELSE 0 END) +
sum(CASE WHEN CupOfCofee <2 THEN CupOfCofee*5 ELSE 10 END) +
sum(CASE WHEN Convert(int,Business)<>0 THEN 10 ELSE 0 END) +
sum(CASE WHEN Training<>0 THEN 10 ELSE 0 END) +
sum(CASE WHEN Visitors<>0 THEN 10 ELSE 0 END) )*1.00/7)
UserScore
from [dbo].[MTS_MemberTracking] MtsTracking
inner join MTS_Members mtsmem on MtsTracking.MemberId = mtsmem.MemberId
where MtsTracking.MonthYear >= @monthYearFrom and MtsTracking.MonthYear <= @monthYearTo
group by MtsTracking.MemberId