SQL查询以查找客户使用特定产品购买的其他产品

时间:2017-06-26 14:59:17

标签: sql hive

以下是显示使用密钥“3500”的客户的查询。但表中还有其他键。 我试图找出以下客户使用的其他密钥(除了3500)。 对于任何建议都会很棒!!

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;

namespace ExampleNestedGrid
{
    public class RefreshCommand : ICommand
    {
        private ObservableCollection<Document> Documents;
        private System.Windows.Controls.DataGrid MainGrid;

        #region public methods
        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }
        #endregion

        #region public methods

        public RefreshCommand(ObservableCollection<Document> Documents, System.Windows.Controls.DataGrid MainGrid)
        {
            // TODO: Complete member initialization
            this.Documents = Documents;
            this.MainGrid = MainGrid;
        }

        public void Execute(object parameter)
        {
            Documents.First().LinkedEmployees.First().Status = !Documents.First().LinkedEmployees.First().Status;

            ICollectionView view = CollectionViewSource.GetDefaultView(Documents);
            view.Filter = (item) => item != null;

            MainGrid.ItemsSource = view;

            var childGrids = FindVisualChildren<DataGrid>(MainGrid);
            foreach (DataGrid childGrid in childGrids)
            {
                MessageBox.Show(childGrid.Name);
            }
        }

        public bool CanExecute(object parameter)
        {
            return Documents != null && MainGrid != null;
        }
        #endregion

        private static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
        {
            if (depObj != null)
            {
                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
                {
                    DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
                    if (child != null && child is T)
                    {
                        yield return (T)child;
                    }

                    foreach (T childOfChild in FindVisualChildren<T>(child))
                    {
                        yield return childOfChild;
                    }
                }
            }
        }
    }
}

结果:(以上列表仅为使用“3500”与其他产品密钥组合使用的客户)

SELECT distinct(b.id2)
FROM tab1 as a, tab2 as c, tab3 as b
WHERE a.id1 = c.id1 and c.id2 = b.id2
group by b.id2
having count
            (
                case
                    when    key in (3500)
                    then    1
                end
            ) > 0

  id2              key
  123             3500
  123             3501
  123             4100
  234             3500
  234             1234
  234             4100
  312             3500
  312             4100

1 个答案:

答案 0 :(得分:0)

在不知道基础表的结构的情况下,不可能给出最具体的答案,但这里应该有效:

with t as (
    SELECT *
    FROM
        tab1 as a
        INNER JOIN tab2 as c ON c.id1=a.id1
        INNER JOIN tab3 as b ON b.id2=c.id2
    ),
custkeys as (
    select distinct id2, key
    from t
    ),
cust3500 as (
    select distinct id2
    from custkeys
    where key = 3500
    )
select
    key,
    count(*) as customers
from
    custkeys as k
    inner join cust3500 as c on c.id2=k.id2
where
    key <> 3500;

custkeys CTE可能是不必要的,或者可能被更简单的东西替换,cust3500 CTE也可能会被更改,具体取决于表的实际结构。

相关问题