Click here to Skip to main content
15,889,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
automatically duplicate rows in sql server 2008 r2 select query
i have two table name 1) tbl_Stock_Journal 2) tbl_Stock_Journal_Details

i run a select query with where condition
i have 5 data in table1 and in table 2 have 3 data when i run the query result is showing 15 row the query show on date wise
i want to maximum data show and if other table in same date have no number of data then show blank or null

please help me.....

What I have tried:

ALter proc P_GetStockJournalRPT --  'all', 'all','2018-07-10' , '2018-07-10'     

   
@SJItmID varchar (100)      ,          
@SJDItmID varchar (100)      , 
@Sdate date,                                  
@EDate Date  
   
As
if @SJItmID='ALL' and @SJDItmID='ALL'
begin
select distinct SJ.SJID, i1.itemnm as itemnmsou, SJ.SJqty, SJ.SJPCS, SJ.SJRate, SJ.SJamt, SJ.SJleft, SJ.SJDate, SJ.nrr,
i2.itemnm as itemnmDES, SJD.SJDqty, SJD.SJDPCS, SJD.SJDRate, SJD.SJDamt, SJD.SJDstock, SJD.SJDdate

from tbl_Stock_Journal SJ

INNER JOIN tbl_item AS i1   ON i1.itemid  =SJ.SJItmID
 inner JOIN tbl_Stock_Journal_Details AS SJD   ON   SJ.SJID = SJD.SJID                             
inner JOIN tbl_item AS i2   ON i2.itemid  =SJd.SJdItmID

where CAST(sj.SJDate AS DATE) BETWEEN @Sdate AND @EDate         
--where CAST(sj.SJDate AS DATE) BETWEEN '2018-07-10' AND '2018-07-10'        
end
Posted
Updated 24-Jul-18 0:03am
Comments
Wendelius 24-Jul-18 5:02am    
Could post some example data and an example of the desired output?
Jayanta Modak 27-Jul-18 23:01pm    
I was very sick. So, I could not answer. Please forgive me ...
https://ibb.co/jfoJM8
https://ibb.co/fgOk18
Naga Sindhura 25-Jul-18 9:27am    
try to change the from part as follows:

from tbl_Stock_Journal SJ
INNER JOIN tbl_item AS i1 ON i1.itemid =SJ.SJItmID
inner JOIN tbl_Stock_Journal_Details AS SJD ON SJ.SJID = SJD.SJID AND SJd.SJdItmID = i1.itemid

If it does work, try to post some sample data.
Jayanta Modak 27-Jul-18 23:01pm    
I was very sick. So, I could not answer. Please forgive me ...
https://ibb.co/jfoJM8
https://ibb.co/fgOk18
Jayanta Modak 28-Jul-18 1:21am    
Sir it is showing no error, but it showing only same match of items of two tables

1 solution

I think what you are describing would be the results if you used a LEFT OUTER JOIN instead of the INNER JOINs that you currently have.

Have a look at these CodeProject articles to explain further
Visual Representation of SQL Joins[^]
Types of Join in SQL Server[^]

Not a lot of change to make...
SQL
select distinct SJ.SJID, i1.itemnm as itemnmsou, SJ.SJqty, SJ.SJPCS, SJ.SJRate, SJ.SJamt, SJ.SJleft, SJ.SJDate, SJ.nrr,
i2.itemnm as itemnmDES, SJD.SJDqty, SJD.SJDPCS, SJD.SJDRate, SJD.SJDamt, SJD.SJDstock, SJD.SJDdate

from tbl_Stock_Journal SJ

LEFT OUTER JOIN tbl_item AS i1   ON i1.itemid  =SJ.SJItmID
LEFT OUTER JOIN tbl_Stock_Journal_Details AS SJD   ON   SJ.SJID = SJD.SJID                             
LEFT OUTER JOIN tbl_item AS i2   ON i2.itemid  =SJd.SJdItmID

where CAST(sj.SJDate AS DATE) BETWEEN @Sdate AND @EDate         
 
Share this answer
 
Comments
Jayanta Modak 24-Jul-18 8:06am    
thanks sir for reply. I try your solution but result is same
please help me
CHill60 24-Jul-18 12:01pm    
To help you further we will need to see some sample data from each of your tables and what you are expecting to see
Jayanta Modak 27-Jul-18 23:01pm    
I was very sick. So, I could not answer. Please forgive me ...
https://ibb.co/jfoJM8
https://ibb.co/fgOk18

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