views:

61

answers:

4

How do I use the value returned by a query in the same procedure? (sorry for noob-ism)

I'm trying to get a number of users with an initial beginning with whatever as a list - i.e. A(4) B(2) c(5) etc

  SELECT DISTINCT LEFT(last_name, 1) AS initial 
    FROM users 
ORDER BY initial

How do I then go on to ask:

select COUNT(*) as NumTeam 
  from users 
 where last_name like initial + '%'
+1  A: 

use a group by query:

select left(last_name, 1) as initial, count(*) as initial_count
  from users
  group by left(last_name, 1)
  order by left(last_name, 1)
Ray
downvote? please explain if there is something wrong with this query so I can learn.
Ray
+1: I don't see anything wrong. Most likely, you were tactically downvoted
OMG Ponies
Thanx, omg. What would a "tactical downvote" accomplish? Would this be a way for another answerer to make his/her answer look better than mine, and so get the rep points?
Ray
@Ray: Yep, you get the idea.
OMG Ponies
gives me a warm fuzzy feeling
Ray
A: 

You can use inital in the GROUP BY clause:

SELECT DISTINCT LEFT(last_name, 1) AS initial, count(1) AS NumTeam 
FROM users
GROUP BY initial ORDER BY initial
Trevor
invalid colum name 'initial'?
Spudhead
As far as I know you cannot reference an alias (the "AS initial" part) in a later part of the query - at least in T-SQL. Maybe other SQL Servers allow this...
Bryan Batchelder
This will work in MySQL
Trevor
Change `GROUP BY initial` to `GROUP BY LEFT(last_name, 1)`
OMG Ponies
+2  A: 

This will definitely work on SQL Server 2005 (which I tested it on):

DECLARE @People TABLE (
    [id] [int] IDENTITY(1,1) NOT NULL,
    [fname] [nvarchar](50) NOT NULL,
    [lname] [nvarchar](50) NOT NULL
)

INSERT INTO @People([fname], [lname])
SELECT 'Joseph', 'Adama' UNION ALL
SELECT 'Adam', 'Joseph' UNION ALL
SELECT 'Bryan', 'Adams' UNION ALL
SELECT 'Charlie', 'Brown' UNION ALL
SELECT 'Charles', 'Babbage' UNION ALL
SELECT 'Charles', 'Schultz'

SELECT        LEFT(lname, 1) [Letter], COUNT(id) [Count]
FROM          @People
GROUP BY      LEFT(lname, 1)
ORDER BY      LEFT(lname, 1)

It yields the result set:

Letter   Count
A        2
B        2
J        1
S        1   
Bryan Batchelder
Don't run this on a database where you have a `People` table already defined! Particularly if it's a production database! @Bryan - you could use a table variable in the demo code
Russ Cam
@Russ: Good point, I edited the answer to use a table variable. Thanks.
Bryan Batchelder
No problem :) I just had visions of the expression on a boss' face when their employee tells them they've lost all the people data for the company because they downloaded da codez and ran it
Russ Cam
A: 

to answer the question proper:

mysql> SET @t1=1, @t2=2, @t3:=4;
mysql> SELECT @t1, @t2, @t3, @t4 := @t1+@t2+@t3;
+------+------+------+--------------------+
| @t1  | @t2  | @t3  | @t4 := @t1+@t2+@t3 |
+------+------+------+--------------------+
|    1 |    2 |    4 |                  7 | 
+------+------+------+--------------------+

this comes from http://dev.mysql.com/doc/refman/5.0/en/user-variables.html

To answer your question specifically, I believe you want something like this:

    SELECT LEFT(last_name, 1) AS initial,COUNT(*) as NumTeam FROM users GROUP BY LEFT(last_name, 1)

If you want the initials alphabetically:

SELECT * FROM(
 SELECT LEFT(last_name, 1) AS initial,COUNT(*) as NumTeam FROM users GROUP BY LEFT(last_name, 1)
) as initial_counts order by initial;
Mike Sherov
Sweet! Many thanks!
Spudhead
Looking back at this, there is no reason to have to do the sub-select. Just doing the order by on the inner clause is correct, as some other answers have pointed out. I was thinking of a different issue.
Mike Sherov