Sum all columns by omitting 3 columns - sql

I have table with more than 34 columns.
I need to aggregate(sum) of the columns.
To find:
Sum of 2 columns
Sum of remaining columns except.
Please find the table structure with following columns.
S.NO
Handling charge
Storage Charge
...
..
.... 34th column charge
I have more than 3000 rows of records based on above mentioned columns.
May I know how to query or calculate above mentioned find.
For Find 1 Query:
**Update dbo.tablename
set columnname isnull(charge1,0)+isnull(charge2,0)**
I am struggling to find find 2:
Can anyone help?

You can query the table to get the list of columns name then apply the Sum() function.
Query table column names:
SELECT *
FROM DatabaseName.INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = N'tableName'

Related

Bigquery join 2 tables with id concated from 4 columns and create a new table dynamically

I have two tables in Bigquery from two different data sources, lets say x and y. I want to join these two tables on os_name, tracker_name, date, country columns. For that i am using concat function and joining like this:
full outer join x on concat(x.date,x.os_name,x.tracker_name, x.country) = concat(y.date,y.os_name,y.tracker_name,y.country_code)
as a query result common columns also gets duplicated. like in the result there is os_name and os_name_1, country_code, country_code_1 etc. columns. I don't want that. Final columns should be as in the example below in Final Table Schema.
I want to return all records from both sides. For example if there is no match in table y
y_install, and y_purcase will be 0, and vice versa.
X TABLE SCHEMA:
os_name,
tracker_name,
date ,
country
install
purchase
Y TABLE SCHEMA:
os_name,
tracker_name,
date,
country,
y_install,
y_purchase
Final Table Schema required:
os_name,
tracker_name,
date ,
country
install
purchase,
y_install,
y_purchase
I am going to schedule the query and write results to destination table at given interval.
Can you help me out with this query.
Regarding the final table, I don't understand whether you want to return first NON NULL result or whether you want to have e.g. an array which will contain both results from both tables in case both tables a valid value. In my sample table, do you want row 1,2 (actually the same thing) or 3?
row_number
x_install
y_install
final_table_install
1
23
50
23
2
NULL
50
50
3
23
50
[23,50]
It comes out that What I wanted to use was union all. First, I added the non-common columns to the two tables so that the schemas of the two tables are equal. So I was able to vertically merge tables using union all. Thanks for trying to help out anyway.

In Snowflake, I want to count duplicates in a table based on all the columns in the table without typing out every column name

I have a table with 60 columns in it. I would like to identify how many duplicates there are in the table based on all the columns being identical.
I don't want to have to type out every field name in the SELECT or GROUP BY clauses. Is there a way to do that?
You can use an approach like this for each table:
SELECT
MD5(OBJECT_CONSTRUCT(SRC.*)::VARCHAR) DUP_MD5, SUM(1) AS TOTAL_COUNT
FROM <table> SRC
GROUP BY 1
HAVING SUM(1) > 1;

SQL: Merge 2 Columns or more from different tables into one Alias new Column

I have 2 tables: DisconnectionsData and DisconnectionsVoice.
both of them have columns: WeekNum,Month,Quarter,Year,IncomeLost and ID.
the names of the columns are different between the 2 tables, but the data inside them is parallel (Quarter and QuarterNumber is literally the same).
my wish is to FULL JOIN both tables into one table with only 6 columns.
i cant figure out how to make an alias, for example: how do i merge DisconnectionsData.Quarter and DisconnectionsVoice.QuarterNumber
into one column with the alias name of QuarterOfDisconnection.
that the desired result:
thank you.
You don't specify which sql are you using? You can try below.
SELECT *
FROM DisconnectionsData
UNION ALL
SELECT *
FROM DisconnectionsVoice

Table whose columns are random samples from an original column

I have numerical data ​​related to clinical information from people with a particular disease recorded in an specific column ('Lab') from Table A.
I need to get a Table B with 30 rows and 50 columns.
The columns of Table B should be random samples from the values ​​contained in column 'Lab' (nearly 3300 registers).
I am able to get a table with one column using:
SELECT Lab FROM Table_A sample (1) WHERE Lab IS NOT NULL;
Is it possible make a query using the SELECT command that results in Table B with all its 50 columns without the need of getting its columns one by one?
You can use the RAND operator with ORDER BY, like this:
SELECT * FROM Table_A ORDER BY RAND( ) LIMIT 0 , 30

SQL Server Sum multiple rows into one - no temp table

I would like to see a most concise way to do what is outlined in this SO question: Sum values from multiple rows into one row
that is, combine multiple rows while summing a column.
But how to then delete the duplicates. In other words I have data like this:
Person Value
--------------
1 10
1 20
2 15
And I want to sum the values for any duplicates (on the Person col) into a single row and get rid of the other duplicates on the Person value. So my output would be:
Person Value
-------------
1 30
2 15
And I would like to do this without using a temp table. I think that I'll need to use OVER PARTITION BY but just not sure. Just trying to challenge myself in not doing it the temp table way. Working with SQL Server 2008 R2
Simply put, give me a concise stmt getting from my input to my output in the same table. So if my table name is People if I do a select * from People on it before the operation that I am asking in this question I get the first set above and then when I do a select * from People after the operation, I get the second set of data above.
Not sure why not using Temp table but here's one way to avoid it (tho imho this is an overkill):
UPDATE MyTable SET VALUE = (SELECT SUM(Value) FROM MyTable MT WHERE MT.Person = MyTable.Person);
WITH DUP_TABLE AS
(SELECT ROW_NUMBER()
OVER (PARTITION BY Person ORDER BY Person) As ROW_NO
FROM MyTable)
DELETE FROM DUP_TABLE WHERE ROW_NO > 1;
First query updates every duplicate person to the summary value. Second query removes duplicate persons.
Demo: http://sqlfiddle.com/#!3/db7aa/11
All you're asking for is a simple SUM() aggregate function and a GROUP BY
SELECT Person, SUM(Value)
FROM myTable
GROUP BY Person
The SUM() by itself would sum up the values in a column, but when you add a secondary column and GROUP BY it, SQL will show distinct values from the secondary column and perform the aggregate function by those distinct categories.