Solved

SqL help

Posted on 2014-02-18
9
227 Views
Last Modified: 2014-02-20
Hi,

I have this query in access


SELECT AUD_MOS_IVR_LOG.DATA_1, Count(*) AS [Count]
FROM AUD_MOS_IVR_LOG
WHERE (((AUD_MOS_IVR_LOG.DATE_AND_TIME_STAMP)>=#2/17/2014# And (AUD_MOS_IVR_LOG.DATE_AND_TIME_STAMP)<=#2/18/2014#) AND ((AUD_MOS_IVR_LOG.RECORD_TYPE)='CB'))
GROUP BY AUD_MOS_IVR_LOG.DATA_1;


the output is like this:

Data_1                    count
8882881718      76
6583      11
8008434876      495
8004229146      1953
4583      78
1530      1
8776712583      713
8777902583      2768
8773542583      952
8005437765      39
3583      9
5583      95


what I want is modify the query to say
if Data_1 = 4583 and 1530 then add the count
if Data_1 = 6583 and 5583 then add the count

Thank you
Lulu
0
Comment
Question by:lulu50
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 2
9 Comments
 
LVL 26

Expert Comment

by:jerryb30
ID: 39868542
Can you show what you want to see? Do same criteria apply?
0
 

Author Comment

by:lulu50
ID: 39868570
yes, sorry

the output that I should see is this
Data_1                Date               count
8882881718      02-17-2014      76      
8008434876      02-17-2014      495      
8005437765      02-17-2014      39      
8773542583      02-17-2014      963      
8776712583      02-17-2014      800      
8777902583      02-17-2014      2863      
8004229146      02-17-2014      1954


add the count with combination of '1718' and '8882881718'
add the count with combination of '4876' and '4877' and '8008434876'
add the count with combination of '7765' and '7766' and '8005437765'
add the count with combination of '6583' and '6584' and '8773542583'
      

the output that I  have right now when I run my query is this:

Data_1         Count
8882881718      76
6583      11
8008434876      495
8004229146      1953
4583      78
1530      1
8776712583      713
8777902583      2768
8773542583      952
8005437765      39
3583      9
5583      95
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 39868591
The best way to do this would be to have another table that shows how the different Data_1 values should be "bucketed".

For example...

tblBucketing

Data_1   Bucket
1718     8882881718
8882881718     8882881718
4876     8008434876
4877     8008434876
8008434876     8008434876
7765     8005437765
7766     8005437765
8005437765     8005437765
6583     8773542583
6584     8773542583
8773542583     8773542583
et cetera

Now, your query becomes:

SELECT b.Bucket, Count(*) AS [Count] 
FROM AUD_MOS_IVR_LOG l INNER JOIN
    tblBucketing b ON l.Data_1 = b.Data_1
WHERE l.DATE_AND_TIME_STAMP >= #2/17/2014# And 
    l.DATE_AND_TIME_STAMP <= #2/18/2014# AND 
    l.RECORD_TYPE = 'CB'
GROUP BY b.Bucket;

Open in new window


If you also want to group on the date, then include that in the SELECT and GROUP BY clauses.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:lulu50
ID: 39868652
I made some changes and insert my query result to a table

the output is like this

ID      Data_1      Count      CreatedDate
2      8882881718      76      2/17/2014
3      6583      11      2/17/2014
4      8008434876      495      2/17/2014
5      8004229146      1953      2/17/2014
6      4583      78      2/17/2014
7      1530      1      2/17/2014
8      8776712583      713      2/17/2014
9      8777902583      2768      2/17/2014
10      8773542583      952      2/17/2014
11      8005437765      39      2/17/2014
12      3583      9      2/17/2014
13      5583      95      2/17/2014


Now I need to do case some case statement

to get this output

Data_1                   Date             Call Count      
8882881718      02-17-2014      76            
8008434876      02-17-2014      495            
8005437765      02-17-2014      39      
8773542583      02-17-2014      963            
8776712583      02-17-2014      800            
8777902583      02-17-2014      2863      
8004229146      02-17-2014      1954
0
 

Author Comment

by:lulu50
ID: 39868659
the count is done by combining count

8882881718 = 1718 + 8882881718
8008434876 = 4876 + 4877 + 8008434876
8005437765 = 7765 + 7766 + 8005437765
8773542583 = 6583 + 6584 + 8773542583
0
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 500 total points
ID: 39868740
lulu50,

Did you attempt what I recommended above?  It will do exactly what you are asking for.

Now, if you insist, you can do this formulaically, but IMHO it is a terrible idea.  Here is how:

SELECT
    Switch(l.DATA_1 In (1718, 8882881718), 8882881718,
        l.DATA_1 In (4876, 4877, 8008434876), 8008434876,
        l.DATA_1 In (7765, 7766, 8005437765), 8005437765,
        l.DATA_1 In (6583, 6584, 8773542583), 8773542583
        True, l.DATA_1) AS Data_1,
    Count(*) AS [Count], 
    l.CreatedDate
FROM AUD_MOS_IVR_LOG l
WHERE l.DATE_AND_TIME_STAMP >= #2/17/2014# And 
    l.DATE_AND_TIME_STAMP <= #2/18/2014# AND 
    l.RECORD_TYPE = 'CB'
GROUP BY l.DATA_1, l.CreatedDate;

Open in new window


For more about Switch, please see my article on it, which includes an example somewhat similar to this.

Patrick
0
 

Author Comment

by:lulu50
ID: 39870182
Patrick,

Thank you so much for all your help.

the switch statement is what I am looking for.  

I am close to the correct result but not there yet.  

SELECT SWITCH(AUD_MOS_IVR_LOG.DATA_1 In ('1718','8882881718'),'8882881718',
AUD_MOS_IVR_LOG.DATA_1 In ('4876','4877','8008434876'),'8008434876',
AUD_MOS_IVR_LOG.DATA_1 In ('7765','7766','8005437765'),'8005437765',
AUD_MOS_IVR_LOG.DATA_1 In ('6583','6584','8773542583'),'8773542583',
AUD_MOS_IVR_LOG.DATA_1 In ('3583','3584','8776712583','4583','4584','8774572583'),'8776712583',
AUD_MOS_IVR_LOG.DATA_1 In ('5583','5584','8777902583'),'8777902583',
AUD_MOS_IVR_LOG.DATA_1 In ('1530','1531','1532','8004229146'),'8004229146',
True,'Error') AS DATA_1, Count(*) AS [Count]
FROM AUD_MOS_IVR_LOG
WHERE (((AUD_MOS_IVR_LOG.DATE_AND_TIME_STAMP)>=#2/18/2014# And (AUD_MOS_IVR_LOG.DATE_AND_TIME_STAMP)<=#2/19/2014#) AND ((AUD_MOS_IVR_LOG.RECORD_TYPE)='CB'))
GROUP BY AUD_MOS_IVR_LOG.DATA_1;


This is the output of my query result:

DATA_1      Count
8882881718      60
8773542583      8
8008434876      540
8004229146      1880
8776712583      65
8004229146      1
8776712583      657
8773542583      954
8777902583      2651
8005437765      39
8776712583      9
8777902583      83

what I want is this output.
It should sum up the count based on the grouping of the DATA_1

8882881718      60      
8008434876      540      
8005437765      39      
8773542583      962      
8776712583      731      
8777902583      2734      
8004229146      1881      

Patrick thank you again I am very close to the correct result.
0
 

Author Comment

by:lulu50
ID: 39873483
ah I finally got it
lol

Thanks, Patrick!!!!
0
 

Author Closing Comment

by:lulu50
ID: 39873487
Thanks,
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

733 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