I want to use the result of a crosstab with a flexible amount of column headings in a form. The query shows how many hours employees (column headings) spent on different tasks (row headings).
The query works if I open it as is. Pulling it into the form I get the error message that I cannot use a crosstab query without fixed column headings. I need the ability to show a flexible amount of column headings since otherwise the resulting table is too big and confusing. Is there a solution for this problem?
That's the query:
PARAMETERS [Forms]![PL_Stundenabfrage]![ProjektWaehler] Long;
TRANSFORM Round(Sum(DateDiff("n",[tblZeiten].[ZE_Start],[tblZeiten].[ZE_Ende])/60),2) AS Stunden
SELECT [KS_Bez] & " (" & [KS_BezLang] & ")" AS Taetigkeit, Sum(Stunden) AS GESAMT
FROM tblMa INNER JOIN (tblKst INNER JOIN (tblPr INNER JOIN tblZeiten ON tblPr.PR_Key = tblZeiten.ZE_Pr) ON tblKst.KS_Key = tblZeiten.ZE_Kst) ON tblMa.MA_ISN = tblZeiten.ZE_MA
WHERE (((tblZeiten.ZE_Pr)=[Forms]![PL_Stundenabfrage]![ProjektWaehler]))
GROUP BY [KS_Bez] & " (" & [KS_BezLang] & ")"
PIVOT tblMa.MA_Nachname;
Related
What I want to accomplish:
1.Set the CaseNo column to for a new group to stand out in a separate line and same to follow for each detail until a new group is recorded.
2.Call the name of the column in a group total for that particular group
Currently, what I have produced is a bit different form the image below.
I have got the fix!
= "Total Gap for " & Fields!CaseNo.Value & " = " & Sum(Fields!Gap.Value)
I'm trying to return records for an alert system based on two conditions.
The first condition is there is a booking in the system for tomorrow's date [Date()+1] with a Type value of B. If that JobNumber also has a Type value (in another record) of A AND the Result field's value is "Not Approved" we need to return an alert.
Example table:
JobNumber Type Company Date Result
58129 B 3 22/03/2013
58129 A 3 20/03/2013 Not Approved
58129 C 3
So far I have been able to create a SQL query in VBA to return the results of the first condition and have looped through the results to return the relevant JobNumbers. How do I insert these JobNumbers as criteria for the second SQL query or is it possible to combine all criteria into one SQL statement?
My SQL so far:
strSQL1 = "SELECT I.JobNumber, I.Type, I.Company, I.Date, I.Result " & _
"FROM tblInspection I " & _
"WHERE (((I.Type)='B') AND ((I.Date)=Date()+1));"
strSQL2 = "SELECT I.JobNumber, I.Type, I.Company, I.Date, I.Result " & _
"FROM tblInspection I " & _
"WHERE (((I.Type)='A') AND ((I.Result)<>'approved'));"
Any help would be much appreciated.
You can get a field from the same or another table. This will give an error if more than one row is returned, but whether or not more than one row will be returned depends on your data. If it is likely, you will need to add another criterion, such as date.
SELECT I.JobNumber, I.Type, I.Company, I.Date, I.Result,
(SELECT Result
FROM tblInspection q
WHERE q.JobNumber=i.JobNumber
AND Result="Not Approved"
AND Type="A")
As ResultA
FROM tblInspection I
WHERE I.Type='B' AND I.Date=Date()+1
How do I ensure that I pick up the right number of records when filtering for dates within an Access Query:
SELECT ID, REF, SalesDate, DCount("ID","tblRecords"," Ref='" & [Ref] & "' AND [SalesDate]=#" & format([SalesDate],"yyyy/mm/dd") & "#") as EXPR1 from tblCurrent
It picks up the date ok if it cannot be misconstrued such as 28-04-12, but if it is 04-06-12 it doesn't pick it up as it's assuming it's the wrong way around.
Note that this query is not created on the fly or generated from a form etc...
I either use yyyy/mm/dd for dates in VBA:
#" & Format([SalesDate],"yyyy/mm/dd") & "#"
Or parameters, for building queries.
EDIT re additional information
Seeing you are using SQL server, I suggest you use a derived table, which you may find faster, for example:
SELECT dbo_Table_1.ADate, ACount FROM dbo_Table_1
LEFT JOIN (SELECT a.ADate,Count(*) As ACount
FROM dbo_Table_1 As a GROUP BY a.ADate) b
ON dbo_Table_1.Adate=b.ADate
EDIT re discussion
SELECT * FROM dbo_vwRecordsCurrent As t
LEFT JOIN (
SELECT a.OpptyIncentiveModifiedDate, a.DataSetID, Count(*) AS ACount
FROM dbo_vwRecordsHistorical AS a
WHERE a.OpportunityIgnored = True
GROUP BY a.OpptyIncentiveModifiedDate, a.DataSetID) AS h
ON t.OpptyIncentiveModifiedDate = h.OpptyIncentiveModifiedDate
AND t.DataSetID = h.DataSetID
I have aliased your tables as the names are very long, so to me, it is more readable to use aliases on the outer sql. They are essential in the inner sql. It is not a good idea to alias a derived table with the name of an existing table.
I'm pulling information that will eventually be from 5 tables at once based off of a filtering system. Right now I have three different databases running, its looking great. My issue is I have certain fields that I only want to display distinct information on and others i want to display all. To better explain I'm going to give my example.
My select code:
SELECT w.event,
w.city,
w.DATE,
a.TIME,
w.tmc,
a.weather,
a.surface_temperature,
p.top,
p.LEFT
FROM weather w
LEFT OUTER JOIN application a
ON a.DATE = w.DATE
AND a.tmc = w.tmc
LEFT OUTER JOIN pinlocations p
ON w.city = p.cityname
WHERE w.DATE = '" & datepicker_value.Text & "'
AND w.TIME LIKE '" & eventTime.SelectedItem.Value & "'
I have a map which I'm placing pins on based of the p.top and p.left. When I click on this I want to display the city name, the tmc, and then under that all the other information based off the filtered search. In the example above it creates pins on top of pins, making a new one for each field, I want it to be distinct.
I know the distinct command exists, just not sure how to use it in this situation.
Thanks!
Use a group by modifier, on the values you want to be distinct.
Then use a group_concat on the values you want to have listed in a comma-separated list.
SELECT group_concat(w.event) as events,
group_concat(w.city) as cities,
group_concat(w.`DATE`) as dates,
group_concat(a.`TIME`) as times,
group_concat(w.tmc) as tmcs,
group_concat(a.weather) as weathers,
group_concat(a.surface_temperature) as temperatures,
p.top,
p.LEFT
FROM weather w
LEFT OUTER JOIN application a
ON a.DATE = w.DATE
AND a.tmc = w.tmc
LEFT OUTER JOIN pinlocations p
ON w.city = p.cityname
WHERE w.DATE = '" & datepicker_value.Text & "'
AND w.TIME LIKE '" & eventTime.SelectedItem.Value & "'
GROUP BY p.top, p.left
If a left-top coordinate only ever links to one city (as you'd expect), there's no need to put it inside a group_concat statement. Nor does MySQL require* you to put it in the group by clause.
See:
http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat
* ) you can force MySQL do enforce strict group by rules, but by default it is off.
You cannot use distinct here, because distinct is an all or nothing affair, it operates in the collectivity of all selected values, not just on one field.
Problem:
I originally had a query that was working great but I'm now having to change it to pull more fields. When I try run the new query it picks a field name and says that I haven't included it as part of the aggregate function. Each time I get this error I can add the field the error specifies to the Group By statement and the error message will choose a new field that isn't included. Anyone have any idea's as to how I can get the same information I was getting with the original query just with more fields?
Description of how query is supposed to work:
The query is meant to pull one record for each distinct set of readings_miu_ids and ReadDates (The PremID field is the same for each distinct readings_miu_id).
Original Query:
strSql3 = " SELECT Distinct readings_miu_id, ReadDate, PremID " & _
"INTO analyzedCopy2 " & _
"FROM analyzedCopy "
DoCmd.SetWarnings False
DoCmd.RunSQL strSql3
DoCmd.SetWarnings True
New Query:
strSql3 = " SELECT Top 1 readings_miu_id, Reading, ReadDate,Format([MIUtime],'hh:mm:ss') AS ReadTime,MIUwindow,SN,Noise,RSSI,ColRSSI,MIURSSI,Firmware,CFGDate,FreqCorr,Active,MeterType,OriginCol,ColID,Ownage,SiteID,PremID , Neptune_prem.prem_group1, Neptune_prem.prem_group2,ReadID " & _
"INTO analyzedCopy2 " & _
"FROM analyzedCopy " & _
"Group By readings_miu_id, ReadDate, PremID " & _
"Order By readings_miu_id, ReadDate, ReadID, PremID "
DoCmd.SetWarnings False
DoCmd.RunSQL strSql3
DoCmd.SetWarnings True
In my experience (which is only moderate) every column in the result set (but NOT every aggregate) must be in the group by.
Here's a decent reference
When you include a GROUP BY clause, each field must either be in the GROUP BY or have an aggregate function (e.g, MAX, MIN, SUM, COUNT) applied to it.
For example, a simple correct implementation might be:
SELECT Department, MAX( Salary ) FROM Employees GROUP BY Department
... and a simple incorrect implementation would be:
SELECT Department, Salary FROM Employees GROUP BY Department.
Consider the two statements above. For the first, you can easily imagine what a datasource would look like and what would be returned. However for the second, what would you return? Which individual value of Salary would you return in your resultset? Hence, when you group fields, each field in the result set must either participate in the GROUPing or be the result of an aggregation of the values collected from the group comprised of the other fields.
You can accomplish this via a subquery or two queries. Also, "CurrentDb.Execute" is the preferred method to run a query like this (instead of "DoCmd.RunSQL").
CurrentDb.Execute strSQL3, dbFailOnError