大家好我有一张桌子(商店)有3列:
id receipt_number receipt_date
111 666 02/02/2016
111 666 02/02/2016
111 667 02/03/2016
112 668 02/02/2016
112 669 03/02/2015
112 670 04/12/2015
113 671 05/11/2013
我想查询查找去年每个id购买了多少次。 决赛桌:
id bought
111 2
112 2
111 have 2 shop because in database have 2 equals receipt
112 have 2 shop because in database one shop It is a year old
113 there is not because the only shop it has been done years ago.
现在我已经这样做了:
SELECT DISTINCT(id),receipt_number,receipt_date
FROM shop
where receipt_number in (SELECT distinct(receipt_number)
FROM shop)
GROUP BY(id)
但完全错了
感谢所有人,抱歉我的英语不好
我找到了结果,谢谢所有
select id, count(*)shop_number
from
(select id,recipt_Date,recipt_number
from shop
where id !='' and recipt_date > DATE_ADD(sysdate(), INTERVAL -1 YEAR)
and recipt_Date <= sysdate()
group by id, recipt_date , recipt_nu,ber) as a
group by id
答案 0 :(得分:0)
我认为你需要这样的东西:
SELECT id, COUNT(DISTINCT receipt_number)
FROM shop
WHERE receipt_date BETWEEN DATE_ADD(NOW(), INTERVAL -1 YEAR) AND NOW()
GROUP BY id
答案 1 :(得分:0)
您使用group by
,where
和count(distinct)
:
select id, count(distinct receipt_number)
from shop
where receipt_date >= date_sub(curdate(), interval 1 year)
group by id;
答案 2 :(得分:0)
SQL Server查询可以是
import com.google.android.gms.common.api.PendingResult;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.common.api.Status;
import com.google.android.gms.drive.Drive;
import com.google.android.gms.drive.DriveApi;
import com.google.android.gms.drive.DriveResource;
import com.google.android.gms.drive.events.ChangeEvent;
import com.google.android.gms.drive.events.ChangeListener;
PendingResult<DriveApi.DriveIdResult> pendingResult = Drive.DriveApi.fetchDriveId(mGoogleApiClient, id);
pendingResult.setResultCallback(new ResultCallback<DriveApi.DriveIdResult>() {
@Override
public void onResult(@NonNull DriveApi.DriveIdResult driveIdResult) {
if (!driveIdResult.getStatus().isSuccess()) {
Log.d(TAG, String.format("fetch drive id error: %s", driveIdResult.getStatus().getStatusMessage()));
return;
}
mCurrentDriveId = driveIdResult.getDriveId();
DriveResource resource = mCurrentDriveId.asDriveResource();
Log.d(TAG, "received driveid from id");
PendingResult<Status> pendingChange = resource.addChangeListener(mGoogleApiClient, new ChangeListener() {
@Override
public void onChange(ChangeEvent changeEvent) {
onResourceChange(changeEvent);
}
});
pendingChange.setResultCallback(new ResultCallback<Status>() {
@Override
public void onResult(@NonNull Status status) {
if (status.isSuccess()) {
Log.d(TAG, "change listener success");
} else {
Log.d(TAG, String.format("change listener error: %s", status.getStatusMessage()));
}
}
});
}
});