|
-
Dec 26th, 2004, 03:19 PM
#1
Thread Starter
Addicted Member
group by
i want to group the results of a querry according to a field called "Status",
i tried a few queries but the all dont work ! wat to do
-
Dec 26th, 2004, 03:44 PM
#2
Frenzied Member
Re: group by
Try ...
SELECT * from YourTable GROUP BY Status
.
-
Dec 26th, 2004, 03:45 PM
#3
Frenzied Member
Re: group by
Post any errors that you get should you get any.
-
Dec 26th, 2004, 04:10 PM
#4
Thread Starter
Addicted Member
Re: group by
well i tried this one earlier but it does'nt work. heres wat it says
if i try like "select * from tableName groupby status" it says can't group on fields select with '*'
now if i try to give column names in place of '*' it says that the mentioned column names are not part of an aggregate function.
im using MS Access.( if it makes some difference )
-
Dec 26th, 2004, 04:14 PM
#5
Frenzied Member
-
Dec 26th, 2004, 04:19 PM
#6
Frenzied Member
Re: group by
Daft question it may seem - but is 'status' a column in the table that you are referencing. Reason I ask is that I have had this before with in a table join situation.
Also - are there any relationships to the table. I'll bet there are.
-
Dec 26th, 2004, 04:25 PM
#7
Frenzied Member
Re: group by
Assuming that you have got relationships, and assuming theyare valid and the database is correctly normalised, you will have to add group by for ALL of the columns that you are returning in the recordset.
The first in th elist being the main grouping that you want.
here is what I had to do...
Code:
SELECT Prevac_Cycle.CycleRef,
Prevac_Cycle.TreatmentDate,
Prevac_Cycle.StartBSV,
Prevac_Cycle.FinBSV,
Prevac_Cycle.FinBSV as UsedBSV,
Prevac_Cycle.OperatorEstCubed as Cubed,
Prevac_Cycle.OperatorEstCubed as RunCubed,
Prevac_Cycle.WorknetEstCubed as LperM3,
CS_Previous.CycleID,
ProcessNotes.Bypass as Bypass,
ProcessNotes.EngNote1 as Engineering_Note1
FROM (Prevac_Cycle INNER JOIN ProcessNotes ON Prevac_Cycle.CycleRef = ProcessNotes.CycleRef) INNER JOIN CS_Previous ON Prevac_Cycle.CycleRef = CS_Previous.CycleRef
WHERE (Prevac_Cycle.TreatmentDate >= #startdate#)
AND (Prevac_Cycle.TreatmentDate <= #finishdate#)
GROUP BY Prevac_Cycle.CycleRef, CS_Previous.CycleID,
Prevac_Cycle.TreatmentDate, Prevac_Cycle.StartBSV, Prevac_Cycle.FinBSV, Prevac_Cycle.OperatorEstCubed,
Prevac_Cycle.WorknetEstCubed,ProcessNotes.Bypass, ProcessNotes.EngNote1
Prevac_Cycle.CycleRef is what I really wanted to group by. But I needed to include the rest to get around the error message you came up against
-
Dec 26th, 2004, 04:41 PM
#8
Thread Starter
Addicted Member
Re: group by
well first of all there are no relations to dat table, infact it doesnt have a primary key as well, n secondly if i try to group all the columns in the table it will return the complete table itself . ne other ideas ?
-
Dec 26th, 2004, 04:43 PM
#9
Re: group by
When you use GROUP BY you must GROUP BY each and every field that is in the SELECT statement...
Or...
Put the field in the SELECT statement within an AGGREGATE function - such as SUM, MAX, MIN...
So you can say:
SELECT STATUS,SUM(1) FROM TABLEX GROUP BY STATUS
or SELECT STATUS,MAX(SOMEDATEFIELD) FROM TABLEX GROUP BY STATUS
-
Dec 26th, 2004, 04:50 PM
#10
Frenzied Member
Re: group by
I think you need to use ORDER BY then. In the simple case you have it will have the same effect.
-
Dec 27th, 2004, 03:12 AM
#11
Thread Starter
Addicted Member
[Resolved]: group by
hi
actually i found the problem myself. i needed to group by the columns on status n then sort the columns on status as well. ( took me 2 hours )
-
Dec 27th, 2004, 04:59 AM
#12
Frenzied Member
Re: group by
Well done that man. It's a pain when your coding flow is halted by something that looks like it should be a no brainer though. We have all been there.
.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|