473,416 Members | 1,546 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

sql join two queries in stored procedure return as one to datareader

I want to group and then join two tables in a stored procedure and return results to a datareader. The non-grouped data in each table has a many to many relationship.

Expand|Select|Wrap|Line Numbers
  1. select entity, count(*) as vcnt, sum(amt) as vamt
  2. from vtable
  3. where date = @dateparm
  4. and rec_type = 'V'
  5. group by entity
  6.  
  7. select entity, count(*) as ccnt, sum(amt) as camt
  8. from ctable
  9. where date = @dateparm
  10. and rec_type = 'C'
  11. group by entity
I want to return joined by entity (vtable left outer join ctable); fields entity, vcnt,vamt,ccnt,camt, all on same row. By entity after group by.

Before group by entity is a many to many relationship.
May 15 '10 #1
2 3597
ck9663
2,878 Expert 2GB
try:

Expand|Select|Wrap|Line Numbers
  1. select entity,
  2. vcnt = sum(case when rec_type = 'v' then 1 else 0 end),
  3. vamt = sum(case when rec_type = 'v' then amt else 0.00 end),
  4. ccnt = sum(case when rec_type = 'c' then 1 else 0 end),
  5. camt = sum(case when rec_type = 'c then amt else 0.00 end),
  6. from ctable
  7. where date = @dateparm and isnull(rec_type,'') in ('c','v')
  8. group by entity
  9.  
Happy Coding!!!

~~ CK
May 17 '10 #2
SELECT V.entity, V.vcnt, V.vamt, C.entity, C.ccnt, C.camt
FROM (SELECT entity, COUNT(*) AS vcnt, SUM(amt) AS vamt FROM vtable WHERE date = @dateparm AND rec_type = 'V' GROUP BY entity) AS V
LEFT OUTER JOIN
(SELECT entity, isnull(COUNT(*),0) AS ccnt, isnull(SUM(amt),0) AS camt FROM ctable WHERE date = @dateparm AND rec_type = 'C' GROUP BY entity) AS C
ON C.entity = V.entity
May 24 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: JT | last post by:
how can i see a stored procedures return value in ASP??
4
by: bala | last post by:
hi gurus have a employee table which consist of the normal employee related fields which the fields empid and supervisor mainly related to this question. have to create a single (the question...
2
by: Mike P | last post by:
I have a SQL Server stored procedure that is returning a 0 if the execution was OK and 1 if not. e.g. : commit trans return 0 on_error: rollback tran
3
by: neil_pat | last post by:
I have a stored procedure which returns a value of between 0 and 4. I want the user to press a button to receive feed back on their last input. The save button takes the input and saves it to...
5
by: Sandy | last post by:
Hello - I need a good example of how to take a return value from a stored procedure and use it in vb code. I have an app that searches a database by city and state. If a user makes a typo, the...
3
by: Adam Knight | last post by:
Hi all, I am wanting to get a stored procedure return value via ADO.NET.. Code: Below:
2
by: Alan T | last post by:
I have a SP: CREATE PROCEDURE dbo.users_name as select name from users order by name Here's my C# code: public string GetUserNames()
2
by: philip | last post by:
hello, i am new to asp.net and sql server, and i have 3 questions for asking: 1. i am writing a store procedure of login validation for my asp.net application and wondering what the different...
1
by: mktilu | last post by:
i want a stored procedure example where it returns both cursor and recordcount as a paramater .
1
by: mazdotnet | last post by:
Hi guys, I can't figure out why this is not working? I need to display all the rows for a given query for a given page index (ex. row 10..20) and the total number of rows. I got the first part...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.