Solved

SqL help

Posted on 2014-02-18
9
222 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
Comment Utility
Can you show what you want to see? Do same criteria apply?
0
 

Author Comment

by:lulu50
Comment Utility
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
Comment Utility
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
Comment Utility
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:lulu50
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
ah I finally got it
lol

Thanks, Patrick!!!!
0
 

Author Closing Comment

by:lulu50
Comment Utility
Thanks,
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
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.​
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
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…

763 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

11 Experts available now in Live!

Get 1:1 Help Now