I have one page as remove duplicates and it's gets all duplicates values from 4 tables. now for grid view bind I use same preference for associated column name. but it'd gets only first query result.

here is my sql management studio result: enter image description here

and here is my code :

string str = "";
            DataTable dt = new DataTable();
            str += "(SELECT ";
            str += "s.Id, s.EmailAddress As ColumnValue,";
            str += "(SELECT Name FROM tbl_User_master ";
            str += "WHERE Id = s.user_id) AS CreatedBy,";
            str += "d.Count ";
            str += "FROM ";
            str += "(SELECT EmailAddress, COUNT(*) as Count ";
            str += "FROM tbl_Subscribers ";
            str += "WHERE user_id = '1' ";
            str += "GROUP BY EmailAddress ";
            str += "HAVING COUNT(*) > 1) AS d ";
            str += "INNER JOIN ";
            str += "tbl_Subscribers s ON s.EmailAddress = d.EmailAddress)";

            str += "(SELECT ";
            str += "f.Id,f.EmailAddress As ColumnValue,";
            str += "(SELECT Name FROM tbl_User_master WHERE Id = f.user_id) AS CreatedBy,";
            str += "d.Count ";
            str += "FROM ";
            str += "(SELECT EmailAddress, COUNT(*) AS Count ";
            str += "FROM tbl_From_master ";
            str += "WHERE user_id = '1' ";
            str += "GROUP BY EmailAddress ";
            str += "HAVING COUNT(*) > 1) AS d ";
            str += "INNER JOIN ";
            str += "tbl_From_master f ON f.EmailAddress = d.EmailAddress)";

            str += "(SELECT ";
            str += "c.Id,c.CategoryName As ColumnValue,(SELECT Name FROM tbl_User_master WHERE Id = c.user_id) As CreatedBy, d.Count ";
            str += "FROM (";
            str += "SELECT CategoryName, COUNT(*) as Count ";
            str += "FROM tbl_Categories WHERE user_id='1' ";
            str += "GROUP BY CategoryName ";
            str += "HAVING COUNT(*) > 1";
            str += ") AS d ";
            str += "INNER JOIN tbl_Categories c ON c.CategoryName = d.CategoryName)";

            str += "(SELECT ";
            str += "t.Id,t.CategoryName As ColumnValue,(SELECT Name FROM tbl_User_master WHERE Id = t.user_id) As CreatedBy, d.Count ";
            str += "FROM (";
            str += "SELECT CategoryName, COUNT(*) as Count ";
            str += "FROM tbl_Template_Categories WHERE user_id='1' ";
            str += "GROUP BY CategoryName ";
            str += "HAVING COUNT(*) > 1";
            str += ") AS d ";
            str += "INNER JOIN tbl_Template_Categories t ON t.CategoryName = d.CategoryName)";

            str += "(SELECT ";
            str += "t.Id,t.TemplateName As ColumnValue,(SELECT Name FROM tbl_User_master WHERE Id = t.user_id) As CreatedBy,d.Count ";
            str += "FROM (";
            str += "SELECT TemplateName, COUNT(*) as Count ";
            str += "FROM tbl_Template_master WHERE user_id='1' ";
            str += "GROUP BY TemplateName ";
            str += "HAVING COUNT(*) > 1";
            str += ") AS d";
            str += " INNER JOIN tbl_Template_master t ON t.TemplateName = d.TemplateName)";
            dt = obj.Get_Data_Table_From_Str(str);
            egrd.DataSource = dt;
            egrd.DataBind();
            ViewState["data"] = dt;

but still returns only first query result : enter image description here

here I include my grid view markup:

<asp:GridView ID="egrd" runat="server" AutoGenerateColumns="true" CssClass="table table-striped table-bordered table-hover"
                        Width="100%" PageSize="9999999" HeaderStyle-CssClass="theme-font" OnPreRender="egrd_PreRender">
                        <EmptyDataTemplate>
                            <asp:Label ID="lblEmptySearch" runat="server" Font-Bold="true" ForeColor="Red">No Data Found To Display . . .</asp:Label>
                        </EmptyDataTemplate>
                        <Columns>
                            </Columns>
                            <HeaderStyle CssClass="theme-font"></HeaderStyle>
                            <RowStyle Wrap="False" />
                        </asp:GridView>

how I achieve this task please help me...

-----------------------Updated---------------------------

ok I done with it :

if (groupByTable == 0)
            {
                str += "(SELECT ";
                str += "s.Id, s.EmailAddress As ColumnValue,";
                str += "(SELECT Name FROM tbl_User_master ";
                str += "WHERE Id = s.user_id) AS CreatedBy,";
                str += "s.SubscriptionDateTime As CreatedDate,d.Count ";
                str += "FROM ";
                str += "(SELECT EmailAddress, COUNT(*) as Count ";
                str += "FROM tbl_Subscribers ";
                str += "WHERE user_id = '" + int.Parse(CommonLogic.GetSessionValue("user_id").ToString()) + "' ";
                str += "GROUP BY EmailAddress ";
                str += "HAVING COUNT(*) > 1) AS d ";
                str += "INNER JOIN ";
                str += "tbl_Subscribers s ON s.EmailAddress = d.EmailAddress)";
                str += " UNION ALL ";
                str += "(SELECT ";
                str += "f.Id,f.EmailAddress As ColumnValue,";
                str += "(SELECT Name FROM tbl_User_master WHERE Id = f.user_id) AS CreatedBy,";
                str += "f.CreatedDate As CreatedDate,d.Count ";
                str += "FROM ";
                str += "(SELECT EmailAddress, COUNT(*) AS Count ";
                str += "FROM tbl_From_master ";
                str += "WHERE user_id = '" + int.Parse(CommonLogic.GetSessionValue("user_id").ToString()) + "' ";
                str += "GROUP BY EmailAddress ";
                str += "HAVING COUNT(*) > 1) AS d ";
                str += "INNER JOIN ";
                str += "tbl_From_master f ON f.EmailAddress = d.EmailAddress)";
                str += " UNION ALL ";
                str += "(SELECT ";
                str += "c.Id,c.CategoryName As ColumnValue,(SELECT Name FROM tbl_User_master WHERE Id = c.user_id) As CreatedBy,NULL As CreatedDate,d.Count ";
                str += "FROM (";
                str += "SELECT CategoryName, COUNT(*) as Count ";
                str += "FROM tbl_Categories WHERE user_id='" + int.Parse(CommonLogic.GetSessionValue("user_id").ToString()) + "' ";
                str += "GROUP BY CategoryName ";
                str += "HAVING COUNT(*) > 1";
                str += ") AS d ";
                str += "INNER JOIN tbl_Categories c ON c.CategoryName = d.CategoryName)";
                str += " UNION ALL ";
                str += "(SELECT ";
                str += "t.Id,t.CategoryName As ColumnValue,(SELECT Name FROM tbl_User_master WHERE Id = t.user_id) As CreatedBy,NULL As CreatedDate,d.Count ";
                str += "FROM (";
                str += "SELECT CategoryName, COUNT(*) as Count ";
                str += "FROM tbl_Template_Categories WHERE user_id='" + int.Parse(CommonLogic.GetSessionValue("user_id").ToString()) + "' ";
                str += "GROUP BY CategoryName ";
                str += "HAVING COUNT(*) > 1";
                str += ") AS d ";
                str += "INNER JOIN tbl_Template_Categories t ON t.CategoryName = d.CategoryName)";
                str += " UNION ALL ";
                str += "(SELECT ";
                str += "t.Id,t.TemplateName As ColumnValue,(SELECT Name FROM tbl_User_master WHERE Id = t.user_id) As CreatedBy,t.Created_date As CreatedDate,d.Count ";
                str += "FROM (";
                str += "SELECT TemplateName, COUNT(*) as Count ";
                str += "FROM tbl_Template_master WHERE user_id='" + int.Parse(CommonLogic.GetSessionValue("user_id").ToString()) + "' ";
                str += "GROUP BY TemplateName ";
                str += "HAVING COUNT(*) > 1";
                str += ") AS d";
                str += " INNER JOIN tbl_Template_master t ON t.TemplateName = d.TemplateName)";
            }

0 Answers