Calculating the percentage whilst grouping SQL - 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.

Related

SUM results is multiplying by number of rows

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.

Trying to create a well count to compare to BOE using the on production date and comparing it to Capital spends and total BOE

I have data that includes the below columns:
Date
Total Capital
Total BOED
On Production Date
UWI
I'm trying to create a well count based on the unique UWI for each On Production Date and graph it against the Total BOED/Total Capital with Date as the x-axis.
I've tried unique count by UWI but it then populates ALL rows of that UWI with the same well count total, so when it is summed the numbers are multiplied by the row count.
Plot Xaxis as Date and Y with Total BOED and Well Count.
Add a calculated column to create a row id using the rowid() function. Then, in the calculation you already have, the one that populates all rows of the UWI with the same well count, add the following logic...
if([rowid] = min([rowid]) over [UWI], uniquecount([UWI]) over [Production Date], null)
This will make it so that the count only populates once.

How to handle monthly and yearly values

I have a Fact table that holds what are more or less, sales goals. The ETL process that populates it, generates 12 "weighted" values into seperate rows, one per month. Each row however, also includes a field that holds the yearly value. I do this with unpivot. This all works. Now Im trying to get at this data in the cube with an SSRS report. The problem seems to be that I can query and see the results that include either the yearly goal values or the monthly, weighted values, but not both in the same set.
[update for fact table details]
My Fact table looks something like this:
FK_Account
FK_User
Target
Projected
GoalYear
FK_DateKey
FK_Dept
MonthlyWeightedTarget
MonthlyWeightedProjected
When I load this fact table via the ETL, I get the date key associated with each monthly value (MonthlyWeightedTarget). That will be 12 seperate records, but each one will have the same yearly value. Im not including next years value as a seperate column, because there are seperate records already associated with that year.
Basically, the users define a set of goals associated with a given year. Then I am applying a "weighting" to generate 12 seperate "monthly" records, which total up to the yearly target goal. Hope this makes sense.
What I need to see is something like this result:
Account Name
YTDgoal
YearGoal
NextYrGoal
I created a calculated member for the NextYrGoal, but now Im not sure I even need it.
What would be a good approach for handling the above (getting the ytd, yearly and next year values) ?
If I was getting at these values with TSQL, I would sum on the monthly values, and just include the associated yearly and next years values, grouping by account, year-goal, next-year-goal

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];

MS Access SQL statement count usage

I am new to SQL. I was given a coursework to report data of usage over the last 2 month. Can someone help me with the SQL statement?
SELECT COUNT(Member_ID,Non_Member_Name) AS Pool_usage_last_2_months
FROM Use_of_pool
WHERE DATEDIFF(‘2012-04-21’,’2012-02-21’)
What I meant to do is to count the total number of member usage(member_ID) and non member usage(no ID,name only) from the last two months and then output the name and date and time,etc. on the same report. Is there any SQL statement to output that kind of information? Correction/Suggestions are welcomed.
You need a different WHERE clause. Assuming your Use_of_pool table includes a Date/Time field, date_field:
WHERE date_field >= #2012-02-21# AND date_field <= #2012-04-21#
If date_field values can include a time component other than midnight, advance the end date range by one day to capture all the possible Date/Time values from Apr. 21:
WHERE date_field >= #2012-02-21# AND date_field <= #2012-04-22#
That should restrict the rows to match what I think you want. It should offer fast performance with an index on date_field.
I'm unclear about the count(s) you want ... whether it is to be one count for all visits (both member and non-member), or separate counts for members and non-members.
Edit: If each row of the table represents a visit by one person, you can simply count the rows to determine the number of visits during your selected time frame.
SELECT Count(*) AS CountOfVisits
FROM Use_of_pool
WHERE date_field >= #2012-02-21# AND date_field <= #2012-04-21#
Notice each visit by the same person will contribute to CountOfVisits, which is what I think you want. If you wanted to know how many different people visited, we will need a different approach.
Edit2:
It sounds like you can use Member_ID and Non_Member_Name to distinguish between member and nonmember visits. Member_ID is Null for nonmembers and non-Null for members. And Non_Member_Name is Null for members and non-Null for nonmembers.
If that is true, try this query to count member and nonmember visits separately.
SELECT
Sum(IIf(Member_ID Is Not Null, 1, 0)) AS member_visits,
Sum(IIf(Non_Member_Name Is Not Null, 1, 0)) AS non_member_visits
FROM Use_of_pool
WHERE date_field >= #2012-02-21# AND date_field <= #2012-04-21#
Aggregate functions of SQL use all the data in a column (more precisely, all the data your WHERE clause selects) to produce a single datum. COUNT gives you the number of data rows that matched your WHERE clause. So for example:
SELECT COUNT(*) AS Non_members FROM Use_of_pool WHERE Member_ID IS NULL
will give you the number of times the pool was used by a non-member, and
SELECT COUNT(DISTINCT Member_ID) AS Members FROM Use_of_pool
will give you the number of members who have used the pool at least once (the DISTINCT tells the database engine to ignore duplicates when counting).
You can expand the WHERE clause to further specify what you want to count. If "last two months" means the current and previous calendar month, you'll need:
... WHERE DateDiff("m",Date_field,Date())<=1
If it means a rolling 2-month period, I'd approximate that with 60 days and say
... WHERE DateDiff("d",Date_field,Date())<60
(Replace Date_field with the name of the field containing the date.)
If you want to count rows according to multiple different criteria, or output both aggregate data and individual data, you'll be best off using separate SELECT statements.