Find Value in Pivoted LookupTable - sql

I'm trying to figure out the best way to query from a pivoted lookup table.
Ideally, you'd have a look up table with 3 columns
min, max, value
1, 2, a
2, 3, b
3, 4, c
here you can write code to pull the correct output:
select value from table
where input >= min and input < max
so if input = 1.5, then value = a and if input = 2.5, value = b. As the rows are disjoint,
However, our table has to be constructed in the following manner as this is a janky one off situation.
1,2,3,4
a,b,c,-
How would I create a query that would find the value in this type of table?
Thanks for looking!

However, our table has to be constructed in the following manner as
this is a janky one off situation.
You have two options:
Unpivot this table and then query from a result of this subquery in the same manner SELECT value FROM ( subquery ) ...:
SELECT 1 as min, 2 as max, "1" as value FROM table1
UNION ALL
SELECT 2 as min, 3 as max, "2" asvalue FROM table1
Union All
SELECT 3 as min, 4 as max, "3" asvalue FROM table1
Demo: http://sqlfiddle.com/#!17/b6b4d/2
| min | max | value |
|-----|-----|-------|
| 1 | 2 | a |
| 2 | 3 | b |
| 3 | 4 | c |
You can create a view using the above query and run queries against this view.
Assumming that there is only 1 row in this table- build a queries like this one:
SELECT CASE
WHEN input >=1 AND input < 2 THEN "1"
WHEN input >=2 AND input < 3 THEN "2"
WHEN input >=3 AND input < 4 THEN "3"
END As value
FROM Table1

I believe you are looking for a CASE WHEN statement
Select case when min >= 1 and max <3 then 'A'
when min >= 2 and max <4 then 'B'
when min >= 3 and max <5 then 'C'
else NULL
end Value
From table
Hope this helps

Related

Get previous value from column A when column B is not null in Hive

I have a table tableA below
ID number Estimate Client
---- ------
1 3 8 A
1 NULL 10 Null
1 5 11 A
1 NULL 19 Null
2 NULL 20 Null
2 2 70 A
.......
I would like to select previous row of Estimate column when number column is not null. For instance, when number = 3, then pre_estimate = NULL, when number = 5, then pre_estimate = 10, and when number = 2, then pre_estimate = 20.
The query below does not seem to return the correct answer in Hive. What should be correct way to do it?
select lag(Estimate, 1) OVER (partition by ID) as prev_estimate
from tableA
where number is not null
Consider the table with following structure:
number - int
estimate - int
order_column - int
order_column is taken as a column on which you want to sort your table rows.
Data in table:
number estimate order_column
3 8 1
NULL 10 2
5 11 3
NULL 19 4
NULL 20 5
2 70 6
I used the following query and got the result you have mentioned.
SELECT * FROM (SELECT number, estimate, lag(estimate,1) over(order by order_column) as prev_estimate from tableA) tbl where tbl.number is not null;
As per my understanding, I didn't find the reason to partition by id, that's why I haven't considered ID in the table.
The reason you were getting wrong results is due to the reason that where clause in main query will select only the records with number as not null and then it computes lag function, but you need to consider all the rows when computing the lag function and then you should select rows with number as not null.

In sequelize, how do I select records that match all values that i am searching for?

As an example, I have the following table:
T | S
------
1 | 5
1 | 6
1 | 7
2 | 6
2 | 7
3 | 6
Query: array [1,2]
I want to select all values in S that have the value 1 AND 2 in the T Column.
So in the above example I should get as a result (6,7) because only 6 and 7 have for column T the values 1 and 2.
But i do not want to have 5 in my results as 5 does not have 2 in the T column.
How would I do this in sequelize?
how do i make (1,2) to be used as an array?
Either you insert the array joined as comma-separated literal into the query text (variant 1) or you join the array into one string literal and transfer it iinto the query as a parameter (variant 2).
Variant 1
SELECT s
FROM sourcetable
WHERE t IN (1,2) -- separate filter values
GROUP BY s
HAVING COUNT(DISTINCT t) = 2 -- unique values count
Variant 2
SELECT s
FROM sourcetable
WHERE FIND_IN_SET(t, '1,2') -- separate filter values
GROUP BY s
HAVING COUNT(DISTINCT t) = 2 -- unique values count
If (s,t) is unique then DISTINCT keyword may be removed.

SQL query to group by integer value

I am trying to do an SQL query to count the total numbers of rows that contain each integer in a 'metric' column.
I.e. my data looks like:
RowName | Metric
Row 1 | 3
Row 2 | 3
Row 3 | 6
Row 4 | 6
And I want to find how many rows have a 'metric' value of 3, and how many have a value of 6:
Metric | Count
6 | 2
3 | 2
I have tried:
SELECT COUNT(Metric) FROM tablename GROUP BY Metric
But that returns an error. I know this is really simple and probably been answered many times before. As I am new to SQL I have tried searching, but probably don't quite know what I am searching for.
Thanks.
Below is for BigQuery Standard SQL
#standardSQL
SELECT Metric, COUNT(Metric) `Count`
FROM `project.dataset.your_table`
GROUP BY Metric
See it in action here
SELECT metric, [count]=COUNT(*)
FROM tablename
GROUP BY Metric

Compare column entry to every other entry in the same column

I have a Column of values in SQLite.
value
-----
1
2
3
4
5
For each value I would like to know how many of the other values are larger and display the result. E.g. For value 1 there are 4 entries that have higher values.
value | Count
-------------
1 | 4
2 | 3
3 | 2
4 | 1
5 | 0
I have tried nested select statements and using the Count(*) function but I do not seem to be able to extract the correct levels. Any suggestions would be much appreciated.
Many Thanks
You can do this with a correlated subquery in SQLite:
select value,
(select count(*) from t t2 where t2.value > t.value) as "count"
from t;
In most other databases, you would use a ranking function such as rank() or dense_rank(), but SQLite doesn't support these functions.

how to get the even and odd column separately with separate column by query

I have an input:
id
1
2
3
4
5
6
7
8
9
10
I want get even and odd columns separately by columns in specified output like this
id col
1 2
3 4
5 6
7 8
9 10
here id and col are separate columns id contains the odd number and col contains the even number for specified input
SELECT MIN(id) as id, MAX(id) as col
FROM YourTable
GROUP BY FLOOR((id+1)/2)
For IDs 1 and 2, (id+1)/2 are 2/2 = 1 and 3/2 = 1.5, respectively, and FLOOR then returns 1 for both of them. Similarly, for 3 and 4, this is 2, and so on. So it groups all the input rows into pairs based on this formula. Then it uses MIN and MAX within each group to get the lower and higher IDs of the pairs.
Joined the table on itself
select *
from yourTable tA
left join yourTable tb on tA.id = (tB.id - 1)
where tA.id % 2 <> 0
If you use SQL you can try:
SELECT CASE WHEN column % 2 = 1
THEN column
ELSE null
END AS odds,
CASE WHEN column % 2 = 2
THEN column
ELSE null
END AS even
FROM yourtable
but not exactl as you ask
To show odd:
Select * from MEN where (RowID % 2) = 1
To show even:
Select * from MEN where (RowID % 2) = 0
Now, just join those two result sets and that's it.
Source