返回状态为=="退休的所有项目"

时间:2016-05-21 19:21:57

标签: sql-server

我需要选择" ItemNames"从具有"状态"的SQL Server 2014表中退休的我曾经尝试过使用GROUP BY,但是还没有能够最终确定查询。任何帮助表示赞赏。

例如,这是表格。我应该只返回SQL查询中的A和B,因为每个都有一行status == Retired:

ItemName       Status    LastUpdated
=========     ========   ============
A              active
A              retired
B              active
B              active
B              retired
C              active
C              inactive

3 个答案:

答案 0 :(得分:2)

使用where子句:

   Select DISTINCT ItemName from YourTableName WHERE status = 'retired'

答案 1 :(得分:0)

您可以使用分组来制作不同的

从YourTable中选择ItemName,其中Status ='Retired'Group by ItemName

答案 2 :(得分:-1)

试试这段代码我觉得它运作良好

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            ServiceModel serviceModel = new ServiceModel()
            {
                ShippingRequest = new List<ShippingRequest>(){
                    new ShippingRequest() {
                        Address = "Address 1",
                        ShippingPackages = new List<ShippingPackage>() {
                            new ShippingPackage() { Package = "Package1"}, 
                            new ShippingPackage() { Package = "Package2"}, 
                            new ShippingPackage() { Package = "Package3"}
                        }
                    },
                    new ShippingRequest() {
                        Address = "Address 2",
                        ShippingPackages = new List<ShippingPackage>() {
                            new ShippingPackage() { Package = "Package5"}, 
                            new ShippingPackage() { Package = "Package8"}, 
                        }
                    },
                    new ShippingRequest() {
                        Address = "Address 3",
                        ShippingPackages = new List<ShippingPackage>() {
                            new ShippingPackage() { Package = "Package11"}, 
                            new ShippingPackage() { Package = "Package12"}, 
                        }
                    }
                },
                QuotesResult = new QuotesResult()
                {
                    Quotes = new List<Quote>() {
                        new Quote() { Cost = 123, Id = "Package1"},
                        new Quote() { Cost = 123, Id = "Package2"},
                        new Quote() { Cost = 123, Id = "Package3"},
                        new Quote() { Cost = 123, Id = "Package11"},
                        new Quote() { Cost = 123, Id = "Package11"}
                    }
                }
            };

            var addrQuotes = (from requests in serviceModel.ShippingRequest.Select(x => x.ShippingPackages.Select(y =>  new { address = x.Address, package = y})).SelectMany(z => z)
                             join quote in serviceModel.QuotesResult.Quotes
                             on requests.package.Package equals quote.Id
                             select new { quote = quote, package = requests }).ToList();
            var results = addrQuotes.GroupBy(m => m.package.address)
               .Select(n => new {
                    quotes = n.Select(c => c).Select(c1 => new {
                     address = c1.package.address,
                     quote = c1.quote
                    }).ToList()
                }).ToList();

        }
    }

    public class ServiceModel
    {
        public List<ShippingRequest> ShippingRequest { get; set; }
        public QuotesResult QuotesResult { get; set; }
    }

    public class ShippingRequest
    {
        public string Address { get; set; }                    // AddressId
        public List<ShippingPackage> ShippingPackages { get; set; }
    }

    public class ShippingPackage
    {
        public string Package { get; set; }                    // PackageId
        public List<string> ShippingItems { get; set; }  // IsSkipped
    }

    public class QuotesResult
    {
        public List<Quote> Quotes { get; set; } // PackageId, Cost
    }
    public class Quote
    {
        public string Id { get; set; }
        public decimal Cost { get; set; }
    }
}