2012-07-24 73 views
0

我的问题:我想不重复的记录,在同一张表和多个表中?我如何继续在SQL中执行此操作?如何过滤sql重复?

让我解释一下我曾尝试:

Select distinct Col1, col2 
from Table 
where order id = 143 

输出

VolumeAnswer1 AreaAnswer1 heightAnswer1 
VolumeAnswer2 AreaAnswer1 heightAnswer2 
VolumeAnswer3 AreaAnswer1 heightAnswer2 

期望输出
它显示了第二个表中重复,但我需要的输出如:

VolumeAnswer1 AreaAnswer1 heightAnswer1 
VolumeAnswer2     heightAnswer2 
VolumeAnswer3 

我需要多个表相同的情况下,我也找到了同样的重复。如果它不能在SQL Server中处理,我们如何在.Net中处理它?我使用了多个选择,但他们用来在单个选择中进行更改。每列应该在下拉列表绑定...

+0

欢迎的StackOverflow!你能解释一下你在多个表格中重复记录的含义吗(也许通过编辑你的问题)?你可以添加列名到你的输出/预期输出吗?谢谢:-) – Josien 2012-07-24 08:47:11

+0

完整的测试数据和sql语法的测试用例会很好。 – nurettin 2012-07-24 09:38:22

回答

1

像这样的东西可能是一个良好的开端:

;with cte1 as (
Select col1, cnt1 
From (
    Select 
    col1 
    ,row_number() over(Partition by col1 Order by col1) as cnt1 
    From tbltest) as tbl_sub1 
Where cnt1 = 1 
), cte2 as (
Select col2, cnt2 
From (
    Select 
    col2 
    ,row_number() over(Partition by col2 Order by col2) as cnt2 
    From tbltest) as tbl_sub2 
Where cnt2 = 1 
), cte3 as (
Select col3, cnt3 
From (
    Select 
    col3 
    ,row_number() over(Partition by col3 Order by col3) as cnt3 
    From tbltest) as tbl_sub3 
Where cnt3 = 1 
) 
Select 
col1, col2, col3 
From cte1 
full join cte2 on col1 = col2 
full join cte3 on col1 = col3 

的Sql小提琴显示例如:http://sqlfiddle.com/#!3/c9127/1

+0

非常感谢,你做了不起的工作..我爱你很多 – Thiyagarajan 2012-08-07 08:18:09