ग्रुप द्वारा पंक्तियों के एक समूह के योग के द्वारा डेटा का सारांश दिया, लौटने एक पंक्ति प्रति समूह आप समेकित फ़ंक्शन max()
का उपयोग कर रहे हैं, जो पंक्तियों के समूह के लिए एक कॉलम से अधिकतम मान वापस कर देगा।
चलो कुछ डेटा देखें। मैंने उस कॉलम का नाम बदल दिया जिसे आपने "दिनांक" कहा था।
create table sales (
customerId integer not null,
saleId integer not null,
saledate date not null
);
insert into sales values
(1, 10, '2013-05-13'),
(1, 11, '2013-05-14'),
(1, 12, '2013-05-14'),
(1, 13, '2013-05-17'),
(2, 20, '2013-05-11'),
(2, 21, '2013-05-16'),
(2, 31, '2013-05-17'),
(2, 32, '2013-03-01'),
(3, 33, '2013-05-14'),
(3, 35, '2013-05-14');
आप
In another words, the first 2 lines are from the same customer(id 19), i wish he'd get only one record for each client, which would be the record with the max date, in the case, the second record from this list.
select s.customerId, max (s.saledate)
from sales s
where s.saledate <= '2013-05-16'
group by s.customerId
order by customerId;
customerId max
--
1 2013-05-14
2 2013-05-16
3 2013-05-14
कहा कि तालिका क्या मतलब है? इसका मतलब है कि 16 मई को या उससे पहले की नवीनतम तारीख जिस पर ग्राहक "1" कुछ खरीदा था 14 मई था; 16 मई को या उससे पहले की नवीनतम तारीख जिस पर ग्राहक "2" कुछ खरीदा था 16 मई। यदि आप इस व्युत्पन्न तालिका का उपयोग जॉइन में करते हैं, तो यह अनुमानित परिणाम को लगातार अर्थ के साथ वापस कर देगा।
अब देखते हैं कि थोड़ा अलग प्रश्न देखें। MySQL इस वाक्यविन्यास को अनुमति देता है, और नीचे परिणाम सेट देता है।
select s.customerId, s.saleId, max(s.saledate) max_sale
from sales s
where s.saledate <= '2013-05-16'
group by s.customerId
order by customerId;
customerId saleId max_sale
--
1 10 2013-05-14
2 20 2013-05-16
3 33 2013-05-14
आईडी "10" के साथ बिक्री 14 मई को नहीं हुई; यह 13 मई को हुआ था। इस प्रश्न ने झूठ पैदा की है।बिक्री लेनदेन की तालिका के साथ इस व्युत्पन्न तालिका में शामिल होने से त्रुटि मिल जाएगी।
यही कारण है कि फायरबर्ड सही ढंग से एक त्रुटि उठाता है। समाधान बिक्री खंड से बिक्री ड्रॉप है।
अब, यह सब कहकर, आप उन ग्राहकों को ढूंढ सकते हैं जिनके पास 16 मई से कोई बिक्री नहीं हुई है।
select distinct customerId from sales
where customerID not in
(select customerId
from sales
where saledate >= '2013-05-16')
और आप सही ग्राहक आईडी और "दाएं" बिक्री प्राप्त कर सकते हैं। (मैं कहता हूँ "सही" saleId, क्योंकि वहाँ सवाल में दिन पर एक से अधिक हो सकता है। मैं सिर्फ अधिकतम चुना है।)
select sales.customerId, sales.saledate, max(saleId)
from sales
inner join (select customerId, max(saledate) max_date
from sales
where saledate < '2013-05-16'
group by customerId) max_dates
on sales.customerId = max_dates.customerId
and sales.saledate = max_dates.max_date
inner join (select distinct customerId
from sales
where customerID not in
(select customerId
from sales
where saledate >= '2013-05-16')) no_sales
on sales.customerId = no_sales.customerId
group by sales.customerId, sales.saledate
व्यक्तिगत रूप से, मुझे लगता है आम तालिका भाव यह आसान मुझे एसक्यूएल को पढ़ने के लिए कर इस तरह के बयान SELECT में खोए बिना।
with no_sales as (
select distinct customerId
from sales
where customerID not in
(select customerId
from sales
where saledate >= '2013-05-16')
),
max_dates as (
select customerId, max(saledate) max_date
from sales
where saledate < '2013-05-16'
group by customerId
)
select sales.customerId, sales.saledate, max(saleId)
from sales
inner join max_dates
on sales.customerId = max_dates.customerId
and sales.saledate = max_dates.max_date
inner join no_sales
on sales.customerId = no_sales.customerId
group by sales.customerId, sales.saledate
हाँ, लेकिन केवल अगर उच्च बिक्री आईडी हमेशा एक नई तारीख का मतलब है। –