0

我正在尝试在 SQL Server 中运行查询,其中我选择的列与其分组方式不同。此查询在 SAP 中使用。

问题是,如果我按所有选定的相同列进行分组,则没有任何分组。

这是我的查询(显然不起作用)。

SELECT 
   T0.[CardCode], T2.[SlpName] as 'Rep', 
   T0.[CardName] as 'Facility', 
   T1.[Address] as 'Attention', 
   T1.[Street] as 'Address 1', 
   T1.[Block] as 'Address 2', T1.[City] as 'City', 
   T1.[State] as 'State', 
   T1.[ZipCode] as 'Zip' 
FROM OCRD T0 
INNER JOIN CRD1 T1 ON T0.[CardCode] = T1.[CardCode] 
INNER JOIN OSLP T2 ON T2.[SlpCode] = T0.[SlpCode] 
WHERE T1.[State] = 'FL' 
GROUP BY T0.[CardCode]

对于任何反馈,我们都表示感谢。

谢谢,

4

2 回答 2

0

如果您真的不在乎返回哪个值。这是一个例子

SELECT 
   T0.[CardCode], 
   T2.[SlpName] as 'Rep', 
   T0.[CardName] as 'Facility', 
   T1.[Address] as 'Attention', 
   T1.[Street] as 'Address 1', 
   T1.[Block] as 'Address 2', T1.[City] as 'City', 
   T1.[State] as 'State', 
   T1.[ZipCode] as 'Zip' 
FROM OCRD T0 
CROSS APPLY (SELECT TOP 1 * FROM CRD1 TT1 ON T0.[CardCode] = TT1.[CardCode] AND TT1.[State] = 'FL') T1
CROSS APPLY (SELECT TOP 1 * FROM OSLP TT2 ON TT2.[SlpCode] = T0.[SlpCode]) T2
于 2013-05-13T17:51:08.133 回答
0

你可以试试这样...

Select CardCode,Rep, 
    Facility, 
    Attention, 
    Address 1, 
    Address 2,City, 
    State, 
    Zip from  (SELECT Row_Number() Over(Partition By T0.[CardCode] order by T0.[CardCode]) as Row,
    T0.[CardCode], T2.[SlpName] as 'Rep', 
    T0.[CardName] as 'Facility', 
    T1.[Address] as 'Attention', 
    T1.[Street] as 'Address 1', 
    T1.[Block] as 'Address 2', T1.[City] as 'City', 
    T1.[State] as 'State', 
    T1.[ZipCode] as 'Zip' 
    FROM OCRD T0 
    INNER JOIN CRD1 T1 ON T0.[CardCode] = T1.[CardCode] 
    INNER JOIN OSLP T2 ON T2.[SlpCode] = T0.[SlpCode] 
    WHERE T1.[State] = 'FL' 
    ) a where row=1
于 2013-05-10T18:55:11.510 回答