Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
SELECT convert(VARCHAR, WH.DATE, 103),
PN.partyname,
BT.birdname,
dt.totalweight,
dt.rateperkg,
dt.dcno,
mr.branch
FROM K_RT_Dailyentryretail DT
INNER JOIN K_RT_PartyName PN
ON pn.sno = dt.partyname
INNER JOIN K_RT_WarehouseDetails WH
ON dt.branchdate = wh.sno
INNER JOIN K_RT_BirdType BT
ON dt.birdtype = bt.sno
INNER JOIN K_RT_MasterRetailStores MR
ON MR.sno = WH.branch
WHERE MR.branch + ' - ' + convert(VARCHAR, WH.DATE, 103) = #date
ORDER BY convert(VARCHAR, WH.DATE, 103) DESC
here..order by date is not working. How can I fix this?
Try changing:
order by convert(varchar,WH.date,103) desc
to:
order by WH.date desc
when you are sorting on a date, converted to a varchar string using format "103", the date-string looks like this:
dd/mm/yyyy
So, the sort, will sort first by "dd", then "mm", then "yyyy".
You can DISPLAY the date in the format using 103, but when you do the order by, use this:
order by WH.date desc
Related
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'
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
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;
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
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.