1

我有这张桌子:

table session(
ID number,
SessionID VarChar,
Date,
Filter
)

此表包含搜索信息,如下所示:

ID  SessionID                   Date                filter
4   peqq421gaspts3nuulq5mwcq    24/05/2012 13:48    meagPixel=5
6   peqq421gaspts3nuulq5mwcq    24/05/2012 13:48    brand=Canon
7   peqq421gaspts3nuulq5mwcq    24/05/2012 13:48    brand=Canon&meagPixel=12.1
8   peqq421gaspts3nuulq5mwcq    24/05/2012 13:48    brand=Canon
10  peqq421gaspts3nuulq5mwcq    24/05/2012 13:48    brand=Nikon
12  peqq421gaspts3nuulq5mwcq    24/05/2012 13:48    meagPixel=12.1
13  peqq421gaspts3nuulq5mwcq    24/05/2012 13:48    meagPixel=12.1&opticalZoom=True
14  peqq421gaspts3nuulq5mwcq    24/05/2012 13:49    meagPixel=12.1&opticalZoom=True&brand=Panasonic
16  peqq421gaspts3nuulq5mwcq    24/05/2012 13:49    price=500.00
18  peqq421gaspts3nuulq5mwcq    24/05/2012 13:49    price=499.00
19  peqq421gaspts3nuulq5mwcq    24/05/2012 13:49    price=499.00&brand=Olympus
21  peqq421gaspts3nuulq5mwcq    24/05/2012 13:49    zoomRange=2000
22  peqq421gaspts3nuulq5mwcq    24/05/2012 13:49    zoomRange=2000&brand=Leica
23  peqq421gaspts3nuulq5mwcq    24/05/2012 13:49    zoomRange=2000&brand=Leica&price=1995.00
24  peqq421gaspts3nuulq5mwcq    24/05/2012 13:49    zoomRange=2000&brand=Leica&price=1995.00&opticalZoom=True
25  peqq421gaspts3nuulq5mwcq    24/05/2012 13:49    zoomRange=2000&brand=Leica&price=1995.00&opticalZoom=True&meagPixel=16.2
26  peqq421gaspts3nuulq5mwcq    24/05/2012 13:50    zoomRange=2000&brand=Leica&price=1995.00&opticalZoom=True&meagPixel=16.2&weight=345
27  peqq421gaspts3nuulq5mwcq    24/05/2012 13:58    zoomRange=2000&brand=Leica&price=1995.00&opticalZoom=True&meagPixel=16.2
41  poiq41111spts00000q5aaaa    27/05/2012 13:48    meagPixel=5

我想获得独特的搜索。独特的搜索是:

  • 用户(会话)的最长搜索(过滤器)
  • 如果第一个过滤器发生变化 - 它需要被视为新搜索(过滤器)

由于 ASP.NET 不保证 SessionID 是唯一的 (SessionID,Date) 是唯一的。

我没走多远:

SELECT        MAX(Filter)
FROM            Session
GROUP BY SessionID

顺便说一句,我给出的示例表数据的结果应该返回:

ID  SessionID                   Date                filter              
4   peqq421gaspts3nuulq5mwcq    24/05/2012 13:48    meagPixel=5     
7   peqq421gaspts3nuulq5mwcq    24/05/2012 13:48    brand=Canon&meagPixel=12.1      
10  peqq421gaspts3nuulq5mwcq    24/05/2012 13:48    brand=Nikon     
14  peqq421gaspts3nuulq5mwcq    24/05/2012 13:49    meagPixel=12.1&opticalZoom=True&brand=Panasonic     
16  peqq421gaspts3nuulq5mwcq    24/05/2012 13:49    price=500.00        
19  peqq421gaspts3nuulq5mwcq    24/05/2012 13:49    price=499.00&brand=Olympus      
26  peqq421gaspts3nuulq5mwcq    24/05/2012 13:50    zoomRange=2000&brand=Leica&price=1995.00&opticalZoom=True&meagPixel=16.2&weight=345     
41  poiq41111spts00000q5aaaa    27/05/2012 13:48    meagPixel=5     

感谢您的任何帮助和指导。

4

3 回答 3

1

@GarethD - 用于 Schema 和插入查询的 Tx。我尝试了稍微不同的方法。我不确定这是否适用于所有情况。它在 mysql 和 mssql 中工作。

          select * 
          from tsession t1 
          where  not exists (
                             select * 
                             from tsession t2 
                             where t2.filter  like concat(t1.filter,'%') 
                             and t1.filter<>t2.filter 
                             and t1.sessionid=t2.sessionid) 
          order by id;

这给出了问题中要求的准确结果。

于 2012-06-07T05:06:20.123 回答
0

要获得最长的搜索过滤器,您需要执行以下操作:

select s.*
from (select s.*,
             row_number() over (partition by sessionid order by len desc) as rownum
      from (select s.*, len(filter) as len
            from session s
           ) s
     ) s
where rownum = 1

我正在使用 Windows 功能执行此操作。您可以通过使用聚合和连接来做同样的事情。

但是,您是说 session 不是真正的标识符。会话/过滤器是。以下查询几乎可以满足您的需求:

select s.*
from (select s.*,
             row_number() overo over (partition by sessionid, filter 
                                      order by len desc) as rownum
      from (select s.*, len(filter) as len
            from session s
           ) s
     ) s
where rownum = 1

(唯一的变化是分区子句包括过滤器。)

您可能有重复项。如果您想要所有重复项,则可以使用稍微不同的查询。

于 2012-06-06T12:14:50.283 回答
0

首先,您的示例数据中似乎存在错误,我认为第 25、26 和 27 行都应该出现在您的最终数据中。27 当然应该,因为它是会话 ID 和日期组合的唯一条目。

假设以上是正确的,那么我认为我已经正确地建立了您的逻辑。

第 1 步是为每个过滤器定义第一个搜索词,以及它在会话中出现的顺序:

;WITH CTE AS
(   SELECT  *, 
            SUBSTRING(Filter, 1, CASE WHEN CHARINDEX('&', Filter) = 0 THEN LEN(Filter) ELSE CHARINDEX('&', Filter) - 1 END) [FirstTerm],
    FROM    Session
)

下一步是确定每次搜索是新搜索还是先前搜索的延续。这是通过获取会话中的上一个搜索词(为什么在最后一个 CTE 中定义了 SessionOrder)并确定第一个搜索词是否相同来完成的。

, CTE2 AS
(   SELECT  T1.*, 
            CASE WHEN T1.SessionOrder = 1 OR T2.SessionOrder IS NOT NULL THEN 1 ELSE 0 END [NewSearch]
    FROM    CTE T1
            LEFT JOIN CTE T2
                ON  T1.SessionID = T2.SessionID
                AND T1.Date = T2.Date
                AND T1.FirstTerm != T2.FirstTerm
                AND T1.SessionOrder = T2.SessionOrder + 1
)

接下来,每个新搜索都需要在会话中拥有自己的排名,以便对目的进行分组。然后,您定义了规则(SessionID、日期和第一个搜索词的唯一组合),然后您可以根据过滤器的长度对唯一组合中的每个项目进行排序:

, CTE3 AS
(   SELECT  *,
            ROW_NUMBER() OVER(PARTITION BY SessionID, Date, ISNULL(SearchNumber, 0) ORDER BY LEN(Filter) DESC) [SearchOrder]
    FROM    CTE2 T1
            OUTER APPLY
            (   SELECT  SUM(NewSearch) [SearchNumber]
                FROM    CTE2 T2
                WHERE   T1.SessionOrder >= T2.SessionOrder
                AND     T1.SessionID = T2.SessionID
                AND     T1.Date = T2.Date
            ) c
)

最后,您需要做的就是将结果限制为 SessionID、Date 和第一个过滤词的每个组合的最长搜索词:

SELECT  ID, SessionID, Date, Filter
FROM    CTE3
WHERE   SearchOrder = 1
ORDER BY ID

通常我会把这一切放在 SQLFiddle 上,而不是在这里发布一个完整的工作示例,但它今天似乎不起作用。所以这是我用来测试数据的完整 SQL:

CREATE TABLE #Session (ID INT, SessionID VARCHAR(50), Date DATETIME, Filter VARCHAR(200))
INSERT INTO #Session VALUES
    (2, 'peqq421gaspts3nuulq5mwcq', '24/05/2012 13:48', 'brand=Canon'),
    (4, 'peqq421gaspts3nuulq5mwcq', '24/05/2012 13:48', 'meagPixel=5'),
    (6, 'peqq421gaspts3nuulq5mwcq', '24/05/2012 13:48', 'brand=Canon'),
    (7, 'peqq421gaspts3nuulq5mwcq', '24/05/2012 13:48', 'brand=Canon&meagPixel=12.1'),
    (8, 'peqq421gaspts3nuulq5mwcq', '24/05/2012 13:48', 'brand=Canon'),
    (10, 'peqq421gaspts3nuulq5mwcq', '24/05/2012 13:48', 'brand=Nikon'),
    (12, 'peqq421gaspts3nuulq5mwcq', '24/05/2012 13:48', 'meagPixel=12.1'),
    (13, 'peqq421gaspts3nuulq5mwcq', '24/05/2012 13:48', 'meagPixel=12.1&opticalZoom=True'),
    (14, 'peqq421gaspts3nuulq5mwcq', '24/05/2012 13:49', 'meagPixel=12.1&opticalZoom=True&brand=Panasonic'),
    (16, 'peqq421gaspts3nuulq5mwcq', '24/05/2012 13:49', 'price=500.00'),
    (18, 'peqq421gaspts3nuulq5mwcq', '24/05/2012 13:49', 'price=499.00'),
    (19, 'peqq421gaspts3nuulq5mwcq', '24/05/2012 13:49', 'price=499.00&brand=Olympus'),
    (21, 'peqq421gaspts3nuulq5mwcq', '24/05/2012 13:49', 'zoomRange=2000'),
    (22, 'peqq421gaspts3nuulq5mwcq', '24/05/2012 13:49', 'zoomRange=2000&brand=Leica'),
    (23, 'peqq421gaspts3nuulq5mwcq', '24/05/2012 13:49', 'zoomRange=2000&brand=Leica&price=1995.00'),
    (24, 'peqq421gaspts3nuulq5mwcq', '24/05/2012 13:49', 'zoomRange=2000&brand=Leica&price=1995.00&opticalZoom=True'),
    (25, 'peqq421gaspts3nuulq5mwcq', '24/05/2012 13:49', 'zoomRange=2000&brand=Leica&price=1995.00&opticalZoom=True&meagPixel=16.2'),
    (26, 'peqq421gaspts3nuulq5mwcq', '24/05/2012 13:50', 'zoomRange=2000&brand=Leica&price=1995.00&opticalZoom=True&meagPixel=16.2&weight=345'),
    (27, 'peqq421gaspts3nuulq5mwcq', '24/05/2012 13:58', 'zoomRange=2000&brand=Leica&price=1995.00&opticalZoom=True&meagPixel=16.2'),
    (41, 'poiq41111spts00000q5aaaa', '27/05/2012 13:48', 'meagPixel=5')

;WITH CTE AS
(   SELECT  *, 
            SUBSTRING(Filter, 1, CASE WHEN CHARINDEX('&', Filter) = 0 THEN LEN(Filter) ELSE CHARINDEX('&', Filter) - 1 END) [FirstTerm],
    FROM    #Session
), CTE2 AS
(   SELECT  T1.*, 
            CASE WHEN T1.SessionOrder = 1 OR T2.SessionOrder IS NOT NULL THEN 1 ELSE 0 END [NewSearch]
    FROM    CTE T1
            LEFT JOIN CTE T2
                ON  T1.SessionID = T2.SessionID
                AND T1.Date = T2.Date
                AND T1.FirstTerm != T2.FirstTerm
                AND T1.SessionOrder = T2.SessionOrder + 1
), CTE3 AS
(   SELECT  *,
            ROW_NUMBER() OVER(PARTITION BY SessionID, Date, ISNULL(SearchNumber, 0) ORDER BY LEN(Filter) DESC) [SearchOrder]
    FROM    CTE2 T1
            OUTER APPLY
            (   SELECT  SUM(NewSearch) [SearchNumber]
                FROM    CTE2 T2
                WHERE   T1.SessionOrder >= T2.SessionOrder
                AND     T1.SessionID = T2.SessionID
                AND     T1.Date = T2.Date
            ) c
)
SELECT  ID, SessionID, Date, Filter
FROM    CTE3
WHERE   SearchOrder = 1
ORDER BY ID

DROP TABLE #Session

附录

好的,根据您的结果集,您实际上并不想按日期列分组,您只需将行按第一个搜索词和 sessionID 分组的长度顺序排列。

此查询产生与您的示例数据相同的结果。我在 2008 R1 中对此进行了测试,但看不出它在 SQL-Server CE 中不起作用的原因。

;WITH CTE AS
(   SELECT  *,
            ROW_NUMBER() OVER(PARTITION BY SessionID, SUBSTRING(Filter, 1, CASE WHEN CHARINDEX('&', Filter) = 0 THEN LEN(Filter) ELSE CHARINDEX('&', Filter) - 1 END) ORDER BY LEN(Filter) DESC) [RowNumber]
    FROM    Session
)
SELECT  *
FROM    CTE
WHERE   RowNumber = 1
ORDER BY ID

最终解决方案的SQL Fiddle

于 2012-06-06T12:39:39.607 回答