当前ID的总和

时间:2015-09-29 07:46:07

标签: sql sql-server tsql sum

我得不到任何回报,任何人都能看到我失踪的简单语言吗?

SELECT SUM(Amount * InPrice) AS TotalItemsOrdered
FROM tblMaterial
WHERE [tblMaterial].[OwnerId] = [tblTask].[Id] 

我是否需要做一些不同的事情来获得结果?

希望得到一个简单的答案......是的我是新来的:)

3 个答案:

答案 0 :(得分:0)

您需要从两个表中选择或执行JOIN

选择选项:

SELECT SUM(Amount * InPrice) AS TotalItemsOrdered
FROM [tblMaterial], [tblTask]
WHERE [tblMaterial].[OwnerId] = [tblTask].[Id] 

加入选项:

SELECT SUM(Amount * InPrice) AS TotalItemsOrdered
FROM [tblMaterial]
INNER JOIN [tblTask] ON [tblMaterial].[OwnerId] = [tblTask].[Id]

如果两个表中都出现了SUM函数中的列和表名,则可能还需要为它们添加前缀。

在这里,您可以阅读有关SQL连接的更多信息:

答案 1 :(得分:0)

您的选择中缺少tblTask​​。

试试这个:

namespace Domain
{
public enum FollowUpActivityStatus
{
    Open,
    Closed,
    Delegated
}

public enum FollowUpActivityBacklogStatus
{
    Work,
    Sprint        
}

public class FollowUpActivity
{
    private String id;              // Generated on server
    private DateTime? creationDate;
    private DateTime? startDate;
    private DateTime? dueDate;
    private DateTime? closingDate;
    private String matter;
    private String comment;
    private FollowUpActivityStatus status;
    private FollowUpActivityBacklogStatus backlogStatus;

    private List<MetaInfoValue> metainfos;
    private List<MetaResource> resources;

    #region Properties

    public String Id
    {
        get { return id; }
        set { id = value; }
    }

    public DateTime? CreationDate
    {
        get { return creationDate; }
        set { creationDate = value; }
    }

    public DateTime? StartDate
    {
        get { return startDate; }
        set { startDate = value; }
    }

    public DateTime? DueDate
    {
        get { return dueDate; }
        set { dueDate = value; }
    }

    public DateTime? ClosingDate
    {
        get { return closingDate; }
        set { closingDate = value; }
    }

    public FollowUpActivityStatus Status
    {
        get { return status; }
        set { status = value; }
    }

    public FollowUpActivityBacklogStatus BacklogStatus
    {
        get { return backlogStatus; }
        set { backlogStatus = value; }
    }

    public String Matter
    {
        get { return matter; }
        set { matter = value; }
    }

    public String Comment
    {
        get { return comment; }
        set { comment = value; }
    }

    public List<MetaInfoValue> Metainfos
    {
      get { return metainfos; }
      set { metainfos = value; }
    }

    public List<MetaResource> Resources
    {
        get { return resources; }
        set { resources = value; }
    }

    #endregion

    #region Constructors

    public FollowUpActivity()
        : this(null, null)
    {

    }

    public FollowUpActivity(String matter, String comment = null, params Domain.MetaInfoValue[] metainfos)
        : this(DateTime.Now, matter, comment, metainfos)
    {

    }

    public FollowUpActivity(DateTime creationDate, String matter, String comment = null, params Domain.MetaInfoValue[] metainfos)
        : this(creationDate, matter, new List<MetaResource>(), comment, metainfos)
    {

    }

    public FollowUpActivity(DateTime creationDate, String matter, List<MetaResource> resources, String comment = null, params Domain.MetaInfoValue[] metainfos)
        : this(creationDate, matter, FollowUpActivityBacklogStatus.Work, new List<MetaResource>(), comment, metainfos)
    {

    }

    public FollowUpActivity(DateTime creationDate, String matter, FollowUpActivityBacklogStatus backlogStatus, List<MetaResource> resources, String comment = null, params Domain.MetaInfoValue[] metainfos)
        : this(creationDate, null, null, null, matter, FollowUpActivityStatus.Open, backlogStatus, new List<MetaResource>(), comment, metainfos)
    {

    }

    public FollowUpActivity(DateTime? creationDate, DateTime? startDate, DateTime? dueDate, DateTime? closingDate, String matter, FollowUpActivityStatus status, FollowUpActivityBacklogStatus backlogStatus, List<MetaResource> resources, String comment = null, params Domain.MetaInfoValue[] metainfos)
    {
        this.id = String.Empty;
        this.creationDate = creationDate;
        this.startDate = startDate;
        this.dueDate = dueDate;
        this.closingDate = closingDate;
        this.matter = matter;
        this.comment = comment;
        this.status = status;
        this.backlogStatus = backlogStatus;

        this.metainfos = new List<MetaInfoValue>(metainfos);
        this.resources = resources;
    }

    #endregion

}
}

或使用联接:

SELECT SUM(Amount * InPrice) AS TotalItemsOrdered
FROM tblMaterial, tblTask
WHERE tblMaterial.OwnerId = tblTask.Id

答案 2 :(得分:0)

如果您想要GROUP BYSUM

,请添加ID
SELECT tblMaterial.OwnerId
    , SUM(Amount * InPrice) AS TotalItemsOrdered
FROM tblMaterial INNER JOIN tblTask ON tblMaterial.OwnerId = tblTask.Id
GROUP BY tblMaterial.OwnerId