当用户为他的产品付款时,我会向PaymentTBL添加付款记录,现在我想知道每月所有首笔付款的数量。 我构建了这个查询:
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
答案 0 :(得分:1)
试试这个
HTML
<div id="id">
<span>hello</span>
<br>
<a href="">hover me</a>
</div>
答案 1 :(得分: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)
答案 2 :(得分: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)
希望这有帮助。
答案 3 :(得分: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)