Return query name Access SQL - 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 ;).

Related

How to return input + string if value doesn't exist in DB?

I want to query a table for an array with user names and I want to not only get the name and user ids if found but also if it wasn't found, I want a string.
I have the following query using Bigquery
SELECT
user_id,
name as user_name
FROM table
WHERE name IN ('userone', 'usertwo', 'userfourtysix')
Current Output
user_ID
user_name
001
userone
002
usertwo
Desire Output
user_ID
user_name
001
userone
002
usertwo
NULL
userfourtysix
or a string with input + 'doesn't exist'
Is that possible with SQL?
It is possible in SQL but not with the IN clause, since you are filtering out any values that don't match with the IN clause.
You need to rewrite your query to take advantage of JOINS.
Create a CTE with the list of names you want to manage
Perform a left join so that values on the CTE are preserved even if there's no match in the right table.

SELECT the FROM table with a sub select and modify the resulting table name

I have the follwing given two tables which can not be changed.
1: DataTypes
+----------------------+-----------------------+
| datatypename(String) | datatypetable(String) |
+----------------------+-----------------------+
Example data:
+-----------+------------+
| CycleTime | datalong |
+-----------+------------+
| InjTime1 | datadouble |
+-----------+------------+
2: datalong_1 (data model does not matter here)
I want to make a query now that reads the datatypetable attribute from the datatypes table, adds the String "_1" to it and selects all content from it.
I imagined it, from a programmatic perspective, to look something similar to this statement which obviously doesn't work yet:
SELECT * FROM
(SELECT datatypetable FROM datatypes WHERE datatypename = 'CycleTime') + '_1'
How can I make this happen in SQL using HSQLDB?
Thanks to Leonidas199x I know now how to get in the '_1' in but how do I tell the FROM statement that the subselect is not a new table I want to read from but instead the name of an existing table I want to read from.
SELECT * FROM
(SELECT RTRIM(datatypetable)+'_1' FROM datatypes WHERE datatypename = 'CycleTime')
According to this question which is identical to mine this is not possible:
using subquery instead of the tablename
:(
Can you explain your data model in a little more detail? I am not sure I understand exactly what it is you are looking to do.
If you are wanting to add _1 to the 'datatypename', you can use:
SELECT datatypename+'_1'
FROM datatypes

Oracle - SQL - Count multiple fields

Using Oracle 10G
Say for example I have a table with three fields in it, I'd like one query which selects the counts of each column where they are not null. Field name
----------------------------------
| strTest1 | strTest2 | strTest3 |
----------------------------------
I know how to get the count of each one individually:
select count(*) from tablename where strTest1 is not null
but I'd like to know if it's possible to do this within one query for all 3 fields.
Thanks
It sounds like you want:
SELECT COUNT(STRTEST1), COUNT(STRTEST2), COUNT(STRTEST3) FROM YOUR_TABLE

How to overwrite values during a select query in sql?

Suppose I have a table foo with schema (name, age). So the table looks like:
NAME | AGE
John | 20
Jane | 50
Jason | 30
Now suppose I wanted to select from this table, but I wanted to change all their ages to 20 via the select query. That is, I don't want to update the original table foo, but I want to select, something like:
SELECT name, 20 as age from foo
In other words, I want to manipulate the data on the select. Of course, my real-life example is much more complicated than this but knowing the answer to this will do. Thanks!
You basically just do what you put in your question:
SELECT name, newValue as age
from yourTable
This will give you the name from your table and then the value you specify for the other column.
See SQL Fiddle with Demo

help in sql command .. i want list names which have defined id s

i have table consist of columns : id,name,job
and i have row stored with this data :
id: 1
name : jason
job: 11,12
id: 2
name : mark
job: 11,14
i want write sql command to fetch names which have value "14" stored in job column only from this table
so how i do that ?
thanks
You can do:
WHERE FIND_IN_SET(14, job)
But that is really not the correct way. The correct way is to normalize your database and separate the job field into its own table. Check this answer for extra information:
PHP select row from db where ID is found in group of IDs
You shouldn't be storing multiple job ids in the same field. You want to normalise your data model. Remove the 'job' column from your names table, and have a second JOB table defined like this:
id | name_id | job_id
1 1 11
2 1 12
3 2 11
4 2 14
where name_id is the primary id ('id') of the entry in the names table.
Then you can do:
SELECT name_id, job_id FROM JOB WHERE name_id = 1;
for example. As well as making your data storage far more extensible - you can now assign unlimited numbers of job_ids to each name for example - it'll also be much faster to execute queries as all your entries are now ints and no string processing is required.
SELECT
*
FROM
MyTable
WHERE
job LIKE '14,%' OR job LIKE '%,14' OR job LIKE '%,14,%'
EDIT: Thanks to onedaywhen
SELECT
*
FROM
MyTable
WHERE
(',' + job + ',') LIKE ('%,14,%')