Author |
Topic |
Villanuev
Constraint Violating Yak Guru
478 Posts |
Posted - 2013-06-19 : 23:33:49
|
Hi,I want to find the records between hours. dailyfor ex. start time will be 06:00 AM to 07:00 of 6/20/2013Next will be 07:00 AM to 08:00 of 6/20/2013and soon..What approach should i apply to get the desired result. Thanks.Sample Data:Create table #sample(ShippedDate datetime)Insert into #sampleSelect '2013-06-20 08:00:00.000' union allSelect '2013-06-20 08:36:50.447' union allSelect '2013-06-20 08:06:14.777' union allSelect '2013-06-20 08:57:00.000' union allSelect '2013-06-20 09:14:56.200' union allSelect '2013-06-20 09:15:05.527' union allSelect '2013-06-20 10:37:11.483' union allSelect '2013-06-20 10:55:19.170'select * from #samplewhere ShippedDate between 'hour' and 'hour' |
|
JanakiRam
Starting Member
22 Posts |
Posted - 2013-06-20 : 00:31:46
|
SELECT * FROM #sampleWHERE CAST(LEFT(CONVERT(TIME,ShippedDate),2) AS INT) BETWEEN 9 AND 10Check it once...JanakiRam |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-06-20 : 01:07:21
|
do you mean this?select min(ShippedDate) AS Start,MAX(ShippedDate) AS End,COUNT(*) from #sample group by (datediff(minute,DATEADD(dd,DATEDIFF(dd,0,ShippedDate),0),ShippedDate)/60) ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
Villanuev
Constraint Violating Yak Guru
478 Posts |
Posted - 2013-07-18 : 22:41:29
|
Hi Visakh,Sorry i forgot to reply and i need to recall this topic.I have a query that needs to pulled out records every hour.i think this can be achieved in subscription but how could i put this in a query..SELECT Trandate,field1, field2, etc..FROM MyTableWHERE Trandate ?? the report started to run at 6:00 AM. so from 6:00AM-7:00AM-------1 hr7:00AM-8:00AM-------1 hrand so on..i need the hourly data..THanks in advance.. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-07-19 : 00:49:54
|
ok...so each one hour it needs to pick everything from that time to 1hr backward?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
Villanuev
Constraint Violating Yak Guru
478 Posts |
Posted - 2013-07-22 : 21:38:10
|
Yes Visakh, thanks for your reply. how could be done this in a query. thanks. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-07-23 : 01:12:11
|
you mean this?select *from #sample where ShippedDate >= DATEADD(hh,-1,GETDATE())AND ShippedDate < DATEADD(ss,1,GETDATE()) ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
Villanuev
Constraint Violating Yak Guru
478 Posts |
Posted - 2013-07-23 : 01:43:04
|
Thanks Visakh.I will try this solution. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-07-23 : 01:46:36
|
ok...let me know how you got on------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
Villanuev
Constraint Violating Yak Guru
478 Posts |
Posted - 2013-07-23 : 01:56:05
|
Hi visakh,OK. btw, i tried to post a new thread but i'm not successful. i got an error posting the new thread. i have no problem using the reply.it's okay if i will post to this thread?my requirements is to get the average date using datediff with condition.i tried some scenario but could not get the exact result. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-07-23 : 02:05:30
|
quote: Originally posted by Villanuev Hi visakh,OK. btw, i tried to post a new thread but i'm not successful. i got an error posting the new thread. i have no problem using the reply.it's okay if i will post to this thread?my requirements is to get the average date using datediff with condition.i tried some scenario but could not get the exact result.
what do you mean by average date? Can you illustrate with some sample data?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
Villanuev
Constraint Violating Yak Guru
478 Posts |
Posted - 2013-07-23 : 02:23:41
|
[code]create table #sample(wrkctrid nvarchar(35),createddate datetime,lastupdate datetime,asustatus int)goinsert into #sampleselect 'Assembly 2','2013-07-10','2013-07-19',5 union allselect 'Assembly 2','2013-07-14','2013-07-23',5 union allselect 'Assembly 2','2013-07-17','2013-07-23',1 union allselect 'Assembly 2','2013-05-14','2013-05-30',6 union allselect 'Assembly 2','2013-07-20','2013-07-22',6 union allselect 'Assembly 2','2013-07-20','2013-07-23',6 union allselect 'Assembly 2','2013-07-18','2013-07-23',2;with cte as( select wrkctrid, createddate, lastupdate, asustatus, datediff(dd,createddate,lastupdate) as nodays from #sample order by asustatus ) Select wrkctrid, Sum(Case When ASUSTATUS=1 OR ASUSTATUS=2 OR ASUSTATUS=5 Then 1 else 0 End) As WIPQty, Sum(Case When ASUSTATUS=6 Then 1 else 0 End) As FailQty, Case When ASUSTATUS=1 OR ASUSTATUS=2 OR ASUSTATUS=5 then avg(nodays) End as avgdayswip, Case When ASUSTATUS=6 then AVG(nodays) End as avgdaysfail, COUNT(wrkctrid) as GrandTotalfrom ctegroup by wrkctrid, ASUSTATUSdesired result:----------------Wrkctrid-------wipqty---failqty---avg days wip----avg days fail----total qty---tot ave days--------------------------------------------------------------------------------------------Assembly 2-------4---------3--------7.25--------------7 -------------7------------7.14[/code][/code] |
|
|
Villanuev
Constraint Violating Yak Guru
478 Posts |
Posted - 2013-07-23 : 02:36:39
|
I trid this but the avg days for wip and fail is not correct..with cte as(selectfrom) Select t2.wrkctrid, SUM(case When t2.asustatus IN (1,2,5) then t2.wipqty else 0 end) as wqty, SUM(case When t2.asustatus =6 then t2.failqty else 0 end) as fqty, AVG(t2.nodays) as TotalAveDays, COUNT(t2.wrkctrid) as GrandTotal, AVG(Case when t2.asustatus IN (1,2,5) then t2.nodays else 0 end) as wavg, AVG(Case when t2.asustatus =6 then t2.nodays else 0 end) as favgfrom cte as t2group by wrkctrid |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-07-23 : 04:02:56
|
Can you post what should be your desired output for the sample data below?create table #sample(wrkctrid nvarchar(35),createddate datetime,lastupdate datetime,asustatus int)goinsert into #sampleselect 'Assembly 2','2013-07-10','2013-07-19',5 union allselect 'Assembly 2','2013-07-14','2013-07-23',5 union allselect 'Assembly 2','2013-07-17','2013-07-23',1 union allselect 'Assembly 2','2013-05-14','2013-05-30',6 union allselect 'Assembly 2','2013-07-20','2013-07-22',6 union allselect 'Assembly 2','2013-07-20','2013-07-23',6 union allselect 'Assembly 2','2013-07-18','2013-07-23',2 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
Villanuev
Constraint Violating Yak Guru
478 Posts |
Posted - 2013-07-23 : 05:13:23
|
This is the desired result:desired result:----------------Wrkctrid-------wipqty---failqty---avg days wip----avg days fail----total qty---tot ave days--------------------------------------------------------------------------------------------Assembly 2-------4---------3--------7.25--------------7 -------------7------------7.14 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-07-23 : 05:27:40
|
[code]SELECT wrkctrid,SUM(CASE WHEN asustatus IN (1,2,5) THEN 1 ELSE 0 END) AS wipqty,SUM(CASE WHEN asustatus = 6 THEN 1 ELSE 0 END) AS failqty,SUM(CASE WHEN asustatus IN (1,2,5) THEN DATEDIFF(dd,createddate,lastupdate) ELSE 0 END)*1.0/SUM(CASE WHEN asustatus IN (1,2,5) THEN 1 ELSE 0 END) AS avgdayswip,SUM(CASE WHEN asustatus = 6 THEN DATEDIFF(dd,createddate,lastupdate) ELSE 0 END)*1.0/SUM(CASE WHEN asustatus = 6 THEN 1 ELSE 0 END) AS avgdaysfail,COUNT(*) AS totalqty,DATEDIFF(dd,createddate,lastupdate)*1.0/COUNT(*) AS totalavgdaysFROM #sampleGROUP BY wrkctrid[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
Villanuev
Constraint Violating Yak Guru
478 Posts |
Posted - 2013-07-23 : 05:43:56
|
hi Visakh, just got an error..Msg 8120, Level 16, State 1, Line 7Column '#sample.createddate' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.Msg 8120, Level 16, State 1, Line 7Column '#sample.lastupdate' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-07-23 : 07:30:52
|
show me the code please?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
Villanuev
Constraint Violating Yak Guru
478 Posts |
Posted - 2013-07-23 : 08:53:29
|
I used your solution.SELECT wrkctrid,SUM(CASE WHEN asustatus IN (1,2,5) THEN 1 ELSE 0 END) AS wipqty,SUM(CASE WHEN asustatus = 6 THEN 1 ELSE 0 END) AS failqty,SUM(CASE WHEN asustatus IN (1,2,5) THEN DATEDIFF(dd,createddate,lastupdate) ELSE 0 END)*1.0/SUM(CASE WHEN asustatus IN (1,2,5) THEN 1 ELSE 0 END) AS avgdayswip,SUM(CASE WHEN asustatus = 6 THEN DATEDIFF(dd,createddate,lastupdate) ELSE 0 END)*1.0/SUM(CASE WHEN asustatus = 6 THEN 1 ELSE 0 END) AS avgdaysfail,COUNT(*) AS totalqty,DATEDIFF(dd,createddate,lastupdate)*1.0/COUNT(*) AS totalavgdaysFROM #sampleGROUP BY wrkctrid |
|
|
Villanuev
Constraint Violating Yak Guru
478 Posts |
Posted - 2013-07-23 : 21:39:47
|
Hi Visakh. just created a separate thread. i think my internet connection is working fine. please no need to reply on this thread. thanks. |
|
|
Next Page
|