How to get specific column from associated value? - sql

I want to know how I can take value associated to company_working_plan (name). I'm using MySqlCommand but I don't know if is possible get the specific column value without iterate on all rows.
NAME | VALUE
COMPANY_WORKING_PLAN XX

You can simply do it like this:
select value from tablename where name = 'company_working_plan'

try this:
SELECT name, value
FROM Tablename
WHERE ID = '13'
that is for the row you highlighted in your image if you want every row and only the value in the column as well as the company name try this:
SELECT name, value
FROM tablename
You edited the question after I posted this and it is still a bit confusing but I think you are looking for:
SELECT name, value
FROM tablename
WHERE value = 'company_working_plan'

Related

How to get particular name from logs

I have code where I am getting case details. Now I need to get LogAgent name for the case.
But it is in the activity log table which have the columns CreatedBy, Type and this table has multiple rows (Logs).
Created by has different agent names and type has different values like LogComment.
I need to get first LogComment from the Type column and corresponding created by name.
Could any one please help how to do?
Below is my data and I need to get highlighted row
Sample Data
If you just want the first Comment for a particular user
Select Top 1 *
From YourTable
Where [Type]='Log Comment'
and [CreatedBy] = 'Benn'
Order By yourdatetimecolumn
If you want the 1st Comment for each User
Select Top 1 with ties *
From YourTable
Where [Type]='Log Comment'
Order By row_number() over (partition by CreatedBy order by yourdatetimecolumn)
Just an aside: Best to post sample data and desired results as text.

Replace "?" with "Ñ" in bigquery table

I need to perform an update in 21 rows. The way I came up with is using a query to get the new values and the id where to update the table. However, I'm getting UPDATE/MERGE must match at most one source row for each target row.
The data looks like this:
new_address address id
ÑANDU 123 ?ANDU 123 17-18
OTOÑAL 12 OTO?AL 12 10-16
The query I'm using looks like this:
UPDATE table
SET direccion = new_address
FROM (SELECT new_address FROM(SELECT REGEXP_REPLACE(address,r'\?',"Ñ") AS new_address,address,id
FROM table) WHERE address LIKE "%?%")
WHERE id IN (SELECT id FROM (SELECT REGEXP_REPLACE(address,r'\?',"Ñ") AS new_address,address,id
FROM table) WHERE address LIKE "%?%")
Why is this error and how can I achieve this?
There is no "join condition" in the WHERE clause (e.g. on the id column) to say which value generated by the FROM clause applies to a given target row.
But in this case you don't need a "joined update"; a simpler "searched update" would do:
UPDATE table
SET address = REGEXP_REPLACE(address,r'\?',"Ñ")
WHERE address LIKE "%?%"
If you did want to use joined update it would look something like:
UPDATE table
SET address = new_address
FROM (SELECT REGEXP_REPLACE(address,r'\?',"Ñ") AS new_address, id as upd_id
FROM table WHERE address LIKE "%?%")
WHERE id=upd_id
Not sure why you are trying some heavy workaround instead of just going as simple as below
UPDATE table
SET address = REGEXP_REPLACE(address, r'\?', "Ñ")
WHERE REGEXP_CONTAINS(address, r'\?')

Return query name Access SQL

In an already existing table I would like to create a new column containing the name of the query I am using.
For example:
ID Name
-----------
1 Max
2 Jack
The desired output is:
ID Name Query
-------------------
1 Max QueryName
2 Jack QueryName
I think you can use a static value field in your query, like this:
SELECT ID, NAME, "QueryName" AS Query
FROM yourTable;
When you just want to add a column with a static value in result of querying a table, above is the solution, If you want to create a Query with a special name then use it, just use its name in its query in creation time.
I mean when you are changing name of a query; also edit its query with using that name.
But, I don't think this is a good idea to store a query then change its name and use its name as a result of its query or other queries, It seems you want to store a string value somewhere then use it by adding it in another query, So:
Create another table(QueryTable) like:
QueryId | QueryName
--------+--------------
1 | Query Name 1
2 | Query Name 2
Use it in your other queries:
SELECT t.*, q.QueryName
FROM yourTable t CROSS JOIN QueryTable q
WHERE q.QueryId = 1;
And I suggest this because it's better to use a tool in its using zone, when you want to store data and use it result some information; use a table for that. HTH ;).

Access combo box to return value from another table

I have a table called initial_eval that has a field called Partno.
What I would like to do is match the Partno value in a field
also called 'Partno' from a table called 'update'.
What I would then like to do is return the value in the
'consumption' field of 'update' where the 'Partno' values match.
I'm ok with SQL and join stuff, but I can't crack this one
Please help.
SELECT U.CONSUMPTION FROM UPDATE U
WHERE U.PARTNO IN (SELECT PARTNO FROM INITIAL_EVAL)
This assumes there is no condition while fetching from INITIAL_EVAL
SELECT U.CONSUMPTION FROM UPDATE U
WHERE U.PARTNO = (SELECT PARTNO FROM INITIAL_EVAL WHERE **some condition**)
In this case you need to frame the inner query in a such a way that it returns exactly one row which will automatically map to your CONSUMPTION table row.

get values from sql rows with same name but different values

i need help concerning a sql query. first of all, i have a database with the following structure (example):
ID NAME VALUE
123 ABC_A Text Text Text
123 ABC_A Some more Text
123 ABC_A Even more Text
123 ABC_B some other text
now, i want to get all the different values of rows with the name "ABC_A". i tried to get those via group by and having, without success.
IS this what you want?
SELECT DISTINCT Value
FROM tableName
WHERE ID = 123 AND Name = 'ABC_A'
but if the value of the ID and Name are unique then you can omit distinct (to avoid overkilling the server)
SELECT Value
FROM tableName
WHERE ID = 123 AND Name = 'ABC_A'
Additional to John, if you use the keyword Distinct you onle get DIFFERENT Values, therefore
SELECT DISTINCT Value
FROM tableName
WHERE ID = 123 AND
Name = 'ABC_A'
i want to get all the different values of rows with the name "ABC_A"
This would be for example:
SELECT value, count(value) FROM tbl WHERE name = 'ABC_A' GROUP BY value;
If you do not need the count of times one value appears, remove it, or use DISTINCT:
SELECT DISTINCT value FROM tbl WHERE name = 'ABC_A';
If you want the different values of rows by ID also,
SELECT value, count(value)
FROM tbl
WHERE id = 123 AND name = 'ABC_A'
GROUP BY value;
Or if you want "ALL" the different values (with duplicates too) remove the GROUP BY (and you no longer can use the count(), which would be always 1):
SELECT value FROM tbl WHERE id = 123 AND name = 'ABC_A';
The GROUP BY statement is used in conjunction with the aggregate functions to group the result-set by one or more columns.
In above case why dont you use simple where clause
select * from <tableName> where name ='ABC_A'