2016-08-03 63 views
0

當用戶爲他的產品付款時,我向PaymentTBL添加了一筆付款記錄,現在我想知道每月所有第一筆付款的數量。 我建這個查詢:獲取SQL Server中第一筆記錄的數量

SELECT Count(*) as OptIn, Month(StartDate) As MonthNum, Year(StartDate) As YearNum 
    FROM [dietdb].[dbo].[PaymentsTBL] 

    group by Month(StartDate), Year(StartDate) 

但它不給我我想要的東西,因爲我只需要那些誰與應用在這個月開始,而不是那些誰再次發生/知道更新他們的付款。

有沒有什麼好的方法來實現這個目標?

下面是PaymentTBL結構:

CREATE TABLE [dbo].[PaymentsTBL](
    [AutoNo] [int] IDENTITY(1,1) NOT NULL, 
    [PersonID] [nvarchar](50) NOT NULL, 
    [UDID] [nvarchar](50) NULL, 
    [StartDate] [datetime] NULL, 
    [Duration] [float] NULL CONSTRAINT [DF_PaymentsTBL_Duration] DEFAULT ((0)), 
    [EndDate] [datetime] NULL, 
    [Points] [float] NULL CONSTRAINT [DF_PaymentsTBL_Points] DEFAULT ((0)), 
    [Cost] [float] NULL CONSTRAINT [DF_PaymentsTBL_Cost] DEFAULT ((0)), 
    [Currency] [int] NULL CONSTRAINT [DF_PaymentsTBL_Currency] DEFAULT ((0)), 
    [TypeID] [int] NULL CONSTRAINT [DF_PaymentsTBL_TypeID] DEFAULT ((2)), 
    [IsActive] [bit] NULL CONSTRAINT [DF_PaymentsTBL_IsActive] DEFAULT ((0)), 
    [InsertDate] [datetime] NULL CONSTRAINT [DF_PaymentsTBL_InsertDate] DEFAULT (getdate()), 
    [InsertUser] [nvarchar](50) NULL, 
    [UpdateDate] [datetime] NULL CONSTRAINT [DF_PaymentsTBL_UpdateDate] DEFAULT (getdate()), 
    [UpdateUser] [nvarchar](50) NULL, 
    [PayBy] [int] NULL CONSTRAINT [DF_PaymentsTBL_PayBy] DEFAULT ((1)), 
CONSTRAINT [PK_PaymentsTBL] PRIMARY KEY CLUSTERED 
(
    [AutoNo] ASC 
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 
) ON [PRIMARY] 

數據樣本,我需要:

OptIn MonthNo YearNo 
47 1 2015 
56 2 2015 
72 3 2015 
61 4 2015 
74 5 2015 
43 6 2015 
154 7 2015 
180 8 2015 
190 9 2015 
139 10 2015 
169 11 2015 
117 12 2015 
147 1 2016 
137 2 2016 
135 3 2016 
154 4 2016 
141 5 2016 
109 6 2016 
162 7 2016 
75 8 2016 
+2

向我們展示一些樣品表數據,結束預期結果! – jarlh

+0

是的,我添加了表 –

+0

的結構向我們展示一些示例表格數據 –

回答

1

試試這個

SELECT Count(*) as OptIn, Month(StartDate) As MonthNum, Year(StartDate) As YearNum FROM 
(
select PersonID, min(startdate) as startdate FROM [dietdb].[dbo].[PaymentsTBL] 
group by PersonID 
) as t 
group by Month(StartDate), Year(StartDate) 
+0

看來你明白了我的意思,但請檢查你的查詢是否有語法錯誤。關鍵字'on'附近的語法錯誤。來自PK_PaymentsTBL的 –

0

你可以試試這個查詢: -

SELECT Count(*) as OptIn, Month(StartDate) As MonthNum, Year(StartDate) As YearNum 
FROM [dietdb].[dbo].[PaymentsTBL] 
WHERE Month(StartDate) = MONTH(GETDATE()) 
AND Year(StartDate) = YEAR(GETDATE()) 
group by Month(StartDate), Year(StartDate) 

希望這有助於。

0

我想你想在每月的第一筆付款的記錄。

id列是表的主鍵

SELECT min(id) as OptIn, Month(StartDate) As MonthNum, Year(StartDate) As YearNum 
FROM [dietdb].[dbo].[PaymentsTBL] 
group by Month(StartDate), Year(StartDate) 
1

我認爲你可以使用ROW_NUMBER()來判斷一個人的首付款,再算上那些每月,像這樣:

select 
     Month(StartDate) MonthNo 
    , Year(StartDate) YearNo 
    , count(case when rn = 1 then 1 end) as OptIn 
    , count(*) as count_all 
from (
    select 
     * 
     , row_number() over(partition by PersonID order by StartDate) as rn 
    from PaymentsTBL 
    ) d 
group by 
     Month(StartDate) 
    , Year(StartDate) 
+0

!!! –

+0

好的,明白了,謝謝 –