1

我有一个表格,每列都有一些图像。

Acd_unq_id  Emp_unq_id  Acd_BImg1   Acd_BImg2  Acd_RImg1  Acd_RImp2  Acd_RImg3  Acd_Active

1          1745     BinaryImg   BinaryImg     NULL      NULL       NULL         1
2          1745        NULL        NULL    BinaryImg    NULL       NULL         1
3          1745        NULL        NULL       NULL      BinaryImg  BinaryImg    1
4          1769     BinaryImg   BinaryImg     NULL      NULL       BinaryImg    1

在上面的示例中,我想为每个唯一的 Emp ID 获取单个列中的数据。所以根据上面给出的例子,我希望结果如下 -

Acd_unq_id  Emp_unq_id  Acd_BImg1   Acd_BImg2  Acd_RImg1  Acd_RImp2  Acd_RImg3  Acd_Active

1          1745     BinaryImg   BinaryImg  BinaryImg  BinaryImg  BinaryImg     1
4          1769     BinaryImg   BinaryImg     NULL      NULL     BinaryImg     1

我为此写了一个查询-

SELECT
(
SELECT TOP 1 ACH_BILL_IMG1 FROM ADVERT_CAMPAIGN_HEADERS INNER JOIN ADVERT_CAMPAIGN_HEADERS
ON ACH_UNQ_ID = ACD_ACH_UNQ_ID WHERE ACD_SOC_UNQ_ID = 1745 AND ACD_IMAGETYPE IN ('H, Q')) as bill_img1

(SELECT TOP 1 ACH_BILL_IMG2 FROM ADVERT_CAMPAIGN_HEADERS INNER JOIN ADVERT_CAMPAIGN_HEADERS
ON ACH_UNQ_ID = ACD_ACH_UNQ_ID WHERE ACD_SOC_UNQ_ID = 1745 AND ACD_IMAGETYPE IN ('H, Q'))

我想使用子查询和前 1 来获得结果。但是在上面我写的示例查询中,我遇到了语法错误....

* FROM 子句中的对象“ADVERT_CAMPAIGN_HEADERS”和“ADVERT_CAMPAIGN_HEADERS”具有相同的公开名称。使用相关名称来区分它们。*

请帮忙。等待您的回复,专家:)

4

3 回答 3

1

怎么样:

SELECT [Acd_unq_id] = MIN([Acd_unq_id])
, [Emp_unq_id]
, [Acd_BImg1] = MAX([Acd_BImg1])
, [Acd_BImg2] = MAX([Acd_BImg2])
, [Acd_RImg1] = MAX([Acd_RImg1])
, [Acd_RImp2] = MAX([Acd_RImp2])
, [Acd_RImg3] = MAX([Acd_RImg3])
, [Acd_Active] = MAX([Acd_Active])
FROM ADVERT_CAMPAIGN_HEADERS
GROUP BY [Emp_unq_id]
于 2013-02-16T16:24:13.953 回答
0

尝试这个

`Select xx.Emp_unq_id,Acd_BImg1,Acd_BImg2,Acd_RImg1,Acd_RImg2,Acd_RImg3 from
((select  Emp_unq_id,Acd_BImg1 from yourtab where Emp_unq_id =1745
and Acd_BImg1 is not null)A 
inner join
select  Emp_unq_id,Acd_BImg2 from yourtab where Emp_unq_id =1745
and Acd_BImg2 is not null) B 
on A.Emp_unq_id=B.Emp_unq_id
inner join
(select  Emp_unq_id,Acd_RImg1 from yourtab where Emp_unq_id =1745
and Acd_RImg1 is not null) C
on B.Emp_unq_id=C.Emp_unq_id
inner join
(select  Emp_unq_id,Acd_RImg2 from yourtab where Emp_unq_id =1745
and Acd_RImg2 is not null) D
on C.Emp_unq_id=D.Emp_unq_id
inner join (select  Emp_unq_id,Acd_RImg3 from yourtab where Emp_unq_id =1745
and Acd_RImg3 is not null) E
on D.Emp_unq_id=E.Emp_unq_id)XX`

对其他员工 ID 重复此查询...

于 2013-02-16T13:36:57.913 回答
0

根据您的查询,我假设图像列中只有一个非空值。请注意,它可能会产生理想的结果,但从性能的角度来看我不知道。我也期待对此的其他替代查询。

;WITH tempCompaignHeaders(ACD_UNQ_ID,Acd_Active) AS
(
    SELECT DISTINCT Emp_unq_id, Acd_Active FROM ADVERT_CAMPAIGN_HEADERS
)
SELECT
       (SELECT TOP 1 ACD_UNQ_ID FROM ADVERT_CAMPAIGN_HEADERS WHERE Emp_unq_id = t.Emp_unq_id ORDER BY ACD_UNQ_ID) Acd_BImg1,
       Emp_unq_id
       (SELECT TOP 1 Acd_BImg1 FROM ADVERT_CAMPAIGN_HEADERS WHERE Emp_unq_id = t.Emp_unq_id AND Acd_BImg1 IS NOT NULL) Acd_BImg1,
       (SELECT TOP 1 Acd_BImg2 FROM ADVERT_CAMPAIGN_HEADERS WHERE Emp_unq_id = t.Emp_unq_id AND Acd_BImg2 IS NOT NULL) Acd_BImg2,
       (SELECT TOP 1 Acd_RImg1 FROM ADVERT_CAMPAIGN_HEADERS WHERE Emp_unq_id = t.Emp_unq_id AND Acd_RImg1 IS NOT NULL) Acd_RImg1,
       (SELECT TOP 1 Acd_RImg2 FROM ADVERT_CAMPAIGN_HEADERS WHERE Emp_unq_id = t.Emp_unq_id AND Acd_RImg2 IS NOT NULL) Acd_RImg2,
       (SELECT TOP 1 Acd_RImg3 FROM ADVERT_CAMPAIGN_HEADERS WHERE Emp_unq_id = t.Emp_unq_id AND Acd_RImg3 IS NOT NULL) Acd_RImg3,
       Acd_Active
FROM
       tempCompaignHeaders t
于 2013-02-16T14:02:04.730 回答