Using SQL to find total amount donated to organizations - sql

I have a SQL table with 2 columns,
organization_id,
amount_donated
I want to return a new table with 2 columns,
organization_id
total_amount_donated
How would I go about finding the total amount donated to each organization? I am thinking I have to use GROUP BY but I haven't been able to find a solution.

You're right, you do want to use GROUP BY. This is because you need to use the SUM aggregate function, and by using GROUP BY organization_id you will sum the amounts corresponding to each organization.
SELECT organization_id, SUM(amount_donated) AS total_amount_donated
FROM your_table
GROUP BY organization_id
This is pretty simple SQL, you should probably get your hands on a tutorial or a book to step you through the basics. :)

You should't to create table, because it will create duplicate data. Instead create a VIEW.
CREATE VIEW vw_total_donation
AS
SELECT organization_id, SUM(amount_donated) tot_donation
FROM table1
GROUP BY organization_id

select organisation_id,sum(amount_donated) as total_amt_donated from your_table
group by organisation_id
You can query like this

Related

SQL to find unique counts between two date fields

I was reading this but can't manage to hack it to work on my own problem.
My data has the following fields, in a single table in Postgres:
Seller_id (varchar) (contains_duplicates).
SKU (varchar) (contains duplicates).
selling_starts (datetime).
selling_ends (datetime).
I want to query it so I get the count of unique SKUs on sale, per seller, per day. If there are any null days I don't need these.
I've tried before querying it by using another table to generate a list of unique "filler" dates and then joining it to where the date is more than the selling_starts and less than the selling_ends fields. However, this is so computationally expensive that I get timeout errors.
I'm vaguely aware there are probably more efficient ways of doing this via with statements to create CTEs or some sort of recursive function, but I don't have any experience of this.
Any help much appreciated!
try this :
WITH list AS
( SELECT generate_series(date_trunc('day', min(selling_starts)), max(selling_ends), '1 day') AS ref_date
FROM your_table
)
SELECT seller_id
, l.ref_date
, count(DISTINCT sku) AS sku_count
FROM your_table AS t
INNER JOIN list AS l
ON t.selling_starts <= l.ref_date
AND t.selling_ends > l.ref_date
GROUP BY seller_id, l.ref_date
If your_table is large, you should create indexes to accelerate the query.

How to GROUP BY a column/s in data studio

I have a sales table with the purchase history of multiple customers. Needless to say a single customer can appear multiple times in the table. I need to group by the customers and do a count of the industries each customer works in and visualize it in a table in data studio. I need to do all of this in data studio itself.
In big query the syntax would look something like this:
SELECT Industry, count(industry) AS industry_count
FROM (SELECT
CustomerID,
Industry
FROM `project1.pr.df_full`
WHERE segment = 'Lost'
GROUP BY CustomerID, Industry)
GROUP BY Industry
ORDER BY industry_count DESC
How can I achieve the same thing in data studio? The WHERE clause doesn't have to be there because I have a segment filter on the page I'm trying to do this on
As I said in the comment, I reproduced your query and it worked fine.
Here you can see a guide about how to connect BigQuery to DataStudio
Please, notice that DataStudio have some limitations in the query syntax:
If you need further information, please let me know
You could query the raw data, and make calculations on Data Studio side. Be sure to use the field you need to group by as dimension.
SELECT
CustomerID,
Industry,
segment
FROM `project1.pr.df_full`
Then in a Data Studio table use "Industry" field as dimension, and "CustomerId" field as metric, using Count as the aggregation for the metric. As you also have a "segment" field in your data source, filtering by this field will not be a problem.
I hope this help!
I'm wondering why you don't write the query as:
SELECT CustomerID, COUNT(DISTINCT Industry) as industry_count
FROM `project1.pr.df_full`
WHERE segment = 'Lost'
GROUP BY CustomerID
ORDER BY industry_count DESC;

SQL Select Distinct

I think this is very easy, I was hoping for verification.
I have 2 columns: ID & DocumentNumber. It's a one-to-many relationship, one ID can have many document numbers.
I need to get ID's where all DocumentNumbers belonging to it are unique.
Is this what Group By is for, in conjunction with Distinct? Is it as simple as Grouping By the ID
You can (as you're suspecting) do it using a simple GROUP BY/HAVING and using DISTINCT;
SELECT id FROM documents
GROUP BY id
HAVING COUNT(DocumentNumber) = COUNT(DISTINCT DocumentNumber)
An SQLfiddle to test with.

SQL grouping results in a select

My SQL table "offers" contains offers users make for products (product_ID, customer_ID, offer).
In my admin page, I want to list the products for which at least one offer exists and show the total offers existing for it.
For example,
PRODUCT #324 Total offers: 42
PRODUCT #99 Total offers: 1
etc.
My guess would be to combine a
SELECT DISTINCT product_ID FROM offers...
And in a second query, to SELECT COUNT(*) FROM offers WHERE product_ID=...
Is it the most efficient way to achieve this, or is there a way to make it inside a single query?
You can do this in one query which will get the count by grouping by the product_id:
SELECT product_ID, COUNT(*)
FROM offers
GROUP BY product_ID
As bluefeet already answered, you achieve it in single query by using group by.
(group by demo)
Another thing to mention is the order by,
select
product_id as id,
count(*) as totals
from
t
group by product_id
order by totals;
If you want to sort with the totals of hits, or if you want to sort by product_id etc.
sqlfiddle

I'm not sure what is the purpose of "group by" here

I'm struggling to understand what this query is doing:
SELECT branch_name, count(distinct customer_name)
FROM depositor, account
WHERE depositor.account_number = account.account_number
GROUP BY branch_name
What's the need of GROUP BY?
You must use GROUP BY in order to use an aggregate function like COUNT in this manner (using an aggregate function to aggregate data corresponding to one or more values within the table).
The query essentially selects distinct branch_names using that column as the grouping column, then within the group it counts the distinct customer_names.
You couldn't use COUNT to get the number of distinct customer_names per branch_name without the GROUP BY clause (at least not with a simple query specification - you can use other means, joins, subqueries etc...).
It's giving you the total distinct customers for each branch; GROUP BY is used for grouping COUNT function.
It could be written also as:
SELECT branch_name, count(distinct customer_name)
FROM depositor INNER JOIN account
ON depositor.account_number = account.account_number
GROUP BY branch_name
Let's take a step away from SQL for a moment at look at the relational trainging language Tutorial D.
Because the two relations (tables) are joined on the common attribute (column) name account_number, we can use a natural join:
depositor JOIN account
(Because the result is a relation, which by definition has only distinct tuples (rows), we don't need a DISTINCT keyword.)
Now we just need to aggregate using SUMMARIZE..BY:
SUMMARIZE (depositor JOIN account)
BY { branch_name }
ADD ( COUNT ( customer_name ) AS customer_tally )
Back in SQLland, the GROUP BY branch_name is doing the same as SUMMARIZE..BY { branch_name }. Because SQL has a very rigid structure, the branch_name column must be repeated in the SELECT clause.
If you want to COUNT something (see SELECT-Part of the statement), you have to use GROUP BY in order to tell the query what to aggregate. The GROUP BY statement is used in conjunction with the aggregate functions to group the result-set by one or more columns.
Neglecting it will lead to SQL errors in most RDBMS, or senseless results in others.
Useful link:
http://www.w3schools.com/sql/sql_groupby.asp