SQL query put nth match in new column - sql

I am using SQL in Microsoft access. The table may contain more than one value for a bar code. I want to a query that returns one row with each unique bar code and columns for each of the first 5 values. Right now, I have a query returning the last value or the first value with min or max. How can I show the second result in the next column? or the nth result in that column? This is in Access but any other SQL help would be appreciated.
Current table:
Current query:
SELECT table.barcode, MIN(table.value)
FROM table
GROUP BY table.barcode
Current output:
Goal query output:

You can use aggregation:
SELECT table.barcode,
MIN(table.value),
IIF(MIN(table.value) = MAX(table.value), MAX(table.VALUE), NULL)
FROM table
GROUP BY table.barcode

Related

print the values that are common from both the tables of a column

I want to print the values that are common from both the tables of a column.
The issue is one column's substring value matches with the other column's string.
Printing the subquery alone fetches the right values (proving the substring query is correct) but I think the entire query after the where clause needs changing.
Kindly suggest.
Code:
select distinct sd.sourceworkitemid
from u_prodstypetest pst, sdidata sd
where sd.keyid1 = 'S-20210719-00000003'
and sd.sourceworkitemid in (select substr(testmethodid,0,INSTR(testmethodid,'|',1)-1) from u_prodstypetest);
I want to create a substring of a value of a column in a table and compare it with a column in another table. But since it is a substring the where clause of column1=column2 does not suffice and hence I wroe the subquery to fetch the substring which if run throws an error in 'in' because the subquery return >1 values.

How I can use where Condition in SQL I have comma separated value in A columns in multiple rows

Column1 EventTypes_pKey
Are 5,3
Test 1,4,5
test 1,3,5
If I am using
Select * from Table name where EventTypes_pKey in('5,1,4)
then I want that record where these value belongs the column.
How I can use where condition on the basis of EventTypes_pKey this is my Varchar column.
I want If I am selecting 5,3,4 the there should be all three row data.
Please help me.
If you are using Postgres, you can do this by by converting the value into an array and then using the overlaps operator &&
select *
from badly_designed_table
where string_to_array(eventtypes_pkey, ',')::int[] && array[5,3,4];
Online example

How do I select a value in a key:value pair within a list in a column using SQL?

In a table called payouts, there is a column stripeResponseData where the data is in the following structure:
{"id":"tr_1BlSHbGQXLV7RqqnHJffUVO0","object":"transfer","amount":39415,"amount_reversed":0,"balance_transaction":"txn_1BlSHbGQXfV7AqqnGi2o7UiY","created":1516239215,"currency":"usd","description":null,"destination":"acct_1BWWAmAzms5xPfV9","destination_payment":"py_1BlSHbAzms5xkfV91RHAOrno","livemode":true,"metadata":{},"reversals":{"object":"list","data":[],"has_more":false,"total_count":0,"url":"/v1/transfers/tr_1BlSHbYQXLV7AqqnHJffUVO0/reversals"},"reversed":false,"source_transaction":null,"source_type":"card","transfer_group":null}
Within my SQL SELECT statement, I want to return only the value of the key "destination". How do I write my SQL query?
My desired result of the query:
SELECT "stripeResponseData" FROM payouts [...]
(where I don't know how to write [...]) should look like the following (assume we have 3 rows with different values on "destination"):
acct_1BWWAmAzms5xPfV9
acct_1AY0phDc9pCDpLR8
acct_1AwG3VL7DXxftOaS
How do I extract that value from the list within the stripeResponseData column?
See this sqlfiddle. This query will fetch the ID from stripResponseData where the id is a specific id (Probably not very useful, but does show you how to select and query):
SELECT data->>'id' FROM stripeResponseData WHERE data #> '{"id":"tr_1BlSHbGQXLV7RqqnHJffUVO0"}';
Because you mentioned your data was a string, you need to to type conversions to query/use it correctly. See this sqlfiddle:
SELECT data::jsonb->>'id' FROM stripeResponseData WHERE data::jsonb #> '{"id":"tr_1BlSHbGQXLV7RqqnHJffUVO0"}';
Per your edit, you can simply query destination in almost the exact same way. This will get all the id's from stripeResponseData where destination = acct_1BWWAmAzms5xPfV9:
SELECT data::jsonb->>'id' FROM stripeResponseData WHERE data::jsonb #> '{"destination":"acct_1BWWAmAzms5xPfV9"}';

Why does this oracle query return null? (avg on number field)

I'm pulling my hair out this morning, as I'm trying to select a simple average from a single field from a table in an Oracle database. My table has 31 rows, the column in question is called AGE and I just want an average. The column is of type "number" and there are no nulls in it.
SELECT AVG(AGE)
FROM COLLECTIONS.CUSTOMERS
This query always returns null. I have also tried:
SELECT SUM(AGE)/COUNT(AGE)
FROM COLLECTIONS.CUSTOMERS
with the same result. Any help is greatly appreciated!
I have tried creating a sample table with single column (age). I kept data type int and number both and getting expected result:
INTEGER : Demo
NUMBER : Demo
NUMBER (with same datatype as OP): Demo

similar to last() in sql, what can i use in derby database?

Select Last(column_name) from table_name will return the last value in the column in standard SQL. What is a similar query that will work in derby to fetch the last value in the column?
Please find the sample table below: 'secondcol' is the column name in the table.
secondcol
33
45
78
Select Last(secondcol) from table_name in sql will return 78 as output. If i want to get similar output in javadb/derby database, how can i query it? I don't want to change any order in the column values.
To select last row in table is little bit different then it is in pure SQL:
SQL:
SELECT * FROM tableName ORDER BY id DESC LIMIT 1;
DERBY:
SELECT * FROM tablename ORDER BY id DESC FETCH FIRST ROW ONLY;
Have a nice day ;)
Is there some unique key on the table where you could form the question as "give me the secondcol value on the row with the max key value"? If so, there is a technique that will work in any database engine - the idea is to concatenate the key plus any desired result data, perform a min/max, then extract the result data.
See here and here.