Solved

select text field in query with aggregate function

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Sql server insert 13 29
SQL Update trigger 5 18
Query Help - MSSQL - Averages 5 27
MSSQL Import .SQL files - Increase Import Speed 9 38
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

770 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