Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
The table structure is like this:
slp_no  doc_type    clearing Date          ui               batch_no

   1       2        2020-02-25  2020022571350010008500001     7135001-000850

   1       3        2021-03-26  2021032571350010008500001     7135001-000850 
   1       3        2021-03-25  2021032571350010008500001     7135001-000850
   1       3        2021-03-25  2021032571350010008500002     7135001-000850
   1       3        2021-03-25  2021032571350010008500003     7135001-000850

   2       2        2020-02-25  2021032571350010008500002     7135001-000850
   2       3        2021-03-26  2021032671350010008500002     7135001-000850 
   2       3        2021-03-26  2021032671350010008500003     7135001-000850 
   3       2        2020-02-25  2021032571350010008500003     7135001-000850 
   3       3        2021-03-25  2021032571350010008500004 	  7135001-000850
So for my result set I would want:
slp_no  doc_type    clearing_Date             ui            batch_no

   1       2        2020-02-25  2020022571350010008500001     7135001-000850
   1       3        2021-03-25  2021032571350010008500001     7135001-000850
   1       3        2021-03-25  2021032571350010008500002     7135001-000850
   1       3        2021-03-25  2021032571350010008500003     7135001-000850
   3       2        2020-02-25  2021032571350010008500003     7135001-000850 
   3       3        2021-03-25  2021032571350010008500004 	  7135001-000850 


What I have tried:

SQL
SELECT * FROM IMG_PDC_OUTWARDCLEARING AS T1
WHERE 1=1 
AND (BATCH_NO = '7135001-000850') 
AND (CLEARING_DATE = '2021-03-25') 
AND (DOC_TYPE = '3')

UNION

SELECT *
FROM IMG_PDC_OUTWARDCLEARING AS T2
WHERE 1=1 
AND (BATCH_NO = '7135001-000850') 
AND (DOC_TYPE = '2') 
Posted
Updated 29-Apr-21 22:59pm
v4

1 solution

There is no need for a UNION - just include the second criterion in your WHERE statement e.g.
SQL
SELECT * FROM IMG_PDC_OUTWARDCLEARING AS T1
WHERE 1=1 
AND (BATCH_NO = '7135001-000850') 
AND (CLEARING_DATE = '2021-03-25') 
AND (DOC_TYPE = '3' OR DOC_TYPE = '2')
Note where the brackets are when combining AND and OR clauses

[EDIT]
I have now realised that this is essentially a repost of https://www.codeproject.com/Questions/5301349/Get-unique-data-between-two-dates

If this solution is not appropriate for your problem, then try using the "Have a Question or Comment?" link next to this solution to explain what the problem actually is. Don't just post another question with no useful information in it - you need to explain your criteria for selecting the specific records

[EDIT 2] I have now worked out what your criteria are and this query should return the values you want
SQL
select slp_no,doc_type,clearing_Date,ui,batch_no
from IMG_PDC_OUTWARDCLEARING
where batch_no = '7135001-000850'
and slp_no IN (1,3)
and [clearing_Date] between '2020-02-25' and '2021-03-25'
Some points to note
- it is always better to list the columns you want than to use select *
- DOC_TYPE is not relevent here - your results exclude slp_no = 2
- Some experts claim that using BETWEEN with date ranges is a bad idea. I personally cannot see a problem with using it in this context as the date format takes away cultural difference issues, and by using Date type you won't run into problems caused by the time element of a date. Note however that if your database table has CLEARING_DATE Datetime then you will need to either convert to a date or use some other adjustment to make sure you include the "whole" day at each end of the range. E.g.
SQL
and CAST([clearing_Date] as Date) between '2020-02-25' and '2021-03-25'
 
Share this answer
 
v3
Comments
chamindat 30-Apr-21 9:00am    
doc_type = 2 clearing date can be differ so we cant use like this,it can be any date

nd CAST([clearing_Date] as Date) between '2020-02-25' and '2021-03-25'
CHill60 30-Apr-21 12:05pm    
I'm sorry I don't understand your comment.
Explain in words what you are trying to get out of the data. The query I have given extracts the records you said you wanted, and only those records.

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