table columns unfindable in query [closed] - sql-server

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I'm using this query, but I get an error vdagbk.bedrag and vdagbk.reden .
Problem is, if I would put in in the second SELECT the result would certainly be wrong as it sums the result to many times due to double data in the table.
SELECT TOP (100) PERCENT
SUM(P.bedrag) AS ex,
C.dosno,
C.dosnm,
SUM(P.betaald) AS TotBetaald,
SUM(CASE vdagbk.reden WHEN 'H' THEN vdagbk.bedrag END) AS Expr1
FROM
dbo.verkopen AS P
INNER JOIN
(SELECT DISTINCT
dbo.doss.dosno, dbo.doss.dosnm, dbo.verkopen.ino
FROM dbo.verkopen
INNER JOIN dbo.doss ON dbo.verkopen.ino = dbo.doss.ino
INNER JOIN dbo.vdagbk ON dbo.verkopen.ino = dbo.vdagbk.ino
WHERE
(dbo.doss.uitvoerder LIKE 'LL')
AND (dbo.doss.dosno LIKE '101520')
GROUP BY
dbo.doss.dosno, dbo.doss.dosnm, dbo.verkopen.ino) AS C ON C.ino = P.ino
INNER JOIN
dbo.vdagbk AS vdagbk_1 ON P.ino = vdagbk_1.ino
GROUP BY
C.dosno, C.dosnm
How can I make this query work ?

You're giving your table dbo.vdagbk an alias vdagbk_1 here:
INNER JOIN
dbo.vdagbk AS vdagbk_1 ON P.ino = vdagbk_1.ino
so you also need to use that alias in your SUM() clause here:
SUM(CASE vdagbk.reden WHEN 'H' THEN vdagbk.bedrag END) AS Expr1
Change this to:
SUM(CASE vdagbk_1.reden WHEN 'H' THEN vdagbk_1.bedrag END) AS Expr1
and it should work.

Related

Get customer with same payment for 2 different account types in SQL [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last year.
Improve this question
Is there a way to get the final payment for a customer same payment key for 2 different account types with the same amount paid in the SQL Server?
SELECT
p.Amount,
t.date,
p.PayKey,
t.BillKey
FROM
Transaction t
INNER JOIN
payment p ON t.PayKey = p.PayKey
LEFT JOIN
BillTable b ON t.BillKey = b.Sbillkey
AND t.Tbillkey = b.BillKey
WHERE
t.BillKey in (123457, 1786243)
My results
Amount date PayKey BillKey
----------------------------------------
2540.00 2020-02-06 762944 123457
2540.00 2020-02-06 762944 1786243
But the final results I would like to obtain is :
Amount date PayKey
-------------------------------
2540.00 2020-02-06 762944
Add a Group By.
SELECT
p.Amount,
t.date,
p.PayKey
FROM
Transaction t
INNER JOIN
payment p ON t.PayKey = p.PayKey
LEFT JOIN
BillTable b ON t.BillKey = b.Sbillkey
AND t.Tbillkey = b.BillKey
WHERE
t.BillKey in (123457, 1786243)
GROUP BY
p.Amount,
t.date,
p.PayKey
Or use Distinct to return unique rows
SELECT DISTINCT
p.Amount,
t.date,
p.PayKey
FROM
Transaction t
INNER JOIN
payment p ON t.PayKey = p.PayKey
LEFT JOIN
BillTable b ON t.BillKey = b.Sbillkey
AND t.Tbillkey = b.BillKey
WHERE
t.BillKey in (123457, 1786243)

Error: group by expression must contain at least one column that is not an outer reference [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
What is wrong in this query? Despite so many posts about the same error I can't figure out what is wrong on following query.
SELECT
COUNT(RI.INSS), D.TICKET_NUMBER, RI.TICKET_NBR, D.STATUS
FROM
[DBIC].[LoIC].[DEMAND] D
INNER JOIN
[DBIC].[LoIC].[DEMAND_REQUEST_INTRODUCED] DRI ON D.ID = DRI.ID_DEMAND
INNER JOIN
[DBIC].[LoIC].[REQUEST_INTRODUCED] RI ON RI.ID = DRI.ID_REQUEST_INTRODUCED
WHERE
D.STATUS = 'DONE'
GROUP BY
'RI.TICKET_NBR'
Try this to start...
select count(RI.INSS), D.TICKET_NUMBER, RI.TICKET_NBR, D.STATUS from [DBIC].[LoIC].[DEMAND] D
inner join [DBIC].[LoIC].[DEMAND_REQUEST_INTRODUCED] DRI on D.ID = DRI.ID_DEMAND
inner join [DBIC].[LoIC].[REQUEST_INTRODUCED] RI on RI.ID = DRI.ID_REQUEST_INTRODUCED
where D.STATUS = 'DONE'
GROUP BY D.TICKET_NUMBER, RI.TICKET_NBR, D.STATUS

SQL query for students mark functionality in SQL server [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
There are 3 tables
Students(student_id, st_name)
Subjects(subject_id,sub_name)
Marks(st_id,sub_id,Score)
Write an SQL query to display the student name, average score of the student, maximum mark obtained by the student , name of the subject in which the student has scored maximum marks.
Below is the code I wrote
I am getting the errors -Column 'students.name' and 'subjects.name' are invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
select students.st_name as Students_Name,avg(Score) as Average, max(Score) as Maximum,subjects.sub_name as Max_Subject_name
from marks
join students on students.student_id=marks.st_id
join subjects on subjects.subject_id=marks.sub_id
join
(
select st_id,avg(score) as Average,max(score) as Maximum from marks group by st_id
) as x on x.st_id=marks.st_id
order by students.st_name,subjects.sub_name;
Try this
;with cte as
(
select students.st_name as Students_Name
,students.student_id
,RANK() over(partition by students.student_id order by marks.score desc) as seq
,subjects.sub_name as Max_Subject_name
,Marks.Score
from marks
INNER join students
on students.student_id=marks.st_id
INNER join subjects
on subjects.subject_id=marks.sub_id
)
select
Students_Name
,student_id
,Max_Subject_name
,Score
,(select avg(score) from cte b where b.student_id=a.student_id) as average
from cte a
where seq=1
Try
select students.name as Students_Name
,avg(Score) as Average
,max(Score) as Maximum
,subjects.name as Max_Subject_name
from marks
INNER join students
on students.student_id=marks.st_id
INNER join subjects
on subjects.subject_id=marks.st_id
GROUP BY students.name
,subjects.name
order by students.name
,subjects.name;
or if you want your query:
select students.name as Students_Name,x.Average, x.Maximum,subjects.name as Max_Subject_name
from marks
join students on students.student_id=marks.st_id
join subjects on subjects.subject_id=marks.st_id
join
(
select st_id,avg(score) as Average,max(score) as Maximum from marks group by st_id
) as x on x.st_id=marks.st_id
order by students.name,subjects.name;

Pivot order by asc in SQL Server 2005 [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
This is my query:
select
Sno, TopEmpName, HighLevel, TopEmpID, CH, ED, DIR, GM, AGM, BDM, SMM, LowEmpID
from
(select
projectallocation.proAllocationID as Sno,
temp.intro_name as TopEmpName,
projectallocation.introtoplevelEmpid as TopEmpID,
temper.csshortname as HighLevel,
companystructure.csshortname as Level,
introducermaster.intro_name as LowEmpName,
projectallocation.introlevelEmpid as LowEmpID
from
projectallocation
inner join
dbo.IntroducerMaster on dbo.introducermaster.empid = projectallocation.introLevelEmpid
inner join
introducermaster as temp on temp.empiD = projectallocation.introtopLevelEmpid
inner join
companyStructure on projectallocation.introleveID = companyStructure.HLevel
inner join
companystructure as temper on projectallocation.introtoplevelID = temper.Hlevel
where
projectallocation.projectID = 1
group by
IntroducerMaster.Intro_Name, temp.intro_name,
companyStructure.CSShortName, projectallocation.proAllocationID,
projectallocation.introlevelEmpid, projectallocation.introtoplevelEmpid,
projectallocation.introtoplevelID, temper.csshortname
order by
projectallocation.introtoplevelID asc
) b
PIVOT
(max(LowEmpName)
for level in (CH, ED, DIR, GM, AGM, BDM, SMM)
) PVT
Where do I put an Order by clause ?
I want order BY CH,ED,.... for topempname
"The ORDER BY clause is invalid in views, inline functions, derived
tables, subqueries, and common table expressions, unless TOP, OFFSET
or FOR XML is also specified."
It is clearly stating that you can't order it when you have derived table and sub queries.
To apply ORDER BY, wrap the output to inside a SELECT and apply your ORDER BY condition like following.
SELECT *
FROM (select Sno,
TopEmpName,
HighLevel,
TopEmpID,
CH,
ED,
DIR,
GM,
AGM,
BDM,
SMM,
LowEmpID
from (select projectallocation.proAllocationID as Sno,
temp.intro_name as TopEmpName,
projectallocation.introtoplevelEmpid as TopEmpID,
temper.csshortname as HighLevel,
companystructure.csshortname as Level,
introducermaster.intro_name as LowEmpName,
projectallocation.introlevelEmpid as LowEmpID
from projectallocation
inner join dbo.IntroducerMaster
on dbo.introducermaster.empid =
projectallocation.introLevelEmpid
inner join introducermaster as temp
on temp.empiD =
projectallocation.introtopLevelEmpid
inner join companyStructure
on projectallocation.introleveID =
companyStructure.HLevel
inner join companystructure as temper
on projectallocation.introtoplevelID =
temper.Hlevel
where projectallocation.projectID = 1
group by IntroducerMaster.Intro_Name,
temp.intro_name,
companyStructure.CSShortName,
projectallocation.proAllocationID,
projectallocation.introlevelEmpid,
projectallocation.introtoplevelEmpid,
projectallocation.introtoplevelID,
temper.csshortname) b
PIVOT (Max(LowEmpName)
for level in (CH,
ED,
DIR,
GM,
AGM,
BDM,
SMM) ) PVT) T
ORDER BY CH,ED

How to merge a select statement with a calculated value as new columns? [duplicate]

This question already has answers here:
How to merge a select statement with a new dynamic values as columns?
(4 answers)
Closed 9 years ago.
In my sql server code, I have this select statement
select distinct
a.HireLastName,
a.HireFirstName,
a.HireID,
a.Position_ID,
a.BarNumber,
a.Archived,
a.DateArchived,
b.Position_Name
from NewHire a
join Position b on a.Position_ID = b.Position_ID
join WorkPeriod c on a.hireID = c.HireID
where a.Archived = 0 and c.InquiryID is not null
order by a.HireID DESC, a.HireLastName, a.HireFirstName
And I want to add a new column to it. However this column is not a column from a table, its just used to store a float from a calculation I make from existing columns.
The number I get is calculated like this:
#acc is the a.HireID from the above select statement.
CAST((select COUNT(*) from Hire_Response WHERE HireID = #acc AND (HireResponse = 0 OR HireResponse = 1)) as FLOAT) /
CAST((select COUNT(*) from Hire_Response WHERE HireID = #acc) as FLOAT)
How can I do this?
Thanks.
You can use the code that GBN did for you on your other question as a sub-query
select distinct
a.HireLastName,
a.HireFirstName,
a.HireID,
a.Position_ID,
a.BarNumber,
a.Archived,
a.DateArchived,
b.Position_Name,
d.percentage
from NewHire a
inner join Position b on a.Position_ID = b.Position_ID
inner join WorkPeriod c on a.hireID = c.HireID
left join (select NH.HireID, ISNULL(1E0 * COUNT(CASE WHEN HireResponse IN (0,1) THEN HR.HireID END) / NULLIF(COUNT(HR.HireID), 0) , 0) AS percentage
from NewHire NH LEFT JOIN Hire_Response HR ON NH.HireID = HR.HireID
group by NH.HireID) d
ON a.HireID = d.HireID
where a.Archived = 0 and c.InquiryID is not null
order by a.HireID DESC, a.HireLastName, a.HireFirstName
... this will add the column percentage to your current query. This could probably be improved, but it should give you a god idea of how you can bring the results of your questions together.

Resources