count unique for field in crosstab query - sql

I've searched for an answer to this, but can't seem to find anything that works to do a unique count in crosstabs. The closest I could find was: select count(myField) from (select distinct myField from myTable), but I keep getting errors.
The training type is in columns, the dates in rows (formatted to month so I can separate out quarters or individual months), and ideally I would have the values as number of unique trainees that are identified by their Grower Record IDs. It currently displays all trainees who have been to the same training multiple times.
Any help would be greatly appreciated! Below is the SQL. Thanks for your time!
SQL:
TRANSFORM Count([TDB - Master - Trainee Attendence].[Grower Record ID]) AS [CountOfGrower Record ID]
SELECT Format([Dat Fòmasyon],'mm/yy') AS Months, Count([TDB - Master - Trainee Attendence].[Grower Record ID]) AS [Total Of Grower Record ID]
FROM [TDB - Master - Trainee Attendence]
WHERE (((Format([Dat Fòmasyon],'mm/yy'))="01/13" Or (Format([Dat Fòmasyon],'mm/yy'))="02/13"))
GROUP BY Format([Dat Fòmasyon],'mm/yy')
PIVOT [TDB - Master - Trainee Attendence].[Tit Fòmasyon an];

I would suggest that you create a query of the data you want to report (presumably):
SELECT DISTINCT [Grower Record ID], [Dat Fomasyon], [Tit Fomasyon an]
FROM [TDB - Master - Trainee Attendance]
Then generate a pivottable using this query as the data source.
Now I am not completely familiar with access's nuances, and dont know for certain that you can generate a pivottable from a query, but if not, I'd suggest changing your code (in a clone of the form) to do the same thing, replacing the from clause with a [aliased] query (and removing the references to the base table appearing in other clauses):
TRANSFORM Count([Grower Record ID] AS [CountOfGrower Record ID]
SELECT Format([Dat Fomasyon], 'mm/yy' AS Months, Count([Grower Record ID]) as [Total of Grower Record ID]
FROM (SELECT DISTINCT [Grower Record ID], [Dat Fomasyon], [Tit Fomasyon]
FROM [TDB - Master - Trainee Attendance]) Uniq
WHERE Format([Dat Fòmasyon],'mm/yy') = "01/13" Or Format([Dat Fòmasyon],'mm/yy' = "02/13"
GROUP BY Format([Dat Fòmasyon],'mm/yy')
PIVOT [Tit Fòmasyon an]
Note, I couldnt help thinking while rewriting the logic that it has been editted since being generated, as the name of the count (in [] after "AS") is different in the transform and select lines. I wouldnt expect this to matter, though in access you never know.
Hope this helps...
Please mark as answer if this helps you.

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 double match function and pulling in values

I have two tables in Access, Master Data and Current Data.
They both have columns "key" in them. Master data also has "From date" and "To Date" columns. Current Data has "Creation Date" column. I need to match two tables and for every "Key" match between the tables, I need the code to take a creation date and figure out if that date is between the To and from date in the master file. If it the creation date doesnt fall between the to and from date, then I need all those rows to show up in the query.
This is what I have so far (this code is pulling the match between the tables for the key tab)
SELECT
us.*
FROM [SAP Data] us
INNER JOIN [Master Data] uss ON uss.Key = us.Key
I can't figure out how to transition from this and further tell it to match the dates and see if the date falls between to-from dates in the master file. Is there anyone who could have any guidance as to how I could proceed?
I think you just want not exists:
SELECT cd.*
FROM [Current Data] as cd
WHERE NOT EXISTS (SELECT 1
FROM [Master Data] as md
WHERE cd.Key = md.Key AND
md.[Creation Date] BETWEEN md.FromDate and md.ToDate
);

Assistance with msaccess sql query

I have a form called “Search” with the question regarding the section under Transaction Search. It invokes a query based on the selection of full name (Combo40) which pulls from a table called “Individuals” and the committee(s) the user selected in Combo40 has contributed to in the Committee field:
Committee Query:
SELECT Transactions.[Committee Name] FROM Transactions GROUP BY Transactions.[Committee Name], Transactions.[Employee Name] HAVING (((Transactions.[Employee Name])=[Forms]![Search]![Combo40]));
I’m trying to add an additional parameter that also compares the year in which an individual gave which is a specific filed in the Transactions table called Combo46.
New Query:
SELECT Transactions.[Committee Name] FROM Transactions GROUP BY Transactions. [Committee Name], Transactions.[Employee Name] HAVING (((Transactions.[Employee Name])=Forms!Search!Combo40) And (((Transactions.Combo46)=Forms!Search!Combo51)))
Forms!Search!Combo40 = pulls the values from a table called “Individuals”
Transactions.Combo46 = year (i.e. 2011, 2012, 2013 etc.)
Forms!Search!Combo51 = year (i.e. 2011, 2012, 2013 etc.)
When I make my selections on the form I get a window that asks me to enter the value for Combo 46 and the committee list displays all records for that individual and not just transactions with the year I entered. I would expect it to not come up with a pop-up at all but instead take the value from Combo46 in the Transactions table and compare it to the value entered in Combo51.
It's too long and unclear for a comment, so I use this anwser space:
I suppose that the table column Transactions.Combo46 does not exist, so Transactions.Combo46 is popuped by Access for Input, it should be Transactions.Year or something meaning this:
SELECT Transactions.[Committee Name]
FROM Transactions
GROUP BY Transactions.[Committee Name], Transactions.[Employee Name]
HAVING (((Transactions.[Employee Name])=Forms!Search!Combo40)
And (((Transactions.Year)=Forms!Search!Combo51)))
What jacouh says is surely the answer (i.e., your SQL is referring to Combo46 as a member of Transactions when it is a member of Forms!Search). As an appendum, I'd simplify things a bit like this:
(1) Name the combo boxes something sensible (e.g. cboName and cboYear) rather than leaving them as Combo40 and Combo51 (yuck!).
(2) The GROUP BY seems unnecessary to me:
SELECT DISTINCT [Committee Name] FROM Transactions
WHERE ([Employee Name] = Forms!Search!cboName) AND ([Year] = Forms!Search!cboYear)

Creating a View from an Aggregated Self-Join

I've been working for around two weeks now on building some detailed reporting options for the company I work at. I asked a question here sometime last week or the week before, and that got me started on a query, which I eventually tightened up substantially.
I'm starting from an inventory ledger which just keeps track of single transactions. The goal is to build a more thorough ledger that will keep a running stock total, a running sales total, and if an item went out of stock, it will track the days until resupply.
The initial query used a With ... as statement to define the table with its aggregates before doing the self join on the aggregated columns. Unfortunately, I can't do the same thing to create a view, so I need to find a way to create those aggregates differently that will still allow me to self-join on them to keep my totals in order.
Here is how I've retooled my statement so far:
Create View 'QLedger' as
Select tcum.txnid,
tcum.Item,
tcum.TxnDate,
tcum.[Tran Type],
tcum.Quantity,
tcum.cumq
from (select *, SUM( Quantity )
OVER (PARTITION BY InventoryLedger.Item
ORDER BY InventoryLedger.TxnID
ROWS UNBOUNDED PRECEDING ) cumq,
abs(
sum(
case when [Tran Type] = 'Shipping'
or [Tran Type] = 'Customer Return'
then Quantity end)
over (partition by qryrptInventoryLedger.item
order by InventoryLedger.txnid
rows unbounded preceding)) LifeSales
from InventoryLedger) tcum
left outer join InventoryLedger tcumnext
on tcum.Item = tcumnext.Item
and tcum.TxnID < tcumnext.TxnID
and
tcum.cumq = 0 and tcumnext.cumq >0
where tcum.Item = '103-02'
and tcum.cumq = 0
group by tcum.TxnID, tcum.TxnDate, tcum.Item, tcum.[tran type], tcum.Quantity
This is almost right, except the table I'm self joining to (tcumnext) doesn't have a running/cumulative quantity column to compare to tcum. I can't at all figure out how to make one to compare with. Can anyone help me out? I'd really appreciate it. It's exciting and frustrating to be so, so close after working on this for so long.
If you already solved the aggregated-functions problem using a with in your query you can do it with the view as well.
Here's an example of a view that uses a with clause which contains aggregate functions:
http://social.msdn.microsoft.com/Forums/sk/sqlgetstarted/thread/302040c6-6a1b-4f99-8a1d-84bb196cb5e6
First post there.
Hope this helps =)
You can use a with statement in a view:
create view xxx as
with <blah blah blah>
select <your query>
Does this solve your problem?

Microsoft Access 2003 - creating a separate field that totals 5 other fields from the table

This is what i have so far in the SQL code........
SELECT DISTINCTROW [OEE/A Query].Press,
Sum([OEE/A Query].[SumOfLabor Hours]) AS [Sum Of SumOfLabor Hours],
Sum([OEE/A Query].[SumOfGood Pieces]) AS [Sum Of SumOfGood Pieces],
Sum([OEE/A Query].[Scrap Pieces]) AS [SumOfScrap Pieces],
Sum([OEE/A Query].[SumOfMachine Hours]) AS [SumOfSumOfMachine Hours],
Sum([OEE/A Query].[Total Parts Hours Earned]) AS [SumOfTotal Parts Hours Earned],
Sum([OEE/A Query].[Standard Pcs Expected]) AS [Stand Pcs Expected]
FROM [OEE/A Query]
GROUP BY [OEE/A Query].Press;
How do i add to this code another field that totals 5 separate other fields?
Here's what i think it might look like but I'm not sure.....
SELECT Sum(Sort+Straighten+Shine+Standardize+Sustain)
SUM(Sort),
SUM(Straighten),
SUM(Shine),
SUM(Standardize),
SUM(Sustain),
FROM [Shift Report Table];
it depends on what you are really asking. I am not sure if your second query is just another example, or, if it is in fact a different data source to be added into your first query.
If you want to add another column to your first query example based on the columns that already exist in your query then yes, simply add them together, as per CK says.
e.g.
select sum(column1) + sum(column2) as sum_c1_c2
or
select sum(column1 + column2) as sum_c1_c2
when doing aggregate functions I have an old habit of handling NULL values to make sure I am getting the results I think I should be getting. e.g.
select sum(nz(column1,0) + nz(column2,0)) as sum_c1_c2
Now if you are asking how to add a new column from a different data source, then you can either join to that other datasource, or, if returning just a single value, can use an inline select.
Also, a word of warning about distinctrow - I am not sure you want to use that in your query. You are doing a group by, so only the unique values for [press] will be selected with the aggregated columns as per your SUM() function. It is really hand for things like determining if (by way of example) a Product has been ordered e.g.
select DISTINCTROW productname
from products
inner join orders on orders.productid = products.productid
will return just 1 row so there is no chance for any aggregation in the above case.
But you might want to clarify your requirement a bit more. CK might have already given the answer, or, we might need to include a different datasource (such as your second query).
Yes, that is correct. You can add fields within the sum function, to sum the sum of the fields.