Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create reports from tables

Status
Not open for further replies.

SnakeEyes909

IS-IT--Management
Jun 17, 2008
26
US
ok, i have a few tables that i joined to get the data that i need (customer orders). i need to split the data into several reports based on the salesman that shows only there sales for that day.

here is the code thus far, which does bring up a list with all salesman together. is there a way to split it up?

Code:
	DECLARE @Salesman varchar(30)
	DECLARE @Salesman_num varchar(30)
	
	/*declare @MonthEnd smalldatetime
		set @MonthEnd = '4/27/10'*/
		
	SET NOCOUNT ON

	CREATE TABLE #ttbldailysales (strSalesman_num varchar(30),
					 strSalesman varchar(30),
					 strcust_name varchar(30),
					 strcust_number varchar(14),
					 strreferral_source varchar(50),
					 strdoctor varchar(50),
					 strreferral_type varchar(20))
					 

	DECLARE cSalesMaster CURSOR FAST_FORWARD FOR
		SELECT sm.SALESMAN_CODE, sm. SALESMAN_NAME/*sm.Salesman*/
		FROM dbo.SALESMAN /*dbo.[Salesman Master]*/ sm 
			
		ORDER BY sm.SALESMAN_CODE /*sm.Salesman*/

	OPEN cSalesMaster

	FETCH NEXT FROM cSalesMaster
		INTO @Salesman_num, @Salesman
	
	WHILE @@FETCH_STATUS = 0
	BEGIN
		
		INSERT INTO #ttbldailysales (strSalesman_num, strSalesman, strcust_name, strcust_number, strreferral_source,
					 strdoctor, strreferral_type )
		SELECT @salesman, s.SALESMAN_NAME, oh.CUST_NAME, oh.CUSTOMER_NUMBER, r.REFERRAL_NAME, d.DOCTORS_NAME, /*t.Type*/ i.ALPHA_SHORT_NAME

		FROM dbo.ORDER_HEADER oh 
			left JOIN dbo.ORDER_LINE ol
				ON ol.CUSTOMER_NUMBER = oh.CUSTOMER_NUMBER
			left JOIN dbo.REFERRAL r
				ON oh.REFERRAL_CODE = r.REFERRAL_CODE
			left JOIN dbo.DOCTOR d
				ON oh.DOCTOR_NUMBER = d.DOCTOR_NUMBER
			RIGHT JOIN dbo.SALESMAN s
				ON oh.SALESMAN_CODE = s.SALESMAN_CODE
			/*INNER JOIN dbo.Type t
				ON ol.HCPC_CODE = t.HCPC*/
			INNER JOIN dbo.INVENTORY i
				ON ol.INVENTORY_ITEM = i.INVENTORY_ITEM

		WHERE ol.ORDER_DATE = /*@MonthEnd*/ (Select Convert(varchar(20),getDate(),101))
		AND @Salesman_num = oh.SALESMAN_CODE
	
		FETCH NEXT FROM cSalesMaster
			INTO @Salesman_num, @Salesman
	END

	CLOSE cSalesMaster
	DEALLOCATE cSalesMaster

	SELECT strSalesman_num, strSalesman, strcust_name, strcust_number, strreferral_source, strdoctor, strreferral_type 
	FROM #ttbldailysales



	DROP TABLE #ttbldailysales


example data retruned

1 CORNELIUS, ANN ODOM, JOHN 51356 CUST CARE DEPT DR davy jones TUBING,CPAP6
1 CORNELIUS, ANN ODOM, JOHN 51356 CUST CARE DEPT DR davy jones MASK,SWIFT
2 ROSSETTI, LARRY HAYNAM, MARILYN 56015 HOSPITAL - CCC DR. JOHN doe BED,SEMI-ELE
2 ROSSETTI, LARRY HAYNAM, MARILYN 56015 HOSPITAL - CCC DR. JOHN doe LIFT, PT HYD

so what i would like to do is have it split out salesman 1 and all there records into 1 report, and salesman 2 out to another and so on.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top