2016-08-02 101 views
0

我正在嘗試製作一份顯示用戶提交表單的報告。分組數據TSQL

我希望每行顯示每個用戶每個字段的出現次數。

用戶名|首先提交表格|表格地址(第一表格)|最後提交表格|申請表地址(最後形式)

這裏就是我有目前

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 

結果:

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 

正如你的結果看到它顯示的所有形式,因爲他們並不是很明顯,我從字面上看只是希望將它分組爲每人一行,但無法確定分組。

+0

你能提供源表和對數據的選擇在他們內部?做這麼多的子選擇通常是一個壞主意...... 你的數據有重複,因爲你根本沒有在你的ORDER BY之前使用'GROUP BY'語句,看起來你需要應用一些額外的過濾在你的「表格」表中,因爲它將所有行都拉回來。 – iamdave

回答

0

假設了很多關於你的vAdvF_155表/視圖的結構,你可以得到你正在尋找這樣的結果:

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

正如你所說,我認爲我使所有的子查詢太複雜了。 我已經打破了代碼,看看它是如何工作(你的答案),並根據需要帶回結果。 非常感謝您的幫助! – Benzz