2015-06-12 91 views
0
TABLE Laptop_Shift_Departments 

    id  Laptop_ID Curr_department Pre_Department 
    -----|----------|-----------------|---------------- 
    9  71   4     3 
    10  68   4     3 
    11  71   5     4 
    12  68   5     4 

用户只能搜索订单号和这PO_Num有2台笔记本电脑(Laptop_ID)获取最大价值

以上就是我的表,现在我想获得最大的价值在此laptop_ID ID

我有尝试这样:

SELECT LD.ID AS Laptop_ID,ld.GulfITBarcode,po.ID as PO_ID,PO.PO_Number as PO_Number,D.Department, 
    D.ID as Crr_DepID1,d2.Department,D2.ID as Pre_DepID2 
    FROM PO_PURCHASEORDER PO 
     INNER JOIN PO_Laptop_Master LM 
     ON LM.PO_ID = PO.ID 
     INNER JOIN PO_LaptopDetail LD 
     ON LD.LapTop_Master_ID = LM.ID 
     INNER JOIN Laptop_Shift_Departments DP 
     ON DP.Laptop_Detail_ID = LD.ID 
     inner join Laptop_Departments d 
     on d.ID = DP.Current_Dep_ID 
     inner join Laptop_Departments d2 
     on d2.ID = DP.Previous_Dep_ID 
     WHERE PO_NUMBER = '5258' 
     AND 
     LD.ID IN (select Max(cSh.id) from Laptop_Shift_Departments cSh) 

但它不工作

我的输出应该是这样的:

id  Laptop_ID Curr_department Pre_Department 
-----|----------|-----------------|---------------- 
11  71   5     4 
12  68   5     4 
+1

你的选择列表不匹配造成设置列... – jarlh

回答

0

你要使用“分组依据”功能,在您想组领域,在选择上的列你想要的“最高”,“最大”函数的结束(或'Min'或'Avg')。应该是这个样子:

SELECT 
    LD.ID AS Laptop_ID 
    ,ld.GulfITBarcode 
    ,po.ID as PO_ID 
    ,PO.PO_Number as PO_Number 
    ,D.Department 
    ,MAX(D.ID) as Crr_DepID1 
    ,d2.Department 
    ,MAX(D2.ID) as Pre_DepID2 
FROM PO_PURCHASEORDER PO 
    INNER JOIN PO_Laptop_Master LM 
     ON LM.PO_ID = PO.ID 
    INNER JOIN PO_LaptopDetail LD 
     ON LD.LapTop_Master_ID = LM.ID 
    INNER JOIN Laptop_Shift_Departments DP 
     ON DP.Laptop_Detail_ID = LD.ID 
    inner join Laptop_Departments d 
     on d.ID = DP.Current_Dep_ID 
    inner join Laptop_Departments d2 
     on d2.ID = DP.Previous_Dep_ID 
WHERE PO_NUMBER = '5258' 
    AND LD.ID IN (select Max(cSh.id) from Laptop_Shift_Departments cSh) 
    AND DP.Current_Dep_ID = 5 
GROUP BY 
    LD.ID 
    ,ld.GulfITBarcode 
    ,po.ID 
    ,PO.PO_Number 
    ,D.Department 
    ,d2.Department 

(。虽然没有见过表结构我不知道作为对查询的其余部分希望这有助于)