Advertisement

04.02.2008 at 08:16AM PDT, ID: 23289584
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

9.8

Need help combining two queries

Asked by YZlat in MS SQL Server

I have 4 tables of the following structure with the following data:

TableC
__________
c_id
field_C1
field_C2



TableCV
_________
c_id
v_id
detect
start_date


TableV
________
v_id
lang
ext_id



TableCGV
_______
ext_id
field_CGV1
field_CGV2


TableC
_________________________________
c_id      field_C1      field_C2
__________________________________
1      test            test2
2      test4            test8
3      test11            test3


TableV
____________________________
v_id      lang      ext_id
_________________________
ABC      en      4
GHI      en      8
EFG      en      7
KLM      en      11
OPR      en      5





TableCV
___________________________________
c_id      v_id      detect      start_date
___________________________________
1      ABC      0      03/11/2008
1      GHI      0      02/21/2008
1      ABC      1      03/01/2008
1      EFG      0      01/21/2008
2      ABC      1      01/11/2008
2      OPR      1      03/21/2008
2      EFG      1      03/02/2008
3      GHI      1      03/12/2008
3      EFG       1      03/17/2008
4      ABC      0      03/15/2008
4      OPR      1      03/28/2008
4      GHI      0      03/11/2008
4      EFG      0      03/30/2008


TableCGV
____________________________________
ext_id      field_CGV1      field_CGV2
____________________________________
4      test            test
5      test            test
7      test            test
8      test            test
11      test            test

I need to create a stored procedure in which for each v_id I need to retrieve 3 fields:

v_id, count of c_id from TableCV where detect > 0, and count of all c_id from TableCV regardless of value of detect. Also the start_date should be between 03/01/2008 and 03/31/2008


From the sample data above my results should be:

ABC      1      3
EFG      2      3
GHI      1      2
OPR      2      2

SELECT v.v_id, COUNT(DISTINCT(c.c_id)) As Value3
FROM TableC c
INNER JOIN TableCV cv ON c.c_id=cv.c_id
INNER JOIN TableV v ON v.v_id=cv.v_id and v.lang='en'
INNER JOIN TableCGV ON cgv.ext_id=v.ext_id
WHERE cv.start_date BETWEEN '03/01/2008' AND '03/31/2008'
GROUP BY v.v_id


The query above retrieves first and third fields that I need but I have trouble retrieving second field:

ABC      3
EFG      3
GHI      2
OPR      2


And the query below returns first and second value.

 SELECT  v.v_id, COUNT(DISTINCT(c.c_id)) As Value2
FROM TableC c
INNER JOIN TableCV cv ON c.c_id=cv.c_id
INNER JOIN TableV v ON v.v_id=cv.v_id and v.lang='en'
INNER JOIN TableCGV ON cgv.ext_id=v.ext_id
WHERE cv.start_date BETWEEN '03/01/2008' AND '03/31/2008'
GROUP BY v.v_id AND cv.detect > '0'


I have trouble consructing a query that would return all 3 values in the same resultset.

Please helpStart Free Trial
[+][-]04.02.2008 at 08:47AM PDT, ID: 21264302

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: MS SQL Server
Sign Up Now!
Solution Provided By: dqmq
Participating Experts: 1
Solution Grade: A
 
 
[+][-]04.02.2008 at 10:12AM PDT, ID: 21265121

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628