I know that I can select elements between two rows like:
SELECT * FROM table WHERE id BETWEEN 20 AND 30
Can someone tell me how can I select using one query between 4 rows like between 20 and 30 and between 40 and 50?
SELECT * FROM table WHERE id BETWEEN 20 AND 30 OR id BETWEEN 40 and 50
Related
Let's say I have a table testpivot that looks like this, with district and metric a unique key:
id
district
metric
value
1
a
work
40
2
a
hours
80
3
b
work
50
4
b
hours
85
I create a view:
CREATE VIEW vpivot
AS
SELECT *
FROM(
SELECT district, metric, value
FROM testpivot
)t
PIVOT(SUM(value) for metric IN(work,hours)) as p
So querying from the view looks like this:
district
work
hours
a
40
80
b
50
85
Is there a way to make an insert query like this work:
INSERT INTO vpivot SELECT 'c',20,80
Select * from vpivot
UNION
SELECT 'c',20,80
After numerous joins building a query, I stuck in a table of products with 3 column identifies ID-Color-Size and the column of data barcode like
Id
Color
Size
Barcode
34
40
4
5205barcode1
34
40
4
extradata1
34
40
5
5205barcode2
34
40
5
extradata2
34
41
4
5205barcode3
34
41
4
extradata3
35
40
5
5205barcode4
35
40
5
extradata4
34
40
3
data4
35
39
5
data5
35
40
3
data6
I need to keep the unique combinations of ID-Color-Size with barcode (starting with '5205%') and remove the rows with same id-color-size (the extradata1-5 are considered duplicate).
The final table would have unique combinations of ID-Color-Size-barcode1-4 and data4-5-6
If I understand correctly you need a window function to order duplicates of id/color/size by the barcode and only select those where the barcode starts 5205:
with p as (
select *,
Row_Number() over(partition by id, color, size order by case when barcode like '5205%' then 1 end desc) rn
from t
)
select id, color, size, barcode
from p
where rn=1
ID Date Value Average
1 10/5/2017 15 15
2 10/6/2017 25 20
3 10/7/2017 35 25
4 10/8/2017 45 35
5 10/9/2017 55 45
6 10/10/2017 65 55
7 10/11/2017 75 65
If this is my table, I want average to be a computed column and its formula in general is average of previous 3 row's Value column.
(Ex. for 2nd row it is (25+15)/2 )
How can i do such a thing in computed column? Is there any better way to achieve this.
Thanks in advance.
i would go with a view and use avg windows function
select
id,
date,
value,
avg(value) over (order by id)
from table
Updated answer: you could use frames clause like below
Working Demo
;with cte(id,date,val)
as
(
select 1 ,'10/5/2017' , 15 UNION ALL
select 2 ,'10/6/2017' , 25 UNION ALL
select 3 ,'10/7/2017' , 35 UNION ALL
select 4 ,'10/8/2017' , 45 UNION ALL
select 5 ,'10/9/2017' , 55 UNION ALL
select 6 ,'10/10/2017', 65 UNION ALL
select 7 ,'10/11/2017', 75
)
SELECT *,avg(VAL) OVER (ORDER BY id rows between 2 PRECEDING and current row ) FROM CTE
I have TrsViewPay view with this sample data:
id DocTypeRef TrsDocPayItemref
---------------------------------
1 10 16
2 20 17
3 30 18
4 40 1
First I don't want to show record with DocTypeRef 40.
Then I don't want to show the records where the id is equal with that record's TrsDocPayItemref.
So I want to show this result (without record 1 and 4)
id DocTypeRef TrsDocPayItemref
---------------------------------
2 20 17
3 30 18
Ravi's answer is close, but I think this one will be better:
SELECT Id, DocTypeRef, TrsDocPayItemref
FROM TrsViewPay
WHERE DocTypeRef <> 40
AND Id <> (SELECT TrsDocPayItemref FROM TrsViewPay WHERE DocTypeRef = 40)
You can go for inner queries or sub queries. You can first Select the value of
DocTypeRef and then compare it with id. use first point as inner query. After that you can retrieve data using the result of first query.
You can try this:
SELECT *
FROM TrsViewPay
WHERE DocTypeRef!=40
AND NOT TrsDocPayItemref IN (SELECT id FROM TrsViewPay )
I have an SQL SELECT that left-joins several tables together, which results in an output with redundant data.
Example query:
SELECT
A.ID, B.ID
FROM A
LEFT JOIN B ON B.ParentID=A.ID
FETCH NEXT 4 ROWS ONLY
Example output:
A.ID B.ID
1 10
1 20
2 30
2 40
My problem is that I want to limit the number of rows from the A table, not from the actual output. In short, I would like to have an output like this when I ask for 4 rows:
A.ID B.ID
1 10
1 20
2 30
2 40
3 50
3 60
4 70
4 80
Any advice?
UPDATE:
Here is a fiddle that could help explaining the problem:
Fiddle
i have applied query on actual data, use sub query to get top 4 result.
declare #temp table
(aId int, bId int)
insert into #temp
values
(1,10),
(1,20),
(2,30),
(2,40),
(3,50),
(3,60),
(4,70),
(4,80),
(5,90),
(6,100)
select * from #temp
where aId in
(select distinct top 4 aId from #temp where aId > 1)