SIMPLE SQL - INNER JOIN / GROUP - return NULL values in results list

Simple one really - I have some SQL, there is an inner join and a group by the inner join eg:

select count(*) AS Total, myTable.myField FROM myTable INNER JOIN myJoinedTable ON myTable.myField = myJoinedTable.myField GROUP BY myJoinedTable.fieldToGroupBy

At the moment it returns:

a: 3
b: 1
c: 8

etc - but if there are any NULL values in the main table in fieldToGroupBy they get ignored. I cannot think of / find the syntax to just return another row for any null values (or values that do not match the joined table).

can any one fill me in?


Thanks!...
niicoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

peter57rCommented:
Hello niico,

> etc - but if there are any NULL values in the main table in fieldToGroupBy
fieldToGroupBy would appear to be in the joined table, not the main table



> they get ignored. I cannot think of / find the syntax to just return
> another row for any null values (or values that do not match the joined table).

I don't think I understand what you expect here

For values to be returned from an inner join, there must be a matching record on both sides of the join.
If there is no record in one of the tables then no values will be returned.

The alternative join types would be a left join or a right join.

In your SQL code then instead of 'FROM myTable INNER JOIN myJoinedTable '
you could have
FROM myTable Left JOIN myJoinedTable
This would return values from myTable whether or not there was a matching record in myJoinedTable.

Similarly
FROM myTable Right JOIN myJoinedTable

would return all records from myJoined tabel irrespective of whether there was a matching record in myTable.

Perhaps one of these is what you need?

Pete
0
niicoAuthor Commented:
>>fieldToGroupBy would appear to be in the joined table, not the main table

sorry you're right - i meant myField


<yes actually I over simplified - there are two joins.

I'll go away and think about exactly what Im trying to do and come back to you - sorry for the vague question...
0
gita_boradCommented:
use a left join?
0
LowfatspreadCommented:
1) NULL does not EQUAL NULL

choose an appropriate value for the coalesce and you can group all your "unknown" rows together....

select count(*) AS Total
  , T.Jkey
FROM (select a.*,coalesce(myfield,'XXXXX') as JKey from myTable as A) as T
 INNER JOIN (select a.*,coalesce(myfield,'XXXXX') as JKey myJoinedTable as A)  as J
 ON T.Jkey = J.Key
 GROUP BY J.fieldToGroupBy

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
niicoAuthor Commented:
hi

sorry about the delay - this thing ended up getting postponed and havent had a chance to look again.

No objection to giving points to lfs.


thanks!...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Databases

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.