Solved

Is there someway I can return this in a single query as a single dataset?

Posted on 2013-05-13
13
240 Views
Last Modified: 2013-05-13
Select count(ColumnA),ColumnB,ColumnC
From Table A
Where dateA > '01/01/2012'

AND

Select count(ColumnA),ColumnB,ColumnC    
From TableA
0
Comment
Question by:rhservan
  • 5
  • 4
  • 4
13 Comments
 
LVL 25

Expert Comment

by:Ron Malmstead
ID: 39162128
Select count(ColumnA),ColumnB,ColumnC
From TableA A
Where dateA > '01/01/2012'

UNION ALL

Select count(ColumnA),ColumnB,ColumnC    
From TableA  B
0
 

Author Comment

by:rhservan
ID: 39162177
The count column in each query needs to remain as separate count columns.  Of course the difference being is one has the date filter the other does not.
0
 
LVL 30

Expert Comment

by:hnasr
ID: 39162252
Modifying your query:

Select count(ColumnA),ColumnB,ColumnC
From Table A
Where dateA > '01/01/2012'
GROUP BY ColumnB, ColumnC
Union
Select count(ColumnA),ColumnB,ColumnC    
From TableA
GROUP BY ColumnB, ColumnC
0
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
LVL 30

Expert Comment

by:hnasr
ID: 39162256
"The count column in each query needs to remain as separate count columns. "
What do you mean? Give example.
0
 

Author Comment

by:rhservan
ID: 39162280
What I really need is this:

ColumnA1  ColumnA2   ColumnB    ColumnC

I need to maintain the two different count columns.  If I use UNION or UNION ALL - won't the counts just be merged?
0
 
LVL 25

Expert Comment

by:Ron Malmstead
ID: 39162297
Try this..
Let me know if this is what you're after.

Select count(ColumnA) As Table1Count,'' as Table2Count,ColumnB,ColumnC
From TableA A
Where dateA > '01/01/2012'

UNION ALL

Select '' as Table1Count, count(ColumnA) as Table2Count,ColumnB,ColumnC    
From TableA  B
0
 
LVL 25

Accepted Solution

by:
Ron Malmstead earned 500 total points
ID: 39162309
Or...

Select count(ColumnA),TotalCount=(SELECT Count(ColumnA) FROM TableA),ColumnB,ColumnC
From TableA A
Where dateA > '01/01/2012'
0
 

Author Comment

by:rhservan
ID: 39162327
ColumnA1&A2 are the same column with just the date filter difference.
I need ColumnA1 count based on the date filter
I need ColumnA2 count based on without date filter

ColumnA1   ColumnA2    
   500              10000
0
 
LVL 25

Expert Comment

by:Ron Malmstead
ID: 39162337
right,...try my last suggestion.
That should work.

Select count(ColumnA) as ColumnA1,ColumnA2=(SELECT Count(ColumnA) FROM TableA),ColumnB,ColumnC
From TableA A
Where dateA > '01/01/2012'
0
 
LVL 30

Expert Comment

by:hnasr
ID: 39162522
This will be a repeating column values, right?
List few records and the required output.

Select (Select count(ColumnA),ColumnB,ColumnC
From Table A Where dateA > '01/01/2012') As ColumnA ,
                   (Select count(ColumnA),ColumnB,ColumnC
From Table A) As ColumnA2,
                   ColumnB,ColumnC
From Table A
0
 

Author Comment

by:rhservan
ID: 39162533
You are correct in that it gives the Total count for ColumnA2, but It doesn't breakout by name as in ColumnA1, see below:

ColumnB      ColumnA1        ColumnA2
Name1                 6                       540
Name2                 16                     540
Name3                 3                       540
Name4                 14                     540
Name5                  1                      540

So ColumnA2 doesn't appear to be using the main query portion for other things.
0
 
LVL 30

Expert Comment

by:hnasr
ID: 39162553
What is the raw data

ColumnA ColumnB
----               Name1
----                ----
To produce the above shown result?
0
 

Author Comment

by:rhservan
ID: 39162600
Raw Data
ColumnB - Full Name
ColumnA1 - interaction dates  
ColumnA2 - interaction dates
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question