SQL数据透视表困境
我一直在写透视表。我有点问题,因为我的一般查询需要是半复杂的。我们想包括一组刚刚旋转的柱SQL数据透视表困境,sql,join,pivot,pivot-table,Sql,Join,Pivot,Pivot Table,我一直在写透视表。我有点问题,因为我的一般查询需要是半复杂的。我们想包括一组刚刚旋转的柱 select distinct a.customer_no, a.fname, isnull(a.mname,'') as 'mname', a.lname, d.description, c.address from T_CUSTOMER a left outer join T_EADDRESS c on a.customer_no = c.customer_no join
select distinct
a.customer_no,
a.fname,
isnull(a.mname,'') as 'mname',
a.lname,
d.description,
c.address
from T_CUSTOMER a
left outer join T_EADDRESS c on a.customer_no = c.customer_no
join TR_EADDRESS_TYPE d on c.eaddress_type = d.id
left outer join lt_nyo_applicants_cust_screen i on a.customer_no = i.customer_no
where a.customer_no in (
Select Distinct a.customer_no
From V_CUSTOMER_WITH_PRIMARY_GROUP a WITH (NOLOCK)
Where IsNull(a.inactive, 1) = 1
AND EXISTS (select * from tx_cust_keyword WITH (NOLOCK) where tx_cust_keyword.customer_no in (select customer_no from V_CUSTOMER_WITH_PRIMARY_GROUP where customer_no = a.customer_no) and tx_cust_keyword.key_value in ('Participant') And tx_cust_keyword.keyword_no = 651)
UNION
Select Distinct a.customer_no
From V_CUSTOMER_WITH_PRIMARY_GROUP a WITH (NOLOCK)
JOIN (Select a1.customer_no From lt_nyo_applicants_cust_screen a1 WITH (NOLOCK) Where a1.application_status in (1) and a1.season in ('2014-2015','2015-2016')) as e ON e.customer_no = a.customer_no
Where IsNull(a.inactive, 1) = 1
)
and c.inactive = 'N'
order by customer_no
数据如下:
客户号
fnmame
mname
名字
然后,我们有一个描述字段email_type,我们需要将其作为轴心
和电子邮件地址
customer_no fname mname lname description address
5 john Smith Primary Email Address js@gmail.com
5 john Smith Secondary Email Address js@hotmail.com
8 Joseph Petty Primary Email Address jp2@gmail.com
我想看什么
customer_no fname mname lname Primary Email Address Secondary Email Address
5 john Smith js@gmail.com js@hotmail.com
8 Joseph Petty jp2@gmail.com
像这样的
:with cte
as
(
select distinct
a.customer_no,
a.fname,
isnull(a.mname,'') as 'mname',
a.lname,
d.description,
...
)
select Customer_no,fname,mname,lname
max(case when description = 'Primary Email Address' then address END) as [Primary Email Address],
max(case when description = 'Secondary Email Address' then address END) as [Secondary Email Address]
from CTE
Group by Customer_no,fname,mname,lname
如果我的第一个问题不清楚,我很抱歉-问题是我有大约6个但最多10种类型的电子邮件地址,我想全部提取。如何在不为每种类型键入case/max语句的情况下执行此操作?