我的输出并不像我预期的那样,我想要的是消除重复数据。我之前尝试使用group by
和distinct
,但它不起作用。=(
我的数据库中的数据
who are u? A
who are u? B
who are u? C
who am i? A
who am i? B
who am i? C
当前输出
列'ExerciseTable.r1'在选择列表中无效,因为它不包含在聚合函数或GROUP BY子句中。
预期输出
who are u? A
B
C
Who am i? A
B
C
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [Question],[r1] FROM [ExerciseTable] GROUP BY [Question]">
</asp:SqlDataSource>
答案 0 :(得分:1)
在您的SQL语句中(c1第一列,第二列c2):
Select
case when t.[RN]>1 and t.[RN]<>4 then '' else t.c1 end [c1],
t.c2 from
(
Select ROW_NUMBER() OVER (ORDER BY c1) AS [RN],c1,c2 from t1
) as t
没有固定的RowNumber(t。[RN]):
Select
case
when
t.[RN]=(
Select min([RN]) from
(
Select ROW_NUMBER() OVER (ORDER BY c1) AS [RN],c1,c2 from t1
) as t where c1='who am i?'
)
or
t.[RN]=(
Select min([RN]) from
(
Select ROW_NUMBER() OVER (ORDER BY c1) AS [RN],c1,c2 from t1
) as t where c1='who are u?'
) then t.c1 else '' end [c1],
t.c2 from
(
Select ROW_NUMBER() OVER (ORDER BY c1) AS [RN],c1,c2 from t1
) as t
或者你可以使用C#/ VB.Net迭代结果集(例如:一个List)并在第一个拥有'我是谁?'之后删除列中的数据。对于“谁是你?”而言也一样。