如何在不同表中使用连接的SQL查询中使用SUM函数? [英] How to Use SUM function in an SQL Query using Joins among different tables ?

查看:415
本文介绍了如何在不同表中使用连接的SQL查询中使用SUM函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!



我想在我的sql查询中使用sum函数,使用多个表之间的连接。



假设我有三张桌子。



1: -

tbl_Items


ItemID [int]

ItemName [nvarchar]



2: -

tbl_Orders


OrderID [int]

ItemID [int]

数量[钱]



3: -

tbl_GatePasses


GatePassID [int]

OrderID [int]

ItemID [int]

数量[钱]



4: -

tbl_Bills


BillID [int]

GatePassID [int]

OrderID [int]

ItemID [int]

数量[钱]



高级是指所有三个表中的某些项目可以同时存在并且任何两个表中的一些项目和一些项目只能在一个表中。



i想写一个应该给我的查询以下信息



1.订单中每件商品的总数量

2.门票中每件商品的总数量

3.账单中每件商品的总数量



特殊情况: -

如果是item只存在于订单表中,然后查询应该显示订单数量的总和,并在其他两个表中显示0数量。



i表示如果没有任何项目的记录然后查询中的任何一个或任何两个表应该告诉其记录存在的表中的总和数量,但是对于不存在其记录的表显示零。



i希望我的信息实际上是我想要的。



i将感谢能帮助我的人。



谢谢。

Hello !

I want to use sum function in my sql query using joins among multiple tables.

Suppose i have three tables.

1:-
tbl_Items

ItemID [int]
ItemName [nvarchar]

2:-
tbl_Orders

OrderID [int]
ItemID [int]
Quantity [money]

3:-
tbl_GatePasses

GatePassID [int]
OrderID [int]
ItemID [int]
Quantity [money]

4:-
tbl_Bills

BillID [int]
GatePassID [int]
OrderID [int]
ItemID [int]
Quantity [money]

the Senior is that some items can be exist in all three tables at the same time and some items in any two tables and some items can be only in one table.

i want to write a query that should give me the following information

1. total quantity of each item in orders
2. total quantity of each item in gate passes
3. total quantity of each item in bills

Special Case:-
if an item exists in only orders table then query should show the sum of quantity in orders and show the 0 quantity in other two tables.

i mean if no record of any item in anyone or any two of the tables then query should tell the sum quantity from the table in which its records exist but show zero for the table in which its record is not exist.

i hope my message is delivered what i want in fact.

i will be grateful to one who will help me.

thanks.

推荐答案

它不是那样的工作。

我们不为你工作。

如果你想要别人写你的代码,你必须付钱 - 我你会去Freelancer.com并问那里。



但要注意:你得到的是你付出的代价。支付花生,买猴子。
It doesn't quite work like that.
We do not do your work for you.
If you want someone to write your code, you have to pay - I suggest you go to Freelancer.com and ask there.

But be aware: you get what you pay for. Pay peanuts, get monkeys.


这篇关于如何在不同表中使用连接的SQL查询中使用SUM函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆