I am running into issues with DISTINCT in MS ACCESS.
Here is what I would like to run and which works in MySQL:
SELECT `orig`.`SONG TITLE`,`orig`.`PUBLISHER`
FROM `Sheet1` AS `orig`
INNER JOIN `Sale type` AS `Sale`
ON orig.`CFG DESCRIPTION`=Sale.`CFG DESC`
GROUP BY orig.`SONG TITLE` , orig.`PUBLISHER`
HAVING COUNT(DISTINCT `Sale type`.`CFG DESC`) > 1
;
The error message I get is:
Syntax error (missing operator) in query expression 'COUNT(DISTINCT Sale type.CFG DESC) > 1'.
Since SELECT DISTINCT is supported in Access, but COUNT(DISTINCT is not, you can use a subquery for SELECT DISTINCT and base GROUP BY, COUNT and HAVING on the subquery.
SELECT sub.`SONG TITLE`, sub.PUBLISHER
FROM
(
SELECT DISTINCT
orig.`SONG TITLE`, orig.PUBLISHER, Sale.`CFG DESC`
FROM
Sheet1 AS orig
INNER JOIN `Sale type` AS Sale
ON orig.`CFG DESCRIPTION`=Sale.`CFG DESC`
) AS sub
GROUP BY sub.`SONG TITLE`, sub.PUBLISHER
HAVING COUNT(sub.`CFG DESC`) > 1;
If the query does work in MySQL, then a passthrough query will allow it to continue to work. See screenshot for area in MS Access for passthrough query..
From w3schools.com:
Note: COUNT(DISTINCT) works with ORACLE and Microsoft SQL Server, but
not with Microsoft Access.
Related
I have a mysql query which contains a correlated subquery. This is not supported within presto which is why I am looking for a way to have the same functionality in ansi sql syntax. The query (from this post) is as follows:
SELECT tA.uid, tA.dt, tA.val_A,
AVG(val_B) AS val_C
FROM
(SELECT uid, dt, val_A,
(SELECT dt FROM tableA ta1
WHERE ta1.uid=ta2.uid
AND ta1.dt > ta2.dt LIMIT 1) AS dtRg
FROM tableA ta2) tA
LEFT JOIN tableB tB
ON tA.uid=tB.uid
AND tB.dt >= tA.dt
AND tB.dt < tA.dtRg
GROUP BY tA.uid, tA.dt, tA.val_A;
If I run the query in the presto db it throws following error message:
[Simba][AthenaJDBC](100071) An error has been thrown from the AWS Athena client. SYNTAX_ERROR: line 5:9: Given correlated subquery is not supported [Execution ID: 60a6f7e2-fb2b-44e0-a4f2-847ac669e905]
How can I fix this?
If you are ok with >= logic and/or dt's are unique within the same uid you can leverage window functions:
(SELECT uid,
dt,
val_A,
lead(dt) over (partition by uid order by dt) AS dtRg
FROM tableA ta2) tA
Otherwise I'm afraid you are limited to performing join and selecting first result from it - for expiration check out this answer (or using row_number window function).
I am trying to convert a stored procedure which is written in T-SQL to BigQuery compatible syntax.
In one of the temp table used inside the proc, there is a function WITHIN GROUP as given in the query below.
SELECT DISTINCT
flr_id, lid, sentinel, liquid, d_id, sent_time, tracker,
(PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY ((DATE_DIFF(second, (sent_time), (tracker))/3600.0)) ASC) OVER (PARTITION BY flr_id, sentinel, d_id)/24.0) AS mct
FROM
history AS h
INNER JOIN
magent AS mag ON mag.flat = h.flat
INNER JOIN
stepand AS stepand ON stepand.soid = h.soid
INNER JOIN
sv AS st_v ON st_v.stoid = stepand.stoid
INNER JOIN
tvr AS tvr ON tvr.trvid = stepand.trvid
INNER JOIN
sdv AS sdv ON st_v.stoid = sdv.stoid
WHERE
liquid > 0
AND mag.flr_id = '1234'
AND tracker <= GETDATE()
AND sent_time >= DATEADD(WEEK, 1, GETDATE())
AND d_id NOT LIKE ('UNKNOWN')
AND part_type_code NOT IN ('ABCDE')
AND h.lid not like 'B%'
AND h.lid not like 'T%'
AND h.lid not like 'VL%'
AND h.step_deleted_sw <> 'Y'
AND h.lid NOT IN (SELECT lid from test)");
I converted all of the query except for this line.
(PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY ((DATE_DIFF(second,
(sent_time), (tracker))/3600.0)) ASC) OVER (PARTITION BY flr_id,
sentinel, d_id)/24.0) AS mct
I looked for WITHIN GROUP in SQL and found a good explanation here
What I don't understand is what is the equivalent function for WITHIN GROUP on Bigquery ?
When I tried to run the query as it is, I get an error
Syntax error: Expected end of input but got keyword WITHIN at <PROCNAME>
Could anyone let me know how can I modify the line using WITHIN GROUP to big query compatible syntax ?
Ant help is much appreciated.
If I understand what you want to do, BigQuery implements this using the ORDER BY for the window function:
PERCENTILE_CONT(0.5) OVER (PARTITION BY flr_id, sentinel, d_id)/24.0
ORDER BY ((DATE_DIFF(second, (sent_time), (tracker))/3600.0)) ASC
) AS mct
Of course the DATE_DIFF() syntax is not correct for BigQuery and your query may have other issues as well. However, this answers the question that you specifically asked.
I am been developing application in java and not much insight into sql.
Meanwhile , I have got to fetch count and username in the same query. My query goes like this :
SELECT count(*),c_user_name
FROM tra_shipment_status
WHERE i_tra_shipment_status_id IN
(SELECT MAX(i_tra_shipment_status_id)
FROM tra_shipment_status
WHERE i_status_code = '4072'
AND c_reference IN ('FILEIO0023','MIASTOFIL003')
Group By C_Reference
);
This throws me an exception "not a single-group group function"
I want the count and C_user_name of the top most row (latest one).
Can somebody please help.
If you use count(*) in the main query you also need a group by there:
SELECT count(*),c_user_name
FROM tra_shipment_status
WHERE i_tra_shipment_status_id IN
(SELECT MAX(i_tra_shipment_status_id)
FROM tra_shipment_status
WHERE i_status_code = '4072'
AND c_reference IN ('FILEIO0023','MIASTOFIL003')
Group By C_Reference
)
Group by c_user_name
When you try to include an aggregate function like count, sum, max,.. you should use group by with other unaggregated columns like c_user_name in your case. There is another thing; you say that you need the top most row but your sub query will return the top most row for each C_Reference, and to get the top most row regardless of C_Reference you should do something like the following:
SELECT count(*),c_user_name
FROM tra_shipment_status
WHERE i_tra_shipment_status_id IN
(SELECT MAX(i_tra_shipment_status_id)
FROM tra_shipment_status
WHERE i_status_code = '4072'
AND c_reference IN ('FILEIO0023','MIASTOFIL003')
);
Group By c_user_name
I have the following two queries that build/feed into the third query. My goal is to have a crosstab query of [MCOs] down the left and possible responses/values for [DrpDown] across the top with the values shown as percentages of the total for each [MCO] (so % of row total).
What I have works, but I want to know if I can do it all in one query.
SELECT tblMCOs.MCOs, tblMCOs.DrpDwn, Count(tblMCOs.ID) AS CountOfID
FROM tblMCOs
GROUP BY tblMCOs.MCOs, tblMCOs.DrpDwn;
SELECT tblMCOs.MCOs, Count(tblMCOs.DrpDwn) AS CountOfDrpDwn
FROM tblMCOs
GROUP BY tblMCOs.MCOs;
TRANSFORM Sum(Round([qryMCODrpDwnCt]![CountOfID]/[qryMCOCtDrpDwn]!
[CountOfDrpDwn],4)*100) AS PCT
SELECT qryMCODrpDwnCt.MCOs
FROM qryMCODrpDwnCt INNER JOIN qryMCOCtDrpDwn ON qryMCODrpDwnCt.MCOs =
qryMCOCtDrpDwn.MCOs
GROUP BY qryMCODrpDwnCt.MCOs
PIVOT qryMCODrpDwnCt.DrpDwn;
Thanks in advance for your help.
What I have works, but I want to know if I can do it all in one query.
Crosstab queries can be a bit fussy, but simply inserting the SQL code as subqueries should work:
TRANSFORM Sum(Round([sqMCODrpDwnCt]![CountOfID]/[sqMCOCtDrpDwn]![CountOfDrpDwn],4)*100) AS PCT
SELECT sqMCODrpDwnCt.MCOs
FROM
(
SELECT tblMCOs.MCOs, tblMCOs.DrpDwn, Count(tblMCOs.ID) AS CountOfID
FROM tblMCOs
GROUP BY tblMCOs.MCOs, tblMCOs.DrpDwn
) AS sqMCODrpDwnCt
INNER JOIN
(
SELECT tblMCOs.MCOs, Count(tblMCOs.DrpDwn) AS CountOfDrpDwn
FROM tblMCOs
GROUP BY tblMCOs.MCOs
) AS sqMCOCtDrpDwn
ON sqMCODrpDwnCt.MCOs = sqMCOCtDrpDwn.MCOs
GROUP BY sqMCODrpDwnCt.MCOs
PIVOT sqMCODrpDwnCt.DrpDwn
I am converting queries from Access 1010 into SQL Server 2012.
The following is part of a query
Count(Stats.SessionNumber) AS TotalSessions, Sum(Stats.Duration) AS TotalDuration,
Round([TotalDuration]/[TotalSessions],1) AS AverageDuration
I get the following Error:
Invalid column name 'TotalDuration'.
Invalid column name 'TotalSessions'.
Does the alias of TotalDuration and TotalSessions need to be handled differently in SQL Server
and if so how?
thanks
You can do something like this....
SELECT TotalSessions
,TotalDuration
,Round([TotalDuration]/[TotalSessions],1) AS AverageDuration
FROM (
SELECT Count([Stats].SessionNumber) AS TotalSessions
, SUM([Stats].Duration) AS TotalDuration
FROM Table_Name
) A
In SQLServer, you can't work on an alias you just assigned. You either need to use a sub query, or reuse your SUM and COUNT.
Re-use:
Count(Stats.SessionNumber) AS TotalSessions, Sum(Stats.Duration) AS TotalDuration,
Round(Sum(Stats.Duration)/Count(Stats.SessionNumber),1) AS AverageDuration
Subquery:
SELECT TotalSessions, TotalDuration, Round(TotalDuration/TotalSessions,1) AS AverageDuration
FROM
(
SELECT RCount(Stats.SessionNumber) AS TotalSessions, Sum(Stats.Duration) AS TotalDuration,
FROM yourTableName
) subquery