2017-08-22 4 views
-1

テーブルに入力された最近の月と年に応じてデータベースからレコードをフェッチします。レコードの月と年を表示するテーブルに列があります。月と年。以下は、使用したspですが、降順でレコードを取得するだけです。私は最近の月と年のすべてのレコードをデータベースからのみ取得します。入力された最近の月と年に応じてレコードを取得します

where [email protected] 
    and Month = Month(DateAdd(month, -1, getDate())) -- month for 1 month ago 
    and Year = Year(DateAdd(month, -1, getDate())) -- year for 1 month ago 

が、良いことでは暦月を(なぜあなたは部品内の格納されているあなたのテーブルに計算列を追加するには、次のようになります。

ALTER PROCEDURE [dbo].[Proc_GetComplianceClientIDBy_ProfileID] 
    @ProfileID varchar(100) 
AS 
BEGIN 
    SET NOCOUNT ON; 
    --Client Wise for the Currenct Month 
select * into #Client_Business_Mapping 
from TLConnectMasterDB.dbo.Client_Business_Mapping with(nolock) 
where [email protected] 
select ClientID,CM_ClientName, SM.SM_Name as StateName, 
    LM.LM_Name as LocationName,CLM.CL_BranchName as BranchName, 
    ROUND(convert(float,sum(CompliedNo)*100/convert(float, 
    (sum(CompliedNo)+sum(NotCompliedNo)))),2) Compliance, 
    Round(convert(float,sum(NotCompliedNo)*100/convert(float, 
    (sum(CompliedNo)+sum(NotCompliedNo)))),2) NotCompliance 
FROM RegulatoryDB..Monthly_Compliance_Summary MS WITH(NOLOCK) 
    join #Client_Business_Mapping 
     on CBM_Client_ID=ClientID collate database_default 
    join RegulatoryDB..Clients_Master CM with(nolock) 
     on CM_ClientID=CBM_Client_ID collate database_default 
    join RegulatoryDB..Client_LocationMaster CLM with(nolock) 
     on CLM.CL_ClientID = CM.CM_ClientID 
      and MS.State = CLM.CL_StateID 
      and MS.Location = CLM.CL_CityID 
      and MS.Branch=CLM.CL_BranchName 
    join RegulatoryDB..State_Master SM 
     on SM.SM_Code=CLM.CL_StateID 
    join RegulatoryDb..Location_Master LM 
     on SM.SM_Code=LM.SM_Code and LM.LM_Code=CLM.CL_CityID 
where [email protected] 
group by month, Year, ClientID, CM_ClientName, SM.SM_Name, 
    LM.LM_Name, CLM.CL_BranchName 
Order by cast(Year as int) desc, cast(Month as int) desc 
END 
+0

MySQLまたはSQL Serverを使用しているデータベースはどれですか? – Noob

+0

SQL Serverを使用しています。 – Prakash

+0

mysqlタグを使用しないでください。 – nacho

答えて

0

ちょうどあなたのwhere句に追加述語を追加最初の場所で別々の列)

Alter table <table with month, year columns> 
    Add SummaryDate As DATEFROMPARTS(year, month, 1) 

その後、あなただけの任意の指定した日付以降にすべてのサマリー行を返すwhere句述語を追加することができます。?

where [email protected] 
    and SummaryDate >= @thresholDate  
+0

私はそれが前月のレコードであり、最近の月のレコードではないと思います。 – Prakash

+0

テーブルを変更できません。 – Prakash

+0

特定の前月の場合は、月、年の述語をパラメータとして明示的に追加します。 –

関連する問題