Click here to Skip to main content
15,885,435 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a table like below and I want to get data by id with status 'sent' and ' ' only and not with status 'delivered'.

message_thread_id | status
229 |
229 | delivered
229 | sent
229 | delivered
229 | delivered
240 | sent
240 | sent
1044 |
1044 |
1044 |
1068 | delivered

What I have tried:

select message_thread_id, created_at, status
from message
where date(created_at)=date(now()) and status <> 'delivered'
Posted
Updated 11-Nov-21 1:44am
Comments
Richard Deeming 10-Nov-21 9:45am    
Aside from the fact that there's no created_at column in your table, you haven't told us what the problem is.
OriginalGriff 10-Nov-21 9:56am    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with.
Use the "Improve question" widget to edit your question and provide better information.
Member 15329613 10-Nov-21 11:02am    
What is the question?

Despite the lack of a clear description of your problem I think you are trying to understand how to check for more than one status in your where clause. It also doesn't help that the code you have posted is not SQL (it looks to be VB?).

I set up your data as follows (for simplicity I have set all of the dates to today)
SQL
declare @message table (message_thread_id int, created_at datetime, [status] varchar(10))
insert into @message (message_thread_id, created_at, [status]) values
(229,  getdate(),''),
(229, getdate(), 'delivered'),
(229, getdate(), 'sent'),
(229, getdate(), 'delivered'),
(229, getdate(), 'delivered'),
(240, getdate(), 'sent'),
(240, getdate(), 'sent'),
(1044, getdate(),''),
(1044, getdate(),''),
(1044, getdate(),''),
(1068, getdate(), 'delivered');
When I run the code you have allegedly tried I get two error messages
Quote:
--'date' is not a recognized built-in function name.
-- 'now' is not a recognized built-in function name.
So the first thing I did was convert that to SQL ...
SQL
select message_thread_id, created_at, status
from @message
where cast(created_at as date)= cast(getdate() as date) and status <> 'delivered'
Unsurprisingly that gives me the rows that are not 'delivered' - which includes the blank status rows. Which is what you want.

However, you originally said that
Quote:
I want to get data by id with status 'sent' and ' ' only
By saying "not 'delivered'" this is getting you the results you want sort of by accident. If you have another status (e.g. 'Pending') then those rows will also be included in your results, which is not what you want.

Reword your requirements slightly - you want data with status 'sent' OR with status '' and that will give you a clue how to progress
SQL
select message_thread_id, created_at, status
from @message
where cast(created_at as date)= cast(getdate() as date) 
and (status = 'delivered' or status = '')
Note the use of parentheses to group the conditions for [status] - I'm not going to expand on that here but if you want to see what happens when you miss them out add the following data into the demo
SQL
insert into @message (message_thread_id, created_at, [status]) values
(229,  getdate() + 1,''),
(229, getdate() + 1, 'delivered'),
(229, getdate() + 1, 'sent'),
(229, getdate() + 1, 'delivered'),
(229, getdate() + 1, 'delivered'),
(240, getdate() + 1, 'sent'),
(240, getdate() + 1, 'sent'),
(1044, getdate() + 1,''),
(1044, getdate() + 1,''),
(1044, getdate() + 1,''),
(1068, getdate() + 1, 'delivered');
This article goes into more detail SQL: Using Parentheses with And / OR condition is necessary with where clause to gives expected result - Codepedia[^]

An alternative method - since you know exactly which [status] values you want, you could use an IN condition in your WHERE clause e.g.
SQL
select message_thread_id, created_at, status
from @message
where cast(created_at as date)= cast(getdate() as date) and status in ('delivered','');
Of course, after all that, if this has nothing to do with your question then please clarify your point as requested
 
Share this answer
 
Ok, if I understand you correctly, you want all messages that hasn't been delivered yet.

Try this:
SQL
SELECT  message_thread_id
       ,created_at
       ,STATUS
FROM    message m
WHERE   DATE (created_at) = DATE (now())
        AND NOT EXISTS (
            SELECT  *
            FROM    message m1
            WHERE   status = 'delivered'
                AND m.message_thread_id = m1.message_thread_id
            )
 
Share this answer
 

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