473,407 Members | 2,314 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,407 software developers and data experts.

GROUP BY a computed column

If I want to count the number of records in a table GROUPed BY the
first word in a string column, must I first compute the first word and
store in a temporary table and then do my GROUP BY select?
Jun 27 '08 #1
3 7997
You can do this in a single query grouping by the expression extracting the
first word from the string:

CREATE TABLE Foo (
keycol INT PRIMARY KEY,
datacol VARCHAR(35));

INSERT INTO Foo VALUES(1, 'one way');
INSERT INTO Foo VALUES(2, 'one day');
INSERT INTO Foo VALUES(3, 'next day');

SELECT LEFT(datacol, CHARINDEX(' ', datacol) - 1) AS first_word,
COUNT(*) AS cnt
FROM Foo
GROUP BY LEFT(datacol, CHARINDEX(' ', datacol) - 1);
HTH,

Plamen Ratchev
http://www.SQLStudio.com

Jun 27 '08 #2
CREATE TABLE Phrases
(phrase_id INTEGER NOT NULL PRIMARY KEY,
phrase_txt VARCHAR(500) NOT NULL);

SELECT start_word, COUNT(*) AS start_word_cnt
FROM (SELECT phrase_id, SUBSTRING (phrase_txt, 1, CHARINDEX(' ',
phrase_txt) - 1)
FROM Phrases)
AS FirstWords (phrase_id, start_word)
GROUP BY start_word;
Jun 27 '08 #3
On Jun 26, 3:12 pm, The Quiet Center <thequietcen...@gmail.comwrote:
If I want to count the number of records in a table GROUPed BY the
first word in a string column, must I first compute the first word and
store in a temporary table and then do my GROUP BY select?
If you do that frequently, consider creating an index on a computed
column.
Jul 2 '08 #4

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

Similar topics

2
by: BCR | last post by:
I created a index on a computed column. I did not see any improvement in performance with a join to this column and also my inserts and updates to this table are failing. Any ideas? Chender
9
by: DMAC | last post by:
If i want to split a computed column into two or more columns based on the the length (its a varchar) of the computed column, how often will sql server determine what the computed column is?...
0
by: Jim Heavey | last post by:
Hello, I have created a computed column which concatenates a name and date. My problem is that if the 10 is not 10 characters, I get an extra character placed into the computed column. Here is the...
4
by: Bill Todd | last post by:
Is there any way to display the RowState of each row in a computed column in the DataTable? -- Bill
0
by: Han Holl | last post by:
Hi, Can anyone explain to me what I'm doing wrong: (The first select is to show that the function rubriek exists, and does work). I want to create an index on a computed column: palga=>...
6
by: jim_geissman | last post by:
Can I create an index on a variation of a column that isn't actually in the table? I have a ParcelNumber column, with values like 123 AB-670 12345ABC 000-00-040 12-345-67 AP34567890
2
by: Dot Net Daddy | last post by:
Hello, I want to assign a column a computed value, which is the multiplication of a value from the table within and a value from another table. How can I do that?
2
by: garridor | last post by:
I am working on a DB I created for a small employee scheduling software (in VB 2005) - to be used my parents in their non profit business. I would like to capture a time span between TimeIn and...
1
by: coder1234 | last post by:
I'm trying to create a computed column in a table in SQL Server 2005 and can't get the syntax right for the Formula. I want the computed column to be a bit data type which resolves to 1/0...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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...

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.