473,386 Members | 1,785 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,386 software developers and data experts.

where is the other records ?

215 128KB
Hi, I'm back here with new simple question, but since I'm noob at SQL, so I can't answer this myself.
assumption I have 2 table, Table 1 (10000 rows) is data raw having lot of infomation of bill, and table 2 (10 rows) content client-to-tracking infomation. Those 2 table have connect by ID.

when I use query, set relationship to left join
(all from table 2 and match from table 1),
the result show less record than number of table 2's record
(4 rows for example)

where is the rest record ? i don't expect left join having this result, I try to change relationship to innerjoin or right join have no different.

need to show up 10 rows in query to tracking... I can't miss even 1 row
I use some total (group by, expression, where...) on design.

code like this:
Expand|Select|Wrap|Line Numbers
  1. SELECT [Table 2].Ter, [Table 2].ID, [Table 2].name, [Table 2].voltarget,  Sum([Table 1].[volume]) AS [Total-MTD], IIf([Total-MTD]>=[voltarget],"yes”,"No") AS [Success (y/n)]
  2. FROM [Table 2] LEFT JOIN [Table 1] ON [Table 2].ID = [Table 1].ID
  3. WHERE ((([Table 1].[Item code]) Like "116*"))
  4. GROUP BY [Table 2].Ter, [Table 2].ID, [Table 2].name, [Table 2].ID;
  5.  
Aug 26 '15 #1
6 1269
jimatqsi
1,271 Expert 1GB
You are testing a column from table 1 in your where clause. That criteria can never pass when the table one record is missing. Change
Expand|Select|Wrap|Line Numbers
  1. ((([Table 1].[Item code]) Like "116*"))
to
Expand|Select|Wrap|Line Numbers
  1. (((NZ([Table 1].[Item code],"what you want when it is missing")) Like "116*"))
The what you want when it is missing must be like 116 if you want to include those records that don't have a matching ID.

Alternatively, you could try
Expand|Select|Wrap|Line Numbers
  1. ((([Table 1].[Item code]) Like "116*") or ([Table 1].[Item code] Is Null))
  2.  
Jim
Aug 26 '15 #2
hvsummer
215 128KB
it have better result, increase from 4 to 6 rows, but I need 10 rows result with left join :( still thank you Jim.

I draw it like this, TABLE1 down to 4 raw, table2 down to 6 raw to make it more simple, with code from Jim, it'll show from 2 to 3 raw, still missing something



in table 2 it have lot of item and Client, but I only need to track item 1 and those client from table1.

any idea ? help me plz, thank you.
Attached Images
File Type: jpg P_20150827_092036.jpg (53.3 KB, 114 views)
Aug 27 '15 #3
jimatqsi
1,271 Expert 1GB
hvsummer, please help us help you. Show your code and do the investigation you can and tell us what your investigation reveals. You want 10 rows and got 6; so which 4 did you not get? What do those 4 have in common; what do the 6 have in common? The work is yours to do and folks here will help when you give them enough information to help with.

Again, show the code after you modified it.

Jim
Aug 27 '15 #4
hvsummer
215 128KB
hi Jim
I simplify code to post here, but I test with real big data, so I can't post what is the diffrent for you since my data is too big
but after I change code to your suggestion, like this
Expand|Select|Wrap|Line Numbers
  1. SELECT [Table 2].Ter, [Table 2].ID, [Table 2].name, [Table 2].voltarget,  Sum([Table 1].[volume]) AS [Total-MTD], IIf([Total-MTD]>=[voltarget],"yes”,"No") AS [Success (y/n)]
  2. FROM [Table 2] LEFT JOIN [Table 1] ON [Table 2].ID = [Table 1].ID
  3. WHERE (((NZ([Table 1].[Item code],"what you want when it is missing")) Like "116*"))
  4. GROUP BY [Table 2].Ter, [Table 2].ID, [Table 2].name, [Table 2].ID;
  5.  
result 600 raws (missing 400 raw). I said 10 (6 miss 4), but that I make it simple to understand..
with code that you want me to change "altenative"

Expand|Select|Wrap|Line Numbers
  1. SELECT [Table 2].Ter, [Table 2].ID, [Table 2].name, [Table 2].voltarget,  Sum([Table 1].[volume]) AS [Total-MTD], IIf([Total-MTD]>=[voltarget],"yes”,"No") AS [Success (y/n)]
  2. FROM [Table 2] LEFT JOIN [Table 1] ON [Table 2].ID = [Table 1].ID
  3. WHERE ((([Table 1].[Item code]) Like "116*") or ([Table 1].[Item code] Is Null))
  4. GROUP BY [Table 2].Ter, [Table 2].ID, [Table 2].name, [Table 2].ID;
  5.  
it showed up 500 rows (mean 5/10).
I have to track client from the list on table 1 (table 1 don't have item code) which buying item code start with 116 in table 2(only table 2 have item code)

I findout in the simple model that the row mising is the client which don't have vol in tracking-item in table 2 (mean that client did not buy tracking-item). but I still need to show him up (client which didn't have vol (total-MTD), or you can say "did not buy tracking-item"). any idea ?
Aug 27 '15 #5
hvsummer
215 128KB
ok, Resolve by myself...
I findout that to show all the client to track I don't use condition in the criterial anymore
code should be like this to work as I want:
Expand|Select|Wrap|Line Numbers
  1. SELECT Table1.NameClient, Table1.ID, Table1.Item, Table1.Vol, Sum(IIf([Table2].[item]=1,[table2].[vol],0)) AS MTD, IIf([MTD]>=[Table1].[Vol],"ok","not ok") AS ok
  2. FROM Table1 INNER JOIN Table2 ON Table1.ID = Table2.ID
  3. GROUP BY Table1.NameClient, Table1.ID, Table1.Item, Table1.Vol;
  4.  
Aug 28 '15 #6
jimatqsi
1,271 Expert 1GB
Congratulations. The best solutions are the ones we can arrive at ourselves.

Jim
Aug 28 '15 #7

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

Similar topics

40
by: Elijah Bailey | last post by:
I want to sort a set of records using STL's sort() function, but dont see an easy way to do it. I have a char *data; which has size mn bytes where m is size of the record and n is the...
8
by: John Welch | last post by:
I have a command button with the following code: DoCmd.OpenForm "frmSearchAssignments", , , "SearchAssignmentID = 1" (SearchAssignmentID is the PK, auto number) When it runs, the form opens but...
0
by: pekx | last post by:
Hi, I create a script to pick up those records with duplication of IMIPROPSERVNO field, see below test1.sql, which is working.... test1.sql 1.SELECT IMIPROPSERVNO, COUNT(*) 2.FROM...
2
by: Catch_22 | last post by:
Hi, I have a stored procedure that has to extract the child records for particular parent records. The issue is that in some cases I do not want to extract all the child records only a...
4
by: LetMeDoIt | last post by:
Greetings, I'm using ASP to retrieve from MSSQL and I then populate a table. After several months of successfull retrieves, this same code now bombs out. It turns out that if I clear out from...
3
by: Cindy | last post by:
I'm trying to use the NEWID function in dynamic SQL and get an error message Incorrect syntax near the keyword 'ORDER'. Looks like I can't do an insert with an Order by clause. Here's the code:...
3
by: Bizmark | last post by:
Access2003, Windows XP Pro OEM user OS, Windows Sever 2000 server OS. Not-so-recently I inherited 3 Access DB's from another company merging with our own. Our own system works off of SQL...
2
by: mfaisalwarraich | last post by:
Hi Everybody, I am using the following code to get the recordset of an external database. Dim dbPatients As Database Dim rsCountPatients As Recordset ' to count number of...
5
by: jmarcrum | last post by:
Hi everyone! I'm getting an error that I can't figure out...here's my code...i'm trying to reference the first sqlStatement from the second sqlStatement and run the second sqlStatement. Am I...
8
by: Cristina Graziani | last post by:
I am working on a database where records have been created starting from 001, now I am moving from record 999 to 1000, but while in "Forms" I can visualize them in the progressive order, in "Tables"...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...

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.