4

私は厄介なデータセットを持っています:

+----+------------------------------------------------------------------------------+
| PK |                                 Medications                                  |
+----+------------------------------------------------------------------------------+
|  1 | NAPROXEN, neurontin, DOCUSATE, HYDROCODONE, BACLOFEN, advil                  |
|  2 | celexa, lortab, lyrica, ambien, xanax                                        |
|  3 | adipex                                                                       |
|  4 | opana, roxicodone                                                            |
|  5 | adderall                                                                     |
|  6 | hydrocodone/apap                                                             |
|  7 | NEXIUM, METOPROLOL, lipitor, VERAPAMIL, ASPIRIN, WARFARIN, ambien            |
|  8 | prozac                                                                       |
|  9 | flexeril                                                                     |
| 10 | soma, LITHIUM, MULTI-VITAMIN, fentanyl patch, percocet, PROPANOLOL, tegretol |
+----+------------------------------------------------------------------------------+

これは 2 つの列だけであることに注意してください。

私が返したいのはmedications、データセット全体で異なる 1 列のリストです。

NAPROXEN
 neurontin
 DOCUSATE
 HYDROCODONE
 BACLOFEN
 advil
celexa
 lortab
 lyrica
 ambien
 xanax
adipex
opana

これについて最善の方法は何ですか?

ご指導ありがとうございました。

4

3 に答える 3

4
DECLARE @Medications TABLE
(
     PK BIGINT PRIMARY KEY IDENTITY(1,1)
    ,Medications NVARCHAR(4000)
)

/* Populate the table with example records*/
INSERT INTO @Medications(Medications) 
VALUES ('NAPROXEN, neurontin, DOCUSATE, HYDROCODONE, BACLOFEN, advil')
      ,('celexa, lortab, lyrica, ambien, xanax   ')
      ,('adipex')
      ,('opana roxicodone')
      ,('adderall')
      ,('hydrocodone/apap')
      ,('NEXIUM,METOPROLOL,lipitor,VERAPAMIL,ASPIRIN,WARFARIN,ambien')
      ,('prozac')
      ,('flexeril')
      ,('soma,LITHIUM,MULTI-VITAMIN,fentanyl patch,percocet,PROPANOLOL,tegretol');

オプション1

select distinct Medication = ltrim(rtrim(O.splitdata ))
from
(
select *,
cast('<X>'+replace(F.Medications,',','</X><X>')+'</X>' as XML) as xmlfilter
from @Medications F
)F1
cross apply
( 
 select fdata.D.value('.','varchar(50)') as splitdata 
 from f1.xmlfilter.nodes('X') as fdata(D)) O
where O.splitdata <> ''
group by O.splitdata

オプション 2

SELECT DISTINCT display_term
FROM @Medications
CROSS APPLY sys.dm_fts_parser('"' + Medications + '"', 1033, 0,0)
where display_term NOT LIKE 'nn%'

オプション 3

SELECT  DISTINCT
    LTRIM(RTRIM(SUBSTRING(Medications, Number ,CHARINDEX(',', Medications + ',', Number ) - Number))) AS Medication 
FROM @Medications 
JOIN master..spt_values ON Number <= DATALENGTH(Medications) + 1  AND type='P'
AND SUBSTRING(',' + Medications, Number , 1) = ','  
于 2012-11-30T08:25:49.417 に答える
3

これはあなたが探している解決策かもしれません:

;WITH SourceDate(PK,MedicationsXML) AS
(
    SELECT PK
          ,CAST(N'<r><![CDATA[' + REPLACE(Medications, ',', ']]></r><r><![CDATA[') + ']]></r>' AS XML)
    FROM @Medications
)
SELECT DISTINCT Medicament
FROM SourceDate
CROSS APPLY (SELECT DISTINCT RTRIM(LTRIM(Tbl.Col.value('.', 'nvarchar(250)'))) AS Medicament FROM MedicationsXML.nodes('//r') Tbl(Col)) AS List

そして、これは完全に機能する例です (重複したレコードを追加して、個別の値のみを返すことを示します。

SET NOCOUNT ON
GO

    /* Declare table that holds the data*/
    DECLARE @Medications TABLE
    (
         PK BIGINT PRIMARY KEY IDENTITY(1,1)
        ,Medications NVARCHAR(4000)
    )

    /* Populate the table with exmaple records*/
    INSERT INTO @Medications(Medications)
    VALUES ('NAPROXEN, neurontin, DOCUSATE, HYDROCODONE, BACLOFEN, advil')
          ,('celexa, lortab, lyrica, ambien, xanax   ')
          ,('adipex')
          ,('opana, roxicodone')
          ,('adderall')
          ,('hydrocodone/apap')
          ,('NEXIUM, METOPROLOL, lipitor, VERAPAMIL, ASPIRIN, WARFARIN, ambien')
          ,('prozac')
          ,('flexeril')
          ,('soma, LITHIUM, MULTI-VITAMIN, fentanyl patch, percocet, PROPANOLOL, tegretol')
          ,('NAPROXEN, neurontin, DOCUSATE, HYDROCODONE, BACLOFEN, advil')
          ,('celexa, lortab, lyrica, ambien, xanax   ')
          ,('adipex')
          ,('opana, roxicodone')
          ,('adderall')
          ,('hydrocodone/apap')
          ,('NEXIUM, METOPROLOL, lipitor, VERAPAMIL, ASPIRIN, WARFARIN, ambien')
          ,('prozac')
          ,('flexeril')
          ,('soma, LITHIUM, MULTI-VITAMIN, fentanyl patch, percocet, PROPANOLOL, tegretol')

    ;WITH SourceDate(PK,MedicationsXML) AS
    (
        SELECT PK
              ,CAST(N'<r><![CDATA[' + REPLACE(Medications, ',', ']]></r><r><![CDATA[') + ']]></r>' AS XML)
        FROM @Medications
    )
    SELECT DISTINCT Medicament
    FROM SourceDate
    CROSS APPLY (SELECT DISTINCT RTRIM(LTRIM(Tbl.Col.value('.', 'nvarchar(250)'))) AS Medicament FROM MedicationsXML.nodes('//r') Tbl(Col)) AS List


SET NOCOUNT OFF
GO

ご不明な点がございましたら、お気軽にお問い合わせください。

于 2012-11-30T07:55:18.817 に答える
2

awkに出力できれば

outputcommand | awk 'BEGIN{FS="|"}{print $3}'|awk 'BEGIN{RS=","}{print $0}'

出力コマンドに使用できる単純なテキスト ファイルの場合cat filename、または:

awk 'BEGIN{FS="|"}{print $3}' some_filename |awk 'BEGIN{RS=","}{print $0}'
于 2012-11-30T00:25:04.400 に答える