Solved

Order by

Posted on 2014-02-27
10
386 Views
Last Modified: 2014-03-04
Hi,

I have an output like this:

DATA_1      Counts      CreatedDate
8004229146      2154      2/25/2014
8005437765      53      2/25/2014
8008434876      638      2/25/2014
8773542583      992      2/25/2014
8776712583      963      2/25/2014
8777902583      3025      2/25/2014
8882881718      78      2/25/2014


but what I want is to have them listed in a certain order

like this
DATA_1      Counts      CreatedDate
8882881718      78      2/25/2014
8008434876      638      2/25/2014
8005437765      53      2/25/2014
8773542583      992      2/25/2014
8776712583      963      2/25/2014
8777902583      3025      2/25/2014
8004229146      2154      2/25/2014

The order by I think needs to have some case statement?
to have them listed in a certain order.

my Query is this:

SELECT DATA_1.DATA_1, Sum(DATA_1.Count) AS Counts, #2/25/2014# AS CreatedDate
FROM (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/25/2014#
And (AUD_MOS_IVR_LOG.DATE_AND_TIME_STAMP)<=#2/26/2014#) And
((AUD_MOS_IVR_LOG.RECORD_TYPE)='CB')) GROUP BY AUD_MOS_IVR_LOG.DATA_1)  AS DATA_1
GROUP BY DATA_1.DATA_1
ORDER BY DATA_1.DATA_1;


Thank you for all your help,
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
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39891839
can you please explain what this "order" is, as none of the 3 columns data seems to be ordered?

I presume you will need a "table" that defines which "DATA_1" will be displayed in which order, and join to that table, and order by that table's data.

create and fill such a table:
create table DATA_1_ORDER ( DATA_1 varchar(20), display_order int )
INSERT INTO DATA_1_ORDER ( DATA_1 , display_order   ) VALUES ( 8882881718    , 1 ) INSERT INTO DATA_1_ORDER ( DATA_1 , display_order   ) VALUES ( 8008434876    ,  2 )
INSERT INTO DATA_1_ORDER ( DATA_1 , display_order   ) VALUES ( 8005437765    ,  3 )
INSERT INTO DATA_1_ORDER ( DATA_1 , display_order   ) VALUES ( 8773542583     ,  4 )
INSERT INTO DATA_1_ORDER ( DATA_1 , display_order   ) VALUES ( 8776712583      ,  5 )
INSERT INTO DATA_1_ORDER ( DATA_1 , display_order   ) VALUES ( 8777902583      ,  6 )
INSERT INTO DATA_1_ORDER ( DATA_1 , display_order   ) VALUES ( 8004229146     ,  7 ) 

Open in new window


and your query becomes:
SELECT sq.DATA_1, sq.Counts, sq.CreatedDate
FROM (
SELECT DATA_1.DATA_1, Sum(DATA_1.Count) AS Counts, #2/25/2014# AS CreatedDate
FROM (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/25/2014#
And (AUD_MOS_IVR_LOG.DATE_AND_TIME_STAMP)<=#2/26/2014#) And
((AUD_MOS_IVR_LOG.RECORD_TYPE)='CB')) GROUP BY AUD_MOS_IVR_LOG.DATA_1)  AS DATA_1
GROUP BY DATA_1.DATA_1 
 ) sq
JOIN DATA_1_ORDER do 
  ON do.DATA_1 = sq.DATA_1
ORDER BY do.display_order  

Open in new window

0
 
LVL 11

Accepted Solution

by:
John_Vidmar earned 250 total points
ID: 39891855
Create a table that explicitly has the datum related to a sort-order, and sort by that field:
SELECT	a.*
FROM	(	SELECT DATA_1.DATA_1, Sum(DATA_1.Count) AS Counts, #2/25/2014# AS CreatedDate
		FROM (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/25/2014#
		And (AUD_MOS_IVR_LOG.DATE_AND_TIME_STAMP)<=#2/26/2014#) And 
		((AUD_MOS_IVR_LOG.RECORD_TYPE)='CB')) GROUP BY AUD_MOS_IVR_LOG.DATA_1)  AS DATA_1
		GROUP BY DATA_1.DATA_1
	) a
JOIN	(	select	DATA_1 = '8882881718'
		,	SortOrder = 1
		union all select '8008434876',2
		union all select '8005437765',3
		union all select '8773542583',4
		union all select '8776712583',5
		union all select '8777902583',6
		union all select '8004229146',7
	) b	ON	a.DATA_1 = b.DATA_1
ORDER
BY	b.SortOrder

Open in new window

0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 39892133
In my article here on using the Switch function in Access, I specifically provide an example of using Switch to force a custom sort order.

Of course, I also point out there that, unless this is a one-off thing, you are better off maintaining your custom sort order in a table.
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

Author Comment

by:lulu50
ID: 39892504
John,

I have syntax error with the Join:

JOIN      (      select      DATA_1 = '8882881718'
            ,      SortOrder = 1
            union all select '8008434876',2
            union all select '8005437765',3
            union all select '8773542583',4
            union all select '8776712583',5
            union all select '8777902583',6
            union all select '8004229146',7
      ) b      ON      a.DATA_1 = b.DATA_1
ORDER
BY      b.SortOrder
0
 
LVL 11

Expert Comment

by:John_Vidmar
ID: 39892965
Looks like you are trying to place that syntax into Microsoft Access, I'm not sure of the limitations. I took your query and made it a nested-table, and joined that to a nested-table... this works in SQL Server.
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39894115
in ms access, you many need to put () around the condition:

 ON      (a.DATA_1 = b.DATA_1)
0
 

Author Comment

by:lulu50
ID: 39894679
I put () around the condition but still not working

it says syntax error

I need to convert John's Sql statement to access database syntax.
0
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 250 total points
ID: 39894688
in ms access, visibly the "inline views" syntax is  [  .... ]  and not ( ... )
SELECT	a.*
FROM	[	SELECT DATA_1.DATA_1, Sum(DATA_1.Count) AS Counts, #2/25/2014# AS CreatedDate
		FROM (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/25/2014#
		And (AUD_MOS_IVR_LOG.DATE_AND_TIME_STAMP)<=#2/26/2014#) And 
		((AUD_MOS_IVR_LOG.RECORD_TYPE)='CB')) GROUP BY AUD_MOS_IVR_LOG.DATA_1)  AS DATA_1
		GROUP BY DATA_1.DATA_1
	] a
JOIN	[	select	DATA_1 = '8882881718'
		,	SortOrder = 1
		union all select '8008434876',2
		union all select '8005437765',3
		union all select '8773542583',4
		union all select '8776712583',5
		union all select '8777902583',6
		union all select '8004229146',7
	] b	ON	a.DATA_1 = b.DATA_1
ORDER
BY	b.SortOrder 

Open in new window

0
 

Author Comment

by:lulu50
ID: 39895455
I tried it but I still have a syntax errorAccess
0
 

Author Closing Comment

by:lulu50
ID: 39903252
Thank you
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

696 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