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)
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 years ago.
Improve this question
I have used CTE and getting compilation error for last CTE. long time search google but still not understand where I made the mistake in code. so please some one tell me what to change in code to fix the error message
Incorrect syntax near '('. Expecting ID, QUOTED_ID
This CTE is throwing the error and this is last CTE in my code.
,Cte2 AS
(
SELECT *
FROM
(SELECT
EarningID, Section, LineItem, DisplayInCSM, Type,
Broker, ItemValue, Period, hierarchy
FROM
Cte1) t
PIVOT
(MAX(ItemValue)
FOR Broker IN ([5W],[8K],[CL],[DA],[EQ],[FA],[GS],[HM],[HQ],[JY],[KW],[ML],[MS],[MV],[SL],[UA],[WB])
) AS P
)
My full code
DECLARE #Columns as VARCHAR(MAX)
DECLARE #Ticker VARCHAR(20),
#TickerID VARCHAR(20),
#ClientCode VARCHAR(20)
DECLARE #CSM_ID INT
SET #TickerID='ADS'
SET #ClientCode='ADS'
IF OBJECT_ID(N'tempdb..#Brokers') IS NOT NULL
BEGIN
DROP TABLE #Brokers
END
SELECT DISTINCT #Ticker=Ticker FROM tblTickerMasterId WHERE MasterId=#TickerID
SELECT TOP 1 #CSM_ID=CSM_ID FROM tblCSM_Tuner_Client WHERE TickerID=#TickerID
SELECT * Into #Brokers FROM
(
Select A.BrokerCode, B.BrokerName
From tblClientBroker_Earnings A
Join tblBroker B ON SUBSTRING(A.BrokerCode,1,len(A.BrokerCode)-charindex('-',A.BrokerCode))=B.Brokercode
Where A.ClientCode=#ClientCode And A.Ticker=#Ticker
/*AND A.BrokerCode IN (SELECT [DATA] FROM SplitStringToTable(#SelectedBrokers,',') WHERE TRIM([DATA])<>'')*/
) x
SELECT #Columns = COALESCE(#Columns + ', ','') + QUOTENAME(BrokerCode)
FROM
(
SELECT DISTINCT BrokerCode
FROM #Brokers where TRIM(BrokerCode) <> ''
) AS B
ORDER BY B.BrokerCode
;WITH DirectReports as
(
SELECT CSM_ID,
ID,
ParentID,
DisplayInCSM,
Type,
FontName,
FontStyle,
FontSize,
UnderLine,
BGColor,
LineItemID,
Presentation,
BrokerOrientation,
AnnualFormat,
CalculationMethod,
Indent,
FGColor,
Box,
HeadingSubHeading,
ColOrder,
#TickerID AS TickerID,
hierarchy = FORMAT(ID,'0000'),
level = 0
FROM tblCSM_ModelDetails
WHERE ISNULL(ParentID, 0) = 0
AND Type<>'BM'
AND CSM_ID=#CSM_ID
UNION ALL
SELECT e.CSM_ID,
e.ID,
e.ParentID,
e.DisplayInCSM,
e.Type,
e.FontName,
e.FontStyle,
e.FontSize,
e.UnderLine,
e.BGColor,
e.LineItemID,
e.Presentation,
e.BrokerOrientation,
e.AnnualFormat,
e.CalculationMethod,
e.Indent,
e.FGColor,
e.Box,
e.HeadingSubHeading,
e.ColOrder,
#TickerID AS TickerID,
hierarchy = d.hierarchy + '.' + FORMAT(e.id,'0000'),
level = level + 1
FROM tblCSM_ModelDetails e
JOIN DirectReports d on e.ParentID = d.ID
WHERE e.Type<>'BM'
AND e.CSM_ID=#CSM_ID
)
,Cte1 as
(
SELECT AA.EarningID,AA.Section,AA.LineItem,AA.Ticker, r.DisplayInCSM, r.Type,r.hierarchy, AA.Broker, AA.ItemValue, AA.Period,r.ColOrder
FROM DirectReports r
LEFT OUTER JOIN
(
Select b.*,L.ID AS LineItemID,L.TickerID
From tblOutputDetl_CSMTuner b
INNER JOIN TblLineItemTemplate L
ON b.LineItem= L.LineItem
WHERE b.Ticker=#Ticker AND L.TickerID=#TickerID
) AA
ON (AA.LineItemID=r.LineItemID
)
,Cte2 as
(
SELECT *
FROM
(
SELECT EarningID,Section,LineItem, DisplayInCSM, Type, Broker, ItemValue, Period,hierarchy
from Cte1
) t
PIVOT
(
MAX(ItemValue)
FOR Broker IN ([5W],[8K],[CL],[DA],[EQ],[FA],[GS],[HM],[HQ],[JY],[KW],[ML],[MS],[MV],[SL],[UA],[WB])
) AS P
)
SELECT * FROM Cte2 Order BY hierarchy
IF OBJECT_ID(N'tempdb..#Brokers') IS NOT NULL
BEGIN
DROP TABLE #Brokers
END
You're missing a closing parenthesis here
ON (AA.LineItemID=r.LineItemID
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 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
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