473,405 Members | 2,294 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

how to avoid duplicate value from this situation, what is the right SQL ?

215 128KB
ok, to be simple, I'll ask short question with short detail.

I have 2 table.
table 1 with [ID] and [Date]

ID Date
1 -- 1
1 -- 2
1 -- 3
2 -- 1
2 -- 2
2 -- 3

Table 2 with [ID], [Datemin], [DateMax], and [KM]

ID Datemin DateMax KM
1 ---- 1 ----- 2 ------ 100
2 ---- 1 ----- 2 ------ 100
2 ---- 3 ----- 4 ------ 200

when I create query like this

Expand|Select|Wrap|Line Numbers
  1. SELECT Table1.ID, Table1.Date, IIf([table1].[date] Between [table2].[from] And [table2].[to],[table2].[KM],0) AS KM
  2. FROM Table1 LEFT JOIN table2 ON Table1.IDItem = table2.IDItem;
  3.  
to determine whether "that ID have date between datemin and datemax" or not, if True, give that ID field value from table2.KM

but I got duplicate from it. Instead 6 rows like table 2, query gave me 9 rows with duplicate 3 rows from ID 2

how to avoid/eliminated those duplicate ?
Oct 7 '15 #1
8 1085
jimatqsi
1,271 Expert 1GB
hvsummer, your questions is not very clear because you have been inconsistent in your descriptions. Your SQL refers to fields 'IDItem' when no such field appears in the two table layouts. I presume IDITEM and ID are one and the same. Also, you describe tables 1 and 2, but the sQL refers to tables 4 and 5. And neither of the described tables has a field for From or To, which also appear in your SQL as part of table 5.

I get it, table 1 is table 4, table 2 is table 5, etc.... But don't make us work so hard to understand your question.

All that being said, you need to add grouping to your query. If you right-click in the lower half of the query editor, you'll see a choice for Grouping. In grouping mode, you can specify which columns to group, sum, average and so on. All you need to do is group by both columns being selected and you will get only one row for each pair of values.

You can accomplish a similar thing by right-clicking on the top half of the query editor and choosing Unique Values.

I hope that helps.

Jim
Oct 7 '15 #2
hvsummer
215 128KB
sorry jimatqsi, I have edited those code.
and unique value does not help, unique row value too
and grouping can't solve this problem

can anyone suggest on SQL code ? I know that problem is inside the code structure. I think this is many to many relationship duplicate problem.. how to force hide or avoid those duplicate ? or any SQL code can deal with it ?
Oct 8 '15 #3
in access where you join the tables, you can click on the properties and then from there you can limit your results. you can do all from one table and only match what is in that table, it will help with the duplicates
Oct 8 '15 #4
Rabbit
12,516 Expert Mod 8TB
You said you wanted "have date between datemin and datemax" but nowhere in your SQL do you do that.
Oct 8 '15 #5
hvsummer
215 128KB
@Rabbit : it's "[table1].[date] Between [table2].[from] And [table2].[to]" that is inside iif. Still thankyou.
@Everyone Else:
I found the way to solve my problem.
should be group with sum(iif(condition, value, 0)) in another query.

then match those value back into first select-query by ID.
thank everyone.
Oct 9 '15 #6
Rabbit
12,516 Expert Mod 8TB
You shouldn't do it in the iif, you should do it in the join so that you don't have to deal with duplicates in the first place.
Oct 9 '15 #7
hvsummer
215 128KB
@Rabbit: how ? I can't deal with that many to many relationship ==
Oct 9 '15 #8
Rabbit
12,516 Expert Mod 8TB
You just take the same thing that's in the first parameter of your iif function call and put it in the join. You'll have to do it in SQL view. You can't use the designer for it.
Oct 9 '15 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: audj | last post by:
Hello, I am trying to use a subquery to avoid duplicate entries when someone submits a form to subscribe to a mailing list. So I want to check if the email exists before adding the record. ...
5
by: mercea | last post by:
hi all, How do i avoid duplicate records on my database? i have 4 textboxes that collect user information and this information is saved in the database. when a user fills the textboxes and clicks...
2
by: Shawn Yates | last post by:
I am creating a Zip Code search tool. I have a Table with every zip code and its corresponding city and state. I have also created an input table that allows the user to input cities and states....
4
by: lucky13 | last post by:
Dear All, can u help in how to check the duplicate value in the listview control before adding new in the list. Regards Lucky13
1
by: vishwa Ram | last post by:
Hi ScriptFamily, I need to remove a duplicate value in array. Any specific method available in perl. pls suggest Regards Raam
3
by: spl | last post by:
What is the fastest way to find a duplicate value in an array of 10 elements. I have to find just any one first occurrence of the duplication. Lets say I have ary={10, 20, 40, 90, 30, 60, 35, 40,...
1
by: krishanusasmal | last post by:
I am learning in PHP and HTMl. AND XAMPP. when i inserting any duplicate value. I want to display message with duplicate message or the data already exist or something like that. Plz. help me.
1
by: inder | last post by:
I have two fields Phone1 and Phone2 in my form. I want Phone1 after update to check its duplicate value in both phone1 and phone2. Similarly Phone2 to check phone2's duplicate value in both phone1...
5
by: hvsummer | last post by:
ok, After I used MVFs (Multivalue Fields) to query thing, I got alittle problem with it, It created duplicate value in query, can anyone spot out the problem and give some solution idea ? the...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.