how to write a select query with one or two column excluded instead of list down required columns.
for example we have a table Table1 with 10 columns (col_1,col_2,col_2 ..... col_10)
and we just want select col_1 to col_8.
instead of writing like
Select col_1, col_2, col_3 .... col_8 from Table1
should we select like this
Select -(col_9, col_10) * from Table1
This is possible but you would have to use dynamic SQL or a stored procedure. It would still mean having to specify the start and end column using fixed column names and indexes and if the columns weren't in running order (colN, colN+1, etc.) then it would become messy.
So I suppose your answer it, just type it out. The benefit you will gain from doing something cleaver is small.
I hope this helps.
Any ideas?
I think we can select column name first then write into query.
1 Select column with excluded.
SHOW COLUMNS FROM _table WHERE FIELD NOT IN ('column1','column2')
2 Then, Use the column result we've got from above query to write to select query column.
SELECT {result_aboved} FROM _table WHERE 1
but I don't think it can be only once query for this case. you have to query twice times at least.
Related
To find for one column, I can use this.
SELECT column1
FROM `dataset.table`
WHERE column1 IS NULL OR column1 = '';
But what if i have 100 columns? Instead of going through column by column, changing column 1 to 2,3,etc.,I'm looking for one for all solution. I'm kinda new to SQL and Data Cleaning.
Consider below approach
select *
from your_table t
where regexp_contains(to_json_string(t), r':(?:null|"")[,}]')
above will return you all rows where any column either null or empty string
Does SQLite offer a way to search every column of a table for a searchkey?
SELECT * FROM table WHERE id LIKE ...
Selects all rows where ... was found in the column id. But instead to only search in the column id, I want to search in every column if the searchstring was found. I believe this does not work:
SELECT * FROM table WHERE * LIKE ...
Is that possible? Or what would be the next easy way?
I use Python 3 to query the SQLite database. Should I go the route to search through the dictionary after the query was executed and data returned?
A simple trick you can do is:
SELECT *
FROM table
WHERE ((col1+col2+col3+col4) LIKE '%something%')
This will select the record if any of these 4 columns contain the word "something".
No; you would have to list or concatenate every column in the query, or reorganize your database so that you have fewer columns.
SQLite has full-text search tables where you can search all columns at once, but such tables do not work efficiently with any other queries.
I could not comment on #raging-bull answer. So I had to write a new one. My problem was, that I have columns with null values and got no results because the "search string" was null.
Using coalesce I could solve that problem. Here sqlite chooses the column content, or if it is null an empty string (""). So there is an actual search string available.
SELECT *
FROM table
WHERE (coalesce(col1,"") || coalesce(col2,"") || coalesce(col3,"") || coalesce(col4,"")) LIKE '%something%')
I'm not quite sure, if I understood your question.
If you want the whole row returned, when id=searchkey, then:
select * from table where id=searchkey;
If you want to have specific columns from the row with the correct searchkey:
select col1, col2, col3 from table where id=searchkey;
If you want to search multiple columns for the "id": First narrow down which columns this could be found in - you don't want to search the whole table! Then:
select * from table where col1=searchkey or col2=searchkey or col3=searchkey;
I understand that AS is used to create an alias. Therefore, it makes sense to have one long name aliased as a shorter one. However, I am seeing a SQL query NULL as ColumnName
What does this imply?
SELECT *, NULL as aColumn
Aliasing can be used in a number of ways, not just to shorten a long column name.
In this case, your example means you're returning a column that always contains NULL, and it's alias/column name is aColumn.
Aliasing can also be used when you're using computed values, such as Column1 + Column2 AS Column3.
When unioning or joining datasets using a 'Null AS [ColumnA] is a quick way to make sure create a complete dataset that can then be updated later and a new column does not need to be created in any of the source tables.
In the statement result we have a column that has all NULL values. We can refer to that column using alias.
In your case the query selects all records from table, and each result record has additional column containing only NULL values. If we want to refer to this result set and to additional column in other place in the future, we should use alias.
It means that "aColumn" has only Null values. This column could be updated with actual values later but it's an empty one when selected.
---I'm not sure if you know about SSIS, but this mechanism is useful with SSIS to add variable value to the "empty" column.
When using SELECT you can pass a value to the column directly.
So something like :
SELECT ID, Name, 'None' AS Hobbies, 0 AS NumberOfPets, NULL AS Picture, '' AS Adress
Is valid.
It can be used to format nicely a query output when using UNION/UNION ALL.
Query result can have a new column that has all NULL values. In SQL Server we can do it like this
SELECT *, CAST(NULL AS <data-type>) AS as aColumn
e.g.
SELECT *, CAST(NULL AS BIGINT) AS as aColumn
How about without using the the as
SELECT ID
, Name
, 'None' AS Hobbies
, 0 AS NumberOfPets
, NULL Picture
Usually adding NULL as [Column] name at the end of a select all is used when inserting into another table a calculated column based on the table you have just selected.
UPDATE #TempTable SET aColumn = Column1 + Column2 WHERE ...
Then exporting or saving the results to another table.
I checked this site for code to stack two tables (put the results from one table beneath the results from another) where the column names are different. I know that UNION ALL works when the names are the same.. and I KNOW THAT UNION ALL with an assignment for the column names that are missing from one table works when one table has more info than the other.. but what if the column names are different? like what if in one table the column name is CHP and in another it is "CHILD HEALTH PLUS" and I need those two columns to be stacked on top of one another?
A UNION can be used as long as the datatypes of the columns are the same. It doesn't matter if the column names are different.
SELECT column1
FROM Table1
UNION
SELECT column1
FROM Table2
If you want to know which table the records are coming from, then you can add another field that will distinguish the rows:
SELECT column1, 'Table1' as TableName
FROM Table1
UNION
SELECT column1, 'Table2' as TableName
FROM Table2
what sql language? mysql/sql server?
mysql doesn't require that the column names be the same, but you could always use 'AS' to match up the column names eg.
select column1 AS col1, column2 AS col2...
EDIT: i just spotted the '2008' in the post title - is that sql server 2008? in any case, sql server doesn't seem to care re. column names either.
I'm am using MS Reporting Services to graph some data from an Oracle database. I want to name the columns in my select statement with values from another select statement. Is this possible?
Like instead of
Select Column1 As 'Test' From Table1
could I do something like
Select Column1 As (Select column2 from Table2 where Value = 1) From Table1
?
I would think you'd have to query out separately, then form the query dynamically. Interested to see if there is a different answer.
My PL/SQL's a little rusty, so what follows is more pseudocode than compilable & tested code. And this is completely off the top of my head. But if you know the specific ordinal location of the column in the table, you may try this:
columnName varchar2(50) :=
Select column_name
From all_tab_columns c
Where lower(table_name) = '<% Your Table2 Name %>' And
column_id = 9 -- The appropriate ordinal
Order By column_id;
Select Column1 As columnName From Table1;
There may be more column values drawn from "all_tab_columns" that'll help you as well. Take a look around and see.
I hope this helps.
You can query all needed column names into separate report dataset, create hidden multivalue report parameter vColumns, set dataset with columns as a parameter default values, and use it as a string array:
Parameters!vColumns(0).Value - will be the first column etc. So you can use them as a query parameters.
See Lesson 4: Adding a Multivalue Parameter