How can I group a set of similar results and add up values for one final result in SQL? - 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

Related

One row per column with column name

background
I am trying to create an SSRS report which will run select * on a table passed in as a parameter and display all the data from that table. As I understand it, I can't use a table for this. I want to use a pivot table to achieve this.
select * from #table will return something like this (from the adventureworks DB)
I want to display the date in this format:
Question
How do I achieve this? I looked at using PIVOT/UNPIVOT, but all the examples I've seen use static column names and aggregates.
I won't know the column names at design time (or run time), I'm assuming I will need column headers like 'table name', 'value1', 'value2' etc?
Limitations
I won't have access to create stored procedures. Ideally, the report should be able to be run entirely from SSRS without having to create new tables etc.
Performance is not a concern.
Edit
Editing to add some clarity. The column names in the example above are only an example. The #table parameter could be any table, column names won't be know at design time. The column names could be col1, col2, or name, address... etc.
You can do this with a normal tablix, with a column group on BusinessEntityID
Create a normal tablix.
Remove the row group (group only)
Insert a new parent column group, grouped by BusinessEntityID
Delete the top row (row only)
Add each of the row titles in, as per your suggested output
Add each of the values in the second column

Create a Patern Rank, Based on THE VALUE(s) IN A COLUMN

I have data like this -
I want to create one more column name Pattern and it looks like this -
How to create this column -
It is based on the column 'NAME'. It should Rank Starting from the Row having Parent_ID=(ContentID of the row having Parent ID 1)
It should increase by one and should make the same Group if it get's 'Only For'/'Not For' in the column NAME.
It will work on the group of Jill_Equipment_ID,Req_Group,Operand..
As my complete data in broken into these groups and it will have multiple Jill_Equipment_ID and for each ID multiple Req_Group and for each group multiple Operands.
Please help me in getting it solved. Thanks in advance.

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

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

How can I change row information in a Query?

I'm using Postgres and I'd like to know how to change row information within a query, Let's say I have a column called Numbers and it's got rows going 1,2,3,4,5 how could I edit the information in those rows? let's say I want the query to display 1,1,1,1,5 how would I write in a query that each row should be changed to 1 unless it's 5? Again it's only to change it within the Query, I'm not trying to do an UPDATE I realize how newbish this is on my part but I couldn't find this on google.
SELECT
CASE WHEN Numbers <> 5 THEN 1 ELSE Numbers END
FROM table
See 9.12. Conditional Expressions

Can anyone explain me about this SQL Query

Select Null as Empty from (select * from TblMetaData)
Looks like, it is trying to get null rows for the same number of rows in tblMetaData.
EDIT: This could be written as
SELECT Null AS Empty FROM tblMetaData
It will yield a result set with one column named Empty which only contains NULL values. The number of rows will be equal to the number of rows available in TblMetaData.
It looks like the result of one of two possible situations:
The developer was getting paid per line, and threw in that query. It was probably originally structured to take more than one line.
The developer was incompetent and this was the only way they could think of to generate a bunch of null values.
The query returns a null value from each line of the table, so the only real information in the result is the number of records in the table.
This can of course be found out a lot more efficently using:
select count(*) as Count from TblMetaData
It's possible that the developer was not at all aware of the count aggregate (or how to search the web) and tried to get the number of records while making the result as small as possible.
It often used in this expression
select * from TableA where exists
(select null from TableB where TableB.Col1=TableA.Col1)
it can be used to give the number of rows in the table TblMetaData with the column's name denoting the first letter of empty(in this case only).
like suppose you gave
Select Null as Empty from (select * from TblMetaData)
so it will give
E
n rows selected
here n is the number of rows in the table.
suppose you gave
Select Null as XYZ from (select * from TblMetaData)
then it would be same but the column's name would change like
X
n rows selected