Click here to Skip to main content
15,902,112 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear all

i have two tables in the table channels three column ( channel_ID - ChannelName - ChannelsStatus) and in the second table SentLog three cloumn (Log_ID - Log_ChannelID - Log_Date) and relationship one to many between
Channels_Channel_ID=SentLog.Log_ChannelID

process is get channelID and do something then add it to SentLog Table in the current datetime

i want to select all channels.Channel_ID that ChannelsStatus=Active and Be older one hour

to specify datetime i use this this c# code to pass datetine in query

C#
DateTime dt=DateTime.Now.AddHour(-1).ToString(@"MM/dd/yyyy HH:mm:ss")


and this is my query

SQL
Select Channel_ID From Channels left join SentLog on Channels.Channel_ID=SentLog.Log_ChannelID and Log_Date < cast('12/02/2014  19:34:55 ' AS DATETIME) AND Channels.Channel_Status='Active'


Result : this query give me all channels that active and not active and not specific the date condition



Moved by PIEBALDconsult

channel table have four recorde

channel_ID ChannelName ChannelsStatus
1          r1          Active
2          r2          Active
3          r3          Active
1          r4          DisActive


Sentlog table have 3 records

Log_ID Log_ChannelID Log_Date
1      1             02/12/2014 10:36:11 pm
2      1             02/12/2014 10:37:11 pm
3      1             02/12/2014 10:38:11 pm


and if the current datetime is 02/12/2014 10:38:11 pm

i want to select all channelID that active and be older 1 hour
in this example it must give me channelsid 2,3
Posted
Updated 2-Dec-14 10:06am
v3
Comments
PIEBALDconsult 2-Dec-14 16:24pm    
So, maybe you want an INNER JOIN ?

Try this:
SQL
SELECT Channel_ID
FROM Channels AS Ch LEFT JOIN SentLog AS Sl ON Ch.Channel_ID=Sl.Log_ChannelID
WHERE Sl.Log_Date < @myDate AND Ch.Channel_Status='Active'


Now pass date as a SqlParameter[^].

For further information, please see:
WHERE[^]
Visual Representation of SQL Joins[^]
 
Share this answer
 
Comments
PIEBALDconsult 2-Dec-14 15:35pm    
I still think the second AND needs to be replaced with WHERE; not the first.
But of course a proper parameter is also required.
Maciej Los 2-Dec-14 16:37pm    
I'm wondering why? The result and the time of query execution should be the same (almost).
Try replacing the second AND with WHERE

Select Channel_ID From Channels left join SentLog on Channels.Channel_ID=SentLog.Log_ChannelID and Log_Date < cast('12/02/2014 19:34:55 ' AS DATETIME) AND WHERE Channels.Channel_Status='Active'
 
Share this answer
 
v2
Comments
Maciej Los 2-Dec-14 15:05pm    
Why second AND? I'd suggest to replace first AND.
PIEBALDconsult 2-Dec-14 15:13pm    
That's what I thought at first too.
beljk 2-Dec-14 15:13pm    
after change and to where . it's give me all active channels but not make a datetime condition
PIEBALDconsult 2-Dec-14 15:15pm    
Yeah, try replacing the second AND instead.
beljk 2-Dec-14 15:28pm    
i replaced it and the same result .. maybe channelID inside the sentlog table more than 3 times with the same datetime but diffrient in seconds and when replace and with where and greeter the current datetime to check it's give me the first three rows for this channelID then i added group by then give me it but one time
SQL
Select Channel_ID From Channels where NOT EXISTS (select SentLog.Log_ChannelID from SentLog where SentLog.Log_ChannelID=Channels.Channel_ID AND exists(select SentLog.Log_ChannelID from SentLog where SentLog.Log_Date >cast('" + DateTime.Now.AddHours(-1).ToString(@"MM/dd/yyyy  HH:mm:ss") + "' AS DATETIME) ) )AND Channels.Channel_Status='Active'";


this is solution
 
Share this answer
 
Comments
Maciej Los 3-Dec-14 5:47am    
How about query performance?
You're using EXISTS twice! It must be really slow...
Try this
SQL
declare @currentdatetime smalldatetime


select * from channels
where channel_ID not in (select Log_ChannelID
                        from SentLog
                        where Log_Date >= DATEADD(hh,-1,@currentdatetime)
    and ChannelsStatus = 'Active'
 
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