2017-03-25 3 views
0

ich eine Datentabelle wie dieseWie von Gruppe SUM wo Spalt bestimmter Wert

| start_date    | end_date    |  sales | is_finished | 
|:------------------------:|---------------------:|:------------:|:------------: 
| 2017-03-24 09:11:00  | 2017-03-24 09:11:00 |  30  |  1 
| 2017-03-24 08:30:00  | 2017-03-24 08:30:00 |  50  |  1 
| 2017-03-24 08:14:00  | 2017-03-24 08:14:00 |  100  |  1 
| 2017-03-24 07:30:00  | 2017-03-24 07:45:00 |  200  |  0 
| 2017-03-24 07:00:00  | 2017-03-24 07:00:00 |  150  |  1 
| 2017-03-24 06:40:00  | 2017-03-24 06:50:00 |  450  |  0 

ich zu einer Gruppe muß unter Berücksichtigung haben haben die Zeilen von Startdatum und Enddatum, dass es auch dort, wo gehören und Enddatum Anfang gruppiert werden zu einer Zeile mit 1. Ich brauche auch de SUM jeder Teilmenge.

Ich brauche nächstes Ergebnis

| start_date    | end_date    | SUM(sales) | 
|:------------------------:|---------------------:|:------------:| 
| 2017-03-24 09:11:00  | 2017-03-24 09:11:00 |  30  | 
| 2017-03-24 08:30:00  | 2017-03-24 08:30:00 |  50  | 
| 2017-03-24 07:30:00  | 2017-03-24 08:14:00 |  300  | 
| 2017-03-24 06:40:00  | 2017-03-24 07:00:00 |  600  | 

Ich habe eine Abfrage zu bekommen, aber ich habe das Problem, dass, wenn es zwei Reihen mit is_finished Wert 1 zusammen die gruppiert sind.

Meine Frage ist:

SELECT SUM(sales) , MIN(start_date) , MAX(end_date) 
FROM sales 
GROUP BY 
start_date > (SELECT start_date FROM sales WHERE is_finished = 1 ORDER BY end_date ASC LIMIT 1 OFFSET 1), 
end_date <= (SELECT end_date FROM sales WHERE is_finished = 1 ORDER BY end_date ASC LIMIT 1) 
ORDER BY end_date DESC 

die Abfrage oben Verwenden Sie mich zu erwartendem Ergebnis der Nähe, aber es funktioniert nicht gut.

Vielen Dank im Voraus.

Antwort

1

bitte versuchen Sie es dieses

SELECT SEDates.sdate as start_date,SEDates.edate as end_date,SUM(sales) 
FROM sales s INNER JOIN 
    (SELECT end_date as edate,is_finished, 
      @prev as prev, 
     CASE WHEN (@prev IS NULL OR @prev = 1) THEN @sdate := start_date 
      ELSE @sdate 
     END as sdate, 
     @prev := is_finished as temp 
    FROM sales,(SELECT @prev:=NULL)t 
    ORDER BY start_date 
    )SEDates 
ON s.start_date BETWEEN SEDates.sdate AND SEDates.edate 
WHERE SEDates.is_finished = 1 
GROUP BY SEDates.sdate,SEDates.edate 
ORDER BY SEDates.edate DESC 

sqlfiddle

+0

Vielen Dank, Dies ist die Lösung die ich brauche. –

2

Dies kann hilfreich sein,

SELECT MIN(START_DATE) START_DATE 
    , MAX(END_DATE) END_DATE 
    , SUM(SALES) SUM_SALES 
FROM (
    SELECT START_DATE 
    , END_DATE 
    , SALES 
    , SALES_ID 
    , ROW_NUMBER() OVER (PARTITION BY START_DATE, END_DATE ORDER BY SALES_ID) RNK 
    FROM (
    SELECT SALES.START_DATE 
     , SALES.END_DATE 
     , SALES.SALES 
     , SALES_ID 
    FROM 
    SALES, (
     SELECT END_DATE 
     , ROW_NUMBER() OVER (ORDER BY END_DATE) SALES_ID 
     FROM SALES 
     WHERE IS_FINISHED = 1 
    ) SALES_ID 
    WHERE SALES.START_DATE <= SALES_ID.END_DATE 
     AND SALES.END_DATE <= SALES.END_DATE 
) 
) 
WHERE RNK = 1 
GROUP BY SALES_ID 
ORDER BY START_DATE DESC 
Verwandte Themen