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

Home Posts Topics Members FAQ

SQL Statement help (GROUP BY)

I need this to work:
SELECT [ID] FROM [test] WITH (NOLOCK) where [a/c/d]='a' GROUP BY [unit
#],[EFF DATE] HAVING COUNT ([unit #]) > 1

The problem is that I get an error that [ID] 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.

Robby
Jul 20 '05 #1
3 17373
Hi

Maybe something like:

SELECT t.ID, t.[unit #]
FROM [TEST] t JOIN
( SELECT [unit #] FROM [test] WITH (NOLOCK) where [a/c/d]='a'
GROUP BY [unit #],[EFF DATE] HAVING COUNT ([unit #]) > 1 ) d ON d.[unit #] =
t.[unit #]
ORDER BY t.[unit #], t.ID

John

"Robby McGehee" <rm******@hotma il.com> wrote in message
news:6e******** *************** ***@posting.goo gle.com...
I need this to work:
SELECT [ID] FROM [test] WITH (NOLOCK) where [a/c/d]='a' GROUP BY [unit
#],[EFF DATE] HAVING COUNT ([unit #]) > 1

The problem is that I get an error that [ID] 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.

Robby

Jul 20 '05 #2
>> SELECT [ID] FROM [test] WITH (NOLOCK) where [a/c/d]='a' GROUP BY
[unit #],[EFF DATE] HAVING COUNT ([unit #]) > 1 <<

Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, datatypes, etc. in
your schema are.
The problem is that I get an error that [ID] 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 [sic]). <<

First of all, stop using IDENTITY as a key! That is almost certainly
how you got redundant duplicates in the first place. Read a book on
RDBMS and learn what a key is; you are imitating a sequential file in
SQL. Read a book on SQL, so your code will be closer to standard SQL,
too.
The whole point of this is to find dups. <<


SELECT MIN(id) AS "IDENTITY-caused screw up"
FROM Test
WHERE "a/c/d" = 'a'
GROUP BY "unit #", "eff date", ... -- rest of columns
HAVING COUNT ("unit #") > 1 ;

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 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
of the 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 give a name to expressions in the SELECT list, too.
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.
Jul 20 '05 #3
If you need it to remove any of the duplicates, then you could use the
MIN or MAX aggregate:

SELECT MIN(id) FROM Test ... GROUP BY "Unit #1","EFF Date" HAVING
COUNT(*)>1

Gert-Jan
Robby McGehee wrote:

I need this to work:
SELECT [ID] FROM [test] WITH (NOLOCK) where [a/c/d]='a' GROUP BY [unit
#],[EFF DATE] HAVING COUNT ([unit #]) > 1

The problem is that I get an error that [ID] 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.

Robby

Jul 20 '05 #4

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

Similar topics

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
2522
by: serge | last post by:
/* This is a long post. You can paste the whole message in the SQL Query Analyzer. I have a scenario where there are records with values pointing to wrong records and I need to fix them using an Update statement. I have a sample code to reproduce my problem. To simplify the scenario I am trying to use Order related tables to explain a little better the tables i have to work with.
7
11445
by: mark | last post by:
Access 2000: I creating a report that has a record source built by the user who selects the WHERE values. An example is: SELECT * FROM CHARGELOG WHERE STDATE Between #10/27/2003# And #11/2/2003# And VehicleID='00000000' And BattID='LKO500HF'. I need to use the records returned to populate text boxes, but the data requires further manipulation. I attempting to use expressions in the control source
2
2647
by: robert kurz | last post by:
hallo ng, i am trying to parse a sql-statement with regular expressions. my goal is to get the parts of the statement. in my thoughts the group-functionality of the regular expressions should do this. my pattern looks like: A) string strPattern = "(select.+)(from.+)(where.+)?(order.+)?";
1
3304
by: Jean-Marc Blaise | last post by:
Hi folks, In case the SQL statement is not written properly, we sometimes get strange messages. The 2 following statements are simple inversion between order by and group by clause. We get an "advice" about expected from or from_clause, why ? db2 select workdept, count(*) from employee order by 1 group by workdept SQL0104N An unexpected token "select workdept, count(*) from employ" was found following "BEGIN-OF-STATEMENT". Expected...
19
8383
by: Steve | last post by:
ASP error number 13 - Type mismatch with SELECT...FOR UPDATE statement I got ASP error number 13 when I use the SELECT...FOR UPDATE statement as below. However, if I use SELECT statement without FOR UPDATE, it is fine and no error. I also tried Set objRs = objConn.Execute("SELECT * FROM EMP UPDATE OF EMPNO"), but it still couldn't help. any ideas? I tried to search in the web but couldn't find similar
2
3179
by: bhanab | last post by:
Hello, Please can someone help me with this select statement? How do I get the statement to keep the week number static? ie if there are no values then it should show up as NULL, zero or just stay blank. This is the statment :-- Select a., b., c., d., e. From (SELECT Weeknum FROM .. Group by Weeknum)
2
544
by: iulian.ilea | last post by:
What is wrong int this SQL statement? select top 10 DOCInt.*, DOCDet.* , Cate.*, Arti.*, .* from DOCInt INNER JOIN DOCDet ON DOCInt.CodDoc=DOCDet.CodDoc LEFT JOIN Cate ON DOCDet.IDCategory=Cate. LEFT JOIN Arti ON DOCDet.IDArti=Arti. INNER JOIN ON DOCInt.IDAnag=. GROUP BY DOCInt.IDDoc
6
2136
by: Twobridge | last post by:
I hope someone can help me out with my problem. I have found a sql statement that basically pulls all bills filed within a certain time period and the payments made on those bills with in the same time period. I group the payments by payment year and filed year which gives me a matrix with the filed year as the row and the pay year as the column....and this appears fine. My problem is that my employer does not want to see the...
0
944
by: wbarona | last post by:
Hello everyone, Could you please provide me with some assistance on how to group by a SQL statement when a selected column comes from a subquery. Here is an example of the SQL statement I am trying to write. I don't know what to add to the group by command in order to make it work. Thanks for your help and guidance select sa.sa_type_cd, nvl((select bsegitem.item_cnt from ps_ci_bseg bseg,
0
9404
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
10168
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
10009
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...
1
7381
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
6651
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
5279
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...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3929
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
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.