A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Access » New Users
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

group by first three characters of a value



 
 
Thread Tools Display Modes
  #1  
Old July 10th, 2009, 11:13 PM posted to microsoft.public.access.gettingstarted
gls858
external usenet poster
 
Posts: 473
Default group by first three characters of a value

I have the following query where the Group By value
ECNHDetail.Department is a four digit value. Is is possible to change
this query to group by is based only on the first three characters of
the dept.

Example. I have depts 101a,101b,101c but I want all of the 101's group
together.

SELECT ECNHDetail.GLLocation, ECNHDetail.CustomerNumber,
ECNHDetail.Department, Sum(ECNHDetail.LineAvgCost) AS SumOfLineAvgCost,
Sum(ECNHDetail.LineAmount) AS SumOfLineAmount
FROM ECNHDetail
WHERE (((ECNHDetail.InvoiceDate) Between #1/1/2009# And #6/30/2009#))
GROUP BY ECNHDetail.GLLocation, ECNHDetail.CustomerNumber,
ECNHDetail.Department
HAVING (((ECNHDetail.GLLocation)=" 1") AND ((ECNHDetail.CustomerNumber)
Like "*3584"))
ORDER BY Sum(ECNHDetail.LineAmount) DESC;

gls858
  #2  
Old July 10th, 2009, 11:44 PM posted to microsoft.public.access.gettingstarted
KARL DEWEY
external usenet poster
 
Posts: 10,767
Default group by first three characters of a value

Does your GLLocation field really have leading spaces?
Try this --
SELECT ECNHDetail.GLLocation, ECNHDetail.CustomerNumber,
Left(ECNHDetail.Department, 3) AS DEPT, Sum(ECNHDetail.LineAvgCost) AS
SumOfLineAvgCost, Sum(ECNHDetail.LineAmount) AS SumOfLineAmount
FROM ECNHDetail
WHERE (((ECNHDetail.InvoiceDate) Between #1/1/2009# And #6/30/2009#))
GROUP BY ECNHDetail.GLLocation, ECNHDetail.CustomerNumber,
Left(ECNHDetail.Department, 3)
HAVING (((ECNHDetail.GLLocation)=" 1") AND ((ECNHDetail.CustomerNumber) Like
"*3584"))
ORDER BY Sum(ECNHDetail.LineAmount) DESC;

--
Build a little, test a little.


"gls858" wrote:

I have the following query where the Group By value
ECNHDetail.Department is a four digit value. Is is possible to change
this query to group by is based only on the first three characters of
the dept.

Example. I have depts 101a,101b,101c but I want all of the 101's group
together.

SELECT ECNHDetail.GLLocation, ECNHDetail.CustomerNumber,
ECNHDetail.Department, Sum(ECNHDetail.LineAvgCost) AS SumOfLineAvgCost,
Sum(ECNHDetail.LineAmount) AS SumOfLineAmount
FROM ECNHDetail
WHERE (((ECNHDetail.InvoiceDate) Between #1/1/2009# And #6/30/2009#))
GROUP BY ECNHDetail.GLLocation, ECNHDetail.CustomerNumber,
ECNHDetail.Department
HAVING (((ECNHDetail.GLLocation)=" 1") AND ((ECNHDetail.CustomerNumber)
Like "*3584"))
ORDER BY Sum(ECNHDetail.LineAmount) DESC;

gls858

  #3  
Old July 11th, 2009, 12:05 AM posted to microsoft.public.access.gettingstarted
John W. Vinson
external usenet poster
 
Posts: 18,261
Default group by first three characters of a value

On Fri, 10 Jul 2009 17:13:35 -0500, gls858 wrote:

I have the following query where the Group By value
ECNHDetail.Department is a four digit value. Is is possible to change
this query to group by is based only on the first three characters of
the dept.

Example. I have depts 101a,101b,101c but I want all of the 101's group
together.


Sure. Just use the Left() function:

SELECT ECNHDetail.GLLocation, ECNHDetail.CustomerNumber,
Left(ECNHDetail.Department, 3), Sum(ECNHDetail.LineAvgCost) AS
SumOfLineAvgCost,
Sum(ECNHDetail.LineAmount) AS SumOfLineAmount
FROM ECNHDetail
WHERE (((ECNHDetail.InvoiceDate) Between #1/1/2009# And #6/30/2009#))
GROUP BY ECNHDetail.GLLocation, ECNHDetail.CustomerNumber,
Left(ECNHDetail.Department, 3)
HAVING (((ECNHDetail.GLLocation)=" 1") AND ((ECNHDetail.CustomerNumber)
Like "*3584"))
ORDER BY Sum(ECNHDetail.LineAmount) DESC;

Since GLLocation and CustomerNumber are in the table rather than being
calculated as an aggregate, I'd move their query criteria from the HAVING
clause (which is applied AFTER all the calculations) into the WHERE (to limit
the number of records BEFORE doing the calculations:

SELECT ECNHDetail.GLLocation, ECNHDetail.CustomerNumber,
Left(ECNHDetail.Department, 3), Sum(ECNHDetail.LineAvgCost) AS
SumOfLineAvgCost,
Sum(ECNHDetail.LineAmount) AS SumOfLineAmount
FROM ECNHDetail
WHERE (((ECNHDetail.InvoiceDate) Between #1/1/2009# And #6/30/2009#))
AND (((ECNHDetail.GLLocation)=" 1")
AND ((ECNHDetail.CustomerNumber) Like "*3584"))
GROUP BY ECNHDetail.GLLocation, ECNHDetail.CustomerNumber,
Left(ECNHDetail.Department, 3)
ORDER BY Sum(ECNHDetail.LineAmount) DESC;
--

John W. Vinson [MVP]
  #4  
Old July 13th, 2009, 11:18 PM posted to microsoft.public.access.gettingstarted
gls858
external usenet poster
 
Posts: 473
Default group by first three characters of a value

Sorry for the delay. I posted this last Friday right before I left for
the week end.

Yes it does. It's an older flat file db that I'm importing and the field
is designed to hold 2 digits. Just one of many PITA's I have to deal
with in this data.

gls858



KARL DEWEY wrote:
Does your GLLocation field really have leading spaces?
Try this --
SELECT ECNHDetail.GLLocation, ECNHDetail.CustomerNumber,
Left(ECNHDetail.Department, 3) AS DEPT, Sum(ECNHDetail.LineAvgCost) AS
SumOfLineAvgCost, Sum(ECNHDetail.LineAmount) AS SumOfLineAmount
FROM ECNHDetail
WHERE (((ECNHDetail.InvoiceDate) Between #1/1/2009# And #6/30/2009#))
GROUP BY ECNHDetail.GLLocation, ECNHDetail.CustomerNumber,
Left(ECNHDetail.Department, 3)
HAVING (((ECNHDetail.GLLocation)=" 1") AND ((ECNHDetail.CustomerNumber) Like
"*3584"))
ORDER BY Sum(ECNHDetail.LineAmount) DESC;

  #5  
Old July 13th, 2009, 11:34 PM posted to microsoft.public.access.gettingstarted
gls858
external usenet poster
 
Posts: 473
Default group by first three characters of a value

John W. Vinson wrote:
On Fri, 10 Jul 2009 17:13:35 -0500, gls858 wrote:

I have the following query where the Group By value
ECNHDetail.Department is a four digit value. Is is possible to change
this query to group by is based only on the first three characters of
the dept.

Example. I have depts 101a,101b,101c but I want all of the 101's group
together.


Sure. Just use the Left() function:

SELECT ECNHDetail.GLLocation, ECNHDetail.CustomerNumber,
Left(ECNHDetail.Department, 3), Sum(ECNHDetail.LineAvgCost) AS
SumOfLineAvgCost,
Sum(ECNHDetail.LineAmount) AS SumOfLineAmount
FROM ECNHDetail
WHERE (((ECNHDetail.InvoiceDate) Between #1/1/2009# And #6/30/2009#))
GROUP BY ECNHDetail.GLLocation, ECNHDetail.CustomerNumber,
Left(ECNHDetail.Department, 3)
HAVING (((ECNHDetail.GLLocation)=" 1") AND ((ECNHDetail.CustomerNumber)
Like "*3584"))
ORDER BY Sum(ECNHDetail.LineAmount) DESC;

Since GLLocation and CustomerNumber are in the table rather than being
calculated as an aggregate, I'd move their query criteria from the HAVING
clause (which is applied AFTER all the calculations) into the WHERE (to limit
the number of records BEFORE doing the calculations:

SELECT ECNHDetail.GLLocation, ECNHDetail.CustomerNumber,
Left(ECNHDetail.Department, 3), Sum(ECNHDetail.LineAvgCost) AS
SumOfLineAvgCost,
Sum(ECNHDetail.LineAmount) AS SumOfLineAmount
FROM ECNHDetail
WHERE (((ECNHDetail.InvoiceDate) Between #1/1/2009# And #6/30/2009#))
AND (((ECNHDetail.GLLocation)=" 1")
AND ((ECNHDetail.CustomerNumber) Like "*3584"))
GROUP BY ECNHDetail.GLLocation, ECNHDetail.CustomerNumber,
Left(ECNHDetail.Department, 3)
ORDER BY Sum(ECNHDetail.LineAmount) DESC;


Thanks John. That worked. Simple when you know how. Appreciate the help.

gls858
 




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 06:45 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.