SQL create columns with sum of another column by unique items - sql

I am a little bit new to SQL. So hope you can help me with this question as I didnt find answer yet.
I have joined tables from other sources and I need to create new columns based on unique values of one column with summ of another col. values.
For example, this is my table:
And this is what I need to get (Summ of A for each unique value in B added as a column):
For now I can do it manually like this:
SELECT
EM.[id]
,SUM(CASE WHEN AM.[B]='ZA' THEN EM.[A] ELSE 0 END) "ZA_sum_A"
,SUM(CASE WHEN AM.[B]='ZB' THEN EM.[A] ELSE 0 END) "ZB_sum_A"
FROM [xxx].x.x AS AM
INNER JOIN [yyy].[y].[y] AS EM ON (AM.ELEMENT = EM.ELEMENT)
WHERE ...
GROUP BY EM.[id]
But issue is that I can have hundreds of unique values in B.
So, question how to do it correctly..

If you just want to see the summed values of EM.[A] for each combination of EM.[id] and AM.[B], you can do:
select
em.[id],
am.[B],
sum(em.[A])
from [xxx].x.x as am
join [yyy].[y].[y] as em on am.ELEMENT = em.ELEMENT
where [...]
group by em.[id], am.[B]
If you then want to have the distinct values of AM.[B] appear as columns, so there is only one row for each distinct value of EM.[id], you either need to know what the distinct values of AM.[B] are and use PIVOT.
If the exact values of AM.[B] are not known, or change often over time, you'll need to do something dynamic, like in this answer, but in the opposite direction.

Related

How can I identify all matching columns for two rows of data?

Using Standard ANSI SQL, how does one return a list of columns which are matching for two specific rows of data? We don't know the names of the columns, only the table name and the ID (or other primary key) to pick out the two specific rows we wish to compare?
Let's say we have a table with a large number of columns for real estate listings. If I choose two specific rows like so:
SELECT *
FROM listing_data
WHERE mls_number IN ('111111', '222222')
How can I identify the names of all other columns which happen to match between these two particular rows?
For example, perhaps there is a column called 'school_district' and they both are in the same district. Or perhaps the two listings share the same street name, or the same listing agent, or all three of these.
To get column names you can select from information_schema.columns however that is a table of column names only and does not have any data. If you are trying to do a select * from tablex where select * from tabley where columnname = 'value' then if it works at all unless your tables are small it may take hours to complete. It is simple if you know your column names to form up a query. Do some research and practice query on your tables and you should get some insight. You are unlikely to have address data in a name column so once you get familiar with your data you should be able to craft a simple query.
You need to explicitly do the comparison for each column. One method is:
SELECT (CASE WHEN ld1.col1 IS NOT DISTINCT FROM ld.col1 THEN 'col1;' ELSE '' END ||
CASE WHEN ld1.col2 IS NOT DISTINCT FROM ld.col2 THEN 'col2;' ELSE '' END ||
. . .
) as matches
FROM listing_data ld1 JOIN
listing_data ld2
ON ld1.mls_number = '111111' AND
ld2.mls_number = '222222'

Creating a pivot table with no agg function while using a group by?

Right now I have a table that looks like this called "student_proficiencies":
The student_application_id are not unique, meaning they could be duplicated depending on how the student answers (As you can see for multiple entries of 23541)
I want to create a table such as this:
I want to group by student_application_id so that every id is unique in the new table, but the columns become the skills, and the values within those columns are the strengths depending on if that student_application_id answered that particular skill ("Python", "Excel" etc...)
How would I go about writing a SQL query here? I know this would be a PIVOT problem, but I'm not sure how an AGG function would play into this.
SELECT * from student_proficiences
GROUP BY student_application_id....
PIVOT (...?)
Thanks
EDIT: I'm using SQL in BigQuery
If you know the list of proficiencies, you can use:
select student_application_id,
max(case when skill = 'statistics' then strength end) as statistics,
max(case when skill = 'Excel' then strength end) as excel,
. . .
from t
group by student_application_id;

SQL Computed Column, CountIF 2 Tables

I have two tables
tblData_VendorMasterSSPaymentTerms
tblData_VendorMasterSSPaymentTermsCLM
tblData_VendorMasterSSPaymentTerms contains a field labled VMSSPayTerms_AribaContractID which the values exist in table tblData_VendorMasterSSPaymentTermsCLM
So in table tblData_VendorMasterSSPaymentTermsCLM I want to create a calculated column that counts how many records in tblData_VendorMasterSSPaymentTerms contains the Contract ID for that record.
This is what I have put together so far but it is still coming up with an error
SELECT Count(VMSSPayTerms_AribaContractID)
From tblData_VendorMasterSSPaymentTerms
Where VMSSPayTerms_AribaContractID=VMSSPayTermsCLM_ContractID
Can someone help me identify what I am doing wrong here?
You must join the tables, group by VMSSPayTermsCLM_ContractID and count:
select
c.VMSSPayTermsCLM_ContractID,
count(t.VMSSPayTerms_AribaContractID) counter
from tblData_VendorMasterSSPaymentTermsCLM c inner join tblData_VendorMasterSSPaymentTerms t
on t.VMSSPayTerms_AribaContractID = c.VMSSPayTermsCLM_ContractID
group by c.VMSSPayTermsCLM_ContractID

sql query on mongodb to get counts of multiple columns against the id value of single column

I am new to mongodb. I am stuck in a problem.
I want to get the counts of multiple columns against a single column's value containing an ID .
Suppose that I have different columns (author1,author2,author3,...so on) for a specific id.
Now I want to get the count of total authors for that specific id .
Therefore i want to count the total number of columns of authors against that ID to get the count.
Is this a good approach?
One solution would be to use an aggregated query with conditional sum, like :
SELECT SUM(
CASE
WHEN (author1 = 'id' OR author2 = 'id' OR author3 = 'id' --more columns...)
THEN 1
ELSE 0
END
)
FROM table
Bottom line, you would be better off using a single column to store authors, so you could query just that column instead of looking at several of them.

performing multiple separate sums with separate filters on a table

I have a table with an amount column a reference field and an id column. What I need to do is sum the amount based on different combinations of ID's for each reference. There are nine different combinations in total that I then need to insert into a separate table.
The best way I've found to do this is to use a cursor and do each SUM separately, assign the amount to a variable and update the table for each reference and for each combination.
Hope that makes sense!
What I was hoping to find out is - is there a better way to do it?
thanks.
You could so something like:
SELECT SUM(CASE WHEN (Id = 9) THEN Val ELSE 0 END) ConditionalSum
From dbo.Table
You can have many of those SUMs with different conditions in one query.
You can create a table called something like combos with the following columns:
Name of combination
reference id in combination
(and perhaps other useful columns like an id and creation time, but that is not important here).
Insert your combinations into this table, something like:
First10 1
First10 2
...
First10 10
MyFavorite 42
Whatever the pairs are.
Then you can do what you want with a single query:
select c.comboName, sum(val) as ConditionalSum
from t join
combos c
on t.referenceId = c.referenceId
group by c.comboName