SUM results is multiplying by number of rows - sql

I'm creating a crystal report from several tables.
One table has fields that I want to have sum totals on, but these sum fields are being distorted by number of rows from another table. There are no fields other than DocEntry that I can link with between the two tables.
Here, total bales is repeating 4 times:
If I sum the field total bales, instead of showing 12 the result is 48:
Please assist.

Insert a Group on DocEntry.
Add a Running Total that sums Bales but evaluates only on change of group.

Related

Calculating the percentage whilst grouping SQL

With reference to the table below, I'm suppose to group by campaign_name and media_plan_id. How can I query a table to include the percentage of a creative_names passing a threshold of 8 (banner_np_count) associated with a media_plan_id.
i.e. there are two creative names associated with some media_plan_ids, I need the percentage of total creative_names meeting the condition banner_np_count >= 8.

Google Sheets Query Function. How can I get only Unique or Distinct Rows?

I am trying to answer a question on a case using the Query function on Google Sheets and am stuck on a particular problem.
I need to get the total number of unique orders per year. I used the formula below and managed to get the total orders per year.
=QUERY(raw_data!$A$1:$U$9995, "select YEAR(C), COUNT(B) group by YEAR(C)", 1)
Where column C is the date and B is the order_id.
The problem is that this returns a total of 9994 orders and includes duplicates of the same order. For example, if a customer purchased 3 different products, they would each be given a line in the database and would count as 3 of the 9994 orders. However, they all have the same order_id.
I need to get the number of unique orders per year. I know this number is 5009 since I did some manual research through Excel, but wanted to find that same total, separated by year, using the Query Function since this is a case to test my SQL Knowledge.
Is this possible? Does the Query Function have a way to get the count for unique order_ids? Thank you very much for your help!
See if this helps
=QUERY(UNIQUE(raw_data!$B$1:$C$9995), "select YEAR(Col2), COUNT(Col1) where Col2 is not null group by YEAR(Col2)", 1)

Postgres cross tabs mixes categories in result set

I have a query returning me three columns 'Date', 'District' and 'Total'. There are four districts but some time a district may not have record for corresponding date. I want to put all my districts in column and their total in rows as ..
Date, District1, District2, District3, District4
While in my rows will be date along with the total value per district.
I am using crosstab to get the desired results. But in the result total for one district is listed in other district though the datewise grand total is same.
I don't know why crosstab query mixes categories. Please help.

send total sum to cross tab stimulsoft report

I have a Cross-Tab report in Stimul-Soft Reports.
In Data Grouping process, if I have more than one Grouping parameter (raw), there's error in the total sum of columns.
So I want to send the total sum of each columns to the report.
Please help me about this problem.
Thanks.

Calculating a percentage of two "Counts" in SQL in Microsoft Access 2010

I have a Microsoft Access 2010 database of thyroid surgeries. I have a query that counts the number of surgeries in the entire database. I have a second query that counts the number of surgeries performed between certain dates. I created a new query using SQL to calculate the percentage of surgeries in that date range (a percentage of the total surgery number). Here is the code:
SELECT
((select count(ThyroidSurgeryType)
from [Thyroid Surgeries]
HAVING ([Thyroid Surgeries].SurgeryDate) Between #1/1/2011# And #12/31/2012#)/(select count(ThyroidSurgeryType)
from [Thyroid Surgeries])) AS Percentage
FROM [Thyroid Surgeries];
I get .33 (I then set the row format to percentage to get 33%), but I get 6 rows of 33% as opposed to just one. Why is it displaying this way? Thanks
The way you're using inline queries, you're executing the same query per row of the outer query. So if your table has six rows, you'd be executing it six time (and getting the same result for each query, naturally). You can simplify things by removing the outer query and using an iif expression:
SELECT SUM (IIF (SurgeryDate Between #1/1/2011# And #12/31/2012#, 1, 0)) /
COUNT(ThyroidSurgeryType) AS Percentage
FROM [Thyroid Surgeries];