0

I have got this query where I'm not able to select distinct I uploaded a Picture so you can understand it better. I also marked it with squares which values fits together. I think I need to add klisluz.subkey column when doint LEFT JOIN but I'm not sure how to do that? and then sort it where klisluz.subkey is "vyberradek"

DataTable dt = new DataTable();
string sZakce = string.Empty;
if (zakce != null && zakce.Text != null)
{
  sZakce = zakce.Text;
}

   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);
   SqlDataAdapter SDA = new SqlDataAdapter(sQuery, spojeni);        
SDA.Fill(dt);
dtg_ksluzby.DataSource = dt;

Edit: I updated the query so now I inserted column klisluz.subkey

enter image description here

Blaze M
  • 200
  • 4
  • 16

1 Answers1

1

your using distinct for multiple column. It would only work if the values of all column find exist on another row for example

1 888 66 Balkon 3 122
is different from
1 888 67 Balkon 5 122
because of the values 67 and 5

unless you change row 1 to
1 888 67 Balkon 5 122
or you change row 2 to
1 888 66 Balkon 3 122 then it will work

zxc
  • 1,504
  • 3
  • 13
  • 32
  • Well I need to sort it So I can see only one square which belongs to the database but I want to see also the rest which isnt marked with checkbox – Blaze M Jul 19 '13 at 08:53
  • 1
    can you provide more detals on what you really need because base on what I understand you focused more on "distinct" subject – zxc Jul 19 '13 at 08:57
  • Well I need to add "klisluz.subkey" to dtg_ksluzby (its the query I posted). Now its selecting me all the checkboxes doesnt matter what the subkey is, because at the moment there is no klisluz.subkey. And also I dont want to loose the "non checked" rows. Is there any way? – Blaze M Jul 19 '13 at 09:01
  • 1
    sorry im still bit confused but if you want klisluz.subkey then you should add it in your select query – zxc Jul 19 '13 at 09:12
  • I updated that, so I have klisluz.subkey in my query, How can I sort it so it would display only the ones which have subkey for example 67? – Blaze M Jul 19 '13 at 09:16
  • 1
    "SELECT DISTINCT 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 ='67' order by zajsluz.id asc – zxc Jul 19 '13 at 09:22
  • That works great! but how can I also display the rows which werent checked? I mean like now it displays only the what I want but I would like to display the others. Is there any way to do that? – Blaze M Jul 19 '13 at 09:23
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/33756/discussion-between-blaze-m-and-bew) – Blaze M Jul 19 '13 at 09:27
  • If you have some free time, may you please have a look at this? http://stackoverflow.com/questions/17744031/group-by-issue-when-select-to-datagridview-from-2-tables I tried to be more specific then now. – Blaze M Jul 19 '13 at 13:20