Home > database > Statistics of all sales of goods appear at the same time the most times
Statistics of all sales of goods appear at the same time the most times
Time:09-18
Sample data volume 1.5 million, table is as follows: Sales order goods
20190101 A
20190101 B
20190101 C
20190102 A
20190102 C
20190103 C
20190103 D
I wrote a SQL, data can also be less, but also very slow, the data quantity of stuck directly, ask the great spirit show
SELECT commodity code 1 y., y. commodity code 2, COUNT (DISTINCT y1. The sales order) as js
The FROM (
Commodity code as commodity code 1, SELECT a. b. commodity code as commodity code 2
The FROM (SELECT DISTINCT commodity code FROM sales table) A,
(SELECT DISTINCT commodity code FROM sales table) B
WHERE A commodity code & lt; B. commodity code
) y INNER JOIN sales table y1 ON y1. The h.s.code=y. The commodity code 1
WHERE E sales table ISTS (SELECT * FROM sales table y2 WHERE y2. The sales order number=y1. The sales order AND y2, commodity code=y. commodity code 2)
GROUP BY commodity code 1 y., y. commodity code 2;
CodePudding user response:
There is distinct must be slow
CodePudding user response:
According to your sample, hope to get the result of what?
CodePudding user response:
Look from the sample table A and C in the same goods sales is the number on the list of the most
CodePudding user response:
Didn't understand, is to look at most times in total, or the most is the number of times in the same sales list?
Look from your example, biggest total C, but your reply is A and C?