GROUP BY issue when SELECT to dataGridView from 2

2019-02-26 00:42发布

With little help of you I made this two QUERY. I posted pictures so you can see that in green squares are empty values which I would like to see and in red squares are values which I wouldnt like to display. Variable vyberradek inserts values for 'subkey' it is INT in DB but I pass it through string.

So I will simplify: Now I can display all or a little, would you help me how to display the correct records?

This first query does this:

enter image description here

string sQuery = string.Format("SELECT zajsluz.akce,zajsluz.text,klisluz.pocet,klisluz.subkey,zajsluz.ID FROM zajsluz LEFT JOIN klisluz ON zajsluz.ID=klisluz.IDzajsluz WHERE zajsluz.akce= '{0}'  GROUP BY klisluz.subkey,zajsluz.akce,zajsluz.text,klisluz.pocet,zajsluz.ID", sZakce);

Here is what does the second query: enter image description here

 string sQuery = string.Format("SELECT zajsluz.akce,zajsluz.text,klisluz.pocet,klisluz.subkey,zajsluz.ID FROM zajsluz LEFT JOIN klisluz ON zajsluz.ID=klisluz.IDzajsluz WHERE zajsluz.akce= '{0}' and klisluz.subkey ='" + vyberradek + "' GROUP BY klisluz.subkey,zajsluz.akce,zajsluz.text,klisluz.pocet,zajsluz.ID", sZakce);

So as you can see I would also like to display the empty ones. I think you might need also the code which checks the checkboxes here it is:

 for (int i = 0; i < dtg_ksluzby.Rows.Count; i++)
            {
                var row = dtg_ksluzby.Rows[i];
                int id = (int)row.Cells["ID"].Value;

                using (var novyprikaz3 = new SqlCommand("SELECT * from klisluz WHERE subkey='" + vyberradek + "'AND IDzajsluz=" + id, spojeni))
                {

                    spojeni.Open();
                    SqlDataReader precti3 = novyprikaz3.ExecuteReader();
                    if (precti3.HasRows)
                    {
                        row.Cells[5].Value = true;
                    }
                    spojeni.Close();
                }
            }

Would you guys please suggest me how should the query look like to select even the non checked value (which arent in table klisluz) and the checked ones?

As I see nobody is replying so well let me give you one simple example: When I create the client I check for example 3 from 10 checkboxes. So it selects 3 rows (and those it adds into table klisluz). And I have these two queries, First query shows the checked checkbox from other clients, but also the non-checked ones for this client. It simply shows everything what is in database, so it shows rows with checked things of other clients (but only those which are the same for this and some other client/s, it DOESNT CREATE duplicates). The second shows only checked for this client but without those which he havent choosen. So when I do edit of this client I would like to display what did he checked and the rest 7(the 7 possibilities what he haven't checked so I could check them). Now I can display only what did I checked (without the non-checked) or everything including other clients checks (which are the same for this client). I hope it is understandable, sorry for my weak english.

P.S. With check I mean checking checkbox (selecting row) Basically its a program that adds service to clients.

Thank you so much for your interest and also for reading if you read that all. Im stuck with this issue for 2 days :/

Picture for kwwallpe: You can see here that "Nic" has never been selected by any costumer so it is displayed.

To make my question more clear - I take some of these columns from another table (as you can see in sql query).

This is what does kwwallpe's code:

 string sQuery = string.Format("SELECT zajsluz.akce,zajsluz.text,klisluz.pocet,klisluz.subkey,zajsluz.ID FROM zajsluz LEFT JOIN klisluz ON zajsluz.ID=klisluz.IDzajsluz WHERE zajsluz.akce= '{0}' and ISNULL(klisluz.subkey, '" + vyberradek + "') = '" + vyberradek + "' GROUP BY klisluz.subkey,zajsluz.akce,zajsluz.text,klisluz.pocet,zajsluz.ID", sZakce);

This code is much way close I think that those 2. Because it SELECT values which have never been selected by any costumer b4. You can see hat "Nic" is displayed because it is not in the table klisluz.

enter image description here

1条回答
唯我独甜
2楼-- · 2019-02-26 00:53

If I'm understanding what you're after, this is the part that's keeping you from getting the rows with no entries in klisluz:

WHERE zajsluz.akce= '{0}' and klisluz.subkey ='" + vyberradek + "'

For the rows that don't exist in klisluz, subkey will be NULL, which won't match anything with that WHERE clause. To get these rows as well, you can replace your current WHERE clause with:

WHERE zajsluz.akce= '{0}' and ISNULL(klisluz.subkey, '" + vyberradek + "') ='" + vyberradek + "'

Here's the full line of code:

string sQuery = string.Format("SELECT zajsluz.akce,zajsluz.text,klisluz.pocet,klisluz.subkey,zajsluz.ID FROM zajsluz LEFT JOIN klisluz ON zajsluz.ID=klisluz.IDzajsluz WHERE zajsluz.akce= '{0}' and ISNULL(klisluz.subkey, '" + vyberradek + "') = '" + vyberradek + "' GROUP BY klisluz.subkey,zajsluz.akce,zajsluz.text,klisluz.pocet,zajsluz.ID", sZakce);

To determine whether the checkbox should be checked:

if (precti3.HasRows)
{
    precti3.Read();
    if (precti3.Item("subkey") != Null)
    {
        row.Cells[5].Value = true;
    }
    else
    {
        row.Cells[5].Value = false;
    }
}

I think that's how it'd go in C#. I work in VB.NET, used an online converter for this.

查看更多
登录 后发表回答