SQL selecting values between two columns with a list - sql

I'm attempting to find rows given a list of values where one of the values is in a range between two of the columns, as an example:
id column1 column2
1 1 5
2 6 10
3 11 15
4 16 20
5 21 25
...
99 491 495
100 496 500
I'd like to give a list of values, e.g. (23, 83, 432, 334, 344) which would return the rows
id column1 column2
5 21 25
17 81 85
87 431 435
67 331 335
69 341 345
The only way I can think of doing this so far has been to split each into it's own call by doing
SELECT * FROM TableA WHERE (column1 < num1 AND num1 < column2)
However this scales quite poorly when the list of numbers is around several million.
Is there any better way of doing this?
Thanks for the help.

Putting millions of numbers into the SQL command itself would be unwieldy.
Obviously, you have to put the numbers into a (temporary) table.
Then you can just join the two tables:
SELECT *
FROM TableA JOIN TempTable
ON TempTable.Value BETWEEN TableA.column1 AND TableA.column2;

Related

Consolidating 5 table into one

So I have 5 tables which need to go into Result; 2-digit, 3-digit, 4-digit, 5-digit, and 6-digit. They are of the same structure. Would the following code accomplish the task:
Insert into Result select * from 2-digit, 3-digit, 4-digit, 5-digit, 6-digit;
Or does it need to look like this
Insert into Result select * from 2-digit, select * from 3-digit, select * from 4-digit,select * from 5-digit,select * from 6-digit;
Below is some sample data. The desired result is to simply consolidate these three tables into one with no manipulation of the data or rows. the end result should have 12 rows.
2 digit
x job code employment
32 10 4569
32 11 4521
3 digit
x job code employment
32 101 1203
32 102 3366
32 111 1000
32 112 3521
4 digit
32 1011 1203
32 1025 1000
32 1028 2366
32 1111 500
32 1112 500
32 1123 2899
32 1124 45
32 1125 577
You can solve this problem with SELECT . . . UNION:
SELECT * FROM table1
UNION ALL
SELECT * FROM table2
UNION ALL
SELECT * FROM table3
. . . and so on
If the table structures are not exactly the same (they have a different number of columns or the columns have slightly different names, or are in a different order) then you will have to replace * with the column names explicitly listed.
Having the five tables in the first place is a flaw in the database design -- why not keep them all in one table and then just SELECT out the rows you want.
use SUBSTRING()
INSERT INTO Results
SELECT SUBSTRING('102100',1,2) [2-digits],
SUBSTRING('102100',1,3) [3-digits],
SUBSTRING('102100',1,4) [4-digits],
SUBSTRING('102100',1,5) [5-digits],
'102100' [6-digits]
FROM tablename
Result
2-digits 3-digits 4-digits 5-digits 6-digits
10 102 1021 10210 102100
substitute '102100' with the column name you have from your table

Get MAX value of a group for each month TSQL

I have a TSQL query which gives a max value of the trend data for the given result set. I would like to get this data based on each month's result. (I have a datetime column in the result set ). If I select data for three months it has to give this vaules for each month. Right now it looks for the max values and give the same result for all the months.
Below is the expression i use to get the trend result. It's part of select statement with number of other columns.
select col1, col2, sampledatecollected, (Select MAX(AvailMemSlope) FROM SlopeCTE) * MetricID + (Select MAX (AvailMemIntercept) FROM InterceptCTE) AS AvailMemTrend
I think i might need to do something like this but given the expression i'm little confused as to how to get the desired results
select name, max(value)
from tbl1
group by name
CPUTrend id the data i get from the expression i specified in the first query.
sample data:
Date AVGCPU MAXCPU CPUTrend
8/22/2016 20 40 44
8/23/2016 20 40 44
8/24/2016 20 40 44
8/25/2016 20 40 44
9/22/2016 20 50 44
9/23/2016 20 50 44
9/24/2016 20 50 44
Expected result:
Date AVGCPU MAXCPU CPUTrend
8/22/2016 20 40 32
8/23/2016 20 40 32
8/24/2016 20 40 32
8/25/2016 20 40 32
9/22/2016 20 50 44
9/23/2016 20 50 44
9/24/2016 20 50 44
Right now all i get is 44 as it's the maximum value.
I think you just want a subquery:
with t as (
select col1, col2, sampledatecollected,
(Select MAX(AvailMemSlope) FROM SlopeCTE) * MetricID +
(Select MAX (AvailMemIntercept) FROM InterceptCTE) AS AvailMemTrend
from ?? -- the SQL in your question is incomplete
)
select year(sampledatecollected), month(sampledatecollected),
max(AvailMemTrend)
from t
group by year(sampledatecollected), month(sampledatecollected)
order by year(sampledatecollected), month(sampledatecollected);

Exclude the specific kind of record

I am using SQL Server 2008 R2. I do have records as below in a table :
Id Sys Dia Type UniqueId
1 156 20 first 12345
2 157 20 first 12345
3 150 15 last 12345
4 160 17 Average 12345
5 150 15 additional 12345
6 157 35 last 891011
7 156 25 Average 891011
8 163 35 last 789521
9 145 25 Average 789521
10 156 20 first 963215
11 150 15 last 963215
12 160 17 Average 963215
13 156 20 first 456878
14 157 20 first 456878
15 150 15 last 456878
16 160 17 Average 456878
17 150 15 last 246977
18 160 17 Average 246977
19 150 15 additional 246977
Regarding this data, these records are kind of groups that have common UniqueId. The records can be of type "first, last, average and additional". Now, from these records I want to select "average" type of records only if they have "first" or "additional" kind of reading in group. Else I want to exclude them from selection..
The expected result is :
Id Sys Dia Type UniqueId
1 156 20 first 12345
2 157 20 first 12345
3 150 15 last 12345
4 160 17 Average 12345
5 150 15 additional 12345
6 157 35 last 891011
7 163 35 last 789521
8 156 20 first 963215
9 150 15 last 963215
10 160 17 Average 963215
11 156 20 first 456878
12 157 20 first 456878
13 150 15 last 456878
14 160 17 Average 456878
15 150 15 last 246977
16 160 17 Average 246977
17 150 15 additional 246977
In short, I don't want to select the record that have type="Average" and have only "last" type of record with same UniqueId. Any solution?
Using EXISTS operator along correlated sub-query:
SELECT * FROM dbo.Table1 t1
WHERE [Type] != 'Average'
OR EXISTS (SELECT * FROM Table1 t2
WHERE t1.UniqueId = t2.UniqueId
AND t1.[Type] = 'Average'
AND t2.[Type] IN ('first','additional'))
SQLFiddle DEMO
Try something like this:
SELECT * FROM MyTable WHERE [Type] <> 'Average'
UNION ALL
SELECT * FROM MyTable T WHERE [Type] = 'Average'
AND EXISTS (SELECT * FROM MyTable
WHERE [Type] IN ('first', 'additional')
AND UniqueId = T.UniqueId)
The first SELECT statement gets all records except the ones with Type = 'Average'. The second SELECT statement gets only the Type = 'Average' records that have at least one record with the same UniqueId, that is of type 'first' or 'additional'.

Can't get unique values joining two tables

I have 2 tables that I need to join and select the unique rows from. Here is a sample of my data: (there are more columns)
tbl1:
MB# MBName PCCNo_PRI Primary_IP PCCNo_SEC Secondary_IP ID
100 name 0 10.1.9.10 30 10.1.9.10 1
103 name3 17 10.1.9.27 47 10.1.9.67 4
403 name13 17 10.1.9.27 47 10.1.9.67 14
tbl2:
RTU PCC#_PRI PCC#_SEC STATION ADDRESS
15 0 30 6
52 12 42 1
53* 17 47 1
54 18 48 1
63 9 39 2
69* 17 47 2
I need to join the two tables and get the unique RTU(s) in tbl2 for a given MB# in tbl1.
Query =
SELECT t1.MB#,t2.RTU,t2.[Device Manufacturer],t2.PCC#_PRI,t2.PCC#_SEC,t2.[STATION ADDRESS]
INTO C300_RTU_MASTERBLK_Map
FROM mbm_PCDIMasterBlk_tbl as t1, dbo.WOA_PCC_Conn_tbl as t2
WHERE t1.PCCNo_PRI = t2.PCC#_PRI
I am getting duplicate rows for tbl2 53 and 69 (* above). 53 ends up with 2 entries; one to 103 and one 403 (69 gets same). How can I query this for unique RTU(s) to MB#?
The duplicate rows appears because you join on "17" which gives 2 rows on each side
Then, as it stands, you can't with that SELECT list.
How do you decide which t1.MB# you want for the t2 columns?
There is no secondary JOIN column that I can see.
So the best you can get is use MAX (or MIN) to pick either 403 or 103.
SELECT
MAX(t1.MB#) AS MB#,
t2.RTU,t2.[Device Manufacturer],t2.PCC#_PRI,t2.PCC#_SEC,t2.[STATION ADDRESS]
INTO C300_RTU_MASTERBLK_Map
FROM
dbombm_PCDIMasterBlk_tbl as t1
JOIN
dbo.WOA_PCC_Conn_tbl as t2 ON t1.PCCNo_PRI = t2.PCC#_PRI
GROUP BY
t2.RTU,t2.[Device Manufacturer],t2.PCC#_PRI,t2.PCC#_SEC,t2.[STATION ADDRESS]

How to do permutation in tsql (set based)

I have the below input
PlayerID MatchPlayed RunsMade
-------- ----------- --------
1 10 200
2 5 100
3 8 24
4 30 50
The output will be
Combined Players Combined Match Played Combined runs Made
---------------- --------------------- ------------------
1 10 200
1,2 15 300
1,3 18 224
1,4 40 250
1,2,3 23 324
1,2,4 45 350
1,3,4 48 274
1,2,3,4 53 374
2 5 100
2,3 13 124
2,4 35 150
2,3,4 43 174
3 8 24
3,4 38 74
4 30 50
The Combined Match Played column is the sum of the values of Match Played column of those players. e.g. for Combined Played 1,2 the Combined Match Played value is 10 + 5 = 15.
similarly, Combined Runs Made is the sum of the Runs MAde column of the individual players. e.g. for the same example, the Combined Runs MAde column is 200 +100 =300.
Thanks
Setup:
create table Input(PlayerId int, MatchPlayed int, RunsMade int)
insert Input
select 1, 10, 200
union all select 2, 5, 100
union all select 3, 8, 24
union all select 4, 30, 50
Query:
with cte(Combined, PlayerId, MatchPlayed, RunsMade)
as
(
select cast(PlayerId as varchar(500)), PlayerId, MatchPlayed, RunsMade
from Input
union all
select cast(cte.Combined + ',' + cast(inp.PlayerId as varchar) as varchar(500)), inp.PlayerId, inp.MatchPlayed + cte.MatchPlayed, inp.RunsMade + cte.RunsMade
from cte
join Input inp on
cte.PlayerId < inp.PlayerId
)
select Combined, MatchPlayed, RunsMade
from cte
order by Combined