Is it possible to generate an extra column in SQL Query,
that adds suffix as "serial number" for distinguishing duplicate ids?
E.g: Expected Output
id (extra column) value
----------------------------
10000 10000-1
10000 10000-2
10001 10001-1
10001 10001-2
10002 10002-1
10003 10003-1
10003 10003-2
10003 10003-3
How to get the result as above formatted output.
Below is for BigQuery Standard SQL
select *, id || '-' || row_number() over(partition by id) extra_column
from table
If applied to sample data in your question - output is
Related
I'm building a page in APEX. In that page I have 2 items, "text field with autocomplete" Both of them are getting the LOV from the same table but different column. text_field_1 getting the id_1; text_field_2 getting the id_2
So If I input/select a value from text_field_1 I can set a value automatically to text_field_2 or vice versa. I can do one is to one relationship, like if I enter "bca" in text_field_2, I can set the correct value for text_field_1 which is "2"
But I'm having a problem for the one is to many relationship. If I enter "1" in text_field_1, It will return 2 values which are "abc,cba" I'm trying to figure it out on how I list the abc and cba so that I can still choose what value I want from the two. Please someone help me with this?
This is my sample table
id_1
id_type_1
id_2
id_type_2
datetime
1
employee_id
abc
employee_code
01-01-1000 00:00:00
1
employee_id
cba
employee_code
02-01-1000 11:00:00
2
employee_id
bca
employee_code
01-01-1000 12:00:00
3
employee_id
acd
employee_code
01-02-1000 14:00:00
Thank you so much in advance. I really need this
If you use list of values, then that's exactly what its name says - a list of values. They are supposed to return more than a single row, that's perfectly normal and expected situation.
Therefore, your LOV1 (which is a "source" of e.g. P1_ITEM_1) should
select distinct id_1 as display_value,
id_1 as return_value
from your_table
while LOV2 would then have P1_ITEM_1 as a parent LOV item and
select id_2 as display_value,
id_2 as return_value
from your_table
where id_1 = :P1_ITEM_1
To answer what you asked:
if I select id_1 = 1 then I will get the 'abc' OR if I select the other id_1 = 1, I will get 'cba'
Can you do that? Sure; the question is which one of abc or cba you want. What we usually do is to select any of them using min or max aggregate function, e.g.
select max(id_2)
from your_table
where id_1 = :P1_ITEM_1
(you'd get cba as a result in this case).
SELECT * FROM hr.NullValueCheck
ID Name
1 abc
2 abc
3 bcd
4 cde
https://oracle-base.com/articles/misc/all-any-some-comparison-conditions-in-sql
Query 1 :
SELECT *
FROM hr.NullValueCheck
where id > All (SELECT NULL FROM DUAL )
Nothing is coming.
But for below quesry. All records are coming while subquesry is returning is NULL same as like above query (SELECT NULL FROM DUAL )
Query 2:
SELECT *
FROM hr.NullValueCheck
where id > All (SELECT id from hr.NullValueCheck where id = 5)
Please explain me why Query 1 is returning No Records but Query 2 is returning all records.
As per my knowledge Query 1 should also return all records.
NULL is different from an empty set.
The first example is saying: "select all rows where the id is greater than all values of NULL". Or more simply, "where id is greater than 'NULL'`.
In SQL, 'NULL' generally has the semantics of "not known". If you don't know the value, then you don't know if a given id is larger. Hence, no rows are returned.
In the second example, instead has an empty set for comparison. An empty set is not NULL. Obviously, any number is greater than all numbers in an empty set. Hence, all rows are returned.
How do I count rows where a column value starts with a another column value ?
For example, I have table products shown below
---------------------------
id code abbreviation
---------------------------
1 AA01 AA
2 AB02 AB
3 AA03 AA
4 AA04 AB
---------------------------
I want to get the count of products whose code starts with abbreviation. A query like this
select count(*) from products where code ilike abbreviation+'%'
I am using postgresql 9.5.3
The string concatenation operator in postgresql is: ||
select count(*) from products where code like abbreviation || '%';
You can try:
select count(*) from products where code like '%'+abbreviation+'%'
But i am not sure why do you need this type of query.
I have a very very small database that I am needing to return a field from a specific row.
My table looks like this (simplified)
Material_Reading Table
pointID Material_Name
123 WoodFloor
456 Carpet
789 Drywall
111 Drywall
222 Carpet
I need to be able to group these together and see the different kinds (WoodFloor, Carpet, and Drywall) and need to be able to select which one I want and have that returned. So my select statement would put the various different types in a list and then I could have a variable which would select one of the rows - 1, 2, 3 for example.
I hope that makes sense, this is somewhat a non-standard implementation because its a filemaker database unfortunately, so itstead of one big SQL statement doing all I need I will have several that will each select an individual row that I indicate.
What I have tried so far:
SELECT DISTINCT Material_Name FROM MATERIAL_READING WHERE Room_KF = $roomVariable
This works and returns a list of all my material names which are in the room indicated by the room variable. But I cant get a specific one by supplying a row number.
I have tried using LIMIT 1 OFFSET 1. Possibly not supported by Filemaker or I am doing it wrong, I tried it like this - it gives an error:
SELECT DISTINCT Material_Name FROM MATERIAL_READING WHERE _Room_KF = $roomVariable ORDER BY Material_Name LIMIT 1 OFFSET 1
I am able to use ORDER BY like this:
SELECT DISTINCT Material_Name FROM MATERIAL_READING WHERE Room_KF = $roomVariable ORDER BY Material_Name
In MSSQL
SELECT DISTINCT Material_Name
FROM MATERIAL_READING
WHERE _Room_KF = 'roomVariable'
ORDER BY Material_Name
OFFSET N ROWS
FETCH NEXT 5 ROWS ONLY
where N->from which row does to start
X->no.of rows to retrieve which were started from (N+1 row)
I am working on an Oracle 11g database query that needs to retrieve a list of the highest NUM value between duplicated rows in a table.
Here is an example of my context:
ID | NUM
------------
1 | 1111
1 | 2222
2 | 3333
2 | 4444
3 | 5555
3 | 6666
And here is the result I am expecting after the query is executed:
NUM
----
2222
4444
6666
I know how to get the GREATEST value in a list of numbers, but I have absolutely no guess on how to group two lines, fetch the biggest column value between them IF they have the same ID.
Programmaticaly it is something quite easy to achieve, but using SQL it tends to be a litle bit less intuitive for me. Any suggestion or advise is welcomed as I don't even know which function could help me doing this in Oracle.
Thank you !
This is the typical use case for a GROUP BY. Assuming your Num field can be compared:
SELECT ID, MAX(NUM) as Max
FROM myTable
GROUP BY ID
If you don't want to select the ID (as in the output you provided), you can run
SELECT Max
FROM (
SELECT ID, MAX(NUM) as Max
FROM myTable
GROUP BY ID
) results
And here is the SQL fiddle
Edit : if NUM is, as you mentioned later, VARCHAR2, then you have to cast it to an Int. See this question.
The most efficient way I would suggest is
SELECT ids,
value
FROM (SELECT ids,
value,
max(value)
over (
PARTITION BY ids) max_value
FROM test)
WHERE value = max_value;
This requires that the query maintain a single value per id of the maximum value encountered so far. If a new maximum is found then the existing value is modified, otherwise the new value is discarded. The total number of elements that have to be held in memory is related to the number of ids, not the number of rows scanned.
See this SQLFIDDLE