| Author |
Topic |
|
msugradus
Starting Member
40 Posts |
Posted - 2007-06-08 : 15:57:18
|
| I need to add up the number of people who joined this month and compare that number to the number of people who joined last month and display the results in a report:FirstName..LastName.....StartedDateRandy......Simpson......5/4/2007 10:00:00 PMSteve......Rowe.........5/2/2007 10:00:00 PMEric.......Dickerson....5/4/2007 10:00:00 PMGloria.....Sanches......5/1/2007 12:00:29 AMAndres.....Marcelino....5/1/2007 12:06:31 AMkatie......ryan.........6/4/2007 12:08:35 AMDenise.....River........6/4/2007 12:27:14 AMKellog.....Stover.......6/5/2007 12:37:20 AMGlenn......Sanders......6/1/2007 12:42:40 AM |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2007-06-08 : 16:04:20
|
something like:declare @prevMonthStart datetime, @prevMonthEnd datetime, @thisMonthStart datetime, @thisMonthEnd datetimeselect @prevMonthStart = '20070501 00:00:00.000', @prevMonthEnd = '20070531 23:59:59.999', @thisMonthStart = '20070601 00:00:00.000', @thisMonthEnd = '20070630 23:59:59.999'select sum(case when StartedDate between @prevMonthStart and @prevMonthEnd then 1 else 0 end) as PrevMonthCount, sum(case when StartedDate between @thisMonthStart and @thisMonthStart then 1 else 0 end) as thisMonthCountfrom yourTablewhere StartedDate >= @prevMonthStart _______________________________________________Causing trouble since 1980blog: http://weblogs.sqlteam.com/mladenp |
 |
|
|
msugradus
Starting Member
40 Posts |
Posted - 2007-06-08 : 16:34:32
|
| I keep getting a ADO error by the declare when i try and run thisALTER FUNCTION dbo.Function6()RETURNS TABLEASRETURN (declare @prevMonthStart datetime, @prevMonthEnd datetime, @thisMonthStart datetime, @thisMonthEnd datetimeselect @prevMonthStart = '20070501 00:00:00.000', @prevMonthEnd = '20070531 23:59:59.999', @thisMonthStart = '20070601 00:00:00.000', @thisMonthEnd = '20070630 23:59:59.999'select sum(case when StartedDate between @prevMonthStart and @prevMonthEnd then 1 else 0 end) as PrevMonthCount, sum(case when StartedDate between @thisMonthStart and @thisMonthStart then 1 else 0 end) as thisMonthCountfrom exigo_data_sync.customerwhere StartedDate >= @prevMonthStart ) |
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2007-06-08 : 16:38:51
|
| of course you are.you need to put variables as parameters in your function.look up the syntax in BOL = books online = sql server help_______________________________________________Causing trouble since 1980blog: http://weblogs.sqlteam.com/mladenp |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-06-09 : 01:18:37
|
| Also you need to define the structure of table that function returnsWhy dont you use stored procedure instead of function?MadhivananFailing to plan is Planning to fail |
 |
|
|
msugradus
Starting Member
40 Posts |
Posted - 2007-06-11 : 10:14:42
|
I have never used any stored procedures, how would I go about creating one for this?quote: Originally posted by madhivanan Also you need to define the structure of table that function returnsWhy dont you use stored procedure instead of function?MadhivananFailing to plan is Planning to fail
|
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-06-11 : 10:40:41
|
| Read about Procedures in sql server help fileMadhivananFailing to plan is Planning to fail |
 |
|
|
msugradus
Starting Member
40 Posts |
Posted - 2007-06-11 : 10:53:57
|
| Ok, I am trying to figure this out to where a user could enter a start and end date for two months and compare the data...is this at all possible?....stored procedure or function? |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-06-11 : 10:56:10
|
| [code]Create Procedure test(@prevMonthStart datetime, @prevMonthEnd datetime, @thisMonthStart datetime, @thisMonthEnd datetime)asselect sum(case when StartedDate between @prevMonthStart and @prevMonthEnd then 1 else 0 end) as PrevMonthCount, sum(case when StartedDate between @thisMonthStart and @thisMonthStart then 1 else 0 end) as thisMonthCountfrom yourTablewhere StartedDate >= @prevMonthStart[/code]Now Execute it by supplying dates[code]EXEC TEST @prevMonthStart = '20070501 00:00:00.000', @prevMonthEnd = '20070531 23:59:59.999', @thisMonthStart = '20070601 00:00:00.000', @thisMonthEnd = '20070630 23:59:59.999'[/code]MadhivananFailing to plan is Planning to fail |
 |
|
|
msugradus
Starting Member
40 Posts |
Posted - 2007-06-11 : 14:41:45
|
| That worked beautiful when the data that i needed was on one table, but how do you do it when there are two tables involved. Say table1 has two fields: date and prodId and table2 had prodId and order. The two tables are tied by the prodId and I want to be able to enter the dates but display a report containing the number of orders for each month? |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-06-12 : 09:02:38
|
| 1 You need to join the tables2 Post some sample data and the result you wantMadhivananFailing to plan is Planning to fail |
 |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2007-06-12 : 09:53:42
|
You are the only person who can tell that! Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
|
subrata4allfriends
Starting Member
24 Posts |
Posted - 2007-06-12 : 15:22:58
|
| FirstName, LastName, StartedDate are the column of table EMPTABLE. Step 01: Replace the table name, column name according to your table structure.Step 02: Execute the following stored procedure.CREATE PROCEDURE RPT_JOINNING_DETAILSASBEGINDECLARE @ROWCOUNT AS INTDECLARE @EMPNAME AS VARCHAR(50)DECLARE @ID AS INTCREATE TABLE #JOINTHISMONTHS(ID INT IDENTITY(1,1) NOT NULL, EMPNAME VARCHAR(50))CREATE TABLE #JOINLASTMONTHS(ID INT IDENTITY(1,1) NOT NULL, EMPNAME VARCHAR(50))INSERT INTO #JOINTHISMONTHS SELECT FirstName +' '+ LastName FROM EMPTABLE WHERE CAST(MONTH(StartedDate) AS VARCHAR(10)) = CAST(MONTH(GETDATE()) AS VARCHAR(10))INSERT INTO #JOINLASTMONTHS SELECT FirstName +' '+ LastName FROM EMPTABLE WHERE CAST(MONTH(StartedDate) AS VARCHAR(10)) = CAST((MONTH(GETDATE())-1) AS VARCHAR(10))SELECT @ROWCOUNT = MAX(ID) FROM #JOINTHISMONTHSPRINT '------------------------------------------------------------------------------------------'PRINT '------------------------COMPARISION REPORT FOR LAST TWO MONTH-----------------------------'PRINT '------------------------------------------------------------------------------------------'PRINT 'TOTAL NO OF EMPLOYEES JOINED THIS MONTHS ('+ CAST(MONTH(GETDATE()) AS VARCHAR(10))+'-'+ RIGHT(CAST(YEAR(GETDATE()) AS VARCHAR(10)),2) +') :' + CAST(@ROWCOUNT AS VARCHAR(50))WHILE @ROWCOUNT > 0BEGINSELECT @EMPNAME = EMPNAME, @ID = ID FROM #JOINTHISMONTHS WHERE ID = @ROWCOUNTPRINT ' * '+@EMPNAMESET @ROWCOUNT = @ROWCOUNT - 1ENDSELECT @ROWCOUNT = MAX(ID) FROM #JOINLASTMONTHSPRINT 'TOTAL NO OF EMPLOYEES JOINED LAST MONTHS ('+ CAST(MONTH((GETDATE())-1) AS VARCHAR(10))+'-'+ RIGHT(CAST(YEAR(GETDATE()) AS VARCHAR(10)),2) +') :' + CAST(@ROWCOUNT AS VARCHAR(50))WHILE @ROWCOUNT > 0BEGINSELECT @EMPNAME = EMPNAME, @ID = ID FROM #JOINLASTMONTHS WHERE ID = @ROWCOUNTPRINT ' * '+@EMPNAMESET @ROWCOUNT = @ROWCOUNT - 1ENDDROP TABLE #JOINTHISMONTHSDROP TABLE #JOINLASTMONTHSENDStep 03: EXEC RPT_JOINNING_DETAILSHope, you will get the report.....Thanks & Regards,4allfriends."Life is not a bed of roses." |
 |
|
|
Kumar_Anil
Yak Posting Veteran
68 Posts |
Posted - 2008-04-24 : 18:06:36
|
| Subrata,Im new to SQL. I followed your path here & tried to bring out data exclusive to that table in comparision with another table data. For example I have table A with columns ID, First name, Last Name & table B with ID, First name, last name. These two tables can be bound by ID. Now I want 1.Data that is in A but not in B & 2.Data that is in B but not in A.Here is what I wrote in here & it is not working. Please correct me._____________________________________________________________________CREATE PROCEDURE RPT_DETAILSASBEGINDECLARE @Rowcount AS INTDECLARE @First_Name AS VARCHAR(50)DECLARE @Last_Name AS VARCHAR(50)DECLARE @Id AS INT CREATE TABLE #Prowess(ID INT NOT NULL, First_Name VARCHAR(50), Last_Name VARCHAR(50))CREATE TABLE #SDK(ID INT NOT NULL, First_Name VARCHAR(50), Last_Name VARCHAR(50)) INSERT INTO #Prowess SELECT bb.beenumber, be.FirstName, be.LastName FROM beebusiness bbjoin beeentity be on bb.beebusinessguid = bb.beebusinessguid INSERT INTO #SDK SELECT cast(sa_ss as INT), first_name, last_name from ml SELECT @ROWCOUNT = MAX(ID) FROM #SDKPRINT '------------------------------------------------------------------------------------------'PRINT '------------------------COMPARISION REPORT Between Prowess & SDK--------------------------'PRINT '------------------------------------------------------------------------------------------' PRINT 'TOTAL Difference ('+ + CAST(@ROWCOUNT AS VARCHAR(50)) WHILE @ROWCOUNT > 0BEGINSELECT @First_Name = First_name, @Last_Name = Last_name, @ID = ID FROM #Prowess WHERE ID = @ROWCOUNTPRINT ' * '+@First_Name+@Last_NameSET @ROWCOUNT = @ROWCOUNT - 1END SELECT @ROWCOUNT = MAX(ID) FROM #Sdk PRINT 'TOTAL Difference ('+ + CAST(@ROWCOUNT AS VARCHAR(50)) WHILE @ROWCOUNT > 0BEGINSELECT @First_Name = First_name, @Last_Name = Last_name, @ID = ID FROM #Sdk WHERE ID = @ROWCOUNTPRINT ' * '+@First_Name+@Last_NameSET @ROWCOUNT = @ROWCOUNT - 1END DROP TABLE #ProwessDROP TABLE #Sdk ENDexec RPT_DETAILS |
 |
|
|
|