?
Solved

SqL help

Posted on 2014-02-18
9
Medium Priority
?
230 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 93

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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 

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 93

Accepted Solution

by:
Patrick Matthews earned 2000 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

Amazon Web Services EC2 Cheat Sheet

AWS EC2 is a core part of AWS’s cloud platform, allowing users to spin up virtual machines for a variety of tasks; however, EC2’s offerings can be overwhelming. Learn the basics with our new AWS cheat sheet – this time on EC2!

Question has a verified solution.

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

We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

718 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