如何从两个表中查询(SumProduct)

时间:2017-02-02 17:02:39

标签: sql-server sql-server-2012

我有两张桌子HOUR包含3个部分的小时(A0001,B0001,C0001),不同的TYPE(A,B,C)和PART包含3个部分的EAU(A0001,B0001,C0001)来自KEYID-29395 :

enter image description here

当使用sumproduct

选择KEYID = 29395时,我希望获得3种不同类型的总小时数
TYPEA_TOTAL_HOUR = 1*100 + 4*200 + 7*300 
TYPEB_TOTAL_HOUR = 2*100 + 5*200 + 8*300
TYPEC_TOTAL_HOUR = 3*100 + 6*200 + 9*300

enter image description here

我如何使用SQL Server 2012实现这一目标?

2 个答案:

答案 0 :(得分:1)

<form class="form-horizontal form-label-left" action="new_rechargecard.php" method="POST">
    <input type="hidden" name="action" value="update_user" />
    <table id="datatable" class="table table-striped table-bordered">
        <tbody>
            <tr>
                <td width='100'>Employee Name: </td>
                <td><input type="text" class="form-control" placeholder="0" name="name" value="<?php echo $name ?>" ></td>
            </tr>
            <tr>
                <td>Available Balance</td>
                <td><input type="text" class="form-control" placeholder="0" name="balance" value=<?php echo $balance ?> ></td>
            </tr>
            <tr>
                <td>Civil id</td>
                <td><input type="text" class="form-control" placeholder="Civil ID Required" name="civil_id" value=<?php echo $pass_name;?> ></td>
            </tr>
            <tr>
                <td width='250'>Card Type (1 KD or 2.5 KD or 5 KD)</td>
                <td><input type="text" class="form-control"   name="card_type"></td>
            </tr>
            <tr>
                <td width='200'>Card Count</td>
                <td><input type="text" class="form-control"  name="count"></td>
            </tr>
            <tr>
                <td>Issue Year</td>
                <td><input type="text" class="form-control"  name="issue_year" value="<?php echo date('Y'); ?>"></td>
            </tr>
            <tr>
                <td>For the Month of</td>
                <td><input type="text" class="form-control"  name="issue_month" value="<?php echo date('M'); ?>"></td>
            </tr>
            <tr>
                <td>Issue date</td>
                <td><input type="text" class="form-control"  name="issue_date"></td>
            </tr>
            <tr>
                <td>Distribute</td>
                <td><select name="distribute[]" class="select2_multiple form-control" tabindex="-1"  multiple="multiple">
                        <option></option>
                        <option>Richard Marcus</option>
                        <option>Rowlant S Peter</option>
                        <option>David.K.Rumpell</option>
                        <option>John Mathew</option>
                    </select></td>
            </tr>
            <tr>
                <td></td>
                <td><input type="submit" class="btn btn-round btn-danger" value="Update"></td>
            </tr>
        </tbody>
    </table>
</form>

答案 1 :(得分:0)

像这样加入两个表和AGGREGATE:

select
    a.keyid,
    sum(a.typea * b.eau) typea_total_hour,
    sum(a.typeb * b.eau) typeb_total_hour,
    sum(a.typec * b.eau) typec_total_hour
from hour a
join part b
on a.partid = b.partid
and a.keyid = b.keyid
group by a.keyid;