2015-09-03 35 views
-1

有一個方案和不同的投標人對它,所以情景是,如果用戶發送SchemeID到程序那麼它應該返回SchemeName,EstimatedCost(一次)和所有投標人對單一計劃即BidderName,BidPrice,SchemeEstimatedCost。 ..在這種格式我應該如何在SQL過程中創建一個列修復程序和其他可重複的列?

SchemeName EstimatedCost BidderName BidPrice 
    Scheme 1  13000   John  12000   
           Aamir  10000 
           Shumail  9000 
           Michael  8090 

請參閱我通過SchemeID搜索數據,它應該返回此格式的詳細信息。

我嘗試:

ALTER PROCEDURE [dbo].[SelectBidsByBidID] 

    @fk_Schemes_Bids_SchemeID int 

AS 
BEGIN 

     Select Schemes.SchemeName, Schemes.SchemeCost as SchemeEstimatedCost, 
       Bids.BidBidderName, Bids.BidPrice 
     From Bids 
     Inner Join Schemes 
     ON Schemes.pk_Schemes_SchemeID= Bids.fk_Schemes_Bids_SchemeID 
     Where Bids.fk_Schemes_Bids_SchemeID= 3 

END 

但重複SchemeName和估計費用。

回答

1

這應該做的伎倆:

ALTER PROCEDURE [dbo].[SelectBidsByBidID] 

@fk_Schemes_Bids_SchemeID int 

AS 
BEGIN 

    Select case when ROW_NUMBER() OVER (Order by BidPrice desc) = 1 then Schemes.SchemeName else '' end as SchemeName, case when ROW_NUMBER() OVER (Order by BidPrice desc) = 1 then Schemes.SchemeCost else '' end as SchemeEstimatedCost, 
      Bids.BidBidderName, Bids.BidPrice 
    From Bids 
    Inner Join Schemes 
    ON Schemes.pk_Schemes_SchemeID= Bids.fk_Schemes_Bids_SchemeID 
    Where Bids.fk_Schemes_Bids_SchemeID= 3 
Order by BidPrice desc 
END 

然而,如前所述,這不應該在查詢完成,但在報表查看器/發電機。

+0

拋出sir錯誤 – Covert

+0

Msg 156,Level 15,State 1,Procedure SelectBidsBySchemeID,Line 8 關鍵字'SELECT'附近的語法不正確。 Msg 102,Level 15,State 1,Procedure SelectBidsBySchemeID,Line 8 '='附近語法不正確。 Msg 102,Level 15,State 1,Procedure SelectBidsBySchemeID,Line 8 '='附近語法不正確。 – Covert

+0

RDLC報告中可能嗎? – Covert

1
ALTER PROCEDURE [dbo].[SelectBidsByBidID] 

    @fk_Schemes_Bids_SchemeID int 

AS 
BEGIN 
;with cte 
as 
(
     Select Schemes.SchemeName, Schemes.SchemeCost as SchemeEstimatedCost, 
       Bids.BidBidderName, Bids.BidPrice,row_number() over(partition by schemename,SchemeCost order by bidprice desc) as rn 
     From Bids 
     Inner Join Schemes 
     ON Schemes.pk_Schemes_SchemeID= Bids.fk_Schemes_Bids_SchemeID 
     Where Bids.fk_Schemes_Bids_SchemeID= 3 
) 
select case when rn=1 then Schemes.SchemeName else '' end as SchemeName, case when rn=1 then cast(Schemes.SchemeCost as varchar(max)) else '' end as SchemeEstimatedCost, 
       Bids.BidBidderName, Bids.BidPrice 
from cte 

END 
+0

先生最後拋出錯誤SELECT line – Covert

+0

Msg 4104,Level 16,State 1,Procedure SelectBidsBySchemeID,Line 18 無法綁定多部分標識符「Schemes.SchemeName」。 Msg 4104,Level 16,State 1,Procedure SelectBidsBySchemeID,Line 18 無法綁定多部分標識符「Schemes.SchemeCost」。 Msg 4104,Level 16,State 1,Procedure SelectBidsBySchemeID,Line 19 無法綁定多部分標識符「Bids.BidBidderName」。 Msg 4104,Level 16,State 1,Procedure SelectBidsBySchemeID,Line 19 無法綁定多部分標識符「Bids.BidPrice」。 – Covert

1

這應該在報告工具/任何其他前端工具中完成。如果你沒有選擇,試試這個

ALTER PROCEDURE [dbo].[SelectBidsByBidID] @fk_Schemes_Bids_SchemeID INT 
AS 
BEGIN 
    SELECT CASE 
      WHEN sno = 1 
       THEN SchemeName 
      ELSE '' 
      END AS SchemeName 
     ,CASE 
      WHEN sno = 1 
       THEN SchemeEstimatedCost 
      ELSE '' 
      END AS SchemeEstimatedCost 
     ,BidBidderName 
     ,BidPrice 
    FROM (
     SELECT row_number() OVER (
       PARTITION BY Schemes.SchemeName 
       ,Schemes.SchemeCost ORDER BY Bids.BidPrice DESC 
       ) AS sno 
      ,Schemes.SchemeName 
      ,Schemes.SchemeCost AS SchemeEstimatedCost 
      ,Bids.BidBidderName 
      ,Bids.BidPrice 
     FROM Bids 
     INNER JOIN Schemes ON Schemes.pk_Schemes_SchemeID = Bids.fk_Schemes_Bids_SchemeID 
     WHERE Bids.fk_Schemes_Bids_SchemeID = 3 
     ) AS t 
END 
+0

先生它會拋出錯誤 – Covert

+0

消息4104,級別16,狀態1,過程SelectBidsBySchemeID,行18 無法綁定多部分標識符「Bids.BidBidderName」。 Msg 4104,Level 16,State 1,Procedure SelectBidsBySchemeID,Line 19 無法綁定多部分標識符「Bids.BidPrice」。 – Covert

+1

固定。請參閱我編輯的答覆 – Madhivanan

相關問題