Solved

select text field in query with aggregate function

Posted on 2004-08-02
3
1,832 Views
Last Modified: 2012-08-14
How do I include a text (or ntext) field in a query that uses an aggregate function

eg

SELECT textField, someOtherField, COUNT(anotherFieldAgain) AS Total
FROM tblTable
GROUP BY textField, someOtherField

In MS access I could use the FIRST or LAST function to do this (on a SQL Server back-end) - and not include it in the group by clause.  How do I do this in SQL Server?
0
Comment
Question by:cdhbs
  • 2
3 Comments
 
LVL 34

Assisted Solution

by:arbert
arbert earned 250 total points
ID: 11700769
The best you can really do is to do a cast/convert on it:


SELECT convert(varchar(8000),textField), someOtherField, COUNT(anotherFieldAgain) AS Total
FROM tblTable
GROUP BY convert(varchar(8000),textField), someOtherField


Of course, you loose anything over 8000 bytes.....Why do you need to group on a text field???
0
 

Author Comment

by:cdhbs
ID: 11700779
It's not so much that I want to group on the field.  What I'm doing is counting how many records there are in related tables and outputting all the data in this table.  Maybe I could count the related records in a subquery?
0
 
LVL 34

Accepted Solution

by:
arbert earned 250 total points
ID: 11701324
Yes, you could count in a subquery--those are about the only options...
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to shrink a transaction log file down to a reasonable size.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now