Solved

SqL help

Posted on 2014-02-18
9
223 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
  • 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
 

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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

920 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now