Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi Guys,

i have a simple problem, but unable to solve, your help would be highly appreciated...
let me explain the problem....

i have two tables,

Table 1: KPI_ID, Unit, Frequency,

Table 2 : KPI_ID, Value, ValueDate.(Value Date format is 20140131)

i joined tables on KPI ID, AND showing result in GridView. working up to here

let say few records in SQL View,

KPI_ID, Unit, Frequency, Value, Value_Date

1, 1001, 10, 15, 55, 20140131
2, 1002, 10, 15, 50, 20140131
3, 1002, 10, 15, 54, 20140231
4, 1003, 10, 15, 53, 20140131
5, 1003, 10, 15, 57, 20140228
6, 1004, 10, 15, null, null
7, 1005, 10, 15, null, null
8, 1006, 10, 15, null , null

when i apply filter as JAN 2014, it shows me records number. 1, 2, 4..
but i want to see record number,, 1, 2, 3,4, 5 and 6, 7, 8

WHEN i apply filter as FEB 2014, it shows me only record number 3 and 5 , but i want to see
all records, WHICH record does not have value for FEB 2014 , should come....

how to get.....

how to achieve this...?

Please....
Posted

If Column value is NULL and still you want to fetch it then use left outer join.

But I am not able to understand If you put Filter JAN 2014 then how you are expecting all records ?? 1,2,3,4,5 and 6, 7, 8

If you use Left outer Join then you will get Records : 1,2,4,6,7,8
 
Share this answer
 
OLD Solution:
SQL
Create table A
(
	KPI_ID Int,
	Unit decimal, 
	Frequency decimal,
)


Create table B
(
	KPI_ID Int,
	Value decimal, 
	ValueDate Datetime,
)
-- Please insert the value you want
-- Now run the query as below
select * from A ta left join B tb on ta.KPI_ID = tb.KPI_ID


Updated Solution:
SQL
Create table table1
(
	KPI_ID Int,
	Unit decimal, 
	Frequency decimal,
)


Create table table2
(
	KPI_ID Int,
	Value decimal, 
	ValueDate Datetime,
)

select * from table1
Go
select * from table1
Go

Insert into table1 (KPI_ID, Unit, Frequency)values(1001, 10, 15)
Insert into table1 (KPI_ID, Unit, Frequency)values(1002, 10, 15)
Insert into table1 (KPI_ID, Unit, Frequency)values(1003, 10, 15)
Insert into table1 (KPI_ID, Unit, Frequency)values(1004, 10, 15)
Insert into table1 (KPI_ID, Unit, Frequency)values(1005, 10, 15)
Insert into table1 (KPI_ID, Unit, Frequency)values(1006, 10, 15)
Insert into table1 (KPI_ID, Unit, Frequency)values(1007, 10, 15)
GO
Insert into table2 (KPI_ID, Value, ValueDate)values(1001, 55, '20140131')
Insert into table2 (KPI_ID, Value, ValueDate)values(1002, 50, '20140131')
Insert into table2 (KPI_ID, Value, ValueDate)values(1002, 54, '20140228')
Insert into table2 (KPI_ID, Value, ValueDate)values(1003, 53, '20140131')
Insert into table2 (KPI_ID, Value, ValueDate)values(1003, 57, '20140228')
GO

--1, 1001, 10, 15, 55, 20140131
--2, 1002, 10, 15, 50, 20140131
--3, 1002, 10, 15, 54, 20140231
--4, 1003, 10, 15, 53, 20140131
--5, 1003, 10, 15, 57, 20140228
--6, 1004, 10, 15, null, null 
--7, 1005, 10, 15, null, null 
--8, 1006, 10, 15, null , null

-- Please insert the value you want
-- Now run the query as below
select * from table1 ta left join table2 tb on ta.KPI_ID = tb.KPI_ID WHERE CONVERT(VARCHAR(24),tb.ValueDate,112) like '201401%' OR CONVERT(VARCHAR(24),tb.ValueDate,112) IS NULL
 
Share this answer
 
v2
Comments
Member 10546877 6-Jan-15 7:16am    
Yes, Up to here, its working... fine..

But i have two drop down list, out side the grid view, where user can select the year AND OR Month to see Values
for selected Year Month... i am using Like keyword and matching selected month year, with value_date..

for example User Select Year = 2014, and month = Jan, then in the query i am getting selected values and concatenating them like where Value_Date Like '201401%',

but if value_Date is null for a record , then its not showing that record...

i want to show all record, if a record in null, then user will enter a value for it, other wise he can see if he already has entered value...
hope its clear now.....

Thanks for your answers...
Er. Puneet Goel 6-Jan-15 7:29am    
OK, Hold on let me update this
Er. Puneet Goel 6-Jan-15 7:32am    
Also, '3, 1002, 10, 15, 54, 20140231' this is invalid date as fab cann't have 31 days. Please cehck this meanwhile
Er. Puneet Goel 6-Jan-15 7:44am    
I have updated the solution. Please check and mark it as solution if its helpful.
Member 10546877 6-Jan-15 8:11am    
Thanks for your Answer,
it took me near to the solution, but not exactly, little bit far from solution...

now let say KPI_ID 1001, and 1002 has values for 201401(JAN 2014),
AND if i select Jan 2014, it shows me correct result...all KPI_ID with or without values..

But if i select Mar 2014, it does not shows me KPI_ID 1001, and 1002..

because i think these two KPI Has values for Jan 2014 and Feb 2014..

But i want to see all .... i will add values for march 2014...

if you pass parameter '201403%' in your query, you will not see KPI_ID 1001, and 1002,,,

Many Thanks...

is it clear...? Please

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