TSQL - 将 Sales_Rep 行分组到列并将 DESCRIP 列显示为行 无聚合

Posted

技术标签:

【中文标题】TSQL - 将 Sales_Rep 行分组到列并将 DESCRIP 列显示为行 无聚合【英文标题】:TSQL - Group Sales_Rep rows to columns and display DESCRIP column as rows No aggregate 【发布时间】:2020-09-22 17:55:05 【问题描述】:

我有以下查询。我需要将销售代表列为列,并在相应的销售代表下的 DESCRIP 中列出信息。 Pivot 不起作用,因为我没有汇总任何内容。还没有找到与我有关的内容,因此我正在寻求您的帮助。感谢您的宝贵时间。

SALES_REP   DESCRIP
AG  (AXI01) EGCG EXTENDED RELEASE CAPSULES (RD)
AG  (DLI01) NWE-01 ESSENTIAL DIGESTIVES ()
AG  (KID01) KIDNEYHOOD S3 (UT)
AG  (AXI01) AB-4166 DISSOLUTION TESTING (RD)
AG  (AXI01) MCC SPHERES COATED W BLACK IRON OXIDE (RD)
AG  (ELY01) MATTER REFORMULATION (RON/A)
BD  (SMA01) VEGETARIAN PROTEIN BLEND W/GREENS  (RD)
BD  (MIT01) ARGIINE AAKG GARN AND TABLETS (ANGEL)
BD  (DLI01) BISMUTH/FURAZOLIDONE/CLARITHROMYCIN COMBINATION DRUG PRODUCT_UTECH GUM (NL)
BD  (00390) COAT 8 KILOS OF TABLET CORES WITH ABOUT 1 % COATING THAT HAS ADDED  SAFETRACES DNA MARKER   ()
BD  (EEX02) GRANULATE SOY PROTEIN WITH ACACIA  ()
BD  (LIQ01) GRANULATE SAMPLES WITH WATER ONLY , SPRAY DRIED LEMON, ORANGE , GRAPEFUIT POWDERS. BASE ALREADY CONTAINS 25-30% ACACIA GUM ()
BD  (BRA01) FOR QUOTING PURPOSES , DEVELOP FORMULA FOR DEXBROMPHENIRAMINE MALEATE 2 MG AND PHENAZOPYRIDE HCL (AZO) 100 & 200 MG TABS ()
CB  (NUS04) JUNGAMALS JAPAN 2021 25 BOTTLE R&D PILOT FOR STABILITY TESTS (ANGEL)
CB  (NUS01) US DIGESTIVE 90 COUNT VERSION OF NUS292C (ANGEL)
CB  (LIM01) QUICK DISSOLVE TABLET - RTA SHOT (BERRY) (UT)
CB  (LIM01) QUICK DISSOLVE TABLET - THRIVE-HGH (TROPICAL) (UT)
CB  (AMB01) AMBRA/LOVIDIA XR FORMULA WITH GYMNEMA SLYVESTRE 465 MG (RD)
CB  (SHA02) RITESTART KIDS & TEENS REFORMULATION (UT)
CB  (BAS01) SEROVITAL B CAPSULE FORMULA OPTIONS - YELLOW PWDR IN CLEAR & "00" WHITE CASPULE SAMPLES (USHA)
CB  (NUS01) TR90 FIT US PLACEBO 20 KG RUN 60 CNT APPROX 600-700 UNLABELED BOTTLES (ANGEL)
MG  (AME02) POWDER BLENDING (ANGEL)
MG  (ZER01) CHRONDOITIN SULFATE COATING X2 (AG)
MG  (ELU01) TOBRAMYCIN GRANULES (NL)
MG  (BRI01) BRILLIANT HEALTH TRANSMIT GRAN AND TABLET (UT)
RND (DLI01) EFFERVESCENT PAIN PROCESS VALIDATION (RD)
RND (DLI01) GRAN D MAGNET QUALIFICATION (RD)
RND (DLI01) BLENDER CAPACITY STUDY (RD)
RND (DLI01) COATING PAN A&B QUALIFICATION (RD)
RND (WYE06) SODD-006 STABILITY EVALUATION (NL)
RND (WYE06) CAL160 PROCESS VALIDATION (RD)
SH  (LIV02) FORMULATION OF HYDRATION DRINK MIX (USHA)
SH  (KOB01) ASPIRIN CAFFEINE POWDER (USHA)
SH  (COP01) APPLE CIDER VINEGAR EFFERVESCENT TABLETS (USHA)
SH  (COP01) SURVIVAL TABLETS 720CT (NATE)
SH  (USA01) GRANULATE 10 KILO POWDER BLEND - USANA (USHA)
TV  (LIF01) PETANDIM LIF-07T AND LIF-13T NEW OMEGA (ANGEL)
TV  (LIF01) L-CARNITINE ISSUE NRF1 JP (ANGEL)
TV  (SHA01) SOY-02 REPLACEMENT IN SHK-06 AND SHK-22/25 PRODUCTS (AG)
TV  (ELY01) PROBIOTIC CAPSULES ()
TV  (VYM01) VYMUNE CHEWABLE TABLETS (UT)
TV  (QUL01) IMMUNITY BOOSTER (UT)
TV  (DOT01) DOT-06 UPDATED FORMULA WITH NEW OIL, STEVIA @ 95% NEED 2 OPTIONS ()
TV  (DOT01) MITO2MAX ()
TV  (DOT01) TERRAZYME ()
TV  (LIF02) GRANULATE 2 PREBLENDED POWDER PRODUCTS , PARTYAID AND FOCUSAID . USE WATER ONLY ()
TV  (PAR01) CALCIUM CITRATE WITH PROBIOTIC STICKPACK ()
SELECT        SALES_REP, '(' + CUSTOMER_ID + ') ' + PROJECT_DESCRIPTION + ' (' + IsNull(RD_REP,'') + ')' as DESCRIP
FROM            DLI_RND_PROJECTS
WHERE        (STATUS = N'OPEN')
ORDER BY SALES_REP

像这样的

AG           BD            CB        ETC
-------------------------------------------------
AXI01...      SMA01...      NUS04...
DLI01...      MIT01...      NUS01...
KID01...      DLI01...      LIM01...

【问题讨论】:

【参考方案1】:

嗯。 . .我想你想要:

select max(case when sales_rep = 'AG' then col end) as ag,
       max(case when sales_rep = 'BD' then col end) as bd,
       . . . 
from (select t.*, row_number() over (partition by sales_rep, col order by col) as seqnum
      from t cross apply
           (values (substring(descr, 2, 5))) v(col)
     ) t
group by seqnum;

【讨论】:

如果销售代表的数量发生变化怎么办?更多的是一种动态方式,因此他们不必每次都更改查询?感谢您的回复。 @Mark 。 . .如果您不知道销售代表列表先验,则必须动态构建查询。

以上是关于TSQL - 将 Sales_Rep 行分组到列并将 DESCRIP 列显示为行 无聚合的主要内容,如果未能解决你的问题,请参考以下文章

如何将mysql内部连接结果从行分组到列

Tsql 将行转置为列,按列分组

翻转具有动态列并修复行的表 sql 服务器

根据多个条件将一列拆分为几列并分组

将 3 列分组为 1 列并生成计数 SQL

TSQL 分组集(Grouping Sets)