Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi,
i am going to fetch data in one table it contains the current_month values as 2013.007. In another table hold the timeid for this currentmonth. now i want to choose the time id for 2013.007 to before 6 value that means(2012.001 t0 2013.007). In mbrVersion table only contains the one curretn month. in Time table only contain the id for 7data how can i get this id?

here i have the code for only 2013.007 its example value only. i want to bind the value?


SQL
SELECT SUM(SIGNEDDATA)
FROM FACPLAN
WHERE TIMEID IN
(SELECT TIMEID FROM Time
WHERE ID IN
(SELECT CURRENT_MONTH FROM mbrVERSION WHERE CURRENT_MONTH!=''))
Posted
Comments
Raja Sekhar S 13-Mar-14 2:25am    
Provide table Structure with some sample data..
vinodhini sekar 13-Mar-14 2:40am    
In version table
versionid currentmonth
A.1012 -
B.2013 2013.007


In Time table
ID timeid period

2013.001 5476587 001
2013.002 7856786 002
2013.003 7856786 003
2013.004 7856786 004
2013.005 78546786 005
2013.006 78546786 006
2013.007 78456786 007

1 solution

SQL
DECLARE @CURRENT_MONTH VARCHAR(20)
  DECLARE @YEAR CHAR(4)
  DECLARE @PERIOD NUMERIC(3)

SET @CURRENT_MONTH= (SELECT CURRENT_MOTH FROM dbo.VERSION WHERE CURRENT_MONTH!='')
  PRINT @CURRENT_MONTH
 SET @YEAR=(select substring(@CURRENT_MONTH, 0, 5))
 SET @PERIOD=(SELECT SUBSTRING(@CURRENT_MONTH,6,9))


  SELECT SUM(SIGNEDDATA) FROM dbo.PLANNING
  WHERE
TIMEID IN
  ( SELECT TIMEID FROM dbo.mbrTime
   WHERE
   ID BETWEEN
   @YEAR+'.'+(SELECT RIGHT ('000'+ CAST (@PERIOD-6 AS varchar), 3))
   AND
    @YEAR +'.'+(SELECT RIGHT ('000'+ CAST (@PERIOD AS varchar), 3)))
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900