From: Muhammad Bilal on
Hi.

Let

Create table #temp1(erpnbr int, svpname varchar(50), dcode char(3), ucode
char(3))
Create table #temp2(nbr char(13))

Insert into #temp1 values(5585,'John','352','001')
Insert into #temp1 values(5585,'John','352','002')
Insert into #temp1 values(5585,'John','352','003')
Insert into #temp1 values(5599,'Tom','351','001')
Insert into #temp1 values(5599,'Tom','351','002')

Insert into #temp2 values('A352001')
Insert into #temp2 values('A352001')
Insert into #temp2 values('A352002')
Insert into #temp2 values('A352002')
Insert into #temp2 values('A352003')
Insert into #temp2 values('A352003')
Insert into #temp2 values('A352003')
Insert into #temp2 values('A352003')
Insert into #temp2 values('A351001')
Insert into #temp2 values('A351001')
Insert into #temp2 values('A351002')
Insert into #temp2 values('A351002')

As
#temp1.dcode = substring(#temp.nbr,2,3)
and #temp1.ucode = substring(#temp.nbr,4,3)


i want to count records againt each erpnbr from #temp2 on basis of dcode and
ucode.

Result should be like this

erpnbr svpname dcode count(substring(#temp.nbr,4,3))
5585 John 352 8
5599 Tom 351 4


Regards,
Muhammad Bilal
From: Tom Moreau on
Try:

select

t1.erpnbr

, t1.svpname

, t1.dcode

, count(*)

from

#temp1 t1

join

#temp2 t2 on t1.dcode = substring(t2.nbr,2,3)

and t1.ucode = right (rtrim (t2.nbr), 3)

group by

t1.erpnbr

, t1.svpname

, t1.dcode


--
Tom

----------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
SQL Server MVP
Toronto, ON Canada
https://mvp.support.microsoft.com/profile/Tom.Moreau


"Muhammad Bilal" <MuhammadBilal(a)discussions.microsoft.com> wrote in message
news:1DA42C90-259D-4B28-809F-ED7AEF5A80EF(a)microsoft.com...
Hi.

Let

Create table #temp1(erpnbr int, svpname varchar(50), dcode char(3), ucode
char(3))
Create table #temp2(nbr char(13))

Insert into #temp1 values(5585,'John','352','001')
Insert into #temp1 values(5585,'John','352','002')
Insert into #temp1 values(5585,'John','352','003')
Insert into #temp1 values(5599,'Tom','351','001')
Insert into #temp1 values(5599,'Tom','351','002')

Insert into #temp2 values('A352001')
Insert into #temp2 values('A352001')
Insert into #temp2 values('A352002')
Insert into #temp2 values('A352002')
Insert into #temp2 values('A352003')
Insert into #temp2 values('A352003')
Insert into #temp2 values('A352003')
Insert into #temp2 values('A352003')
Insert into #temp2 values('A351001')
Insert into #temp2 values('A351001')
Insert into #temp2 values('A351002')
Insert into #temp2 values('A351002')

As
#temp1.dcode = substring(#temp.nbr,2,3)
and #temp1.ucode = substring(#temp.nbr,4,3)


i want to count records againt each erpnbr from #temp2 on basis of dcode and
ucode.

Result should be like this

erpnbr svpname dcode count(substring(#temp.nbr,4,3))
5585 John 352 8
5599 Tom 351 4


Regards,
Muhammad Bilal

From: Ahmad Bilal Ahmad on
A procedure that returns all the columns in all the rows on a table
1> create table employee(
2> ID int,
3> name nvarchar (10),
4> salary int,
5> start_date datetime,
6> city nvarchar (10),
7> region char (1))
8> GO
1>
2> insert into employee (ID, name, salary, start_date, city, region)
3> values (1, 'Jason', 40420, '02/01/94', 'New York', 'W')
4> GO

(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (2, 'Robert',14420, '01/02/95', 'Vancouver','N')
3> GO

(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (3, 'Celia', 24020, '12/03/96', 'Toronto', 'W')
3> GO

(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (4, 'Linda', 40620, '11/04/97', 'New York', 'N')
3> GO

(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (5, 'David', 80026, '10/05/98', 'Vancouver','W')
3> GO

(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (6, 'James', 70060, '09/06/99', 'Toronto', 'N')
3> GO

(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (7, 'Alison',90620, '08/07/00', 'New York', 'W')
3> GO

(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (8, 'Chris', 26020, '07/08/01', 'Vancouver','N')
3> GO

(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (9, 'Mary', 60020, '06/09/02', 'Toronto', 'W')
3> GO

(1 rows affected)
1>
2> select * from employee
3> GO
ID name salary start_date city region
----------- ---------- ----------- ----------------------- ---------- ------
1 Jason 40420 1994-02-01 00:00:00.000 New York W
2 Robert 14420 1995-01-02 00:00:00.000 Vancouver N
3 Celia 24020 1996-12-03 00:00:00.000 Toronto W
4 Linda 40620 1997-11-04 00:00:00.000 New York N
5 David 80026 1998-10-05 00:00:00.000 Vancouver W
6 James 70060 1999-09-06 00:00:00.000 Toronto N
7 Alison 90620 2000-08-07 00:00:00.000 New York W
8 Chris 26020 2001-07-08 00:00:00.000 Vancouver N
9 Mary 60020 2002-06-09 00:00:00.000 Toronto W

(9 rows affected)
1>
2> drop procedure spEmployee;
3> GO
1>
2> -- Creating the Sproc: A sproc that returns all the columns in all the
rows on a table
3> CREATE PROC spEmployee
4> AS
5> SELECT * FROM Employee
6> GO
1>
2> EXEC spEmployee
3> GO
ID name salary start_date city region
----------- ---------- ----------- ----------------------- ---------- ------
1 Jason 40420 1994-02-01 00:00:00.000 New York W
2 Robert 14420 1995-01-02 00:00:00.000 Vancouver N
3 Celia 24020 1996-12-03 00:00:00.000 Toronto W
4 Linda 40620 1997-11-04 00:00:00.000 New York N
5 David 80026 1998-10-05 00:00:00.000 Vancouver W
6 James 70060 1999-09-06 00:00:00.000 Toronto N
7 Alison 90620 2000-08-07 00:00:00.000 New York W
8 Chris 26020 2001-07-08 00:00:00.000 Vancouver N
9 Mary 60020 2002-06-09 00:00:00.000 Toronto W

(9 rows affected)
1>
2> drop table employee
3> GO
1>
Ahmad Bilal