(Access 2010)
Can anyone tell the reason why column 2 and 3 retrieve exactly the same numbers? (they should not - different conditions and consequently different number of records)
Cannot see what's wrong...
SELECT tblAssssDB.[Division:], Count(([Mod New Outcome]="SELF EMPLOYED" And [ET Outcome]="GREY AREA")) AS [Undecided to Self-employed], Count(([Mod New Outcome]="EMPLOYED" And [ET Outcome]="GREY AREA")) AS [Undecided to Employed], Count(([Mod New Outcome]="SELF EMPLOYED" And [tblAssssDB].[ET Outcome]="EMPLOYED")) AS [Employed to Self-Employed], Count((IsNull([ET Outcome] And [ET Outcome]=[Mod New Outcome]))) AS [No change in Outcome]
FROM tblAssssDB
WHERE (((tblAssssDB.[ET Comment]) Is Not Null))
GROUP BY tblAssssDB.[Division:];
Help!!
Count is not really intended to be used to give you a quantity of specific outcomes. I think that you would be better served by using something like this
SUM(IIF(critiera, 1, 0))
Related
I've trying to understand GoogleBigQuery and I've seen this in a Query : AS t0
I also see t0 attached to some metrics or dimension like this t0.postId
Here is the full query I'm trying to understand :
SELECT t0.Author, COUNT(DISTINCT t0.postId, 50000) AS t0.calc_FPB538 FROM (SELECT
MAX(IF (hits.customDimensions.index = 10, hits.customDimensions.value, NULL)) WITHIN RECORD AS postId,
date(MAX(IF (hits.customDimensions.index = 4, hits.customDimensions.value, NULL))) WITHIN RECORD AS Datepublished,
MAX(IF (hits.customDimensions.index = 1, hits.customDimensions.value, NULL)) WITHIN RECORD AS Country,
MAX(IF (hits.customDimensions.index = 7, hits.customDimensions.value, NULL)) WITHIN RECORD AS Author,
FROM
[My_data.ga_sessions_20161104]) AS t0 WHERE (STRFTIME_UTC_USEC(TIMESTAMP_TO_USEC(TIMESTAMP(STRING(t0.Datepublished))), '%Y%m%d') >= '20161102' AND STRFTIME_UTC_USEC(TIMESTAMP_TO_USEC(TIMESTAMP(STRING(t0.Datepublished))), '%Y%m%d') <= '20161108') GROUP EACH BY t0.Author ORDER BY t0.calc_FPB538 DESC
What does it mean, how should I use it ?
Thanks.
I think you really need to find a tutorial on basic sql/query terms and methods, but in general (and I'm going to use general terms like object as it applies whether table or not) when you see syntax like this:
[My_data.ga_sessions_20161104]) AS t0
You are saying look at this object/table [My_data.ga_session_20161104] and give it a label of t0 so I can reference columns/datapoints on that object. Then when you later see things like t0.postId you know that you are referencing [My_data.ga_sessions_20161104]. This way if you reference another similar table that has a datapoint/column of postId both you and the engine running the query knows what the heck you are talking about.
You can also label columns/data points as you see in your query with COUNT(DISTINCT t0.postId, 50000) AS t0.calc_FPB538 this is saying perform a count on the number of postId results and label it as t0.calc_FPB538 because I will want to reference it as such later (or you just like your resutls to have specific names).
I posted a few weeks back with an issue, now I've gotten past it and found a new need and new issue. I'm using access 2010, unsure what version database, have on;y access grunt tools to use (no objects, for instance). I have a query that works, it looks like this. The goal of this query is to use one row as a baseline and find all related rows that come after it chronologically. For all I know, this may be a very crude solution.
SELECT QueueA.cnlyMemberId, QueueA.updateUser AS 305UpdateUser, QueueA.updateDt AS 305UpdateDt, QueueB.statusCd
FROM (SELECT cnlyMemberID, updateUser, updateDt
FROM V_Queue_History
WHERE statusCd = "305" AND
V_Queue_History.updateDt Between [Enter Start Date:] And [Enter End Date: (must be at least one day apart)]
) AS QueueA INNER JOIN
V_Queue_History AS QueueB
ON (QueueA.cnlyMemberID = QueueB.cnlyMemberID AND
QueueA.updateDt < QueueB.updateDt
)
Now what I want to do is I want to find the first 305 statusCd and the next statusCd chronologically, rather than every statusCd that follows. I've tried a few things now, mostly trying to take the TOP 2 with an ORDER BY updateDt slipped in the ON condition. Then I tried replacing the V_Queue_History with a table and doing the same, didn't work. I just tried using a where condition where I check to see if the
QueueB.cnlyMemberId is IN (SELECT TOP 2 cnlylMemberID FROM QueueB WHERE conditions);
but still nothing. The system would tell me it didn't know what QueueB was anymore, so none of it would run in the WHERE statement. I would be more specific on but I just made Access crash and my mind is starting to get hazy from how frustrating this can be. I don't have much experience with Access/SQL at all and I'm learning it pretty much as I go.
So to recap, the first query runs fine but I need to return only the row with the status updated first after the initial 305.
Thanks!
I'm not sure what you want without a sample output, maybe something like this would help:
SELECT QueueA.cnlyMemberId, QueueA.updateUser AS 305UpdateUser, QueueA.updateDt AS 305UpdateDt, QueueB.statusCd
FROM (SELECT cnlyMemberID, updateUser, updateDt
FROM V_Queue_History
WHERE statusCd = "305" AND
V_Queue_History.updateDt Between [Enter Start Date:] And [Enter End Date: (must be at least one day apart)]
) AS QueueA INNER JOIN
V_Queue_History AS QueueB
ON QueueA.cnlyMemberID = QueueB.cnlyMemberID
WHERE (QueueB.updateDt =
(SELECT Min(updateDt) FROM V_Queue_History V2
WHERE V2.cnlyMemberID = QueueA.cnlyMemberID
AND V2.updateDt > QueueA.updateDt))
This assumes there is always a status code after the 305, and also that no two records have the same update date.
If you need to get the top N status codes after the 305 then you could change the WHERE clause as following, although I don't know if the performance will be good enough for your situation:
WHERE QueueB.updateDt IN
(SELECT TOP 5 updateDt FROM V_Queue_History V2
WHERE V2.cnlyMemberID = QueueA.cnlyMemberID
AND V2.updateDt > QueueA.updateDt
ORDER BY V2.updateDt)
I'm a big confused on this one. I'm trying to use a SQL statement that sums up two different records but displays both 'Makes' of each row. I currently have this statement..
SELECT
tDealerships.CapShortName,
SUM(tObjective.CommitObj) AS CommitObj,
SUM(tObjective.ActualMTD) AS MTD,
SUM(tObjective.DelToday) AS DelT,
SUM(tObjective.ActualMTD)/Sum(tObjective.CommitObj) AS CommitUnit,
SUM(tObjective.CommitGrossObj) AS CommitGrossObj,
SUM(tObjective.GrossActual) AS GrossActual,
SUM(tObjective.GrossActual)/Sum(tObjective.CommitGrossObj) as CommitGross,
Sum(tObjective.ActualMTD)/Sum(tObjective.GrossActual) AS MTDPRU
FROM
tObjective, tMake, tDealerships
WHERE
tObjective.DealershipID = 10
AND NewUsed = 'New'
AND tObjective.MakeID = tMake.MakeID
AND tObjective.DealershipID = tDealerships.DealershipID
AND (tMake.Make LIKE '%BUICK%' OR tMake.Make LIKE '%GMC%')
GROUP BY
tDealerships.CapShortName
which returns this..
However, I need to display the two makes that it is summing up which is Buick and GMC.
If I add the make in the statement, I must group by the make which then separates them into two row sums.. one for Buick and then one for GMC. Is there a better way or doing this? I am able to make it do what I need it to do? I have been stuck on this one for a bit now. Any suggestions/help is greatly appreciated!
EDIT: The ideal result is having 1 result with an additional column named Make that displays both Buick/GMC together.
You had to hard-code the makes for the LIKE strings. Why not just hard code them in a literal for the make column?
SELECT
tDealerships.CapShortName,
SUM(tObjective.CommitObj) AS CommitObj,
SUM(tObjective.ActualMTD) AS MTD,
SUM(tObjective.DelToday) AS DelT,
SUM(tObjective.ActualMTD)/Sum(tObjective.CommitObj) AS CommitUnit,
SUM(tObjective.CommitGrossObj) AS CommitGrossObj,
SUM(tObjective.GrossActual) AS GrossActual,
SUM(tObjective.GrossActual)/Sum(tObjective.CommitGrossObj) as CommitGross,
Sum(tObjective.ActualMTD)/Sum(tObjective.GrossActual) AS MTDPRU
------
'GMC/Buick' As Make
------
FROM tObjective, tMake, tDealerships
WHERE tObjective.DealershipID = 10
AND NewUsed = 'New'
AND tObjective.MakeID = tMake.MakeID
AND tObjective.DealershipID = tDealerships.DealershipID
AND (tMake.Make LIKE '%BUICK%'
OR tMake.Make LIKE '%GMC%')
GROUP BY tDealerships.CapShortName
I think that something as simple as grouping on the make as well would solve your problem, if I am reading it correctly?
SELECT
tDealerships.CapShortName,
SUM(tObjective.CommitObj) AS CommitObj,
SUM(tObjective.ActualMTD) AS MTD,
SUM(tObjective.DelToday) AS DelT,
SUM(tObjective.ActualMTD)/Sum(tObjective.CommitObj) AS CommitUnit,
SUM(tObjective.CommitGrossObj) AS CommitGrossObj,
SUM(tObjective.GrossActual) AS GrossActual,
SUM(tObjective.GrossActual)/Sum(tObjective.CommitGrossObj) as CommitGross,
Sum(tObjective.ActualMTD)/Sum(tObjective.GrossActual) AS MTDPRU
FROM tObjective, tMake, tDealerships
WHERE tObjective.DealershipID = 10
AND NewUsed = 'New'
AND tObjective.MakeID = tMake.MakeID
AND tObjective.DealershipID = tDealerships.DealershipID
AND (tMake.Make LIKE '%BUICK%'
OR tMake.Make LIKE '%GMC%')
GROUP BY tMake.Make, tDealerships.CapShortName
In CF I am trying to do a QoQ where the rows are in a list of other rows. Basically moving some code from cftags to cfscript (Not important why). In tags we have a main query and we have several nests that do some heavy lifting. I am moving this to cfscript and have the following syntax that is working:
var submissionList = new Query(dbtype="query", QoQsrcTable=ARGUMENTS.answers, sql="
SELECT submission_id FROM QoQsrcTable GROUP BY submission_id
").execute().getResult();
var submissions = new Query(dbtype="query", QoQsrcTable=ARGUMENTS.answers, sql="
SELECT * FROM QoQsrcTable WHERE submission_id IN (#submissionList.submission_id#)
").execute().getResult();
I have tried the following but it fails to work:
var submissions = new Query(dbtype="query", QoQsrcTable=ARGUMENTS.answers, sql="
SELECT * FROM QoQsrcTable WHERE submission_id IN (SELECT submission_id FROM QoQsrcTable GROUP BY submission_id)
").execute().getResult();
I think the second example should work. I've tried messing with it in various ways. But can't seem to figure out what I am doing wrong. Maybe a nested QoQ doesn't work like that. Is there another way I can accomplish what I am trying without two chunks of code? Just so it's more readable and I don't have to assign variables twice.
QoQ doesn't support subqueries. That's the long and the short of it.
Docs
In Coldfusion 10 or Railo 4, you can utilize the groupBy function of Underscore.cfc to accomplish what you want in much less code:
_ = new Underscore();// instantiate the library
submissions = _.groupBy(arguments.answers, 'submission_id');
groupBy() returns a structure where the keys are the values of the group element (in this case, submission_id).
(Disclaimer: I wrote Underscore.cfc)
I have a database the tells each systems current and new model. (Each row has a system name, current model, and new model) I need to group the results by the current model and new model. So if the current and new model are the same i want one result. I got that to work but i also need to know how many systems are in each group. How can I do that? Im using CI
This is my query
$query2 = $this->db->query('SELECT * FROM rollout_systems WHERE scope_ID = '.$id.' GROUP BY EAM_Model, new_Model');
(EAM_Model = current model)
Don't know if i clarified enough. I need to be able to display the Current System, the New System, and the number of systems in that group.
So if 3 rows have "blah" as there current system and "blahblah" as there new system, i want it to say
Current: Blah
New: Blah Blah
number of systems: 3
It doesn't make sense to do a select * and a group by in the same statement. The columns you group by need to be in your select but then you should have an aggregate function like count(*) in your select. I'm not sure I totally understand what you are trying to do, but I think you want something like:
SELECT EAM_Model, new_Model, count(*)
FROM rollout_systems
WHERE scope_ID = '.$id.'
GROUP BY EAM_Model, new_Model
If you just want the number of items, use SELECT EAM_Model, new_Model, COUNT(*) FROM ... etc. When you group a query, you normally want to be performing an aggregate like COUNT or SUM or else it doesn't make sense to be grouping the data.
$query2 = $this->db->query('SELECT count(*) FROM rollout_systems WHERE scope_ID = '.$id.' GROUP BY EAM_Model, new_Model');
Is it this you are asking for?