Can I create a table without any columns in SQL Server by t-sql?
A table is a collection of columns and rows. You need at least one column.
No. What would you use it for?
Related
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.
I've been trying to add multiple partition columns, to a BigQuery table, but it seems to only take one field, even if I add multiple partition fields in the query parameters.
I'm partitioning by date time and integer range.
It only takes the later of the pair to create partitions and ignores the first partition field.
Any ideas, would be appreciated?
BigQuery only supports partitioning on one column. If you want to partition on multiple columns, you can consider partitioning+clustering. The table can be clustered on up to 4 columns.
I use coalesce to combine the columns and partition the new field created from coalesce, worked for my purpose.
I have a customers table that houses contacts. Each contact is added as another row to that customer.
I want to create my query so that I can create columns for contact1, contact2, contact3 and so forth. What's the best way to do this?
My rows are dynamic and I have been looking into unpivot and pivot but I'm not sure how I would build that into my query as I have a few joins and other data in the query.
SQL Fiddle example
Dynamic pivot query is what you need. See this:
http://buysql.com/mysql/14-how-to-automate-pivot-tables.html
and this:
SQL Server dynamic PIVOT query?
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.
in ms-access, how to write a query / DDL to change multiple column names with a single query.
You have to create a new table, copy data, drop old table
ALTER TABLE does not support multiple ...ALTER COLUMN clauses
You could ADD multiple columns on one go, populate then, then drop the old columns.
Multiple ADDs are supported.