为什么distinct()在我的sqldatasource中不起作用?

时间:2015-06-22 10:38:32

标签: sql asp.net sql-server

enter image description here enter image description here enter image description here enter image description here我想向教师显示时间表,即他们将使用相同日期分配给他们的不同批次,但我想在单个网格中显示view.I尝试过不同但它没有工作。他们将有不同批次的第一小时和第二小时,我想在单列中显示主题和批次,但它在单个网格视图中显示在两列中。请帮助我解决这些问题

<label>From Date</label>
<asp:TextBox ID="fromdate" runat="server" TextMode="Date"></asp:TextBox>
<label>To Date</label>
<asp:TextBox ID="todate" runat="server" TextMode="Date"></asp:TextBox>
<asp:Button ID="viewid" runat="server" OnClick="viewid_Click" Text="View" CssClass="button2"/>
<asp:GridView ID="Viewsubjects" runat="server" AutoGenerateColumns="False" DataSourceID="batch1" CssClass="mGrid1">
    <Columns>
        <asp:BoundField DataField="datedif" HeaderText="datedif" SortExpression="datedif" />
        <asp:TemplateField HeaderText="Hour1" SortExpression="subject1">
           <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Visible='<%# Eval("subject1")!=DBNull.Value ? true:false %>' Text='<%#"Subject:"+Eval("subject1")%>'></asp:Label>
                <br />
                <asp:Label ID="label6" runat="server" Visible='<%# (Eval("batch")!=DBNull.Value && Eval("subject1")!=DBNull.Value) ? true:false %>' Text='<%#"Batch:"+Eval("batch")%>'></asp:Label>
        </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Hour2" SortExpression="subject2">
            <ItemTemplate>
                <asp:Label ID="Label2" runat="server" Visible='<%# Eval("subject2")!=DBNull.Value ? true:false %>' Text='<%#"Subject:"+Eval("subject2")%>'></asp:Label>
                <br />
                <asp:Label ID="label7" runat="server" Visible='<%# (Eval("batch")!=DBNull.Value && Eval("subject2")!=DBNull.Value) ? true:false %>' Text='<%#"Batch:"+Eval("batch")%>'></asp:Label>
        </ItemTemplate>
            </asp:TemplateField>
         <asp:TemplateField HeaderText="Hour3" SortExpression="subject3">
            <ItemTemplate>
                <asp:Label ID="Label3" runat="server" Visible='<%# Eval("subject3")!=DBNull.Value ? true:false %>' Text='<%#"Subject:"+Eval("subject3")%>'></asp:Label>
                <br />
                <asp:Label ID="label8" runat="server" Visible='<%# (Eval("batch")!=DBNull.Value && Eval("subject3")!=DBNull.Value) ? true:false %>' Text='<%#"Batch:"+Eval("batch")%>'></asp:Label>
        </ItemTemplate>
            </asp:TemplateField>
        <asp:TemplateField HeaderText="Hour4" SortExpression="subject4">
            <ItemTemplate>
                <asp:Label ID="Label4" runat="server" Visible='<%# Eval("subject4")!=DBNull.Value ? true:false %>' Text='<%#"Subject:"+Eval("subject4")%>'></asp:Label>
                <br />
                <asp:Label ID="label9" runat="server" Visible='<%# (Eval("batch")!=DBNull.Value && Eval("subject4")!=DBNull.Value) ? true:false %>' Text='<%#"Batch:"+Eval("batch")%>'></asp:Label>
        </ItemTemplate>
            </asp:TemplateField>
         <asp:TemplateField HeaderText="Hour5" SortExpression="subject5">
           <ItemTemplate>
                <asp:Label ID="Label5" runat="server" Visible='<%# Eval("subject5")!=DBNull.Value ? true:false %>' Text='<%#"Subject:"+Eval("subject5")%>'></asp:Label>
                <br />
                <asp:Label ID="label10" runat="server" Visible='<%# (Eval("batch")!=DBNull.Value && Eval("subject5")!=DBNull.Value) ? true:false %>' Text='<%#"Batch:"+Eval("batch")%>'></asp:Label>
        </ItemTemplate>
            </asp:TemplateField>
    </Columns>
</asp:GridView>
<asp:SqlDataSource ID="batch1" runat="server" 
     ConnectionString="<%$ ConnectionStrings:Database1ConnectionString1 %>" 
     SelectCommand="SELECT DISTINCT [datedif], [subject1], [subject2], [subject3], [subject4], [subject5],[batch] FROM [test] WHERE ([datedif] &gt;= @datedif)">
     <SelectParameters>
        <asp:ControlParameter ControlID="fromdate" Name="datedif" PropertyName="Text" Type="String" />
     </SelectParameters>
</asp:SqlDataSource>

这是获取输出的方法

enter image description here

2 个答案:

答案 0 :(得分:2)

您获得的内容实际上是截然不同的,因为每一行都有不同的值。您正在寻找的是&#34;&#34;组,它将按日和教师对您的价值进行分组。我的解决方案基于替换您当前的解决方案:

date       hour1   subject1     hour2   subject2    hour3   subject3    faculty
2015-06-25  1      subject 1    0       (null)      0       (null)      ms. hart
2015-06-25  0      (null)       1       subject 2   0       (null)      ms. hart
2015-06-25  0      (null)       0       (null)      1       subject 3   ms. hart

使用:

date        hour1       hour2       hour3       faculty
|----------|----------|-----------|-----------|----------|
2015-06-25  subject 1   subject 3   subject 3   ms. hart

如果这是假设是正确的,那么这里是我使用的查询(我创建了自己的表,因为你没有提供DDL但是如果你有问题请告诉我)

select date,max(subject1) hour1, max(subject3) hour2, max(subject3) hour3, faculty from timetable
group by date, faculty

这是你试验的sql小提琴:)

http://sqlfiddle.com/#!6/c1850/3

答案 1 :(得分:2)

尝试下面给出的这些查询 -

create table timetable1(date date,hour1 int,subject1 varchar(255),batch1 varchar(20),hour2 int,subject2 varchar(255),batch2 varchar(20),hour3 int,subject3 varchar(255),batch3 varchar(20),faculty varchar(255));

insert into timetable1 values(cast(getdate() as date), 1, 'subject1','B1',0, 's2','b2',3,'s3','b3','ms. hart');
insert into timetable1 values(cast(getdate() as date), 1, 'subject12','B12',0, 's12','b12',3,'s13','b13','ms. hart11');
insert into timetable1 values(cast(getdate() as date), 1, 'subject13','B122',0, 's222','b222',3,'s23','b23','ms. hart22');  

select date,(subject1+' '+batch1) as hour1, (subject2+' '+batch2) as hour2,(subject3+' '+batch3) as hour3,faculty from timetable1group by date, faculty,subject1,subject2,subject3,batch1,batch2,batch3