SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 If value > 200 Then add to next record
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

amanalyn
Starting Member

USA
9 Posts

Posted - 12/03/2012 :  13:34:04  Show Profile  Reply with Quote
I am trying to find a way to add the value (if less then 200) to the next record/row of data in a query. I and getting the value using count.

The reason for this is we have late starts on Sunday's so it throws the numbers off when the percentage per week is done by day.

My query so far for week to date is this:
WITH stat_tab AS
(
SELECT DISTINCT CONVERT(VarChar(20), vwPouringUnion.Date, 101) AS Date, COUNT(vwPouringUnion.PT) AS Data, COUNT(DISTINCT vwPouringUnion.HN) AS Heats

FROM vwPouringUnion

WHERE (vwPouringUnion.PT BETWEEN CONVERT(VarChar(20), DATEADD(hh, - 2, DATEADD(wk, DATEDIFF(wk, 0, DATEADD(hh, - 22, GETDATE())), 0)), 101) AND
CONVERT(VarChar(20), DATEADD(d,-1, GETDATE()) + '10:00:00 PM', 101))

GROUP BY CONVERT(VarChar(20), vwPouringUnion.Date, 101)
HAVING (COUNT(vwPouringUnion.PT) > 200)
)
,
mean_tab AS
(
SELECT AVG(Data) AS [Average] FROM stat_tab
)
,
dev_tab AS
(
SELECT STDEV(Data) AS [StDev] FROM stat_tab
)

SELECT st.Date
,st.Data
,mt.[Average]
,mt.[Average] + (2*dt.[StDev]) AS UWL2
,mt.[Average] + (3*dt.[StDev]) AS UCL
,mt.[Average] - (2*dt.[StDev]) AS LWL2
,mt.[Average] - (3*dt.[StDev]) AS LCL

FROM stat_tab st
CROSS JOIN mean_tab mt
CROSS JOIN dev_tab dt

ORDER BY Date

Edited by - amanalyn on 12/03/2012 13:37:27

Lamprey
Flowing Fount of Yak Knowledge

4614 Posts

Posted - 12/03/2012 :  15:20:16  Show Profile  Reply with Quote
Sample data and expected output?
Go to Top of Page

amanalyn
Starting Member

USA
9 Posts

Posted - 12/04/2012 :  09:57:39  Show Profile  Reply with Quote
Sample Data now;
Date Data
11/18/2012 5
11/19/2012 1049
11/20/2012 1013
11/21/2012 784
11/26/2012 1099
11/27/2012 1151
11/28/2012 1200
11/29/2012 1046
11/30/2012 1083
12/2/2012 15
12/3/2012 1034

Expected output would be;
Date Data
11/19/2012 1054
11/20/2012 1013
11/21/2012 784
11/26/2012 1099
11/27/2012 1151
11/28/2012 1200
11/29/2012 1046
11/30/2012 1083
12/3/2012 1049

Edited by - amanalyn on 12/04/2012 09:59:18
Go to Top of Page

jimf
Flowing Fount of Yak Knowledge

USA
2869 Posts

Posted - 12/04/2012 :  10:17:06  Show Profile  Reply with Quote
This seems a bit awkward

DECLARE @Table TABLE (Col1 DATE, Col2 int)
INSERT INTO @Table
VALUES
('11/18/2012', 5 ),
('11/19/2012', 1049),
('11/20/2012', 1013),
('11/21/2012', 784),
('11/26/2012', 1099),
('11/27/2012', 1151),
('11/28/2012', 1200),
('11/29/2012', 1046),
('11/30/2012', 1083),
('12/2/2012', 15),
('12/3/2012', 1034)



SELECT t.Col1,
CASE WHEN t.Col2 > 200
THEN t3.Col2
ELSE t.Col2 + t3.Col2
END
FROM @table t
CROSS APPLY (select top 1 t2.Col2
from @table t2
where t.Col1 < t2.Col1
order by t.col2 asc
) t3

WHERE CASE WHEN t.Col2 > 200
THEN t3.Col2
ELSE t.Col2 + t3.Col2
END >200
ORDER BY Col1 asc

Jim


Everyday I learn something that somebody else already knew
Go to Top of Page

amanalyn
Starting Member

USA
9 Posts

Posted - 12/04/2012 :  11:03:25  Show Profile  Reply with Quote
quote:
Originally posted by jimf

This seems a bit awkward

DECLARE @Table TABLE (Col1 DATE, Col2 int)
INSERT INTO @Table
VALUES
('11/18/2012', 5 ),
('11/19/2012', 1049),
('11/20/2012', 1013),
('11/21/2012', 784),
('11/26/2012', 1099),
('11/27/2012', 1151),
('11/28/2012', 1200),
('11/29/2012', 1046),
('11/30/2012', 1083),
('12/2/2012', 15),
('12/3/2012', 1034)



SELECT t.Col1,
CASE WHEN t.Col2 > 200
THEN t3.Col2
ELSE t.Col2 + t3.Col2
END
FROM @table t
CROSS APPLY (select top 1 t2.Col2
from @table t2
where t.Col1 < t2.Col1
order by t.col2 asc
) t3

WHERE CASE WHEN t.Col2 > 200
THEN t3.Col2
ELSE t.Col2 + t3.Col2
END >200
ORDER BY Col1 asc

Jim


Everyday I learn something that somebody else already knew



I am sorry but I am not able to follow what you mean with the t, t2, t3 part of that code, I am fairly new to SQL and SSRS still. I am getting my number by COUNT(vwPouringUnion.PT), and the date is vwPouringUnion.Date. Maybe I forgot to mention that I am writing this in a SSRS query not a view in SQL?

Edited by - amanalyn on 12/04/2012 11:07:36
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.09 seconds. Powered By: Snitz Forums 2000