2016-08-03 10 views
0

Wenn Benutzer für sein Produkt bezahlen, füge ich einen Zahlung Datensatz zu PaymentTBL hinzu, jetzt möchte ich die Anzahl aller ersten Zahlungen pro Monat wissen. Ich baute diese Abfrage:Anzahl der ersten Datensätze in SQL Server

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

    group by Month(StartDate), Year(StartDate) 

Aber es gibt mir nicht, was ich genau will, weil ich weiß, müssen nur diejenigen, die mit App in diesem Monat beginnen und nicht diejenigen, die/reoccurring ihre Zahlungen erneuern.

Gibt es einen guten Weg, dies zu erreichen?

Unten ist die PaymentTBL Struktur:

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] 

Beispiel von Daten, die ich brauche:

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

uns anzeigen Einige Beispieltabellendaten beenden das erwartete Ergebnis! – jarlh

+0

Ja, ich habe die Struktur der Tabelle hinzugefügt –

+0

Zeigen Sie uns einige Beispieltabellendaten –

Antwort

1

Versuchen Sie, diese

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

Es scheint, Sie haben, was ich meine, aber bitte überprüfen Sie Ihre Abfrage, es hat Syntaxfehler. Falsche Syntax in der Nähe des Schlüsselwortes 'on'. –

0

Sie können diese Abfrage versuchen: -

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) 

Hoffnung, das hilft.

0

Ich glaube, Sie Aufzeichnung in jedem Monat erste Zahlung soll.

id Spalte Primärschlüssel der Tabelle

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

Ich glaube, Sie row_number verwenden können() eine Person, die erste Zahlung zu bestimmen, dann die pro Monat zählen, wie folgt aus:

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

von PK_PaymentsTBL !!! –

+0

ok, hab es, danke –

Verwandte Themen