Find first of a set of related records, dependent on a value in another row - sql

I have a table in which a row represents a subsection of a larger block of data.
Given an input parameter which identifies one of these rows, I would like to return a different row which represents the root record.
Specifically I would like to retrieve the first record in this set.
For example:
Find the row with column X value of Y.
Get the value A, of column Z.
Return the first row with column Z value of A.
What is the best way of doing this?
Two separate queries on the original table?
A single query on the original table?
Construct a new view that would enable a single query?
Something else?

You can get the same row, if you will not order you rows by some column, but general answer will be
select top 1 *
from table
where Z = (select Z from table where X = #parameter)
order by ???

Related

HIVE table - Get records which are closest to a given numeric value

From a hive table, I want records which are closest to a given value of each of the columns.
E.g.
The table has columns - total_score, avg_score, etc. I want to get records which have total_score and avg_score close or equal to "a given value".
Note - Table has approx. 183 million rows and I want 1,50,000 records which are closest/equal to the given value of each of the columns.
Please help me with the process of doing it.
The general concept needs to be top x, ordered by the absolute value of difference between parameter value and values in list.

Get latest data for all people in a table and then filter based on some criteria

I am attempting to return the row of the highest value for timestamp (an integer) for each person (that has multiple entries) in a table. Additionally, I am only interested in rows with the field containing ABCD, but this should be done after filtering to return the latest (max timestamp) entry for each person.
SELECT table."person", max(table."timestamp")
FROM table
WHERE table."type" = 1
HAVING table."field" LIKE '%ABCD%'
GROUP BY table."person"
For some reason, I am not receiving the data I expect. The returned table is nearly twice the size of expectation. Is there some step here that I am not getting correct?
You can 1st return a table having max(timestamp) and then use it in sub query of another select statement, following is query
SELECT table."person", timestamp FROM
(SELECT table."person",max(table."timestamp") as timestamp, type, field FROM table GROUP BY table."person")
where type = 1 and field LIKE '%ABCD%'
Direct answer: as I understand your end goal, just move the HAVING clause to the WHERE section:
SELECT
table."person", MAX(table."timestamp")
FROM table
WHERE
table."type" = 1
AND table."field" LIKE '%ABCD%'
GROUP BY table."person";
This should return no more than 1 row per table."person", with their associated maximum timestamp.
As an aside, I surprised your query worked at all. Your HAVING clause referenced a column not in your query. From the documentation (and my experience):
The fundamental difference between WHERE and HAVING is this: WHERE selects input rows before groups and aggregates are computed (thus, it controls which rows go into the aggregate computation), whereas HAVING selects group rows after groups and aggregates are computed.

Create a Patern Rank, Based on THE VALUE(s) IN A COLUMN

I have data like this -
I want to create one more column name Pattern and it looks like this -
How to create this column -
It is based on the column 'NAME'. It should Rank Starting from the Row having Parent_ID=(ContentID of the row having Parent ID 1)
It should increase by one and should make the same Group if it get's 'Only For'/'Not For' in the column NAME.
It will work on the group of Jill_Equipment_ID,Req_Group,Operand..
As my complete data in broken into these groups and it will have multiple Jill_Equipment_ID and for each ID multiple Req_Group and for each group multiple Operands.
Please help me in getting it solved. Thanks in advance.

How would you total the values in one column and keep a distinct value in another?

I have a database table that has multiple codes in one column that correspond to certain values in another column. For example, a particular code in column A corresponds to a value in column B. There are thousands of duplicate entries in column A that correspond to different values in column B. I want to add up all of the values in column B that have the particular code in column A, while only keeping one copy of the code from column A. You may think of the columns as key-value pairs, where column A contains the key and column B contains the value.
Basically, I want to add all the values in column B where column A is a specific value, and I want to do for this all of the unique "keys" in column A. I'm sure that this is a simple task; however, I am pretty new to SQL. Any help would be greatly appreciated.
Here is the result I'm looking for.
This should work:
SELECT
A,
SUM(B) AS sum_b
FROM [yourTable]
GROUP BY A
SELECT COLUMNA, SUM(ISNULL(COLUMNB,0)) AS TOTAL
FROM
dbo.TableName
GROUP BY COLUMNA

Select single row from database table with one field different and all other being same

I have a database table with 8 fields say Table(a,b,c,d,e,f,g,h).For some rows one of the field is different(say 4 different a values) while all other field values(b-h) in the schema are same.I have to make a table selecting just one rows from such rows with different a's but same b-h.That is I can select any one of the different a's and keep b-h same which they are and display it in table as 1 single row instead of 4.
SELECT MIN(a) a,b,c,d,e,f,g,h
FROM mytable
GROUP BY b,c,d,e,f,g,h