473,387 Members | 1,771 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

sum() for textual fields?

Hi all,

Many times would be useful (for me at least) if sum() could summarize
textual fields by simply concatenating them :

eg a table named 'lessons' contains
Lesson Teacher
math Mr. Brown
history Mr. Brown
math Ms. White
gym Mr. Green
geo Ms. White

so I could use a select like this:

select Lesson,sum(Teacher) group by Lesson

(and sum might take a second parameter as a separator between added fields)

Of course, these things aren't exists so I wanted to write my own
"textsum()" function, but in MSSQL2000 user functions can take only
scalar variables AFAIK.

Anyone has faced and/or has solved this problem other way (with a simple
select)?

Thanks

zf

Jul 20 '05 #1
2 1605
:-))

Posting my question I've noticed that the previous post asks the very
same thing as my letter asks.
Anyway I would be grateful for answers....

Zaka Ferenc wrote:
Hi all,

Many times would be useful (for me at least) if sum() could summarize
textual fields by simply concatenating them :

eg a table named 'lessons' contains
Lesson Teacher
math Mr. Brown
history Mr. Brown
math Ms. White
gym Mr. Green
geo Ms. White

so I could use a select like this:

select Lesson,sum(Teacher) group by Lesson

(and sum might take a second parameter as a separator between added fields)

Of course, these things aren't exists so I wanted to write my own
"textsum()" function, but in MSSQL2000 user functions can take only
scalar variables AFAIK.

Anyone has faced and/or has solved this problem other way (with a simple
select)?

Thanks

zf


Jul 20 '05 #2
CREATE TABLE Lessons (lesson VARCHAR(10), teacher VARCHAR(10), PRIMARY KEY
(lesson, teacher))

INSERT INTO Lessons VALUES ('math','Mr. Brown')
INSERT INTO Lessons VALUES ('history','Mr. Brown')
INSERT INTO Lessons VALUES ('math','Ms. White')
INSERT INTO Lessons VALUES ('gym','Mr. Green')
INSERT INTO Lessons VALUES ('geo','Ms. White')

SELECT lesson,
MIN(CASE seq WHEN 1 THEN teacher END)+
COALESCE(', '+MIN(CASE seq WHEN 2 THEN teacher END),'')+
COALESCE(', '+MIN(CASE seq WHEN 3 THEN teacher END),'')+
COALESCE(', '+MIN(CASE seq WHEN 4 THEN teacher END),'')+
COALESCE(', '+MIN(CASE seq WHEN 5 THEN teacher END),'')
FROM
(SELECT L1.lesson, L2.teacher, COUNT(*) AS seq
FROM Lessons AS L1
JOIN Lessons AS L2
ON L1.lesson = L2.lesson
AND L1.teacher<=L2.teacher
GROUP BY L1.lesson, L2.teacher) AS X
GROUP BY lesson

--
David Portas
------------
Please reply only to the newsgroup
--
Jul 20 '05 #3

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

Similar topics

15
by: U N Me | last post by:
I have a continuous form that lists records from a query. One of the columns is an amount field, In the footer of the form I place a text box that displays the total (sum) of the amount. The...
3
by: John Baker | last post by:
Hi: I have a field on a form that is names 125POP. It is currency. I can refer perfectly well to it in a text box when I want to add it to something as in: =+ HOWEVER when i want to sum...
2
by: ampebbles | last post by:
I'm fairly new to asp and am not sure why my web page doesn't sum the amount from the access database correctly. Example, one teacher has 4 donations that should total $1,238.68, but the total on...
7
by: richard | last post by:
I have a table with 5 fields into which the user will enter numbers, which must add up to 100%. Say the fields are Toyotas, Missans, Mazdas, Hondas and Other. I am having trouble writing a query...
1
by: robertmeyer1 | last post by:
I have a query that I am trying to run a sum of (sigma) on a group of about 6 fields. I am trying to sum different record sources together. Right now I have 6 fields of numeric data. The 1st field...
1
by: gillian3114 | last post by:
why this sum function won't work ? eg Sum(personal) lblPersonal.Caption it wont retrieve the sum value of personal Dim cn6 As New ADODB.Connection Dim strCNString4 As String Dim rs6 As New...
2
by: gillian3114 | last post by:
why this sum function won't work ? eg Sum(personal) lblPersonal.Caption it wont retrieve the sum value of the personal field Dim cn6 As New ADODB.Connection Dim strCNString4 As String Dim rs6...
1
by: ArizonaState | last post by:
I have a Stored Proc which outputs the sum of various fields from a table. CODE ... ... COMPUTE SUM(Field1), SUM(Field2), SUM(Field3)... /CODE I am able to execute the SProc and obtain...
6
by: azegurb | last post by:
Hello, I have one question again i created one table again and in this table i added some another options for ex at the previous table there were only one problem sum of the dynamically added...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...

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.