日期:2014-05-17  浏览次数:20514 次

急求sql 非常感谢
797EA420-E9E0-428A-A8B4-35B3B11BC838 上海组 1 1 1 1 1
98F591F8-7C58-4611-92ED-8663E61832F8 上海组 1 1 1 1 1
B4745602-02AD-4CB5-8194-9A1DC1CAA4F4 上海组 1 0 1 0 1
97A59D8D-1C5D-4303-8192-9B76CB442F8E 上海组 0 0 1 1 0
97A59D8D-1C5D-4303-8192-9B76CB442F8E 上海组 1 1 1 1 0
017F233A-74D2-4039-90E5-9D944D2B8EEC 上海组 1 1 1 1 0
017F233A-74D2-4039-90E5-9D944D2B8EEC 上海组 1 1 1 1 1
FEF53CF1-C353-4356-9D7A-9E8366A696E7 上海组 0 0 1 0 0
3955D68C-7D7B-4AC7-B6D1-CD4F31983F8A 上海组 1 1 1 1 0
F387E6A8-9DD7-4278-8AAE-EDBC87461FA6 上海组 1 1 1 1 0
CA0338C9-F037-45BD-85A8-F6390A7BA6AF 上海组 1 1 0 0 0

想要得到的结果

797EA420-E9E0-428A-A8B4-35B3B11BC838 上海组 1 1 1 1 1
98F591F8-7C58-4611-92ED-8663E61832F8 上海组 1 1 1 1 1
B4745602-02AD-4CB5-8194-9A1DC1CAA4F4 上海组 1 0 1 0 1
97A59D8D-1C5D-4303-8192-9B76CB442F8E 上海组 1 1 1 1 0
017F233A-74D2-4039-90E5-9D944D2B8EEC 上海组 1 1 1 1 1

FEF53CF1-C353-4356-9D7A-9E8366A696E7 上海组 0 0 1 0 0
3955D68C-7D7B-4AC7-B6D1-CD4F31983F8A 上海组 1 1 1 1 0
F387E6A8-9DD7-4278-8AAE-EDBC87461FA6 上海组 1 1 1 1 0
CA0338C9-F037-45BD-85A8-F6390A7BA6AF 上海组 1 1 0 0 0

注意红字部分就行了。

------解决方案--------------------
select distinct * from tb,这个效率不知道怎么样

------解决方案--------------------
SQL code

--> 测试数据:@T
DECLARE @T TABLE([C1] UNIQUEIDENTIFIER,[C2] VARCHAR(6),[C3] INT,[C4] INT,[C5] INT,[C6] INT,[C7] INT)
INSERT @T
SELECT '797EA420-E9E0-428A-A8B4-35B3B11BC838','上海组',1,1,1,1,1 UNION ALL
SELECT '98F591F8-7C58-4611-92ED-8663E61832F8','上海组',1,1,1,1,1 UNION ALL
SELECT 'B4745602-02AD-4CB5-8194-9A1DC1CAA4F4','上海组',1,0,1,0,1 UNION ALL
SELECT '97A59D8D-1C5D-4303-8192-9B76CB442F8E','上海组',0,0,1,1,0 UNION ALL
SELECT '97A59D8D-1C5D-4303-8192-9B76CB442F8E','上海组',1,1,1,1,0 UNION ALL
SELECT '017F233A-74D2-4039-90E5-9D944D2B8EEC','上海组',1,1,1,1,0 UNION ALL
SELECT '017F233A-74D2-4039-90E5-9D944D2B8EEC','上海组',1,1,1,1,1 UNION ALL
SELECT 'FEF53CF1-C353-4356-9D7A-9E8366A696E7','上海组',0,0,1,0,0 UNION ALL
SELECT '3955D68C-7D7B-4AC7-B6D1-CD4F31983F8A','上海组',1,1,1,1,0 UNION ALL
SELECT 'F387E6A8-9DD7-4278-8AAE-EDBC87461FA6','上海组',1,1,1,1,0 UNION ALL
SELECT 'CA0338C9-F037-45BD-85A8-F6390A7BA6AF','上海组',1,1,0,0,0

SELECT * FROM @T T
WHERE C3+C4+C5+C6+C7=(SELECT MAX(C3+C4+C5+C6+C7) FROM @T WHERE C1=T.C1)
/*
C1                                   C2     C3          C4          C5          C6          C7
------------------------------------ ------ ----------- ----------- ----------- ----------- -----------
797EA420-E9E0-428A-A8B4-35B3B11BC838 上海组    1           1           1           1           1
98F591F8-7C58-4611-92ED-8663E61832F8 上海组    1           1           1           1           1
B4745602-02AD-4CB5-8194-9A1DC1CAA4F4 上海组    1           0           1           0           1
97A59D8D-1C5D-4303-8192-9B76CB442F8E 上海组    1           1           1           1           0
017F233A-74D2-4039-90E5-9D944D2B8EEC 上海组    1           1           1           1           1
FEF53CF1-C353-4356-9D7A-9E8366A696E7 上海组    0           0           1           0           0
3955D68C-7D7B-4AC7-B6D1-CD4F31983F8A 上海组    1           1           1           1           0
F387E6A8-9DD7-4278-8AAE-EDBC87461FA6 上海组    1           1           1           1           0
CA0338C9-F037-45BD-85A8-F6390A7BA6AF 上海组    1           1           0           0           0
*/

------解决方案--------------------
SQL code

CREATE  FUNCTION fn_CalcBin(@cN1 VARCHAR(8000),@cN2 VARCHAR(8000),@Sign VARCHAR(5))
RETURNS VARCHAR(8000)
AS 
BEGIN
    DECLARE @Result VARCHAR(8000)
    DECLARE @M INT 
    DECLARE @TMaxc VARCHAR(8000),@TMinc VARCHAR(8000)
    DECLARE @i INT 
    
    IF ISNULL(@cN1,'')='' OR ISNULL(@cN2,'')=''
        SET @Result=ISNULL(@cN1,'')+ISNULL(@cN2,'')
    ELSE
    BEGIN
        SELECT  @M=CASE WHEN LEN(@cN1)>LEN(@cN2) THEN LEN(@cN1) ELSE LEN(@cN2) END