PHV: NM... I finally got my head around it.
![[dazed] [dazed] [dazed]](/data/assets/smilies/dazed.gif)
It's just a completely different way of thinking.
Just in case anybody
else was having trouble with the logic, here it is:
PHV's original statement:
Code:
SELECT A.OrderNumber,A.SKU
FROM tblSKU AS A INNER JOIN tblSKU AS B ON A.OrderNumber=B.OrderNumber AND A.SKU>=B.SKU
GROUP BY A.OrderNumber,A.SKU
HAVING Count(*)<=5
Using data filched from MajP:
Code:
ID OrderNumber SKU
1 94682 16-5873
2 94682 16-584
3 94682 16-5867
4 94682 16-5824
5 94682 16-5896
6 94682 16-5848
7 94682 16-5835
8 94682 16-5628
9 94682 16-5849
10 94682 16-5792
11 94682 16-5768
12 94682 16-5834
13 94682 16-59
14 94682 16-5725
15 94682 16-5766
16 94682 16-5888
17 94682 16-5712
18 94683 16-5713
19 94683 16-5711
20 94683 16-5722
21 94683 16-5742
22 94683 16-5719
23 94683 16-5733
24 94683 16-5745
25 94683 16-5703
If we temporarily take out the "GROUP BY" and the "HAVING" from PHV's statement (and sort it for readability), we end up with this ("OrderNumber" is abbreviated for formatting):
Code:
Ord SKU
94682 16-5628
94682 16-5712
94682 16-5712
94682 16-5725
94682 16-5725
94682 16-5725
94682 16-5766
94682 16-5766
94682 16-5766
94682 16-5766
94682 16-5768
94682 16-5768
94682 16-5768
94682 16-5768
94682 16-5768
94682 16-5792
94682 16-5792
94682 16-5792
94682 16-5792
94682 16-5792
94682 16-5792
94682 16-5824
94682 16-5824
94682 16-5824
94682 16-5824
94682 16-5824
94682 16-5824
94682 16-5824
94682 16-5834
94682 16-5834
94682 16-5834
94682 16-5834
94682 16-5834
94682 16-5834
94682 16-5834
94682 16-5834
94682 16-5835
94682 16-5835
94682 16-5835
94682 16-5835
94682 16-5835
94682 16-5835
94682 16-5835
94682 16-5835
94682 16-5835
94682 16-584
94682 16-584
94682 16-584
94682 16-584
94682 16-584
94682 16-584
94682 16-584
94682 16-584
94682 16-584
94682 16-584
94682 16-5848
94682 16-5848
94682 16-5848
94682 16-5848
94682 16-5848
94682 16-5848
94682 16-5848
94682 16-5848
94682 16-5848
94682 16-5848
94682 16-5848
94682 16-5849
94682 16-5849
94682 16-5849
94682 16-5849
94682 16-5849
94682 16-5849
94682 16-5849
94682 16-5849
94682 16-5849
94682 16-5849
94682 16-5849
94682 16-5849
94682 16-5867
94682 16-5867
94682 16-5867
94682 16-5867
94682 16-5867
94682 16-5867
94682 16-5867
94682 16-5867
94682 16-5867
94682 16-5867
94682 16-5867
94682 16-5867
94682 16-5867
94682 16-5873
94682 16-5873
94682 16-5873
94682 16-5873
94682 16-5873
94682 16-5873
94682 16-5873
94682 16-5873
94682 16-5873
94682 16-5873
94682 16-5873
94682 16-5873
94682 16-5873
94682 16-5873
94682 16-5888
94682 16-5888
94682 16-5888
94682 16-5888
94682 16-5888
94682 16-5888
94682 16-5888
94682 16-5888
94682 16-5888
94682 16-5888
94682 16-5888
94682 16-5888
94682 16-5888
94682 16-5888
94682 16-5888
94682 16-5896
94682 16-5896
94682 16-5896
94682 16-5896
94682 16-5896
94682 16-5896
94682 16-5896
94682 16-5896
94682 16-5896
94682 16-5896
94682 16-5896
94682 16-5896
94682 16-5896
94682 16-5896
94682 16-5896
94682 16-5896
94682 16-59
94682 16-59
94682 16-59
94682 16-59
94682 16-59
94682 16-59
94682 16-59
94682 16-59
94682 16-59
94682 16-59
94682 16-59
94682 16-59
94682 16-59
94682 16-59
94682 16-59
94682 16-59
94682 16-59
94683 16-5703
94683 16-5711
94683 16-5711
94683 16-5713
94683 16-5713
94683 16-5713
94683 16-5719
94683 16-5719
94683 16-5719
94683 16-5719
94683 16-5722
94683 16-5722
94683 16-5722
94683 16-5722
94683 16-5722
94683 16-5733
94683 16-5733
94683 16-5733
94683 16-5733
94683 16-5733
94683 16-5733
94683 16-5742
94683 16-5742
94683 16-5742
94683 16-5742
94683 16-5742
94683 16-5742
94683 16-5742
94683 16-5745
94683 16-5745
94683 16-5745
94683 16-5745
94683 16-5745
94683 16-5745
94683 16-5745
94683 16-5745
The reason is this. Here's the same thing, also showing the "B" columns. It's returning everything where A>=B:
Code:
SELECT A.OrderNumber,A.SKU, B.OrderNumber, B.SKU
FROM tblSKU AS A INNER JOIN tblSKU AS B ON A.OrderNumber=B.OrderNumber AND A.SKU>=B.SKU
GROUP BY A.OrderNumber,A.SKU, B.OrderNumber, B.SKU
Code:
A.Ord A.SKU B.Ord B.SKU
94682 16-5628 94682 16-5628
94682 16-5712 94682 16-5628
94682 16-5712 94682 16-5712
94682 16-5725 94682 16-5628
94682 16-5725 94682 16-5712
94682 16-5725 94682 16-5725
94682 16-5766 94682 16-5628
94682 16-5766 94682 16-5712
94682 16-5766 94682 16-5725
94682 16-5766 94682 16-5766
94682 16-5768 94682 16-5628
94682 16-5768 94682 16-5712
94682 16-5768 94682 16-5725
94682 16-5768 94682 16-5766
94682 16-5768 94682 16-5768
94682 16-5792 94682 16-5628
94682 16-5792 94682 16-5712
94682 16-5792 94682 16-5725
94682 16-5792 94682 16-5766
94682 16-5792 94682 16-5768
94682 16-5792 94682 16-5792
94682 16-5824 94682 16-5628
94682 16-5824 94682 16-5712
94682 16-5824 94682 16-5725
94682 16-5824 94682 16-5766
94682 16-5824 94682 16-5768
94682 16-5824 94682 16-5792
94682 16-5824 94682 16-5824
94682 16-5834 94682 16-5628
94682 16-5834 94682 16-5712
94682 16-5834 94682 16-5725
94682 16-5834 94682 16-5766
94682 16-5834 94682 16-5768
94682 16-5834 94682 16-5792
94682 16-5834 94682 16-5824
94682 16-5834 94682 16-5834
94682 16-5835 94682 16-5628
94682 16-5835 94682 16-5712
94682 16-5835 94682 16-5725
94682 16-5835 94682 16-5766
94682 16-5835 94682 16-5768
94682 16-5835 94682 16-5792
94682 16-5835 94682 16-5824
94682 16-5835 94682 16-5834
94682 16-5835 94682 16-5835
94682 16-584 94682 16-5628
94682 16-584 94682 16-5712
94682 16-584 94682 16-5725
94682 16-584 94682 16-5766
94682 16-584 94682 16-5768
94682 16-584 94682 16-5792
94682 16-584 94682 16-5824
94682 16-584 94682 16-5834
94682 16-584 94682 16-5835
94682 16-584 94682 16-584
94682 16-5848 94682 16-5628
94682 16-5848 94682 16-5712
94682 16-5848 94682 16-5725
94682 16-5848 94682 16-5766
94682 16-5848 94682 16-5768
94682 16-5848 94682 16-5792
94682 16-5848 94682 16-5824
94682 16-5848 94682 16-5834
94682 16-5848 94682 16-5835
94682 16-5848 94682 16-584
94682 16-5848 94682 16-5848
94682 16-5849 94682 16-5628
94682 16-5849 94682 16-5712
94682 16-5849 94682 16-5725
94682 16-5849 94682 16-5766
94682 16-5849 94682 16-5768
94682 16-5849 94682 16-5792
94682 16-5849 94682 16-5824
94682 16-5849 94682 16-5834
94682 16-5849 94682 16-5835
94682 16-5849 94682 16-584
94682 16-5849 94682 16-5848
94682 16-5849 94682 16-5849
94682 16-5867 94682 16-5628
94682 16-5867 94682 16-5712
94682 16-5867 94682 16-5725
94682 16-5867 94682 16-5766
94682 16-5867 94682 16-5768
94682 16-5867 94682 16-5792
94682 16-5867 94682 16-5824
94682 16-5867 94682 16-5834
94682 16-5867 94682 16-5835
94682 16-5867 94682 16-584
94682 16-5867 94682 16-5848
94682 16-5867 94682 16-5849
94682 16-5867 94682 16-5867
94682 16-5873 94682 16-5628
94682 16-5873 94682 16-5712
94682 16-5873 94682 16-5725
94682 16-5873 94682 16-5766
94682 16-5873 94682 16-5768
94682 16-5873 94682 16-5792
94682 16-5873 94682 16-5824
94682 16-5873 94682 16-5834
94682 16-5873 94682 16-5835
94682 16-5873 94682 16-584
94682 16-5873 94682 16-5848
94682 16-5873 94682 16-5849
94682 16-5873 94682 16-5867
94682 16-5873 94682 16-5873
94682 16-5888 94682 16-5628
94682 16-5888 94682 16-5712
94682 16-5888 94682 16-5725
94682 16-5888 94682 16-5766
94682 16-5888 94682 16-5768
94682 16-5888 94682 16-5792
94682 16-5888 94682 16-5824
94682 16-5888 94682 16-5834
94682 16-5888 94682 16-5835
94682 16-5888 94682 16-584
94682 16-5888 94682 16-5848
94682 16-5888 94682 16-5849
94682 16-5888 94682 16-5867
94682 16-5888 94682 16-5873
94682 16-5888 94682 16-5888
94682 16-5896 94682 16-5628
94682 16-5896 94682 16-5712
94682 16-5896 94682 16-5725
94682 16-5896 94682 16-5766
94682 16-5896 94682 16-5768
94682 16-5896 94682 16-5792
94682 16-5896 94682 16-5824
94682 16-5896 94682 16-5834
94682 16-5896 94682 16-5835
94682 16-5896 94682 16-584
94682 16-5896 94682 16-5848
94682 16-5896 94682 16-5849
94682 16-5896 94682 16-5867
94682 16-5896 94682 16-5873
94682 16-5896 94682 16-5888
94682 16-5896 94682 16-5896
94682 16-59 94682 16-5628
94682 16-59 94682 16-5712
94682 16-59 94682 16-5725
94682 16-59 94682 16-5766
94682 16-59 94682 16-5768
94682 16-59 94682 16-5792
94682 16-59 94682 16-5824
94682 16-59 94682 16-5834
94682 16-59 94682 16-5835
94682 16-59 94682 16-584
94682 16-59 94682 16-5848
94682 16-59 94682 16-5849
94682 16-59 94682 16-5867
94682 16-59 94682 16-5873
94682 16-59 94682 16-5888
94682 16-59 94682 16-5896
94682 16-59 94682 16-59
94683 16-5703 94683 16-5703
94683 16-5711 94683 16-5703
94683 16-5711 94683 16-5711
94683 16-5713 94683 16-5703
94683 16-5713 94683 16-5711
94683 16-5713 94683 16-5713
94683 16-5719 94683 16-5703
94683 16-5719 94683 16-5711
94683 16-5719 94683 16-5713
94683 16-5719 94683 16-5719
94683 16-5722 94683 16-5703
94683 16-5722 94683 16-5711
94683 16-5722 94683 16-5713
94683 16-5722 94683 16-5719
94683 16-5722 94683 16-5722
94683 16-5733 94683 16-5703
94683 16-5733 94683 16-5711
94683 16-5733 94683 16-5713
94683 16-5733 94683 16-5719
94683 16-5733 94683 16-5722
94683 16-5733 94683 16-5733
94683 16-5742 94683 16-5703
94683 16-5742 94683 16-5711
94683 16-5742 94683 16-5713
94683 16-5742 94683 16-5719
94683 16-5742 94683 16-5722
94683 16-5742 94683 16-5733
94683 16-5742 94683 16-5742
94683 16-5745 94683 16-5703
94683 16-5745 94683 16-5711
94683 16-5745 94683 16-5713
94683 16-5745 94683 16-5719
94683 16-5745 94683 16-5722
94683 16-5745 94683 16-5733
94683 16-5745 94683 16-5742
94683 16-5745 94683 16-5745
Now we just need to get the distinct "A" columns, and filter for those records that are duplicated 5 or fewer times:
Code:
Ord SKU
94682 16-5628
94682 16-5712
94682 16-5725
94682 16-5766
94682 16-5768
94683 16-5703
94683 16-5711
94683 16-5713
94683 16-5719
94683 16-5722
Thanks, PHV, for sharing this... it's always good to learn new ways of programming things!
Sadly, the reason I'd originally asked was that, I'd hoped, if I could understand it, then I could maybe adapt it to let me get details about latest payments (which is something I have to do
constantly) without subqueries, but it looks like that's not what this logic is for... I'd
still have to use subqueries for that in Access. Ah well... I guess I'm just starting to get spoiled by TSQL.
Anyway, even though I'm not the original poster, I'd still like to thank everybody in this thread for sharing... guess I'm never finished learning.
Katie