Avatar of welcome 123
welcome 123

asked on 

sql query help

I have a table which looks like the below:

Fund Table

CBRNO    item    FY1       Amount1      FY2             Amount2    
CCC01          1     2012       100              2013               200
CCC01          2     2012       100              2013               100
AAA01           1   2012       100              2013              300

Request Table
CBRNO     Meetingdate
CCC01       05/05/2012
AAA01       09/05/2012

I want results like this:
CBRNO   Amount
CCC01    200
AAA01    300    

That means I want the result of total amount of the request where meeting date falls under he curent fiscal year i.e for CCC01 the meetingdate is 2012 fiscalyear(before jube 30th 2012) so the FY1 amount is tottaled for all items and similarly the meetingdate for AAA01 is 2013 fiscal year do FY2 amount was considered.

I need to know how I can make up a sql query for this. I use sqlserver and use .Net,kindly help?
Microsoft SQL Server.NET Programming

Avatar of undefined
Last Comment
Sharath S
Avatar of welcome 123
welcome 123

ASKER

I found a query to find out current fiscalyear online which is: select YEAR(DATEADD(month, 6 + DATEDIFF(month, 0, meetingdate), 0)) as 'fiscalyear'
ASKER CERTIFIED SOLUTION
Avatar of Sara bhai
Sara bhai
Flag of India image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of welcome 123
welcome 123

ASKER

when I ran the above query the first thing I got was a error message :Msg 245, Level 16, State 1, Line 1
Conversion failed when converting the varchar value 'NULL' to data type int.

Then I Took only one cbrno whose valuse in both tables as below:

Fund Table

 CBRNO     Item       FY1         FY2            Amount1                 Amount2
  LOR01       1            2008      2009      25946116.00      27085265.00
  LOR01       2           2008      2009      18743774.00      18693328.00
  LOR01       3          2008      2009      2554500.00      2446500.00
  LOT01       4            2008      2009      50419630.00      51250704.00

Request Table
CBRNO     Meetingdate
LOR01       05/05/2008

and the result came different when I ran the above query which is:

SELECT r.CBRNO, CASE YEAR(r.Meetingdate) WHEN fy1 THEN A1 WHEN fy2 THEN A2 ELSE 0 END as Amount
FROM request r
INNER JOIN (SELECT CBRNO , SUM(newfund1) AS A1, SUM(newfund2) AS A2,fy1,fy2
FROM fund  GROUP BY fy1,fy2, CBRNO ) AS t2
ON r.CBRNO = t2.CBRNO

where r.cbrno in ('LOR01')

CBRNO                     Amount
LOT0100004      9832000.00

where as the amount total should be  97664020
Avatar of Sara bhai
Sara bhai
Flag of India image

you should get the idea how to handle query, only thing is remains your financial year logic.
Avatar of welcome 123
welcome 123

ASKER

sorry i was using different columns and hot the query right but how do I handle null values which is throwing exceptions?
SOLUTION
Avatar of Sharath S
Sharath S
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Microsoft SQL Server
Microsoft SQL Server

Microsoft SQL Server is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.SQL Server is available in multiple versions, typically identified by release year, and versions are subdivided into editions to distinguish between product functionality. Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning.

171K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo