Error with converting access query to SQL Server 2012 query - sql

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

Related

Self join SQL is taking too much time to execute

Below SQL is taking too much time to execute.Dont know where is am doing wrong but yes getting proper result.can i further simplify this sql.
This is oracle db and jmc_job_step table contains huge records.
select *
from
jmc_job_run_id jobrunid0_
inner join
jmc_job_step jobsteps1_
on jobrunid0_.id=jobsteps1_.job_run_id
where
(
jobsteps1_.creation_date in (
select
min(jobstep2_.creation_date)
from
jmc_job_step jobstep2_
where
jobrunid0_.id=jobstep2_.job_run_id
group by
jobstep2_.job_run_id ,
jobstep2_.job_step_no
)
)
or jobsteps1_.job_step_progress_value in (
select
max(jobstep3_.job_step_progress_value)
from
jmc_job_step jobstep3_
where
jobrunid0_.id=jobstep3_.job_run_id
group by
jobstep3_.job_run_id ,
jobstep3_.job_step_no
)
)
order by
jobrunid0_.job_start_time desc
This is useless; it says "I don't care what those columns contain", but - yet - you give the database engine to check those values anyway.
(
upper(jobrunid0_.tenant_id) like '%'|| null
)
and (
upper(jobrunid0_.job_run_id) like '%'||null||'%'
)

Transform and PIVOT Microsoft Access - SQL Server

I am trying to convert an Microsoft Access query into SQL Server.
In Access, the SQL is...
TRANSFORM Count(Time_Difference.sc) AS CountOfsc
SELECT Time_Difference.sc
FROM Time_Difference
WHERE (((Time_Difference.Week)>=44 And (Time_Difference.Week)<=48))
GROUP BY Time_Difference.sc
PIVOT Time_Difference.spc;
I tried to convert to SQL...
SELECT *
FROM
(
SELECT (Time_Difference.sc AS CountOfsc,
Time_Difference.sc,spc
FROM Time_Difference
WHERE (((Time_Difference.Week)>=44 And (Time_Difference.Week)<=48))
GROUP BY Time_Difference.sc
)T
PIVOT
(
COUNT(CountOfsc)
FOR Time_Difference.spc IN (A,B,C,D,E)
)P
Does anyone know what I am doing wrong?
Without seeing any example data, I would guess you are looking for something like this:
select [O1_supplier],A,B,C,D,E
from (
select
[O1_supplier]
, spc
, sc
from Time_Difference
where Time_Difference.Week>=44
and Time_Difference.Week<=48
) as T
pivot (count(sc) for Time_Difference.spc in (A,B,C,D,E))P

DISTINCT function in ms access

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.

Show %s in Access 2010 Crosstab query instead of just counts

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

Finding Duplicate names sql

SELECT COUNT(organization.ID)
FROM organization
WHERE name in ( SELECT name FROM organization GROUP BY name HAVING count( name ) >1 )
AND organization.APPROVED=0
AND organization.CREATED_AT>'2010-07-30 10:30:21'
I'm trying to find duplicates, but this query is taking a very long time roughly 5-6 seconds. Is there another way I can find duplicates without using my method? Thanks.
SubQuery: 0.28 seconds. Everything 5.98 seconds.
SELECT organization.name, COUNT(organization.ID)
FROM organization
WHERE organization.APPROVED=0
AND organization.CREATED_AT>'2010-07-30 10:30:21'
GROUP BY name
HAVING count(organization.id) > 1;
There is no need to use the query in the WHERE clause. You can use a GROUP BY and a HAVING clause to accomplish this:
SELECT COUNT(o.ID)
FROM organization o
WHERE o.APPROVED=0
AND o.CREATED_AT>'2010-07-30 10:30:21'
GROUP BY o.Name
HAVING COUNT(o.name) > 1
Why not just do it like:
SELECT COUNT(organization.ID)
FROM organization
WHERE organization.APPROVED=0
AND organization.CREATED_AT>'2010-07-30 10:30:21'
GROUP BY organization.name
HAVING count(organization.name) > 1;