I have a query here which works without the GROUP BY clause - 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]
-
FROM qryAlarmLog AS A
-
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: - 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: - SELECT A.USERNAME, A.SC
-
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]
-
FROM qryAlarmLog AS A
-
WHERE A.Date >= (Date()-240)) A
-
GROUP BY A.USERNAME, A.SC;
But this cause another error : Invalid Procedure Call
Any help would be greatly appreciate.
OK try this ... -
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
Try this ... - 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]
-
FROM qryAlarmLog AS A
-
WHERE A.Date >= (Date()-240)
-
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
Still having the Invalid procedure call...is it because the source is a query, so there's some limitations to the groupings?
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: - 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]
-
FROM qryAlarmLog AS A
-
WHERE A.Date >= (Date()-240)
-
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.
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.
If you create this query without the group by .. - 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]
-
FROM qryAlarmLog AS A
-
WHERE A.Date >= (Date()-240)
Assuming we call this Query1 then create another query. - SELECT Username, SC
-
FROM Query1
-
GROUP BY Username, SC
Does that work?
Nope....I want to slam that error message....
LOL, I know that feeling.
You said the query works fine when you don't add the group by?
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.
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..
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?
Yes of course, so do you suggest I should save those results into a table and group them from that table?
@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?
@MMcCarthy
Yes the number is always behind the word 'user', but the numbers are 2 to 4 digits long which is not stable.
@Mary,
I just learned something. Thank you.
OK try this one ... -
LEFT(RIGHT(A.ALARMDETAILS, InstrRev (A.ALARMDETAILS, "User", -1, vbTextCompare)+5),LEN(RIGHT(A.ALARMDETAILS, InstrRev (A.ALARMDETAILS, "User", -1, vbTextCompare)+5))-1)
-
What's this vbTextCompare, it asked me to enter a parameter for it.
Just leave it out, it's optional
Without the vbTextCompare -
LEFT(RIGHT(A.ALARMDETAILS, InstrRev (A.ALARMDETAILS, "User", -1)+5),LEN(RIGHT(A.ALARMDETAILS, InstrRev (A.ALARMDETAILS, "User", -1)+5))-1)
-
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
OK let me play with it for a bit. Give me 10 mins.
OK try this ... -
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.
Works great with charm~ many thanks for your help Mary =D
You're welcome, glad we figured it out.
Mary
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
...
|
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 ...
|
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 ...
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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
|
by: phpgurullc |
last post by:
i have one table say A
+------------+--------------+----------------+
| value1 | value 2 | value 3 |
+------------+--------------+----------------+
| 5 | 4adb83c1d71b | 1 |
| 5 | 44fcb93589fe...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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...
|
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...
| |