473,414 Members | 1,621 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Query not accepting GROUP BY

255 100+
I have a query here which works without the GROUP BY clause

Expand|Select|Wrap|Line Numbers
  1. SELECT A.USERNAME, Mid(A.ALARMDETAILS,(InStr(1,A.ALARMDETAILS,"User",1)+4),(InStr(InStr(1,A.ALARMDETAILS,"User",1),A.ALARMDETAILS,")",1))-(InStr(1,A.ALARMDETAILS,"User",1)+4)) AS [SC]
  2. FROM qryAlarmLog AS A
  3. WHERE A.Date >= (Date()-240);
Generally, this query is getting the user names, and specific numbers called SC which are in a part of the ALARMDETAIL. Both fields are from the query qryAlarmLog, which the result it generates with many duplicate user names and SC. I tried GROUP BY so I can get rid of the duplicated records, but it pops the following error:
Expand|Select|Wrap|Line Numbers
  1. You tried to execute a query that does not include the specified expression 'Mid(A.AL.....<skipped>....)' as part of an aggregate function.
I also tried using DISTINCT function but its not working, and even putting this as a subquery like this:
Expand|Select|Wrap|Line Numbers
  1. SELECT A.USERNAME, A.SC
  2. FROM (SELECT A.USERNAME, Mid(A.ALARMDETAILS,(InStr(1,A.ALARMDETAILS,"User",1)+4),(InStr(InStr(1,A.ALARMDETAILS,"User",1),A.ALARMDETAILS,")",1))-(InStr(1,A.ALARMDETAILS,"User",1)+4)) AS [SC]
  3. FROM qryAlarmLog AS A
  4. WHERE A.Date >= (Date()-240)) A
  5. GROUP BY A.USERNAME, A.SC;
But this cause another error : Invalid Procedure Call

Any help would be greatly appreciate.
Sep 27 '10 #1

✓ answered by MMcCarthy

OK try this ...
Expand|Select|Wrap|Line Numbers
  1. Left(Right([ALARMDETAILS],Len([AlarmDetails])-InStrRev([ALARMDETAILS],"User",-1)-4),Len(Right([ALARMDETAILS],Len([AlarmDetails])-InStrRev([ALARMDETAILS],"User",-1)-4))-1)
Although it looks as long if not longer the fact that it's using InStrRev instead of InStr should make the query load less.

23 4217
MMcCarthy
14,534 Expert Mod 8TB
Try this ...

Expand|Select|Wrap|Line Numbers
  1. SELECT A.USERNAME, Mid(A.ALARMDETAILS,(InStr(1,A.ALARMDETAILS,"User",1)+4),(InStr(InStr(1,A.ALARMDETAILS,"User",1),A.ALARMDETAILS,")",1))-(InStr(1,A.ALARMDETAILS,"User",1)+4)) AS [SC] 
  2. FROM qryAlarmLog AS A 
  3. WHERE A.Date >= (Date()-240)
  4. GROUP BY A.USERNAME, Mid(A.ALARMDETAILS,(InStr(1,A.ALARMDETAILS,"User",1)+4),(InStr(InStr(1,A.ALARMDETAILS,"User",1),A.ALARMDETAILS,")",1))-(InStr(1,A.ALARMDETAILS,"User",1)+4))
Essentially Access doesn't recognise the Alias SC
Sep 28 '10 #2
colintis
255 100+
Still having the Invalid procedure call...is it because the source is a query, so there's some limitations to the groupings?
Sep 28 '10 #3
MMcCarthy
14,534 Expert Mod 8TB
I doubt it. I would guess it has more to do with the length of the SC field. Only other option is to run the query without the group by and then run a second query off it with a group by.

Try one other thing first, put the SC field in brackets as follows:

Expand|Select|Wrap|Line Numbers
  1. SELECT A.USERNAME, (Mid(A.ALARMDETAILS,(InStr(1,A.ALARMDETAILS,"User",1)+4),(InStr(InStr(1,A.ALARMDETAILS,"User",1),A.ALARMDETAILS,")",1))-(InStr(1,A.ALARMDETAILS,"User",1)+4))) AS [SC] 
  2. FROM qryAlarmLog AS A 
  3. WHERE A.Date >= (Date()-240)
  4. GROUP BY A.USERNAME, (Mid(A.ALARMDETAILS,(InStr(1,A.ALARMDETAILS,"User",1)+4),(InStr(InStr(1,A.ALARMDETAILS,"User",1),A.ALARMDETAILS,")",1))-(InStr(1,A.ALARMDETAILS,"User",1)+4)))
Both in the query and the group by.
Sep 28 '10 #4
colintis
255 100+
Still the same error message. I also just tried to put this SC to the query source qryAlarmLog, and extract it back in this query, but still showing the same error...

The ALARMDETAIL contains a long string (around 100~120 characters), which SC only looks for a particular part of this string field.
Sep 28 '10 #5
MMcCarthy
14,534 Expert Mod 8TB
If you create this query without the group by ..


Expand|Select|Wrap|Line Numbers
  1. SELECT A.USERNAME, (Mid(A.ALARMDETAILS,(InStr(1,A.ALARMDETAILS,"User",1)+4),(InStr(InStr(1,A.ALARMDETAILS,"User",1),A.ALARMDETAILS,")",1))-(InStr(1,A.ALARMDETAILS,"User",1)+4))) AS [SC] 
  2. FROM qryAlarmLog AS A 
  3. WHERE A.Date >= (Date()-240)
Assuming we call this Query1 then create another query.

Expand|Select|Wrap|Line Numbers
  1. SELECT Username, SC
  2. FROM Query1
  3. GROUP BY Username, SC
Does that work?
Sep 28 '10 #6
colintis
255 100+
Nope....I want to slam that error message....
Sep 28 '10 #7
MMcCarthy
14,534 Expert Mod 8TB
LOL, I know that feeling.

You said the query works fine when you don't add the group by?
Sep 28 '10 #8
MMcCarthy
14,534 Expert Mod 8TB
Can you tell me the logic of what you are trying to do with the SC field. Break it down into steps and lets see if we can reduce the size of it.
Sep 28 '10 #9
colintis
255 100+
Yes it works fine with the duplicating usernames and SCs.

The SC as seen is obtaining a part of data in the ALARMDETAILS that looks like this:

Chall 1 (RIGHT WING) Access Granted with Region: Door 777 (RECEPTION FLYER IN), Region 4 (region 4) (George Bush, User 218)

The 218 is SC, and the result of this query will shows a duplication of such SC numbers in >1k times to each users. This query is used by a form and a report to list the access record of users within a period of time. Or by user, whereas simply showing the SC on the form and generate another report. As this query will make the report listing duplicate user names so I need to group that up without wasting more papers..
Sep 28 '10 #10
Oralloy
988 Expert 512MB
I don't think you can use GROUP BY without specifing an aggrigate, like COUNT(*) among the selected fields.

Does that make sense to you?
Sep 28 '10 #11
colintis
255 100+
Yes of course, so do you suggest I should save those results into a table and group them from that table?
Sep 28 '10 #12
MMcCarthy
14,534 Expert Mod 8TB
@Orally

No you can use group by without an aggregate field as long as you group by all fields.

@Colintis
Is user always at the end of the field and is the number always 3 digits?
Sep 28 '10 #13
colintis
255 100+
@MMcCarthy
Yes the number is always behind the word 'user', but the numbers are 2 to 4 digits long which is not stable.
Sep 28 '10 #14
Oralloy
988 Expert 512MB
@Mary,

I just learned something. Thank you.
Sep 28 '10 #15
MMcCarthy
14,534 Expert Mod 8TB
OK try this one ...

Expand|Select|Wrap|Line Numbers
  1. LEFT(RIGHT(A.ALARMDETAILS, InstrRev (A.ALARMDETAILS, "User", -1, vbTextCompare)+5),LEN(RIGHT(A.ALARMDETAILS, InstrRev (A.ALARMDETAILS, "User", -1, vbTextCompare)+5))-1)
  2.  
Sep 28 '10 #16
colintis
255 100+
What's this vbTextCompare, it asked me to enter a parameter for it.
Sep 28 '10 #17
MMcCarthy
14,534 Expert Mod 8TB
Just leave it out, it's optional
Sep 28 '10 #18
MMcCarthy
14,534 Expert Mod 8TB
Without the vbTextCompare

Expand|Select|Wrap|Line Numbers
  1. LEFT(RIGHT(A.ALARMDETAILS, InstrRev (A.ALARMDETAILS, "User", -1)+5),LEN(RIGHT(A.ALARMDETAILS, InstrRev (A.ALARMDETAILS, "User", -1)+5))-1)
  2.  
Sep 28 '10 #19
colintis
255 100+
The qurey runs, but SC is showing back the whole line of ALARMDETAILS..

That's what it becomes
ll 1 (RIGHT WING) Access Granted with Region: Door 777 (RECEPTION FLYER IN), Region 4 (region 4) (George Bush, User 218
Sep 28 '10 #20
MMcCarthy
14,534 Expert Mod 8TB
OK let me play with it for a bit. Give me 10 mins.
Sep 28 '10 #21
MMcCarthy
14,534 Expert Mod 8TB
OK try this ...
Expand|Select|Wrap|Line Numbers
  1. Left(Right([ALARMDETAILS],Len([AlarmDetails])-InStrRev([ALARMDETAILS],"User",-1)-4),Len(Right([ALARMDETAILS],Len([AlarmDetails])-InStrRev([ALARMDETAILS],"User",-1)-4))-1)
Although it looks as long if not longer the fact that it's using InStrRev instead of InStr should make the query load less.
Sep 28 '10 #22
colintis
255 100+
Works great with charm~ many thanks for your help Mary =D
Sep 28 '10 #23
MMcCarthy
14,534 Expert Mod 8TB
You're welcome, glad we figured it out.

Mary
Sep 28 '10 #24

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: AJ | last post by:
Hi, I have a view in which I have 3 cols...(pno,ptno,diff)..diff is the difference in time in minutes.I want to calculate Median(diff) group by pno,ptno...using a sql query for SQL server... ...
9
by: cyrus.kapadia | last post by:
Let's say I have the following table: entry product quality 1 A 80 2 A 70 3 A 80 4 B 60 5 B 90 6 ...
3
by: roberto | last post by:
I have a table (ORDERS) with this kind structure (this is not the really table): OrderId Product Amount Agent1 Agent2 Agent3 ________ _______ ______ ______ ______ ______ 000001 ...
0
by: laurenquantrell | last post by:
I have a table with 6 million rows. I'm trying to perform a group by query that incudes a row count of the PK column: SELECT COUNT(DataID) AS Cnt, Column1Name FROM dbo.TableName WHERE...
0
by: miaminemo | last post by:
guys - Does anyone have the code lying around to query who all the members of a specific active directory global group are? I need a web service that I can pass GG$GlobalGroupName to and have...
4
by: rush2112 | last post by:
I have a table with 7 columns and 50 rows. The first column is a unique ID#, the other 6 columns are for names. (Name1, Name2, ......Name6). I need to create a 2 column query to extract the six...
4
by: SamBiju | last post by:
I have a query to retrieve few columns from different tables. select distinct sso.sales_order_nb, sso.shipment_id, ssh.tracking_nb, sta_container.box_number, ste.sales_organization_code,...
3
by: jackiefm | last post by:
I have a crosstab query that I need the returned records to be limited to the top 10 for each group. How can I accomplish this? My sql TRANSFORM Count(Violation.Driver) AS CountOfDriver SELECT...
4
by: viki1967 | last post by:
Query SQL GROUP BY Hello everyone. I have this problem with table of database MySQL. My table with fields ID, Number and Description: ID ====== NUMBER ======= DESCRIPTION
0
by: phpgurullc | last post by:
i have one table say A +------------+--------------+----------------+ | value1 | value 2 | value 3 | +------------+--------------+----------------+ | 5 | 4adb83c1d71b | 1 | | 5 | 44fcb93589fe...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.