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

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:

Related

SQL Frequency report with null values in columns

I need help creating a frequency report for data that crosses multiple columns of data. Here's an example of my data:
[Sample Data Example]
And here's how I need for the data to read, except that I can't figure out how to get the frequency.
[Frequency Report]
There may be null values in a column in a particular row, but I still need that distinct row counted. I have all of the code working except how to get the frequency/count.
This is what I've tried, but all I get are 0s for the frequency.
select distinct TEST, PANEL, UNITS, LOINC, count(distinct TEST, PANEL, UNITS, LOINC) as FREQ
from DEV_HEALTHTERM.SOURCE.LAB
GROUP BY TEST, PANEL, UNITS, LOINC
ORDER BY FREQ DESC;
Can someone please help?
Thank you.
Robin
you should count columns that are not participating in the group by .in your case you can just use * :
select TEST, PANEL, UNITS, LOINC, count(*) as FREQ
from DEV_HEALTHTERM.SOURCE.LAB
GROUP BY TEST, PANEL, UNITS, LOINC
ORDER BY FREQ DESC;
also you don't need distinct , since you are groping by , It's redundant.

How to construct a crosstab query table

I have data in an Access query in this format:
Depending on how/when the scores were entered, which can be random as above. Fixture is always parent to 2 teams.
I would like to present it as follows:
Fixture1: Team A 1-2 Team B
Fixture2: Team C 3-1 Team D
I think I can still write code to do that, but I was thinking it should possibly be done in a query rather than code...?
I have tried a Crosstab query but I am ending up with something like this:
This is my current Crosstab query:
TRANSFORM Sum(FixtureResultGoalQ.Score) AS SumOfScore SELECT
FixtureResultGoalQ.FixtureID, Teamname, Sum(FixtureResultGoalQ.Score)
AS [Total Of Score] FROM FixtureResultGoalQ GROUP BY
FixtureResultGoalQ.FixtureID PIVOT FixtureResultGoalQ.TeamName;
Is this something I should be able to do in a (Crosstab) query?
Thanks.
Remove the TEAMNAME out of your select:
TRANSFORM Sum(FixtureResultGoalQ.Score) AS SumOfScore SELECT FixtureResultGoalQ.FixtureID, Sum(FixtureResultGoalQ.Score) AS [Total Of Score] FROM FixtureResultGoalQ GROUP BY FixtureResultGoalQ.FixtureID PIVOT FixtureResultGoalQ.TeamName;

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.

Access 2007 Crosstab Query Expression

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;

JSP result set from MS Access - how to get the location name?

I have a table called "form" in ms access which have following fields:
formno, location, status.
I want to create a report which calculates:
the total no of forms(columns) at each location
the total no of forms(columns) with status= "pending" at each location.
I tried to do it with this query:
select count(formno) as totalforms
from form
group by location;
select count(formno) as pendingforms
from form
group by location
WHERE status = 'pending';
SELECT Location, COUNT(FormNo) AS TotalForms,
SUM(IIF(status = "pending", 1, 0)) AS Pending
FROM FORM GROUP BY Location
Does this help?
The second SQL Statment has the Where Clause in the wrong place. It should look like one of these:
SELECT Count(form.formno) AS CountOfformno, form.location
FROM form
GROUP BY form.location, form.status
HAVING (((form.status)='pending'));
SELECT Count(form.formno) AS CountOfformno, form.location
FROM form
WHERE (((form.status)='pending'))
GROUP BY form.location;
Basically if you wish to group by a column and set criteria on it use the Having Clause. The Where Clause comes before the group by clause.
To learn the SQL syntax you may find it easier to use the Query By Design Grid (in Design View) first then switch to SQL View after you get the results you are after in Datasheet View.
Form is a reserved word and must be enclosed in square brackets: http://support.microsoft.com/kb/286335. I would suggest renaming the table, because it will continue to cause problems.
select location,count(formno) as totalforms
from [form]
group by location
select location, count(formno) as pendingforms
from [form]
WHERE status = 'pending'
group by location
Note that count(*) can also be used if you wish to include all records in the table: In SQL is there a difference between count(*) and count(<fieldname>)