Select previous and next row with condition in MSSQL - sql-server

ID BegCha EnCha Val
10 20 30 250
10 30 40 140
10 50 60 189
20 10 20 250
20 20 30 157
20 30 40 199
20 40 50 70
Find all the rows where value is above 249
select * from table where Val >249
select One row previous and one row after if the values are 50 of 250
select * from table where id in (select * from table where Val >249) and Val > 149
What I am expecting to get is as under:
ID BegCha EnCha Val
10 20 30 250
10 30 40 140
10 50 60 189
20 10 20 250
20 20 30 157

With this table
create table prueba(
id int ,
BegCha int,
EndCha int,
Val int);
this data
id BegCha EndCha Val
10 20 30 250
10 30 40 140
10 50 60 189
20 10 20 250
20 20 30 157
20 30 40 199
20 40 50 70
and this Query
WITH pruebaNumerada AS
(
SELECT
ROW_NUMBER() OVER(ORDER BY id ASC) AS RowNumber,
id, BegCha, EndCha, val
FROM prueba
)
SELECT b.id, b.BegCha, b.EndCha, b.val
FROM pruebaNumerada a
inner join pruebaNumerada b on b.RowNumber between a.RowNumber-1 and a.RowNumber+1
WHERE a.val >=250;
I obtain this result
id BegCha EndCha val
10 20 30 250
10 30 40 140
10 50 60 189
20 10 20 250
20 20 30 157

Are looking for something like this?
SELECT *
FROM table
WHERE id IN
(
SELECT DISTINCT id
FROM table
WHERE Val >249
)
AND Val > 149

Related

Join on Multiple columns with duplicates

I have two tables Master table and the Matched Master Table based on the same saldate , saltime , lctid ,masid the counts has to be pulled up
For count(masid,saldate,saltime,lctid)>1 , for masid 121 in master table there are 5 records in Master Matched 3 records, I need to get the output which are not in Mactched and how much in Master and the total amount with the masid
Master
ID Saldate SalTime lctid masid Sal_amt
101 1/1/2000 100 120 121 15
102 1/1/2000 100 120 121 25
103 1/1/2000 100 120 121 100
118 1/1/2001 120 118 201 25
119 1/1/2009 302 222 187 60
104 1/1/2000 100 120 121 125
108 1/1/2000 100 120 121 22
Master matched
ID Saldate SalTime lctid masid Sal_Amt
101 1/1/2000 100 120 121 15
102 1/1/2000 100 120 121 25
118 1/1/2001 120 118 201 25
119 1/1/2009 302 222 187 60
OP1-Master OP2-Master matched
Masid Count(iD) SalAMt Masid Count(iD) Sal_Amt
121 3 247 121 2 40

Spliting one row of record into multiple between two date

Can anybody help me out to do this:
My table data is like
Datefiled Field1 Field2 Field2 Field4
01-01-2012 ABC 10 15 17
01-04-2013 PQR 7 80 65
01-05-2014 XYZ 15 25 77
The output would look something like this
Month/Year Field1 Field2 Field2 Field4
01/2012 ABC 10 15 17
02/2012 ABC 10 15 17
03/2012 ABC 10 15 17
04/2012 ABC 10 15 17
.....
03/2013 ABC 10 15 17
04/2013 PQR 7 80 65
05/2013 PQR 7 80 65
06/2013 PQR 7 80 65
07/2013 PQR 7 80 65
.......
04/2014 PQR 7 80 65
05/2014 XYZ 15 25 77
Just create a calendar table, either one row per day or one row per month, and you can use a tally table (=virtual, constructed for example with a CTE) too and do something like this:
select
*
from
calendar c
cross apply (
select top 1 *
from yourtable y
where y.Datefiled < c.calendardate
order by Datefiled desc
) y
;WITH datat as (
SELECT *
FROM (VALUES
('2012-01-01', 'ABC', 10, 15, 17),('2012-12-12', 'LKJ', 16, 12, 13),
('2013-01-15', 'CVB', 16, 11, 19),('2013-04-01', 'PQR', 7, 80, 65),
('2014-04-24', 'XYZ', 15, 25, 77),('2014-05-17', 'DEF', 12, 40, 50)
) as t(Datefiled, Field1, Field2, Field3, Field4)
), cte AS (
SELECT CAST(MIN(Datefiled) AS DATE) as date_,
CAST(MAX(Datefiled) AS DATE) AS maxd
FROM datat
UNION ALL
SELECT DATEADD(day,1,date_), maxd
FROM cte
WHERE date_ < maxd
)
SELECT CAST([Month] as nvarchar(2)) + '/' + CAST([Year] as nvarchar(4)) as [Month/Year],
Field1,
Field2,
Field3,
Field4
FROM (
SELECT DISTINCT
MONTH(date_) as [Month],
YEAR(date_) as [Year],
Field1,
Field2,
Field3,
Field4
FROM cte c
LEFT JOIN (
SELECT TOP 1 WITH TIES CAST(DATEADD(MONTH, DATEDIFF(MONTH, 0, d1.Datefiled ), 0) as date) as [DateStart],
CAST(DATEADD(MILLISECOND, -3, DATEADD(MONTH, DATEDIFF(MONTH, 0, d2.Datefiled), 0)) as date) as [DateEnd],
d1.Field1,
d1.Field2,
d1.Field3,
d1.Field4
FROM datat d1
LEFT JOIN datat d2
ON d1.Datefiled < d2.Datefiled
ORDER BY ROW_NUMBER() OVER (PARTITION BY d1.Datefiled ORDER BY d2.Datefiled)
) as d
ON c.date_ between d.[DateStart] and ISNULL(d.[DateEnd],c.date_)
WHERE Field1 IS NOT NULL
) as t
ORDER BY [Year], [Month]
OPTION (MAXRECURSION 0)
Output:
Month/Year Field1 Field2 Field3 Field4
---------- ------ ----------- ----------- -----------
1/2012 ABC 10 15 17
2/2012 ABC 10 15 17
3/2012 ABC 10 15 17
4/2012 ABC 10 15 17
5/2012 ABC 10 15 17
6/2012 ABC 10 15 17
7/2012 ABC 10 15 17
8/2012 ABC 10 15 17
9/2012 ABC 10 15 17
10/2012 ABC 10 15 17
11/2012 ABC 10 15 17
12/2012 LKJ 16 12 13
1/2013 CVB 16 11 19
2/2013 CVB 16 11 19
3/2013 CVB 16 11 19
4/2013 PQR 7 80 65
5/2013 PQR 7 80 65
6/2013 PQR 7 80 65
7/2013 PQR 7 80 65
8/2013 PQR 7 80 65
9/2013 PQR 7 80 65
10/2013 PQR 7 80 65
11/2013 PQR 7 80 65
12/2013 PQR 7 80 65
1/2014 PQR 7 80 65
2/2014 PQR 7 80 65
3/2014 PQR 7 80 65
4/2014 XYZ 15 25 77
5/2014 DEF 12 40 50
(29 row(s) affected)

Average value depending on cumulative value from other column aka running total

I've sifted through the various sql-server tagged threads using AVERAGE and Cumulative as search terms. Various desperate answers, but I can't cobble them together for my needs. The use case is to find the initial average value (cumulative value/cumulative days on) for a time period when cumulative days on is greater than 60 and less than 90.
Below is a table where ID identifies the object, VALUE is the amount reported on a monthly basis and DAYSON is the number of days in that month where the object ran to produce the value. YEARMONTH is date value on which on can sort.
ID VALUE DASYON YEARMONTH
1 166 27 201502
1 1 2 201505
1 569 19 201507
1 312 19 201508
2 364 27 201502
2 328 31 201503
2 242 29 201504
2 273 31 201505
2 174 30 201506
2 188 25 201507
2 203 25 201508
3 474 28 201502
3 521 31 201503
3 465 30 201504
3 473 31 201505
3 434 30 201506
3 404 31 201507
I would like to create a summary table that averages the cumulative value divided by the cumulative days uniquely for each ID where cumulative days is greater than 60 and less than 90. Below is a table that with the cumulative values. (I generated this in Excel)
ID VALUE cumValue DASYON cumDaysOn YEARMONTH
1 166 166 27 27 201502
1 1 167 2 29 201505
1 569 736 19 48 201507
1 312 1048 19 67 201508
2 364 364 27 27 201502
2 328 692 31 58 201503
2 242 934 29 87 201504
2 273 1207 31 118 201505
2 174 1381 30 148 201506
2 188 1569 25 173 201507
2 203 1772 25 198 201508
3 474 474 28 28 201502
3 521 505 31 59 201503
3 465 535 30 89 201504
3 473 566 31 120 201505
3 434 596 30 150 201506
3 404 627 31 181 201507
I try this based on other threads:
SELECT
ID,
Value,
SUM(Value) OVER (ORDER BY ID, YearMonth) [cumValue],
DaysOn,
SUM (DaysOn) OVER (Order by ID, YearMonth) as cumDaysOn,
YearMonth
FROM table
WHERE DAYSON > 0 and Liquid > 0 and YearMonth > 201501
GROUP BY ID, YearMonth, Value, DaysOn
ORDER BY ID, yearmonth
I can't get it to iterate over the ID; it just keeps summing down the column. If I could create a table or view like the one above, then I could always use a select statement and divide cumvalue by cumdayson.
Below is a table to show where I would get the initial average value (InititalAverageValue) based on the criteria:
ID VALUE cumValue DASYON cumDaysOn YEARMONTH InitalAvgValue
1 166 166 27 27 201502
1 1 167 2 29 201505
1 569 736 19 48 201507
1 312 1048 19 67 201508 55
2 364 364 27 27 201502
2 328 692 31 58 201503
2 242 934 29 87 201504 32
2 273 1207 31 118 201505
2 174 1381 30 148 201506
2 188 1569 25 173 201507
2 203 1772 25 198 201508
3 474 474 28 28 201502
3 521 505 31 59 201503
3 465 535 30 89 201504 18
3 473 566 31 120 201505
3 434 596 30 150 201506
3 404 627 31 181 201507
Ultimately what I desire is table as such:
ID InitalAvgValue
1 55
2 32
3 18
Thanks in advance for any help.
The crux is that you need a running total. There are several approaches to calculating running totals, but they have various tradeoffs between simplicity and performance. The "best" approach depends on the expected size of your data set and whether you are using SQL Server 2012 or an earlier version. The following article describes some different options along with the pros and cons:
http://sqlperformance.com/2012/07/t-sql-queries/running-totals
Here's a quick example using correlated subqueries, which may be reasonable for small data sets, but likely would not scale well to larger data:
SELECT
ID,
ROUND(AVG(CAST(CumulativeValue AS FLOAT) / CAST(CumulativeDaysOn AS FLOAT)), 1) AS Average
FROM
(
SELECT
ID,
Value,
DaysOn,
(SELECT SUM(Value) FROM ExampleTable t2 WHERE t1.ID = t2.ID and t2.YearMonth <= t1.YearMonth) AS CumulativeValue,
(SELECT SUM(DaysOn) FROM ExampleTable t2 WHERE t1.ID = t2.ID and t2.YearMonth <= t1.YearMonth) AS CumulativeDaysOn
FROM
ExampleTable t1
) AS ExampleWithTotals
WHERE
CumulativeDaysOn > 60 AND CumulativeDaysOn < 90
GROUP BY
ID
ORDER BY
ID
;
Output:
ID Average
1 15.6
2 10.7
3 16.4

How do I get a total from a SQL query with a caculated field

I have a table that lists visits to a clinic. I'd like to get a "histogram" of sorts showing how frequently patients visit the clinic along with totals. Here's some sample code (tested under MS SQL Server 2005) to show what I'm talking about:
CREATE TABLE #test (
visit_id int IDENTITY(1,1),
patient_id int
);
DECLARE #num_patients int;
SELECT #num_patients = 1000 + ABS(CHECKSUM(NEWID())) % 250;
INSERT INTO #test (patient_id)
SELECT TOP 15 PERCENT ABS(CHECKSUM(NEWID())) % #num_patients
FROM sysobjects a, sysobjects b;
-- SELECT COUNT(*) AS total_visits FROM #test;
-- SELECT COUNT(DISTINCT patient_id) AS distinct_patients FROM #test;
SELECT CASE GROUPING(num_pat_visits) WHEN 1 THEN 'Total'
ELSE CAST(num_pat_visits AS varchar(5)) END AS num_pat_visits,
COUNT(*) AS num_patients, num_pat_visits * COUNT(*) AS tot_pat_visit
FROM
(SELECT patient_id, COUNT(*) AS num_pat_visits FROM #test GROUP BY patient_id) a
GROUP BY num_pat_visits WITH ROLLUP
ORDER BY CAST(num_pat_visits AS int) DESC;
This gets me almost to where I want:
num_pat_visits num_patients tot_pat_visit
-------------- ------------ -------------
60 1 60
54 2 108
52 2 104
51 4 204
50 3 150
49 3 147
48 7 336
47 7 329
46 15 690
45 15 675
44 29 1276
43 36 1548
42 45 1890
41 45 1845
40 59 2360
39 71 2769
38 51 1938
37 72 2664
36 77 2772
35 74 2590
34 72 2448
33 82 2706
32 90 2880
31 74 2294
30 69 2070
29 47 1363
28 30 840
27 27 729
26 26 676
25 21 525
24 13 312
23 4 92
22 5 110
21 4 84
20 2 40
18 2 36
Total 1186 NULL
However, I can't seem to get SQL Server to display the total number of visits where it says NULL on the total row.
Any ideas?
I think you can just do:
sum(num_pat_visits) as tot_pat_visit
SELECT CASE GROUPING(num_pat_visits) WHEN 1 THEN 'Total'
ELSE CAST(num_pat_visits AS varchar(5)) END AS num_pat_visits,
COUNT(*) AS num_patients,
--num_pat_visits * COUNT(*) AS tot_pat_visit
sum(num_pat_visits) as tot_pat_visit
FROM
(SELECT patient_id, COUNT(*) AS num_pat_visits FROM #test GROUP BY patient_id) a
GROUP BY num_pat_visits WITH ROLLUP
ORDER BY CAST(num_pat_visits AS int) DESC;

joining two tables [duplicate]

This question already has an answer here:
Closed 12 years ago.
Possible Duplicate:
How to join two tables
table 1
Date StartingAum
07/01/2010 120
08/01/2010 220
09/01/2010 320
table 2
Date DepContr withdra
01/01/2010 60 15
02/01/2010 70 25
03/01/2010 80 15
04/01/2010 30 89
05/01/2010 40 15
06/01/2010 25 85
07/01/2010 16 17
08/01/2010 19 21
09/01/2010 68 79
the output should be
Date StartingAum DepContr withdra
01/01/2010 0 60 15
02/01/2010 0 70 25
03/01/2010 0 80 15
04/01/2010 0 30 89
05/01/2010 0 40 15
06/01/2010 0 25 85
07/01/2010 120 16 17
08/01/2010 220 19 21
09/01/2010 320 68 79
i need the output exactly similar to that
DECLARE #Table1 table ([date] datetime, StartingAum int)
DECLARE #Table2 table ([date] datetime, DepContr int, withdra int)
INSERT #Table1 VALUES ('07/01/2010', 120)
INSERT #Table1 VALUES ('08/01/2010', 220)
INSERT #Table1 VALUES ('09/01/2010', 320)
INSERT #Table2 VALUES ('01/01/2010', 60 , 15)
INSERT #Table2 VALUES ('02/01/2010', 70 , 25)
INSERT #Table2 VALUES ('03/01/2010', 80 , 15)
INSERT #Table2 VALUES ('04/01/2010', 30 , 89)
INSERT #Table2 VALUES ('05/01/2010', 40 , 15)
INSERT #Table2 VALUES ('06/01/2010', 25 , 85)
INSERT #Table2 VALUES ('07/01/2010', 16 , 17)
INSERT #Table2 VALUES ('08/01/2010', 19 , 21)
INSERT #Table2 VALUES ('09/01/2010', 68 , 79)
SELECT
t2.[Date]
,ISNULL(t1.StartingAum, 0) AS StartingAum
,t2.DepContr
,t2.withdra
FROM #Table2 t2
LEFT JOIN #Table1 t1 ON t2.[Date] = t1.[Date]
ORDER BY t2.[Date]
OUTPUT:
Date StartingAum DepContr withdra
----------------------- ----------- ----------- -----------
2010-01-01 00:00:00.000 0 60 15
2010-02-01 00:00:00.000 0 70 25
2010-03-01 00:00:00.000 0 80 15
2010-04-01 00:00:00.000 0 30 89
2010-05-01 00:00:00.000 0 40 15
2010-06-01 00:00:00.000 0 25 85
2010-07-01 00:00:00.000 120 16 17
2010-08-01 00:00:00.000 220 19 21
2010-09-01 00:00:00.000 320 68 79
I think you should have a look at this page: http://www.w3schools.com/sql/sql_union.asp
That might be helpful?

Resources