473,767 Members | 2,131 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

group by clause Query help

This is my query

select ano,max(date),a _subject from MY_TAB where table_name='xyz ' and
ano=877
group by a_subject,ano order by a_subject

ANO max(Date) A_Subject
877 2005-01-20 00:00:00.000 Subject_1
877 1900-01-01 00:00:00.000 Subject_2
877 2004-12-20 00:00:00.000 Subject_3
877 2005-01-19 00:00:00.000 Subject_4
--------------------------------------------------------------------------
When I put the status column in, it fetches all the rows.

select ano,max(date),a _subject,status from MY_TAB where
table_name='xyz ' and ano=877 group by a_subject,ano,s tatus order by
a_subject

ANO max(Date) A_Subject Status
877 2005-01-20 00:00:00.000 Subject_1 Not Started
877 1900-01-01 00:00:00.000 Subject_2 Not Started
877 2004-12-20 00:00:00.000 Subject_3 Completed
877 1900-01-01 00:00:00.000 Subject_3 Not Started
877 1900-01-01 00:00:00.000 Subject_4 Not Started
877 2005-01-19 00:00:00.000 Subject_4 Not Started
-----------------------------------------------------------------------
now what i want is

ANO max(Date) A_Subject Status
877 2005-01-20 00:00:00.000 Subject_1 Not Started
877 1900-01-01 00:00:00.000 Subject_2 Not Started
877 2004-12-20 00:00:00.000 Subject_3 Completed
877 2005-01-19 00:00:00.000 Subject_4 Not Started
Thanks a lot for your help.

AJ

Jul 23 '05 #1
2 2362
[posted and mailed, please reply in news]

(aj*****@hotmai l.com) writes:
select ano,max(date),a _subject from MY_TAB where table_name='xyz ' and
ano=877
group by a_subject,ano order by a_subject

ANO max(Date) A_Subject
877 2005-01-20 00:00:00.000 Subject_1
877 1900-01-01 00:00:00.000 Subject_2
877 2004-12-20 00:00:00.000 Subject_3
877 2005-01-19 00:00:00.000 Subject_4
--------------------------------------------------------------------------
When I put the status column in, it fetches all the rows.

select ano,max(date),a _subject,status from MY_TAB where
table_name='xyz ' and ano=877 group by a_subject,ano,s tatus order by
a_subject

ANO max(Date) A_Subject Status
877 2005-01-20 00:00:00.000 Subject_1 Not Started
877 1900-01-01 00:00:00.000 Subject_2 Not Started
877 2004-12-20 00:00:00.000 Subject_3 Completed
877 1900-01-01 00:00:00.000 Subject_3 Not Started
877 1900-01-01 00:00:00.000 Subject_4 Not Started
877 2005-01-19 00:00:00.000 Subject_4 Not Started
-----------------------------------------------------------------------
now what i want is

ANO max(Date) A_Subject Status
877 2005-01-20 00:00:00.000 Subject_1 Not Started
877 1900-01-01 00:00:00.000 Subject_2 Not Started
877 2004-12-20 00:00:00.000 Subject_3 Completed
877 2005-01-19 00:00:00.000 Subject_4 Not Started
Thanks a lot for your help.


With the example you have given, you would get away with:

select ano,max(date),a _subject, MIN(status)
from MY_TAB
where table_name='xyz ' and ano=877
group by a_subject,ano
order by a_subject

But this does not work well, if you have Status values like "Anticipati ng"
which represents intermediate stages.

If you are looking for the status on the selected date, then this might be
better:

SELECT a.ano, a.maxdate, a.a_subject, b.status
FROM (select ano, maxdate, max(date), a_subject
from MY_TAB
where table_name='xyz ' and ano=877
group by a_subject, ano) AS b
JOIN MY_TAB b ON a.ano = b.ano
AND a.maxdate = b.date
AND a.a_subject = b.a_subject

This assumes that ano/a_subject/date constitutes some form of key in
the table.
--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #2
Here is how a SELECT works in SQL ... at least in theory. Real
products will optimize things when they can.

a) Start in the FROM clause and build a working table from all of the
joins, unions, intersections, and whatever other table constructors are
there. The table expression> AS <correlation name> option allows you
give a name to this working table which you then have to use for the
rest of the containing query.

b) Go to the WHERE clause and remove rows that do not pass criteria;
that is, that do not test to TRUE (reject UNKNOWN and FALSE). The
WHERE clause is applied to the working set in the FROM clause.

c) Go to the optional GROUP BY clause, make groups and reduce each
group to a single row, replacing the original working table with the
new grouped table. The rows of a grouped table must be group
characteristics : (1) a grouping column (2) a statistic about the group
(i.e. aggregate functions) (3) a function or (4) an expression made up
those three items.

d) Go to the optional HAVING clause and apply it against the grouped
working table; if there was no GROUP BY clause, treat the entire table
as one group.

e) Go to the SELECT clause and construct the expressions in the list.
This means that the scalar subqueries, function calls and expressions
in the SELECT are done after all the other clauses are done. The
"AS" operator can also give names to expressions in the SELECT
list. These new names come into existence all at once, but after the
WHERE clause, GROUP BY clause and HAVING clause has been executed; you
cannot use them in the SELECT list or the WHERE clause for that reason.
If there is a SELECT DISTINCT, then redundant duplicate rows are
removed. For purposes of defining a duplicate row, NULLs are treated
as matching (just like in the GROUP BY).

f) Nested query expressions follow the usual scoping rules you would
expect from a block structured language like C, Pascal, Algol, etc.
Namely, the innermost queries can reference columns and tables in the
queries in which they are contained.

g) The ORDER BY clause is part of a cursor, not a query. The result
set is passed to the cursor, which can only see the names in the SELECT
clause list, and the sorting is done there. The ORDER BY clause cannot
have expression in it, or references to other columns because the
result set has been converted into a sequential file structure and that
is what is being sorted.

As you can see, things happen "all at once" in SQL, not from left to
right as they would in a sequential file/proceudral language model. In
those languages, these two statements produce different results:
READ (a, b, c) FROM File_X;
READ (c, a, b) FROM File_X;

while these two statements return the same data:

SELECT a, b, c FROM Table_X;
SELECT c, a, b FROM Table_X;

Think about what a confused mess this statement is in the SQL model.

SELECT f(c2) AS c1, f(c1) AS c2 FROM Foobar;
That is why such nonsense is illegal syntax.

Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
14052
by: Randell D. | last post by:
Folks, I have a SELECT that returns with multiple records - It works when I have a LIMIT clause but when I try to include a GROUP BY clause, the select returns nothing (ie no records, no errors). The GROUP BY specifies a column that does exist in my db (and is also the only field that is select from the db). Where am I going wrong? Because my select uses hashes (which I provide with
3
17374
by: Robby McGehee | last post by:
I need this to work: SELECT FROM WITH (NOLOCK) where ='a' GROUP BY , HAVING COUNT () > 1 The problem is that I get an error that needs to be in the GROUP BY clause or aggregate function. if I put it in there, I will get no duplicates (because it is the identity field). The whole point of this is to find dups. Thanks for any help.
4
28824
by: Chad Richardson | last post by:
I've always been mistified why you can't use a column alias in the group by clause (i.e. you have to re-iterate the entire expression in the group by clause after having already done it once in the select statement). I'm mostly a SQL hobbiest, so it's possible that I am not doing this in the most efficient manner. Anyone care to comment on this with relation to the following example (is there a way to acheive this without re-stating the...
2
3091
by: Paxton | last post by:
Hi, I'm trying to display the total page views per page within a given date range, but the correct SQL is seemingly beyond me. I get the correct result with a straightforward Group By and Count clause eg SELECT DISTINCT tblPageViews.PageVisited, Count(tblPageViews.PageVisited) AS CountOfPageVisited FROM tblPageViews GROUP BY tblPageViews.PageVisited;
2
5988
by: JJA | last post by:
Please advise on how to get the GROUP BY coded in an acceptable way: DECLARE @LO INT DECLARE @HI INT DECLARE @StartDate varchar(10) DECLARE @EndDate varchar(10) SELECT @StartDate = '01/01/2005' SELECT @EndDate = '06/30/2005' SELECT @LO = 250
4
3754
by: eliffman | last post by:
I get the following error when trying to run a report: Multi-level GROUP BY clause is not allowed in a subquery. (Error 3612) The report's recordsource is a query based on a single table. However, one of the fields in the query is a subquery. This field is also used in the calculations for a few other fields in the query. I'm able to run the report with no group levels. But the addition of even one group level causes the error.
12
25300
by: Bill Moran | last post by:
Hey all. I've hit an SQL problem that I'm a bit mystified by. I have two different questions regarding this problem: why? and how do I work around it? The following query: SELECT GCP.id, GCP.Name FROM Gov_Capital_Project GCP,
2
2119
by: Lorenzo Thurman | last post by:
I'm using PHP 5 to make an ODBC connection to an MS Access database using PEAR and I'm getting DB Error: Syntax Error when using the GROUP BY clause. Here's my query: SELECT * FROM HardwareInstallationsSummary WHERE LocationID = $loc_ID GROUP BY EquipmentTypeID Just for kicks, I changed the GROUP BY to ORDER BY. The query works, and the items are sorted as expected, but this is not what I want. Is there a bug in here somewhere? TIA
6
12175
by: MM | last post by:
Hello users. I have a query that I can run and see the result on the grid on design time, but when i run the program i get the folowing message:ORA-00979: not a GROUP BY expression. When I run the same query with out parameters it runs and it was runing well before in Oracle 8.Also If I don't use the GROUP BY function but with the parameter, it works also well ?What is the problem? The query is:select t1.id,t2.nad,max(t2.name) as namefrom...
0
9571
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9405
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10169
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10013
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8838
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7383
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6655
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5280
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3930
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.