2016-08-02 43 views
0

Ich versuche einen Bericht zu erstellen, der die vom Benutzer übermittelten Formulare anzeigt.Gruppieren von Daten TSQL

Ich möchte jede Zeile ein Vorkommen von jedem Feld pro Benutzer anzeigen.

Benutzername | Erstes Formular eingereicht | Formularadresse (1. Formular) | Letztes Formular eingereicht | Formular Adresse (letzte Form)

Hier ist, was ich habe derzeit:

SELECT form.Name 
      ,(SELECT COUNT (*) FROM dbo.vAdvF_155 af WHERE af.Name = form.Name) AS [TotalForms] 
      ,(SELECT TOP 1 p.Timetag1 FROM dbo.vAdvF_155 af WHERE af.Name = form.Name ORDER BY [TimeTag1] ASC) AS [Started] 
      ,(SELECT TOP 1 af.aField143 FROM dbo.vAdvF_155 af WHERE af.TimeTag1 = form.TimeTag1 ORDER BY [TimeTag1] DESC) AS [FirstFormAddress] 
      ,(SELECT TOP 1 p.Timetag2 FROM dbo.vAdvF_155 af WHERE af.Name = form.Name ORDER BY [TimeTag2] DESC) AS [Submitted] 
      ,(SELECT TOP 1 af.aField143 FROM dbo.vAdvF_155 af WHERE af.RecId = form.RecId ORDER BY [TimeTag2] DESC) AS [LastFormAddress] 

FROM   dbo.vAdvF_155 AS form INNER JOIN 
      dbo.PhoneData AS p ON form.RecId = p.RecID 
      ORDER BY form.Name 

Ergebnisse:

Name TotalForms Started FirstFormAddress Submitted LastFormAddress 
CARL SUTTON 14 2016-07-22 09:30:55.000 19 Lilac Close KEYWORTH 2016-07-22 11:17:36.000 19 Lilac Close KEYWORTH 
CARL SUTTON 14 2016-07-22 12:46:31.000 23 Lincoln Street NEWARK 2016-07-22 13:20:19.000 23 Lincoln Street NEWARK 
CARL SUTTON 14 2016-07-25 10:24:52.000 104 Shireoaks COMMON 2016-07-25 12:04:59.000 104 Shireoaks COMMON 
CARL SUTTON 14 2016-07-25 13:59:11.000 43 Milton DRIVE RAVENSHEAD 2016-07-25 15:53:28.000 43 Milton DRIVE RAVENSHEAD 
CARL SUTTON 14 2016-07-26 10:22:53.000 17 LISMORE COURT MANSFIELD 2016-07-26 11:36:07.000 17 LISMORE COURT MANSFIELD 
CARL SUTTON 14 2016-07-26 13:52:02.000 3 Ruby's AVENUE BALDERTON 2016-07-26 15:51:42.000 3 Ruby's AVENUE BALDERTON 
CARL SUTTON 14 2016-07-27 09:35:54.000 The Elms Station Road NG14 7GD 2016-07-27 14:53:28.000 The Elms Station Road NG14 7GD 
CARL SUTTON 14 2016-07-28 09:09:10.000 Main Road BULCOTE 2016-07-28 10:35:17.000 Main Road BULCOTE 
CARL SUTTON 14 2016-07-28 12:04:17.000 NULL 2016-07-28 12:06:21.000 NULL 
CARL SUTTON 14 2016-07-28 13:13:48.000 2 Midlands AVENUE STAPLEFORD 2016-07-28 15:14:32.000 2 Midlands AVENUE STAPLEFORD 
CARL SUTTON 14 2016-07-31 08:14:03.000 Summit Close KIRKBY 2016-07-31 11:44:32.000 Summit Close KIRKBY 
CARL SUTTON 14 2016-07-31 12:49:29.000 4 Archway Old Clipstone  2016-07-31 14:07:05.000 4 Archway Old Clipstone 
CARL SUTTON 14 2016-08-01 08:20:21.000 5 RAVENSHEAD COURT 2016-08-01 10:08:39.000 5 RAVENSHEAD COURT 
CARL SUTTON 14 2016-08-02 07:56:23.000 Field CLOSE GEDLING 2016-08-02 09:48:13.000 Field CLOSE GEDLING 
CASEY MORTON 13 2016-07-22 09:12:08.000 10 ByRON CRESENT Ng162sx 2016-07-22 11:42:30.000 10 ByRON CRESENT Ng162sx 
CASEY MORTON 13 2016-07-22 13:27:12.000 146  2016-07-22 16:05:29.000 146 
CASEY MORTON 13 2016-07-25 09:14:37.000 5 Cossall Road NG93PG 2016-07-25 14:16:53.000 5 Cossall Road NG93PG 
CASEY MORTON 13 2016-07-26 08:28:14.000 TenYSON Street NG74GA 2016-07-26 11:46:42.000 TenYSON Street NG74GA 
CASEY MORTON 13 2016-07-26 14:31:17.000 NULL 2016-07-26 14:54:23.000 NULL 
CASEY MORTON 13 2016-07-27 06:38:28.000 34 Sturton STREET Ng76hu 2016-07-27 09:24:37.000 34 Sturton STREET Ng76hu 
CASEY MORTON 13 2016-07-27 09:59:05.000 12 TUDOR Close Ng42dr 2016-07-27 15:04:08.000 12 TUDOR Close Ng42dr 
CASEY MORTON 13 2016-07-28 08:43:23.000 12 Ardmore Ng24gp 2016-07-28 11:27:35.000 12 Ardmore Ng24gp 
CASEY MORTON 13 2016-07-28 11:48:28.000 9 Sycamore Close NG122DJ 2016-07-28 13:46:55.000 9 Sycamore Close NG122DJ 
CASEY MORTON 13 2016-07-28 14:40:49.000 15 GoodLIFFE STREET Ng76fz 2016-07-28 15:54:07.000 15 GoodLIFFE STREET Ng76fz 
CASEY MORTON 13 2016-08-01 09:50:08.000 24 VALESIDE Gardens NG42EP 2016-08-01 12:28:27.000 24 VALESIDE Gardens NG42EP 
CASEY MORTON 13 2016-08-01 13:51:53.000 285 Derby Road Ng93ja 2016-08-01 16:09:11.000 285 Derby Road Ng93ja 
CASEY MORTON 13 2016-08-02 07:21:38.000 Melrose House Raleigh Street Ng74hf  2016-08-02 11:24:27.000 Melrose House Raleigh Street Ng74hf 

Wie Sie mit den Ergebnissen sehen kann es zeigt alle Formen, wie sie sind nicht eindeutig, ich möchte es buchstäblich nur in eine Zeile pro Person gruppieren, kann aber die Gruppierung nicht finden.

+0

Können Sie die Quelltabelle und eine Auswahl der Daten zur Verfügung stellen in ihnen? Es ist normalerweise eine schlechte Idee, so viele Sub-Selects zu machen ... Du hast Duplikate in deinen Daten, weil du vor deinem 'ORDER BY' keine' GROUP BY'-Anweisung benutzt hast und es scheint, dass du etwas zusätzliche Filterung anwenden musst auf Ihrer "Formular" -Tabelle, da sie alle Zeilen zurückzieht. – iamdave

Antwort

0

viel über die Struktur Ihrer vAdvF_155 Tabelle/View Unter der Annahme, können Sie in der Lage sein, die Ergebnisse bekommen Sie mit diesem suchen:

declare @vAdvF_155 table (Name nvarchar(50) 
          ,aField143 nvarchar(50) 
          ,Timetag1 datetime 
          ,Timetag2 datetime 
          ) 
insert into @vAdvF_155 values 
('CARL SUTTON' ,'4 Archway Old Clipstone' ,'2016-07-31 12:49:29.000','2016-07-31 14:07:05.000') 
,('CARL SUTTON' ,'5 RAVENSHEAD COURT'  ,'2016-08-01 08:20:21.000','2016-08-01 10:08:39.000') 
,('CARL SUTTON' ,'Field CLOSE GEDLING'  ,'2016-08-02 07:56:23.000','2016-08-02 09:48:13.000') 
,('CASEY MORTON','10 ByRON CRESENT Ng162sx' ,'2016-07-22 09:12:08.000','2016-07-22 11:42:30.000') 
,('CASEY MORTON','146 Street Name'   ,'2016-07-22 13:27:12.000','2016-07-22 16:05:29.000') 
,('CASEY MORTON','5 Cossall Road NG93PG' ,'2016-07-25 09:14:37.000','2016-07-25 14:16:53.000') 


select f.Name 
     ,f.TotalForms 
     ,f.FirstForm 
     ,ff.aField143 as FirstFormAddress 
     ,f.LastForm 
     ,lf.aField143 as LastFormAddress 
from (
    select Name 
      ,count(1) as TotalForms 
      ,min(Timetag1) as FirstForm 
      ,max(Timetag2) as LastForm 
    from @vAdvF_155 
    group By Name 
    ) f 
    inner join @vAdvF_155 ff 
     on(f.Name = ff.Name 
      and f.FirstForm = ff.Timetag1 
      ) 
    inner join @vAdvF_155 lf 
     on(f.Name = ff.Name 
      and f.LastForm = lf.Timetag2 
      ) 
+1

Wie Sie gesagt haben, ich glaube, ich habe es mit all den Unterabfragen zu sehr verkompliziert. Ich habe den Code gebrochen, um zu sehen, wie alles funktioniert hat (Ihre Antwort) und dies bringt die Ergebnisse nach Bedarf zurück. Vielen Dank für Ihre Hilfe zu diesem Thema! – Benzz