sql query join for more than 2 tables [closed] - sql-server

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I have 4 tables:
#table 1 columns - areacode,section_number,maint_charge
#table 2 columns - section_number,discount,fee,total_Maint_charge
#table 3 columns - areacode, statename
#table 4 columns - section_number , customer
mapping goes like:
#table1.areacode = #table3.areacode
#table1.section_number = #table2.section_number
I want to get something like this:
state,section_number,maint_charge,maint_charge/total_maint_charge,
fee*(maint_charge/total_maint_charge)
Thanks

SELECT state,
table1.section_number,
maint_charge,
(maint_charge / total_maint_charge),
(fee * (maint_charge / total_maint_charge))
FROM table1
JOIN table3 ON table1.areacode=table3.areacode
JOIN table2 on table1.section_number=table2.section_number;

Try this one -
SELECT
t3.statename
, t1.section_number
, t1.maint_charge
, t1.maint_charge / t2.total_maint_charge
, t2.fee * (t1.maint_charge / t2.total_maint_charge)
FROM #table1 t1
JOIN #table2 t2 ON t1.section_number = t2.section_number
JOIN #table3 t3 ON t1.areacode = t3.areacode

Related

Incorrect Syntax near the Keyword Select [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 months ago.
Improve this question
Create a function that when passed in a VendorID will return the balance remaining for the vendor. Use this function in a query to return all the CA vendors and their balances
USE AP
GO
CREATE FUNCTION fnBalanceVendor
(#vendorid INT)
Returns Table
RETURN (SELECT Vendors.VendorID, (InvoiceTotal - PaymentTotal - CreditTotal) AS Balance
FROM Vendors Join Invoices ON Vendors.VendorID = Invoices.VendorID
WHERE Vendors.VendorID = #vendorid)
SELECT *
FROM Vendors INNER JOIN dbo.fnBalanceVendor(8) AS ca ON Vendors.VendorID = ca.VendorID
WHERE VendorState = 'CA'
Msg 156, Level 15, State 1, Procedure fnBalanceVendor, Line 9 [Batch Start Line 2]
Incorrect syntax near the keyword 'SELECT'.
Unsure what I am doing wrong here any help would be appreciated thanks!
I'd suggest sticking a "go" to close out the batch after the RETURN in your CREATE FUNCTION statement, so:
USE AP
GO
CREATE FUNCTION fnBalanceVendor
(#vendorid INT)
Returns Table
RETURN (SELECT Vendors.VendorID, (InvoiceTotal - PaymentTotal - CreditTotal) AS Balance
FROM Vendors Join Invoices ON Vendors.VendorID = Invoices.VendorID
WHERE Vendors.VendorID = #vendorid)
GO
SELECT *
FROM Vendors INNER JOIN dbo.fnBalanceVendor(8) AS ca ON Vendors.VendorID = ca.VendorID
WHERE VendorState = 'CA'

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;

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