0

I am using SQL server compact 4.0 and VB.net and Visual studio 2019.

I am using this request to extract information from 4 tables : Table0, Table1, Table2 and Table3. This request is working fine before, but now I have one problem because one column in Table 3 (ID_C) is now duplicated, and this request needs table 3 without this duplication.

One solution for my problem is to create a new table (table 3 without duplication) and the problem will be solved. But I am thinking because I am beginner in SQL, maybe there is a solution to skip one row in Table3 inside the request. I have searched, but I didn't find a solution for this problem.

"SELECT Table0.M " +
 "FROM Table1 INNER JOIN " +
 "Table2 ON Table1.ID = Table2.ID_V INNER JOIN " +
 "LM ON Table2.ID_M = Table0.ID_M INNER JOIN " +
 "Table3 ON Table2.ID_C = Table3.ID_C " +
 "WHERE (Table1.IDX_V = #IDX_V#) AND (Table3.ID_C = '#ID_C#')"

In the code #IDX_V# and #ID_C# are replaced by some values.

asked Oct 1, 2021 at 15:12
3
  • While this doesn't impact the answer to your question, I wanted to note that SQL Server Compact was deprecated almost 9 years ago, in February 2013, and is now fully unsupported, as even extended support ended earlier this year. Commented Oct 1, 2021 at 19:32
  • How would you decide what row to "skip" in Table3? Add that as the query criteria. Commented Oct 1, 2021 at 19:40
  • @mustaccio A row is added twice so I can choose even rows or odd rows Commented Oct 1, 2021 at 21:26

2 Answers 2

0

You can use an EXISTS subquery

"SELECT Table0.M
FROM Table1
INNER JOIN Table2 ON Table1.ID = Table2.ID_V
INNER JOIN LM ON Table2.ID_M = Table0.ID_M
WHERE Table1.IDX_V = @IDX_V
 AND EXISTS (SELECT 1
 FROM Table3
 WHERE Table2.ID_C = Table3.ID_C
 AND Table3.ID_C = @ID_C);"

Note the use of proper parameters, rather than injection. And the use of a multiline string

answered Oct 3, 2021 at 1:25
2

If the row is 100% duplicate, you could try to use distinct . This will give you unique results. This would not be "skipping" a row but would only show 1 row for any duplicates.

"SELECT DISTINCT Table0.M " +
 "FROM Table1 INNER JOIN " +
 "Table2 ON Table1.ID = Table2.ID_V INNER JOIN " +
 "LM ON Table2.ID_M = Table0.ID_M INNER JOIN " +
 "Table3 ON Table2.ID_C = Table3.ID_C " +
 "WHERE (Table1.IDX_V = #IDX_V#) AND (Table3.ID_C = '#ID_C#')"
answered Oct 1, 2021 at 15:38
2
  • I will test, but the problem is only one column (ID_C) is duplicated in Table3, the other columns in Table3 are not duplicated Commented Oct 1, 2021 at 15:43
  • 1
    you are only returning one column in your example. if you provide table creation scripts and sample data, you can get a better answer Commented Oct 1, 2021 at 16:04

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.