SQL 服务器 [PATSTAT] 查询 |多个 charindex 值 &

SQL Server [PATSTAT] query | Multiple charindex values &

你好 Stack Overflow 社区。

我正在使用 SQL 从 PATSTAT(来自欧洲专利局的专利数据库)检索数据。我有两个问题(见下文)。为了您的信息,PATSAT sql 命令非常有限。


我。具有多个值的 Charindex

我正在寻找特定的两个特定专利组 ["Y02E" 和 "Y02C"] 并希望检索有关它们的数据。我发现如果我插入一组,使用 charindex 函数 works;

and charindex ('Y02E', cpc_class_symbol) > 0

但是如果我想使用另一个 charindex 函数,查询只是 超时;

and charindex ('Y02E', cpc_class_symbol) > 0 or charindex ('Y02C', cpc_class_symbol) >0

我绝对是 SQL 菜鸟,但非常感谢您的帮助!


二.以逗号分隔列出一个单元格中列的值

基本上我想应用我发现的 "string_agg" 命令,但是,它不适用于此数据库。我有一个具有唯一 ID 的条目,其中有多个专利类别。例如:

appln_nr_epodoc | cpc_class_symbol

EP20110185794 | Y02E  10/125

EP20110185794 | Y02E  10/127

我想要这样,但是:

appln_nr_epodoc | cpc_class_symbol

EP20110185794 | Y02E  10/125, Y02E  10/127

同样,我是 sql 的新手,所以非常感谢您的帮助!谢谢!

为了透明起见,我还将在此处附上完整代码

SELECT  a.appln_nr_epodoc, a.appln_nr_original, psn_name, person_ctry_code, person_name, person_address, appln_auth+appln_nr,  
appln_filing_date, cpc_class_symbol
FROM
tls201_appln a
join tls207_pers_appln b on a.appln_id = b.appln_id
join tls206_person c on b.person_id = c.person_id
join tls801_country on c.person_ctry_code= tls801_country.ctry_code
join tls224_appln_cpc on a.appln_id = tls224_appln_cpc.appln_id
WHERE appln_auth = 'EP'
and appln_filing_year between 2005 and 2012
and eu_member = 'Y'
and granted = 'Y'
and psn_sector = 'company'
and charindex ('Y02E', cpc_class_symbol) > 0

对于您的第 2 部分,这是我创建的示例数据 这是代码。它给了我你要求的输出。

create table #test_1 (
            appln_nr_epodoc varchar(20) null
            ,cpc_class_symbol varchar(20) null
            )

            insert into #test_1 values
            ('EP20110185794','Y02E  10/125')
            ,('EP20110185794','Y02E  10/127')
            ,('EP20110185795','Y02E  10/130')
            ,('EP20110185796','Y02E  20/140')
            ,('EP20110185796','Y02E  21/142')



            with CTE_1 as (select *
            from (
            select * 
            ,R1_1 =  Rank() over(partition by appln_nr_epodoc order by cpc_class_symbol )
            from #test_1
            ) as a
            where R1_1 = 1
            )

            ,CTE_2 as (select *
            from (
            select * 
            ,R1_1 =  Rank() over(partition by appln_nr_epodoc order by cpc_class_symbol )
            from #test_1
            ) as a
            where R1_1 = 2 ) 

            select a.appln_nr_epodoc
            ,a.cpc_class_symbol+','+c.cpc_class_symbol
            from CTE_1 a
            join CTE_2 c on c.appln_nr_epodoc = a.appln_nr_epodoc

输出