我需要一个SQL语句,它将派生一个唯一的行列表。
鉴于这组数据,我希望结果删除重复行,其中复制定义为Column1相同且第2列和第3列为空
for(int m=0; m<3;m++){
firstThree[m]=one[i][m];
nextThree[m]=one[i+5][m];
}
预期结果:
INSERT INTO @xmlDataTable(Tag, Parent, [Customer!1!CustomerId],
[CustomerIdentifier!2!CustomerIdentifierId], [OrderDetail!3!OrderDetailId])
SELECT rows.*
FROM Customer.CustomerIdentifier custIdent
JOIN Customer.Customer cust
ON cust.CurrentCustomerIdentifierId = custIdent.CustomerIdentifierId
JOIN [order].OrderDetail detail
ON detail.CustomerIdentifierId = custIdent.CustomerIdentifierId
WHERE detail.CustomerIdentifierId = @customerIdentifierId
OUTER APPLY (
SELECT 1, NULL, cust.CustomerId, NULL, null
UNION ALL
SELECT 2, 1, NULL, custIdent.CustomerIdentifierId, null
UNION ALL
SELECT 3, 1, NULL, null, detail.OrderDetailId
) rows
请注意,a,z,4在结果中出现两次,这是正确的。当Column1相同且Column2和Column3为空时,只会合并行。
如何构建查询以获得此结果?
答案 0 :(得分:1)
当它为非null时使用唯一(因此不可分组)的id,并且当它为null时使用group-able null应该有效。
select Column1, Column2, Column3
from Things
group by case when Column1 is null then null else ID end,
case when Column2 is null then null else ID end,
case when Column3 is null then null else ID end
答案 1 :(得分:0)
如果空列包含空值,那么这个应该在mysql中工作。查询的第一部分将返回所有&#39;额外的&#39;行,但只有那些列不为null的额外内容(这是值比较在mysql中的方式,至少)。第二个(不同的)查询返回所有内容之一,包括空值。最终结果正是您所要求的:
select a.Column1, a.Column2, a.Column3
from Things a, Things b
where a.Column1 = b.Column1
and a.Column2 = b.Column2
and a.Column3 = b.Column3
and a.ID < b.ID
union all
select distinct Column1, Column2, Column3 from Things;
答案 2 :(得分:0)
SELECT
DISTINCT
Column1
, Column2
, Column3
FROM MyTable
应该这样做:
SELECT
Column1
, Column2
, Column3
FROM MyTable
GROUP BY 1, 2, 3
答案 3 :(得分:0)
你可以做工会
select column1,column2,column3 from Things
union
select column1,column2,column3 from Things
where col2 = '' and col3 = '' ;