Access 2007 Crosstab Query Expression - ms-access-2007

Goal: to create a percentage column based off the values of calculated columns.
Here's the SQL code of the Crosstab query:
TRANSFORM Count(Master_Calendar.ID) AS CountOfID
SELECT Master_Calendar.Analyst, Count(Master_Calendar.ID) AS [Total Of ID]
FROM Master_Calendar
GROUP BY Master_Calendar.Analyst
PIVOT Master_Calendar.[Current Status];
This gives me a crosstab query that displays the amount of entries in the database that are "Completed", "In Process", or "Not Started", sorted by which Analyst they belong to.
What I'm trying to do is add another column to calculate the Percent Complete -- so (Completed / Total of ID) * 100. I tried putting that into an expression in another cell, but it returns with a "[Completed]" not found, even though it gives me it as an option in the Expression Builder.
Am I just naming my variables wrong, or is it not possible to do it this way? Can I reference the total count of the records that contain "Completed" using query code instead of finding out the value using a Pivot table?
Thanks for your help.

Try:
SELECT
xTab.Analyst,
[Completed]/([Total of ID]/100) AS [Complete%],
[In Process]/([Total of ID]/100) AS [In Process%],
[Not Started]/([Total of ID]/100) AS [Not Started%]
FROM xTab;

Related

How to write a query to parse data into multiple columns from one column

I have an existing table (link #1) that I am trying to write a query for so that the query reformats the data as seen in the second link. Basically it is a table listing the completed email types for a group of users. The "Completed Type" is a single column with multiple values. I am trying to parse out the individual values (3 of them) from the "Completed Type" into their own column with a total count. I also would like to add a seperate column called "Completed" which is simply a sum of "Closed without response" and "Replied" for that particular user for that particular month.
I plan on then creating a pivot in Excel that will read off of the new query with the reformated data. For the life of me, I can't figure out how to write this in SQL. I tried creating individual queries to total the different "Completed" types and then tried to union them, but it is not working.
Existing table
Future Query Output
Any advice or guidance you can provide in writing a SQL query in Access that will produce image # 2 would be GREATLY appreciated! Thank you in advance!
You can use case when and sum, for example:
select month,
id,
sum(case when completed_type = "completed" then 1 else 0 end) as completed
from table
group by month, id
Use a crosstab query:
TRANSFORM
Sum([Case Count]) AS [SumOfCase Count]
SELECT
[Month],
ID,
[Adjusted Name],
Mgr,
Sup,
Region,
Region2,
Sum(Abs([Completed Type] Not Like "Closed*")) AS Completed
FROM
Cases
GROUP BY
[Month],
ID,
[Adjusted Name],
Mgr,
Sup,
Region,
Region2
ORDER BY
ID,
[Month] DESC
PIVOT
[Completed Type] In ("Replied","Sent","Closed without response");
Output:

SQL sum, multiple Group By's and Date criteria

I'm looking to perform a sum calculation on a SQL table to find the quantity of a particular stock item held against a particular salesperson up to and including a specific date.
I'm able to perform the sum function to find the quantities on a Salesperson/item basis whenever I do not factor in the date range criteria, but as soon as i add that aspect, it all goes a bit pear shaped! Here is my code so far:
SELECT Salesperson, Item No, Sum(Quantity) AS 'Quantity'
FROM dbo
WHERE (Location Code='VAN')
GROUP BY Salesperson, Item No,
HAVING (Registering Date<={ts '2017-05-03 00:00:00'})
The location code = VAN filter is required to ensure it ignores Warehouse quantities.My SQL knowledge is limited to the few instances I run into it at work and my interaction is largely based through Microsoft Query in Excel. When looking at the above code, i figured that the 'Registering date' criteria should be in the 'WHERE' section, however when i add the criteria using the options available in Microsoft Query, it creates the 'HAVING' line.
If anyone could provide any pointers, it would be much appreciated!
Cheers
Peter
I would imagine a query like this:
SELECT Salesperson, [Item No], Sum(Quantity) AS Quantity
--------------------^ escape the non-standard column name
FROM dbo.??
---------^ table name goes here
WHERE Location Code = 'VAN' AND
[Registering Date] <= '2017-05-03'
------^ put the filtering condition in the correct clause
GROUP BY Salesperson, Item No
-----------------------------^ remove the comma
Your code, as written, has multiple errors. I am guessing that most are transcription errors rather than in the original query (queries don't run if no table is given in the FROM for instance). The "major" error would then be filtering in the HAVING clause rather than the WHERE clause.

Query cannot be parse SELECT avg_galoon, avg_galoon

I dont know if its the right way to multiply that way, I want to multiply the avg_galoon that is sold weekly by the price of it which is 20
SELECT avg_galoon,
avg_galoon * 20 + NVL(total income , 0)
FROM customer;
total income isn't a column name from the customer table.
If you disagree and can see that column name (via methods mentioned in the comments already) then you will need to enclose that column name in the appropriate syntax for the database engine you are using i.e. [total income] for Microsofts SQL Server, `total income` for MySQL etc.
In future avoid the use of spaces in column names.

Sum of distinct values in field SSRS 2005

I'm working on SSRS report builder that is using a dataset calling a SQL Server 2000 database.
The query is getting sums of a few different fields and is also pulling out all records that have to do with that client number. I want to get the sum of the sum but it is way over because of the detail rows. Basically what I want is the sum of the distinct sum column values.
=Sum(Fields!tot.Value, "table1_Group3")
I saw that you can get sums by the groups and I tried the expression above but it comes back with an error:
The Value expression for the textbox 'tot' has a scope parameter that is not
valid for an aggregate function...
table1_Group3 is the name of the group that holds the sum value in the report.
Any suggestions on how to get the distinct values to sum them in this report.
=Sum(Fields!tot.Value, "table1_Group3")
The code above will give you the sum of "tot" for all rows in the current "table1_Group3." This means that this expression only makes sense somewhere within table1_Group3. Otherwise, SSRS doesn't know which is the current instance of that group.
Sounds like you would like to sum this value across multiple groups, but only take one "tot" from each instance of the group. (Are you sure that all rows in that group will have the same "Tot?")
If tot is the total of other fields in your returned data, then simply add those up in your formula. This may have the added benefit of simplifying your SQL query as well.
Some other options that could work:
- Change your SQL query so that only one row per group gets the Tot field set.
- Use Embedded code in the report to keep a running total which is added to only once per group, such as in the group header.
(If upgrading to 2008R2 SSRS is an option, then the Lookup function could be used here, maybe even to look back at the same dataset.)
change the query/ dataset to sum(distinct tot) using the temp table on the sql server
I suppose you need to write sum(distinct columnName).

Ms Access : Query to work out percentage

I have a database which currently records the amount of times someone does a certain procedure and they scores they have received. The scoring is done by select a value of either N, B or C.
I currently have written a query which will count the total number of times a procedure is done and the amount of times each score is received.
Here is the result of the query (original: http://www.flickr.com/photos/mattcripps/6673555339/)
and here is the code
TRANSFORM Count(ed.[Entry ID]) AS [CountOfEntry ID]
SELECT ap.AdultProcedureName, ap.Target, Count(ed.[Entry ID]) AS [Total Of Entry ID]
FROM tblAdultProcedures AS ap LEFT JOIN tblEntryData AS ed ON ap.AdultProcedureName = ed.[Adult Procedure]
GROUP BY ap.AdultProcedureName, ap.Target
PIVOT ed.Grade;
If a score of N or B is given that is deemed below standard and C is deemed at standard. Is there a way I can add something to my query which will show me in percentage how many of the procedures we at standard and how many below?
I really cant get my head round this so any help would be great.
Thanks in advance
UPDATE TabProd
SET PrecProd = (PrecProd * 1.1)
WHERE Código IN (1,2,3,4)
I did something very similar to this on a pretty large scale.
My issue was the need to be able to run queries over specific (but user variable) timeframes and output similar percentage of total results in a report.
I won't get into the date issue but my solution was to run the "sum" function on the total line on my specific reject criteria to get totals of the rejects then use a divide expression to create a new column element (defined expression) in the same query pulling from the joined table of "Total net production" - joined by a common reference - job ID.
For your case it sounds like you want to sum the two failure types - which you would simply add defined expressions dividing your total instances into your various failure modes and formatting in your output report as percents. To finish the data portion of your report you then need a third expression defining your "non-fail percent" - which would be 1.0 - N/total - B/total - both of which you will have previously defined in the query to determine the N and B failure rates.
Then its a matter of pulling that information into your report and formatting. It definitely CAN be done.
Hope this helps.