Click here to Skip to main content
15,888,733 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I want output as Printers,Printers,null (select at least first two fields)
with this very existing query and any kind of join
select a.acName,a.discription,s.quentity from #Accounts a
inner join #stock s on a.id = s.acId where addDate between '2001-11-01' and '2001-11-11' and a.id = 1


What I have tried:

this is the scenario

create table #Accounts(id int, acName nvarchar(50), discription nvarchar(128))
insert into #Accounts values (1, 'Printers', 'Printers')
insert into #Accounts values (2, 'HD', 'HD')
insert into #Accounts values (3, 'Mouse', 'Mouse')
insert into #Accounts values (4, 'Monitor', 'Monitor')
create table #stock(id int, acId int, quentity int,addDate datetime )
insert into #stock values (1,1,20,'2001-01-01')
insert into #stock values (1,2,10,'2001-10-01')
insert into #stock values (1,3,11,'2001-12-01')
insert into #stock values (1,4,5,'2001-11-01')

select a.acName,a.discription,s.quentity from #Accounts a
inner join #stock s on a.id = s.acId where addDate between '2001-11-01' and '2001-11-11' and a.id = 1
Posted
Updated 19-Apr-17 0:38am

INNER JOIN restricts the result to those records that have matches on both sides. To always return all records from first table (and matching records or NULL values from second table) use LEFT JOIN.
 
Share this answer
 
Comments
QuentinEllison 19-Apr-17 4:54am    
not working in this case because of where
try the given table n show me the solution if you do have any
thanks
Use this:

SQL
select TOP 1  a.acName,a.discription,IsNULL(CAST(s.quentity AS nvarchar(10)), 'null') from #Accounts a
left join #stock s on a.id = s.acId where addDate between '2001-11-01' and '2001-11-11' and a.id = 1
 
Share this answer
 
Comments
CHill60 19-Apr-17 6:19am    
Still returns no data because of the WHERE - see my solution for how to overcome that
You need to use LEFT OUTER JOIN and cater for scenarios where nothing is being returned by the join. Like this
SQL
select * from #Accounts A
left outer join #stock S on A.id=S.acId
where A.id = 1 
AND ((addDate between '2001-11-01' and '2001-11-11')
	OR addDate IS NULL)
 
Share this answer
 
Thanks CHill60 - I've overseen that addDate is also from second table.
To return the 'null' for quantity let's complete it at last:

SQL
select a.acName,a.discription,IsNULL(CAST(s.quentity AS nvarchar(10)), 'null') 
from #Accounts a
left join #stock s on a.id = s.acId 
where ((s.addDate between '2001-11-01' and '2001-11-11')
or (s.addDate is null))
and (a.id = 1)
 
Share this answer
 
Comments
CHill60 19-Apr-17 7:31am    
I only found this comment by accident. If you want to respond then use the  Have a Question or Comment?  link next to a post.

It's also not deemed good practice to post multiple solutions to the same question - other members coming across this will be confused as to which of your solutions to use. You can use the Improve solution link to update your earlier efforts. Particularly so in this case as Solution 3 is incorrect and Solution 5 is essentially a repost of mine

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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