How would you total the values in one column and keep a distinct value in another? - sql

I have a database table that has multiple codes in one column that correspond to certain values in another column. For example, a particular code in column A corresponds to a value in column B. There are thousands of duplicate entries in column A that correspond to different values in column B. I want to add up all of the values in column B that have the particular code in column A, while only keeping one copy of the code from column A. You may think of the columns as key-value pairs, where column A contains the key and column B contains the value.
Basically, I want to add all the values in column B where column A is a specific value, and I want to do for this all of the unique "keys" in column A. I'm sure that this is a simple task; however, I am pretty new to SQL. Any help would be greatly appreciated.
Here is the result I'm looking for.

This should work:
SELECT
A,
SUM(B) AS sum_b
FROM [yourTable]
GROUP BY A

SELECT COLUMNA, SUM(ISNULL(COLUMNB,0)) AS TOTAL
FROM
dbo.TableName
GROUP BY COLUMNA

Related

Populate todays date as column name with values derived from existing column in PostgreSQL

I am trying to create a dynamic column in a table whose column name will be today's date and values will be populated based on a mathematical operation of an existing column. For example table, t1 has column a,b,c and I want to add a column called '04-26-2021' whose value will be a+b+c values. Does anyone know how we can implement it in Postgresql? Thanks

Delete rows from a table "A" where column "1" values in "A" match column "1" values in table "B" but column "2" values in "A" do not exist in "B"

Sorry for the confusing title, I think my question is a bit difficult to word. I have two tables, let's call them "A" and "B" and they each have many columns. For simplicity, let's say they have just columns "1" and "2".
Now for the fun part, I want to write an SQL query that for each numeric value in column 1 of table A checks to see if the values of column 2 of table A exist in column 2 of table B when filtered by that value of column 1. And if those values in table B do not exist, delete that row in table A.
For instance, the values of row 1 in each table are ID's of objects used elsewhere. When I filter by a specific ID in each tables, I see more rows in table A than I would like, the difference is column 2, which is an associated date. I want to delete those extra rows associated with the dates that are in A and not in B when I filter for that ID.
I can't just use a NOT IN or NULL statement like the ones used here:
Delete sql rows where IDs do not have a match from another table
or here:
Delete from table if the id doesn't exists in another table
because some all the values in each column of each table do exist somewhere in the other table, just not with the corresponding filter.
This is my first time asking a question on SE and I tried my best to explain but let me know if I can provide any other info! Thanks!
Ad far as I can tell, you want to delete from one table if there are no corresponding values in the other. This strongly suggests not exists (regardless of the database).
As best that I can parse your description:
delete from a
where not exists (select 1
from b
where b.col1 = a.col1 and b.col2 = a.col2
);

How can I group a set of similar results and add up values for one final result in SQL?

I know there has to be a simple answer to this question, but I am a total SQL noob. In my result set I have multiple results in one column that have one specific value that is repeated several times. In another column there is another value that needs to be added. Please see the attached photo for clarification.
I want to be able to find all of the values in Column B that correspond to 'A' and add them up for one result like shown. I want to be sure to delete the duplicates in Column A. Any help is greatly appreciated.
You're trying to SUM the values in column B and you're trying to group them by (GROUP BY) the value in column A when you do that. This is accomplished like so:
SELECT
col_a,
SUM(col_b)
FROM
My_Table
GROUP BY
col_a

Creating a join list between two tables VBA

Good afternoon,
I am still quite a novice with VBA but am trying to create a loop that will be able to sift through a long list of data within a given column (in my case, both tables have one common identifier, the system ID) and if a system ID is matched in one column with a column from the other table, then a new sheet is created that combines all of the rows associated with both sets of data into one row.
For example, if my data looked like this:
Table 1
Column A, Column B, Column C |
ID, Name, Birthday
Table 2
Column A, Column B, Column C|
Purchase, Amount, ID
And I had the same ID in both Tables 1 and 2, for each match, I would like to have all rows associated with the match joined together.
This would really enable me to speed things up with organizing information, so I was not sure if it would be possible... Any Ideas are welcome!
since excel is not a database program like access, you can not use sql-like joins natively. you would have to program your own join function:
(Since i do not have MS Office installed, i can only give you pseudo-code)
for each-loop going through IDs of Table1
for each-loop going through IDs of Table2
if(Table1.ID = Table2.ID) then
copy data of Table1 into a new sheet
copy data of Table2 into the same sheet, next to Table1 data
PS: i assume you use excel because of the vocabulary (column, worksheet,..)

Select single row from database table with one field different and all other being same

I have a database table with 8 fields say Table(a,b,c,d,e,f,g,h).For some rows one of the field is different(say 4 different a values) while all other field values(b-h) in the schema are same.I have to make a table selecting just one rows from such rows with different a's but same b-h.That is I can select any one of the different a's and keep b-h same which they are and display it in table as 1 single row instead of 4.
SELECT MIN(a) a,b,c,d,e,f,g,h
FROM mytable
GROUP BY b,c,d,e,f,g,h