Solved

Order by

Posted on 2014-02-27
10
383 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
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 142

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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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 142

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 142

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

813 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

9 Experts available now in Live!

Get 1:1 Help Now