SQL Server如何将两个查询相乘

时间:2017-07-12 15:36:28

标签: sql-server database multiplying select-query

所以我有两个不同的查询如下:

Query1
CPT        Resource     1      2        3      4      5
2017-06-12  RM1        5.00   5.00    4.00   4.00   2.00
2017-06-12  RM2        3.00   6.00    4.00   7.00   4.00
2017-06-12  RM3        3.00   4.00    6.00   8.00   6.00
2017-06-13  RM1        3.00   7.00    5.00   3.00   5.00
2017-06-13  RM2        4.00   5.00    4.00   2.00   4.00
2017-06-13  RM3        2.00   4.00    5.00   2.00   7.00
2017-06-14  RM1        2.00   4.00    6.00   4.00   2.00
2017-06-14  RM2        6.00   5.00    4.00   5.00   2.00
2017-06-14  RM3        5.00   3.00    7.00   4.00   5.00

    Query2
    CPT        Resource     1      2        3      4      5
    2017-06-12  RM1        0.00  -2.00    0.00   0.00  -2.00
    2017-06-12  RM2       -3.00  -3.00    0.00   0.00   0.00
    2017-06-12  RM3       -1.00  -3.00    0.00   0.00   0.00
    2017-06-13  RM1        0.00  -1.00    0.00   0.00   0.00
    2017-06-13  RM2        0.00  -1.00   -1.00  -2.00  -2.00
    2017-06-13  RM3       -2.00  -3.00   -1.00   0.00   0.00
    2017-06-14  RM1        0.00   0.00    0.00   0.00   0.00
    2017-06-14  RM2        0.00  -4.00   -3.00  -2.00   0.00
    2017-06-14  RM3        0.00  -3.00   -1.00   0.00  -2.00

通过这两个查询,我将如何创建一个新查询,该查询将来自query1的数据与查询2中相应的数字相乘,该数字位于基于该日期,资源和小时的相同位置(这是标题1 ,2,3,4和5)。我也只想要正数,所以新数据应该乘以-1。

如果我手动执行此操作,新表应如下所示:

Query3
    CPT        Resource     1      2        3      4      5
    2017-06-12  RM1        0.00  10.00    0.00   0.00   4.00
    2017-06-12  RM2        9.00  18.00    0.00   0.00   0.00
    2017-06-12  RM3        3.00  12.00    0.00   0.00   0.00
    2017-06-13  RM1        0.00   7.00    0.00   0.00   0.00
    2017-06-13  RM2        0.00   5.00    4.00   4.00   8.00
    2017-06-13  RM3        4.00  12.00    5.00   0.00   0.00
    2017-06-14  RM1        0.00   0.00    0.00   0.00   0.00
    2017-06-14  RM2        0.00  20.00   12.00  10.00   0.00
    2017-06-14  RM3        0.00   9.00    7.00   0.00   10.00

6 个答案:

答案 0 :(得分:3)

只需加入关键字段,然后使用ABS()返回肯定的结果。

SELECT Q1.CPT, 
       Q1.Resource,
       ABS(Q1.[1] * Q2.[1]) as [1],
       ABS(Q1.[2] * Q2.[2]) as [2],
       ABS(Q1.[3] * Q2.[3]) as [3],
       ABS(Q1.[4] * Q2.[4]) as [4],
       ABS(Q1.[5] * Q2.[5]) as [5]
FROM Query1 Q1
JOIN Query2 Q2
  ON Q1.CPT = Q2.CPT
 AND Q1.Resourece = Q2.Resource

答案 1 :(得分:1)

您需要加入CPT和资源,将table1.1 * table2.1作为1返回,如下所示:

SELECT t1.1 * t2.1 as 1 from t1 join t2 on t1.CPT = t2.CPT and t1.Resource = t2.Resource

答案 2 :(得分:1)

您可以使用以下简单连接

Select q1.CPT, q1.[Resource]
     , [1] = abs(q1.[1]*q2.[1])
     , [2] = abs(q1.[2]*q2.[2])
     , [3] = abs(q1.[3]*q2.[3])
     , [4] = abs(q1.[4]*q2.[4])
     , [5] = abs(q1.[5]*q2.[5])   
   from Query1 q1
   join Query2 q2
     on q1.CPT = q2.CPT
    and q1.[Resource] = q2.[Resource]

如果需要,用您的子查询替换query1和2

答案 3 :(得分:1)

假设你有两个有问题的表作为t1和t2,那么你的查询应该是

select 
    t1.CPT,
    t1.resource,
    ABS(t1.[1]*t2.[1]) as [1],
    ABS(t1.[2]*t2.[2]) as [2],
    ABS(t1.[3]*t2.[3]) as [3],
    ABS(t1.[4]*t2.[4]) as [4],
    ABS(t1.[5]*t2.[5]) as [5]
from
    t1 join t2
        on t1.CPT=t2.CPt 
        and t1.resource=t2.resource

如果您没有表t1和t2,这些是您的查询结果;请将查询封装为子查询,别名为t1和t2

select 
    t1.CPT,
    t1.resource,
    ABS(t1.[1]*t2.[1]) as [1],
    ABS(t1.[2]*t2.[2]) as [2],
    ABS(t1.[3]*t2.[3]) as [3],
    ABS(t1.[4]*t2.[4]) as [4],
    ABS(t1.[5]*t2.[5]) as [5]
from
    --( your query 1) 
     t1 join 
    --( your query 2)
     t2
        on t1.CPT=t2.CPt 
        and t1.resource=t2.resource

答案 4 :(得分:1)

SELECT a.CPT
    , a.Resource 
    , ABS(a.Col1 * b.Col1) AS 'Col1'
    , ABS(a.Col2 * b.Col2) AS 'Col2'
    , ABS(a.Col3 * b.Col3) AS 'Col3'
    , ABS(a.Col4 * b.Col4) AS 'Col4'
    , ABS(a.Col5 * b.Col5) AS 'Col5'
FROM Query1 AS a
    INNER JOIN Query2 AS b ON (a.CPT = b.CPT AND a.Resource = b.Resource)

答案 5 :(得分:1)

select Query1.CPT
     , Query1.Resource
     , Query1.1 * Query2.1 as 1
     , Query1.2 * Query2.2 as 2
     , Query1.3 * Query2.3 as 3
     , Query1.4 * Query2.4 as 4
     , Query1.5 * Query2.5 as 5 
from Query1 
Join Query2 on Query1.Resource= Query2.Resource

尝试这样,我现在没有sql server自己尝试,你可能需要对查询进行一些更改。

对于负值,您可以单独使用以下查询,也可以将其嵌入主查询中。

update NewTableName field = field * -1 where field < 0