I have a SQL table with 150+ columns and I want to apply on them an aggregation function when selecting values but I don't to list all column names by hand. Instead I want to use a for loop through the column names of the table.
I want to do something like this:
SELECT
AGREEMENT_NO,
COUNT(DISTINCT column) FOR column LOOP columns -- Instead of 150+ lines of count
WHERE ...
GROUP BY AGREEMENT_NO
Does anyone know if it's possible to do it in SQL and if yes how?
This can be done by dynamic query.
Take every column of a table by in temporary table by Information_Schema.columns.
Take a while loop for temporary table for all row.
Write dynamic query and concat column name for your requirement.
Execute the query. Eg. Exec("Query" / variable).
This will give you exact result.
Related
I am struggling to understand what the output of SELECT is meant to be in SQL (I am using MS ACCESS), and what sort of criteria this output needs to specify, if any. As a result, I don't understand why some queries work and others don't. So I know it retrieves data from a table, does calculations with it and displays it. But I don't understand the "inner" working of SELECT function. For instance, what is the name of data structure / entity it displays? Is it a "new" table?
And for example, suppose I have a table called "table_name", with 5 columns. One of the columns called "column_3", and there are 20 records.
SELECT column_3, COUNT(*) AS Count
FROM table_name;
Why does this query fail to run? By logic, I would expect it to display two columns: first column will be "column_3", containing 20 rows with relevant data, and second column will be "Count", containing just one non-empty row (displaying 20), and other 19 rows will be empty (or NULL maybe)?
Is it because SELECT is meant to produce equal number of rows for each column?
Your questions involve a basic understanding of SQL. SELECT statements do not create tables, but instead return virtual result sets. Nothing is persisted unless you change it to an INSERT.
In your example question, you will need to "tell" the SQL engine what you want a count "of". Because you added column_3, you need to write:
SELECT column_3, COUNT(*) AS Count
FROM table_name
GROUP BY column_3
If you wanted a count of all the rows, simply:
SELECT COUNT(*) FROM table_name
I WANT LIKE THIS IN THE ATTACHMENT
thanks
For getting output what you need ,first table record you need to de-concatenate, i mean single rows record make multi rows record and then put inner join. then again you need to apply concatenation on result using stuff function in sql server.
I have a dataset with over 100,000 rows, over 100 columns and where some values are NULL. Now I want to remove all the rows which contain NULL values.
Can anybody suggest the sql command for it?
With the little information you've provided:
DELETE FROM table WHERE colA IS NULL OR colB is NULL
Add further conditions for each column that you want to check.
Change OR to AND if you only want to delete rows where all of the columns are NULL.
It's fairly easy to generate the SQL for this using a query on user_tab_columns if you don't want to type it out by hand.
use a scripting language like PHP to retreive all column names and then construct your SQL query.
Using pure SQL could get tricky.
How can i fetch column names from a table on index basis, like I want to make a tables whose column name should be the name of last column fields of a result set of a query, but those result sets last columns value may be different at different execution time, so i want to know how can i fetch those index value of that last column to make a temp table with column name of those last columns value of a result set.
Is there any way/function in sql server to dynamically form that?
sp_helpindex:
Reports information about the indexes
on a table or view.
You can also use ROW_NUMBER as explained here
I want to return columns based on some meta data in an other table. i.e. i have my table which contains 10 columns, and another table which contains those columns denormalise with metadata to do with them.
i.e.
Table - Car:
columns - Make,Model,Colour
and another table called "Flags" which has a row for each of the above columns and each row has a column for "IsSearchable" and "ShowOnGrid" - that sort of thing.
The query i want is one which will return all columns from the cars table that are flagged in the "Flags" table as "ShowInGrid"
----EDIT
Apologise, I should have stated that this is on SQL Server 2008.
Also, I dont want to have to physically state the columns which i would like to return, i.e. If i add a column to the car table, then add it into the Flags table and declare it to be searchable, I don't want to have to physically state in the SQL Query that i want to return that column, i want it to automatically pull through.
You need to use dynamic SQL; this can easily be done with a stored procedure.
Something like this might work:
Select
D.CarID,
Case D.ShowMake When True Then D.Make Else NULL END AS Make
...
From
(Select
C.CarID, C.Make, C.Model, C.Colour, F.IsSearchable, F.ShowOnGrid, F.ShowMake
From
Cars C
Inner Join
Flags F
On C.CarID = F.CarID) D
I didn't write in all the case statements and don't know how many flags you're working, but you can give it a try. It would require to filter on null values in your application. If you actually want the columns omitted on the basis of the Flag column value the other answer and comment are both right on. Either Dynamic SQL or build your query outside in another language first.