is there a way to query same db field with two different where clause in the same query?

Viewed 17

I have a condition that I need to query Couchbase database field with two different where clauses in the same query. for example: for 'sales' field in a database entity, I want to have one query to get the sales in a single hour and whole day

output example:

`{`
`hourly_sales = 500`
`total_day_sales = 5000
`}`

I know that I can use 2 different queries, but requirement is to use one query for both

1 Answers

You can achieve that using case when statement. For example -

SELECT SUM(CASE WHEN `TIME` BETWEEN "13:00:00" AND "14:00:00" THEN `SALES`) AS HOURLY_SALES,
SUM(CASE WHEN `TIME` BETWEEN "00:00:00" AND "23:59:59" THEN `SALES`) AS TOTAL_DAY_SALES
FROM `BUCKET_NAME`

Make use of the time format according to your need.

Related