0

I have this two tables and I want to join their two ID's.

Household Info                             
1
2
3

Household Members
1
1
1
2
3
3
3
3
3

The values is repeating over and over again, as you have noticed on my screenshot. The output I want is, I want a query of this:

Household Info.HID                Household Members.HID
1                                 1
                                  1
                                  1
2                                 2
3                                 3
                                  3
                                  3
                                  3
                                  3

enter image description here

Since in the Table Household Info there are only 3 HID while the table Household Members there are three:1, one:2, and five:3

Hope you can help me on this one :3 EDITED: I am using Microsoft Access as RDBMS

pjforum
  • 83
  • 12
  • Could you show the query you try so far? – Pham X. Bach May 13 '16 at 07:33
  • You're confusing an issue of data storage and retrieval with one of data display. The latter is normally handled in application level code (e.g. a simple PHP loop – Strawberry May 13 '16 at 07:37
  • Have you tried with mohan111 post. Is that you are looking at – StackUser May 13 '16 at 07:41
  • @PhamX.Bach I just tried simple query, UNION, DISTINCT. I have not tried complicated queries... But they aren't coming up with the output that I seek. – pjforum May 13 '16 at 08:11
  • @Strawberry I just want to display "how many members of the family that are associated with the household head" – pjforum May 13 '16 at 08:11
  • @StackNewUser yes I did, but I can't get it working... – pjforum May 13 '16 at 08:12
  • That's not what your question says – Strawberry May 13 '16 at 08:22
  • @Strawberry it might not be, but on the description that I stated above, I meant for that... Because when I use **"Natural,equi,full join, or even distinct"** I can't get the output that I desire :X And btw, I just tried simple queries, but I can't get it running. I mean I don't get my desired output. – pjforum May 13 '16 at 08:31
  • Fine, but it still isn't clear what you actually want as a result. – Strawberry May 13 '16 at 09:46
  • @Strawberry, please check I edit it, that should be clear by now.. – pjforum May 16 '16 at 02:49
  • The query for "how many members of the family are associated with the household head" and the query for the original question are obviously different. Either delete the comment, or edit the question. – Strawberry May 16 '16 at 06:02

2 Answers2

0

For an RDBMS which supports CTE...

DECLARE @Household  TABLE 
    ( Household  VARCHAR(10))
;

INSERT INTO @Household
    ( Household )
VALUES
    (1),
    (2),
    (3)
;


declare @HouseholdMembers TABLE 
    ( HouseholdMembers  VARCHAR(10))
;

INSERT INTO @HouseholdMembers
    ( HouseholdMembers )
VALUES
    (1),
    (1),
    (1),
    (2),
    (3),
    (3),
    (3),
    (3),
    (3)
;

Select 
CASE WHEN RN = 1 THEN Household ELSE '' END Household,
HouseholdMembers 
from (
select h.Household,
hm.HouseholdMembers,
ROW_NUMBER()OVER(PARTITION BY hm.HouseholdMembers ORDER BY h.Household)RN from @Household h
LEFT JOIN @HouseholdMembers hm
ON hm.HouseholdMembers = h.Household)T
Strawberry
  • 33,750
  • 13
  • 40
  • 57
mohan111
  • 8,633
  • 4
  • 28
  • 55
  • You are a man of few words ;) some explanation would be useful for him. – AxelH May 13 '16 at 07:46
  • I tried the code you stated, but it seems that I can't get it working.. `WITH CTE AS ( select h.HID,hm.HID,ROW_NUMBER()OVER(PARTITION BY hm.HID ORDER BY h.HID)RN from [Household Info] h LEFT JOIN [Household Members] hm ON hm.HID = h.HID) select CASE WHEN RN = 1 THEN h.HID ELSE '' END h.HID,hm.HID from CTE` I tried any possibilities, but I really can't get it working. It seems that's a bit complicated query :3 – pjforum May 13 '16 at 07:59
  • @mohan111 My RDBMS doesn't support **CTE** I am using **Microsoft access** I can't get my desired output :X – pjforum May 13 '16 at 08:34
  • @mohan111 [click here](http://stackoverflow.com/questions/14286417/mysql-join-return-null-for-duplicate-results-in-left-table) <------- This might be the output that I am looking for, but I can't get it working.... – pjforum May 13 '16 at 08:36
  • I have modified code by removing CTE and used Derived Column – mohan111 May 13 '16 at 08:38
  • [Here is the error](http://i.imgur.com/vAsE3Yf.png) Yes I saw that, and I tried, but It is still not working.... [Here is the error](http://i.imgur.com/vAsE3Yf.png) @mohan111 – pjforum May 13 '16 at 08:41
  • @mohan111 http://i.imgur.com/vAsE3Yf.png And also checked the comment below the "EDIT 2" by sir Vanko.. That's the output I desire.. – pjforum May 13 '16 at 08:45
0

You didn't mention what are you using as RDBMS.

I think that you can use pivot for your case:

http://www.codeproject.com/Tips/500811/Simple-Way-To-Use-Pivot-In-SQL-Query

or to use grouping:

select c2, c3
  , sum( case when no_of_days <= 7 then 1 else 0 end) as dlt8
  , sum( case when no_of_days between 8 and 14 then 1 else 0 end) as d8to14
  , sum( case when no_of_days between 15 and 21 then 1 else 0 end) as d15to21
  , sum( case when no_of_days between 22 and 27 then 1 else 0 end) as d22to27
from mytable
group by c2, c3
order by c2, c3;

Here you can find similar answer to your question:

Dynamic alternative to pivot with CASE and GROUP BY

Edit 1

If you need something like this:

SubjectID       StudentName
----------      -------------
1               Mary
1               John
1               Sam
2               Alaina
2               Edward

Result I expected was:

SubjectID       StudentName
----------      -------------
1               Mary, John, Sam
2               Alaina, Edward

you can check this example:

Concatenate many rows into a single text string?

Edit 2

And the last option that I can remember is this one. It's for MySQL but you can reuse the logic:

MySQL JOIN - Return NULL for duplicate results in left table

Community
  • 1
  • 1
DataScientYst
  • 442
  • 2
  • 7
  • 19
  • I don't think so, or it is just I don't know? I am really having hard time here. I don't think pivot can solved this kind of problem :3 – pjforum May 13 '16 at 07:52
  • maybe you need something closer like this: http://stackoverflow.com/questions/194852/concatenate-many-rows-into-a-single-text-string – DataScientYst May 13 '16 at 08:10
  • The edit 2, might be the one that I am looking for, but I can't get it working :(( `select case when rownum = 1 then title else null end HID, HID, [Member Name] from ( SELECT c.HID, i.HID, i.Member Name, @row:=(case when @prev=HID and @precat=[Member Name] then @row else 0 end) + 1 as rownum, @prev:=HID ptitle, @precat:=[Member Name] pcat FROM [Household Members] AS i INNER JOIN [Household Info] AS c ON c.hid = i.Member Name order by i.Member Name, c.HID ) src order by [Member Name], rownum` **PLEASE HELP :3** – pjforum May 13 '16 at 08:29