Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

SqL help

Posted on 2014-02-18
9
Medium Priority
?
232 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 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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 

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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Recursive SQL is one of the most fascinating and powerful and yet dangerous feature offered in many modern databases today using a Common Table Expression (CTE) first introduced in the ANSI SQL 99 standard. The first implementations of CTE began ap…
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…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

580 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