Click here to Skip to main content
15,881,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I have two tables in my SQLSERVER DB as shown below.

Table1
ID   Version
1      A
2      B
3      C
4      D
5      A
6      C
7      B
8      E


Table 2
ID      Version
1          A
2          C


I would like to get a result as shown below

TableResult
ID    Version     Value   Count
1       A          1        2
2       B          1        2
3       C          1        2
4       D          0        1
5       E          0        1


What I have tried:

I have tried some joins and unions operations. but could not find the result
Posted
Updated 7-Sep-16 18:19pm
v2
Comments
OriginalGriff 7-Sep-16 1:46am    
You need to explain rather better what the values in your output are and mean: the Version and Count are simple, the ID is a row count, but I have no idea where Value comes from - it's not at all obvious from the data.
Tomas Takac 7-Sep-16 2:27am    
What is the role of the second table? How do you calculate value?
vani suresh 8-Sep-16 0:05am    
What i understand you are calculating the value column from second table
Ex: 'A' is present in second table value is 1
'E' is not present in second table value is 0
is it correct ?

1 solution

If vani suresh is correct then that makes sense and the "Value" for "B" is incorrect and that is why everyone is confused.

I am not a SQL expert but if the preceding is what you need to do then you need to sort Table 1 on Version to get the count then do a join or union (I am not sure which) with Table 2.
 
Share this answer
 
Comments
Maciej Los 8-Sep-16 2:25am    
Sounds like a comment to the question, not like an answer. I'd suggest to remove it to avoid down-voting.
Sam Hobbs 8-Sep-16 2:48am    
I don't understand. I really don't.

I am a long-time member. If this is not what is wanted then I will continue to avoid this place. I have seen some replies that surprised me with how unsupportive they are.

Honestly, I don't understand the problem.
Maciej Los 8-Sep-16 4:05am    
The problem does not exists. In my opinion, your answer is not a solution. That does not help OP in solving his issue. Such of content - in my opinion - should be posted as a comment to the question.
Regards,
Maciej
Sam Hobbs 8-Sep-16 4:38am    
I still don't understand. Oh, okay; I guess I see there is a difference between a comment and a solution. This web site does not make that obvious. Okay, go ahead and downvote me; I don't care. For the future, you need to understand that the problem might be that the web site does not make the difference between a comment and a solution obvious instead of jumping on the person as if they understand.
Maciej Los 8-Sep-16 4:47am    
I'd suggest to read this: QA FAQ - How is this different from the message boards section.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900