SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


combining two tables.


combining two tables.

Author
Message
soldout6000
soldout6000
SSC Rookie
SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)

Group: General Forum Members
Points: 29 Visits: 31
I obtaining information from one main table to retrieve employees and spouses, and display the fields in my report. I'm counting employees and spouses based on different scenarios or grouping. I'm having trouble coming tb_emp and tb_spouse. I'm getting cartesian product with the following join.

Please advise me as to how can I combine two mini tables. Thanks in advance!

With tb_main as
(
SELECT CLIENT_NO, AMOUNT, ACTIVITY_TYPE, PERSON_ID, EMP_SPOUSE_NUMBER
FROM table_main_db
WHERE EMPLOYER_NUMBER = 1234
),

tb_emp as
(SELECT e.CLIENT_NO, e.AMOUNT, e.EMP_SPOUSE_NUMBER,
COUNT(e.CLIENT_NO) OVER (PARTITION BY e.CLIENT_NO) emp_group,
SUM(e.AMOUNT) OVER (PARTITION BY e.CLIENT_NO) emp_amt_group,
COUNT(DISTINCT e.ACTIVITY_TYPE) OVER (PARTITION BY e.CLIENT_NUMBER) emp_act_group
FROM tb_main e
WHERE PERSON_ID = 11 -- id for employees
),

tb_spouse as
(SELECT s.CLIENT_NO, s.AMOUNT, s.EMP_SPOUSE_NUMBER,
COUNT(s.CLIENT_NO) OVER (PARTITION BY s.CLIENT_NO) spouse_group,
SUM(s.AMOUNT) OVER (PARTITION BY s.CLIENT_NO) spouse_amt_group,
COUNT(DISTINCT s.ACTIVITY_TYPE) OVER (PARTITION BY s.CLIENT_NO) spouse_act_group
FROM tb_main s
WHERE PERSON_ID = 99 -- id for spouse
),

SELECT e.CLIENT_NO, e.AMOUNT, e.emp_group, e.emp_amt_group,
s.CLIENT_NO, s.AMOUNT, s.spouse_group, s.spouse_amt_group
FROM tb_emp e
LEFT OUTER JOIN tb_spouse s ON (e.EMP_SPOUSE_NUMBER = s.EMP_SPOUSE_NUMBER)

J Livingston SQL
J Livingston SQL
One Orange Chip
One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)

Group: General Forum Members
Points: 27406 Visits: 41382
https://spaghettidba.com/2015/04/24/how-to-post-a-t-sql-question-on-a-public-forum/

________________________________________________________________
you can lead a user to data....but you cannot make them think
and remember....every day is a school day

Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search