I have a table with column name quarter, year, budget val and other. I want to fetch value as quarter wise and year wise description wise.condition if the quarter is more than 4, again grouping should be there.
my data is coming like this
I have used this query:
ALTER PROCEDURE USP_EDELGIVE_GETBUDGET_DATA --47
(
@RFP_ID INT
)
AS
BEGIN
DECLARE @ROWCOUNT INT , @ROWID INT , @MAXQUATERCOUNT INT, @MAXQUATER_ROWID INT
DECLARE @SUMMARYMASTER TABLE (
ID int identity(1,1),
SUM_ID INT ,
SUM_DESC NVARCHAR(500)
)
DECLARE @QUATERDATA TABLE (
ID INT IDENTITY(1,1) ,
DENS_ID INT,
BUDGETID INT,
RFPID VARCHAR(100),
NGOID VARCHAR(100),
DESC_ID INT,
SUM_DESCRIPTION NVARCHAR(300),
QAURTER VARCHAR(200),
B_YEAR VARCHAR(200),
B_TYPE VARCHAR(200),
QUARTERVAL VARCHAR(200),
BUDGETVAL VARCHAR(200),
FROMYEAR VARCHAR(200),
TOYEAR VARCHAR(200),
FULLYEAR VARCHAR(200)
)
DECLARE @ACTUALDATA TABLE (
ID INT IDENTITY(1,1) ,
BUDGETID INT,
RFPID VARCHAR(100),
NGOID VARCHAR(100),
DESC_ID INT,
SUM_DESCRIPTION NVARCHAR(300),
QAURTER VARCHAR(200),
B_YEAR VARCHAR(200),
B_TYPE VARCHAR(200),
QUARTERVAL VARCHAR(200),
BUDGETVAL VARCHAR(200),
FROMYEAR VARCHAR(200),
TOYEAR VARCHAR(200),
FULLYEAR VARCHAR(200)
)
INSERT INTO @SUMMARYMASTER(SUM_ID,SUM_DESC)
SELECT DISTINCT SUM_ID,SUM_DESCRIPTION FROM EDELGIVE_BUDGET_SUMARYMASTER
WHERE B_TYPE='SUMMARY-2'
SELECT @MAXQUATERCOUNT = MAX(X.QAURTER) from (select CAST(RTRIM(LTRIM(SUBSTRING(BUD.QAURTER,CHARINDEX('Q', BUD.QAURTER)+1,CHARINDEX('Q', BUD.QAURTER)+1))) AS INT) as QAURTER,
BUD.DESC_ID
from EDELGIVE_BUDGET_SUMMARY_1 BUD
INNER JOIN EDELGIVE_BUDGET_SUMARYMASTER SM ON BUD.DESC_ID=SM.SUM_ID
where BUD.rfpid=@RFP_ID )
X
group by X.DESC_ID
order by X.DESC_ID
insert into @QUATERDATA(dens_id,BUDGETID,RFPID,NGOID,DESC_ID,SUM_DESCRIPTION,QAURTER,
B_YEAR,B_TYPE,QUARTERVAL,BUDGETVAL,FROMYEAR,TOYEAR,FULLYEAR)
SELECT
DENSE_RANK() OVER(ORDER BY X.QAURTER ASC) DENSEROWID,
X.BUDGETID,X.RFPID,X.NGOID,X.DESC_ID,X.SUM_DESCRIPTION,
X.QAURTERS,X.B_YEAR,X.B_TYPE,X.QUARTERVAL,X.BUDGETVAL,X.FROMYEAR,X.TOYEAR,X.FULLYEAR
FROM
(SELECT
CAST(RTRIM(LTRIM(SUBSTRING(BUD.QAURTER,CHARINDEX('Q', BUD.QAURTER)+1,CHARINDEX('Q', BUD.QAURTER)+1))) AS INT) AS QAURTER,
BUD.BUDGETID,
BUD.RFPID, BUD.NGOID, BUD.DESC_ID ,SM.SUM_DESCRIPTION,QAURTER AS QAURTERS, BUD.B_YEAR, BUD.B_TYPE, QUARTERVAL, BUDGETVAL,
FROMYEAR, TOYEAR,FROMYEAR+'TO'+TOYEAR AS FULLYEAR
FROM EDELGIVE_BUDGET_SUMMARY_1 BUD
INNER JOIN EDELGIVE_BUDGET_SUMARYMASTER SM ON BUD.DESC_ID=SM.SUM_ID
WHERE BUD.RFPID=47
)X
ORDER BY X.DESC_ID , X.B_YEAR , X.QAURTER
select * from @QUATERDATA
SET @MAXQUATER_ROWID= 1
SET @ROWID = 1
DECLARE @SUM_ID INT,@DENSE_ROWID INT
select @ROWCOUNT = COUNT(*) from @SUMMARYMASTER
SET @DENSE_ROWID = 1;
WHILE @MAXQUATERCOUNT >= @MAXQUATER_ROWID
BEGIN
SET @ROWID = 1
WHILE @ROWCOUNT >= @ROWID
BEGIN
SELECT @SUM_ID = SUM_ID FROM @SUMMARYMASTER WHERE ID=@ROWID
--@ACTUALDATA
INSERT INTO @ACTUALDATA(BUDGETID,RFPID,NGOID,DESC_ID,SUM_DESCRIPTION,QAURTER,
B_YEAR,B_TYPE,QUARTERVAL,BUDGETVAL,FROMYEAR,TOYEAR,FULLYEAR)
SELECT TOP 4 BUDGETID,RFPID,NGOID,DESC_ID,SUM_DESCRIPTION,QAURTER,
B_YEAR,B_TYPE,QUARTERVAL,BUDGETVAL,FROMYEAR,TOYEAR,FULLYEAR FROM @QUATERDATA
WHERE DENS_ID >= @DENSE_ROWID AND DESC_ID=@SUM_ID
SET @ROWID = @ROWID +1
END
SET @MAXQUATER_ROWID = @MAXQUATER_ROWID + 1
SET @DENSE_ROWID = @DENSE_ROWID + 4;
END
SELECT * FROM @ACTUALDATA
Select @MAXQUATERCOUNT
END
Fetched data:
ID BUDGETID RFPID NGOID DESC_ID SUM_DESCRIPTION QAURTER B_YEAR B_TYPE QUARTERVAL BUDGETVAL
1 4799 47 152 1 1.1 Salaries-Program management staff Q1 Year-1 2 Apr-17 to may-17 81000
2 4800 47 152 1 1.1 Salaries-Program management staff Q2 Year-1 2 Jun-17 to july-17 81000
3 4798 47 152 1 1.1 Salaries-Program management staff Q3 Year-1 2 Aug-17 to Sep-17 81000
4 4797 47 152 1 1.1 Salaries-Program management staff Q4 Year-1 2 Oct-18 to Nov-18 81000
5 4762 47 152 2 1.2 Salaries-Field staff, outreach workers, medical staff and other service providers Q1 Year-1 2 Apr-17 to may-17 381000
6 4761 47 152 2 1.2 Salaries-Field staff, outreach workers, medical staff and other service providers Q2 Year-1 2 Jun-17 to july-17 381000
7 4763 47 152 2 1.2 Salaries-Field staff, outreach workers, medical staff and other service providers Q3 Year-1 2 Aug-17 to Sep-17 381000
8 4764 47 152 2 1.2 Salaries-Field staff, outreach workers, medical staff and other service providers Q4 Year-1 2 Oct-18 to Nov-18 196000
9 4759 47 152 3 1.3 Other HR Costs Q1 Year-1 2 Apr-17 to may-17 0
10 4760 47 152 3 1.3 Other HR Costs Q2 Year-1 2 Jun-17 to july-17 0
11 4758 47 152 3 1.3 Other HR Costs Q3 Year-1 2 Aug-17 to Sep-17 0
12 4757 47 152 3 1.3 Other HR Costs Q4 Year-1 2 Oct-18 to Nov-18 0
193 4796 47 152 1 1.1 Salaries-Program management staff Q5 Year-1 2 Dec-18 to Mar-18 81000
194 4795 47 152 1 1.1 Salaries-Program management staff Q6 Year-2 2 Apr-18 to Jun-18 405000
195 4794 47 152 1 1.1 Salaries-Program management staff Q7 Year-2 2 Jul-18 to Sep-18 89100
196 4793 47 152 1 1.1 Salaries-Program management staff Q8 Year-2 2 Jan-19 to Mar-19 89100
197 4765 47 152 2 1.2 Salaries-Field staff, outreach workers, medical staff and other service providers Q5 Year-1 2 Dec-18 to Mar-18 196000
198 4766 47 152 2 1.2 Salaries-Field staff, outreach workers, medical staff and other service providers Q6 Year-2 2 Apr-18 to Jun-18 1535000
199 4767 47 152 2 1.2 Salaries-Field staff, outreach workers, medical staff and other service providers Q7 Year-2 2 Jul-18 to Sep-18 419100
200 4768 47 152 2 1.2 Salaries-Field staff, outreach workers, medical staff and other service providers Q8 Year-2 2 Jan-19 to Mar-19 215600
201 4756 47 152 3 1.3 Other HR Costs Q5 Year-1 2 Dec-18 to Mar-18 0
202 4755 47 152 3 1.3 Other HR Costs Q6 Year-2 2 Apr-18 to Jun-18 0
203 4754 47 152 3 1.3 Other HR Costs Q7 Year-2 2 Jul-18 to Sep-18 0
204 4753 47 152 3 1.3 Other HR Costs Q8 Year-2 2 Jan-19 to Mar-19 0
385 4792 47 152 1 1.1 Salaries-Program management staff Q9 Year-3 2 Apr-19 to Jun-19 98010
386 4791 47 152 1 1.1 Salaries-Program management staff Q10 Year-3 2 Jul-19 to Sep-19 98010
387 4790 47 152 1 1.1 Salaries-Program management staff Q11 Year-3 2 Oct-19 to Dec-19 98010
388 4789 47 152 1 1.1 Salaries-Program management staff Q12 Year-3 2 Jan-20 to Mar-20 98010
389 4769 47 152 2 1.2 Salaries-Field staff, outreach workers, medical staff and other service providers Q9 Year-3 2 Apr-19 to Jun-19 461010
390 4770 47 152 2 1.2 Salaries-Field staff, outreach workers, medical staff and other service providers Q10 Year-3 2 Jul-19 to Sep-19 461010
391 4771 47 152 2 1.2 Salaries-Field staff, outreach workers, medical staff and other service providers Q11 Year-3 2 Oct-19 to Dec-19 461010
392 4772 47 152 2 1.2 Salaries-Field staff, outreach workers, medical staff and other service providers Q12 Year-3 2 Jan-20 to Mar-20 237160
393 4752 47 152 3 1.3 Other HR Costs Q9 Year-3 2 Apr-19 to Jun-19 0
394 4751 47 152 3 1.3 Other HR Costs Q10 Year-3 2 Jul-19 to Sep-19 0
395 4750 47 152 3 1.3 Other HR Costs Q11 Year-3 2 Oct-19 to Dec-19 0
396 4749 47 152 3 1.3 Other HR Costs Q12 Year-3 2 Jan-20 to Mar-20 0
577 4788 47 152 1 1.1 Salaries-Program management staff Q13 Year-4 2 0
578 4787 47 152 1 1.1 Salaries-Program management staff Q14 Year-4 2 0
579 4786 47 152 1 1.1 Salaries-Program management staff Q15 Year-4 2 0
580 4783 47 152 1 1.1 Salaries-Program management staff Q16 Year-4 2 0
581 4773 47 152 2 1.2 Salaries-Field staff, outreach workers, medical staff and other service providers Q13 Year-4 2 0
582 4774 47 152 2 1.2 Salaries-Field staff, outreach workers, medical staff and other service providers Q14 Year-4 2 0
583 4775 47 152 2 1.2 Salaries-Field staff, outreach workers, medical staff and other service providers Q15 Year-4 2 0
584 4778 47 152 2 1.2 Salaries-Field staff, outreach workers, medical staff and other service providers Q16 Year-4 2 0
585 4748 47 152 3 1.3 Other HR Costs Q13 Year-4 2 0
586 4747 47 152 3 1.3 Other HR Costs Q14 Year-4 2 0
587 4746 47 152 3 1.3 Other HR Costs Q15 Year-4 2 0
588 4743 47 152 3 1.3 Other HR Costs Q16 Year-4 2 0
769 4784 47 152 1 1.1 Salaries-Program management staff Q17 Year-5 2 0
770 4785 47 152 1 1.1 Salaries-Program management staff Q18 Year-5 2 0
771 4782 47 152 1 1.1 Salaries-Program management staff Q19 Year-5 2 0
772 4781 47 152 1 1.1 Salaries-Program management staff Q20 Year-5 2 0
773 4777 47 152 2 1.2 Salaries-Field staff, outreach workers, medical staff and other service providers Q17 Year-5 2 0
774 4776 47 152 2 1.2 Salaries-Field staff, outreach workers, medical staff and other service providers Q18 Year-5 2 0
775 4779 47 152 2 1.2 Salaries-Field staff, outreach workers, medical staff and other service providers Q19 Year-5 2 0
776 4780 47 152 2 1.2 Salaries-Field staff, outreach workers, medical staff and other service providers Q20 Year-5 2 0
777 4744 47 152 3 1.3 Other HR Costs Q17 Year-5 2 0
778 4745 47 152 3 1.3 Other HR Costs Q18 Year-5 2 0
779 4742 47 152 3 1.3 Other HR Costs Q19 Year-5 2 0
780 4741 47 152 3 1.3 Other HR Costs Q20 Year-5 2 0
Expected result:
ID BUDGETID RFPID NGOID DESC_ID SUM_DESCRIPTION QAURTER B_YEAR B_TYPE QUARTERVAL BUDGETVAL
1 4799 47 152 1 1.1 Salaries-Program management staff Q1 Year-1 2 Apr-17 to may-17 81000
2 4800 47 152 1 1.1 Salaries-Program management staff Q2 Year-1 2 Jun-17 to july-17 81000
3 4798 47 152 1 1.1 Salaries-Program management staff Q3 Year-1 2 Aug-17 to Sep-17 81000
4 4797 47 152 1 1.1 Salaries-Program management staff Q4 Year-1 2 Oct-18 to Nov-18 81000
5 4762 47 152 2 1.2 Salaries-Field staff, outreach workers, medical staff and other service providers Q1 Year-1 2 Apr-17 to may-17 381000
6 4761 47 152 2 1.2 Salaries-Field staff, outreach workers, medical staff and other service providers Q2 Year-1 2 Jun-17 to july-17 381000
7 4763 47 152 2 1.2 Salaries-Field staff, outreach workers, medical staff and other service providers Q3 Year-1 2 Aug-17 to Sep-17 381000
8 4764 47 152 2 1.2 Salaries-Field staff, outreach workers, medical staff and other service providers Q4 Year-1 2 Oct-18 to Nov-18 196000
9 4759 47 152 3 1.3 Other HR Costs Q1 Year-1 2 Apr-17 to may-17 0
10 4760 47 152 3 1.3 Other HR Costs Q2 Year-1 2 Jun-17 to july-17 0
11 4758 47 152 3 1.3 Other HR Costs Q3 Year-1 2 Aug-17 to Sep-17 0
12 4757 47 152 3 1.3 Other HR Costs Q4 Year-1 2 Oct-18 to Nov-18 0
193 4796 47 152 1 1.1 Salaries-Program management staff Q5 Year-1 2 Dec-18 to Mar-18 81000
197 4765 47 152 2 1.2 Salaries-Field staff, outreach workers, medical staff and other service providers Q5 Year-1 2 Dec-18 to Mar-18 196000
201 4756 47 152 3 1.3 Other HR Costs Q5 Year-1 2 Dec-18 to Mar-18 0
194 4795 47 152 1 1.1 Salaries-Program management staff Q6 Year-2 2 Apr-18 to Jun-18 405000
195 4794 47 152 1 1.1 Salaries-Program management staff Q7 Year-2 2 Jul-18 to Sep-18 89100
196 4793 47 152 1 1.1 Salaries-Program management staff Q8 Year-2 2 Jan-19 to Mar-19 89100
198 4766 47 152 2 1.2 Salaries-Field staff, outreach workers, medical staff and other service providers Q6 Year-2 2 Apr-18 to Jun-18 1535000
199 4767 47 152 2 1.2 Salaries-Field staff, outreach workers, medical staff and other service providers Q7 Year-2 2 Jul-18 to Sep-18 419100
200 4768 47 152 2 1.2 Salaries-Field staff, outreach workers, medical staff and other service providers Q8 Year-2 2 Jan-19 to Mar-19 215600
202 4755 47 152 3 1.3 Other HR Costs Q6 Year-2 2 Apr-18 to Jun-18 0
203 4754 47 152 3 1.3 Other HR Costs Q7 Year-2 2 Jul-18 to Sep-18 0
204 4753 47 152 3 1.3 Other HR Costs Q8 Year-2 2 Jan-19 to Mar-19 0
385 4792 47 152 1 1.1 Salaries-Program management staff Q9 Year-3 2 Apr-19 to Jun-19 98010
386 4791 47 152 1 1.1 Salaries-Program management staff Q10 Year-3 2 Jul-19 to Sep-19 98010
387 4790 47 152 1 1.1 Salaries-Program management staff Q11 Year-3 2 Oct-19 to Dec-19 98010
388 4789 47 152 1 1.1 Salaries-Program management staff Q12 Year-3 2 Jan-20 to Mar-20 98010
389 4769 47 152 2 1.2 Salaries-Field staff, outreach workers, medical staff and other service providers Q9 Year-3 2 Apr-19 to Jun-19 461010
390 4770 47 152 2 1.2 Salaries-Field staff, outreach workers, medical staff and other service providers Q10 Year-3 2 Jul-19 to Sep-19 461010
391 4771 47 152 2 1.2 Salaries-Field staff, outreach workers, medical staff and other service providers Q11 Year-3 2 Oct-19 to Dec-19 461010
392 4772 47 152 2 1.2 Salaries-Field staff, outreach workers, medical staff and other service providers Q12 Year-3 2 Jan-20 to Mar-20 237160
393 4752 47 152 3 1.3 Other HR Costs Q9 Year-3 2 Apr-19 to Jun-19 0
394 4751 47 152 3 1.3 Other HR Costs Q10 Year-3 2 Jul-19 to Sep-19 0
395 4750 47 152 3 1.3 Other HR Costs Q11 Year-3 2 Oct-19 to Dec-19 0
396 4749 47 152 3 1.3 Other HR Costs Q12 Year-3 2 Jan-20 to Mar-20 0
385 4792 47 152 1 1.1 Salaries-Program management staff Q9 Year-3 2 Apr-19 to Jun-19 98010
389 4769 47 152 2 1.2 Salaries-Field staff, outreach workers, medical staff and other service providers Q9 Year-3 2 Apr-19 to Jun-19 461010
393 4752 47 152 3 1.3 Other HR Costs Q9 Year-3 2 Apr-19 to Jun-19 0
577 4788 47 152 1 1.1 Salaries-Program management staff Q13 Year-4 2 0
578 4787 47 152 1 1.1 Salaries-Program management staff Q14 Year-4 2 0
579 4786 47 152 1 1.1 Salaries-Program management staff Q15 Year-4 2 0
580 4783 47 152 1 1.1 Salaries-Program management staff Q16 Year-4 2 0
581 4773 47 152 2 1.2 Salaries-Field staff, outreach workers, medical staff and other service providers Q13 Year-4 2 0
582 4774 47 152 2 1.2 Salaries-Field staff, outreach workers, medical staff and other service providers Q14 Year-4 2 0
583 4775 47 152 2 1.2 Salaries-Field staff, outreach workers, medical staff and other service providers Q15 Year-4 2 0
584 4778 47 152 2 1.2 Salaries-Field staff, outreach workers, medical staff and other service providers Q16 Year-4 2 0
585 4748 47 152 3 1.3 Other HR Costs Q13 Year-4 2 0
586 4747 47 152 3 1.3 Other HR Costs Q14 Year-4 2 0
587 4746 47 152 3 1.3 Other HR Costs Q15 Year-4 2 0
588 4743 47 152 3 1.3 Other HR Costs Q16 Year-4 2 0
769 4784 47 152 1 1.1 Salaries-Program management staff Q17 Year-5 2 0
770 4785 47 152 1 1.1 Salaries-Program management staff Q18 Year-5 2 0
771 4782 47 152 1 1.1 Salaries-Program management staff Q19 Year-5 2 0
772 4781 47 152 1 1.1 Salaries-Program management staff Q20 Year-5 2 0
773 4777 47 152 2 1.2 Salaries-Field staff, outreach workers, medical staff and other service providers Q17 Year-5 2 0
774 4776 47 152 2 1.2 Salaries-Field staff, outreach workers, medical staff and other service providers Q18 Year-5 2 0
775 4779 47 152 2 1.2 Salaries-Field staff, outreach workers, medical staff and other service providers Q19 Year-5 2 0
776 4780 47 152 2 1.2 Salaries-Field staff, outreach workers, medical staff and other service providers Q20 Year-5 2 0
777 4744 47 152 3 1.3 Other HR Costs Q17 Year-5 2 0
778 4745 47 152 3 1.3 Other HR Costs Q18 Year-5 2 0
779 4742 47 152 3 1.3 Other HR Costs Q19 Year-5 2 0
780 4741 47 152 3 1.3 Other HR Costs Q20 Year-5 2 0