Solved

select text field in query with aggregate function

Posted on 2004-08-02
3
1,835 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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

896 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

18 Experts available now in Live!

Get 1:1 Help Now