How to select rows where one column value is the same on separate dates without using variables?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I have a DB Fiddle as an example: https://www.db-fiddle.com/f/vX7yFVk8j6FPTSm2QVoPNE/2



I'm trying to figure out how to find all bookings for a guest who has booked one room several times on different dates. I can't use a static value/variable like bookings.check_in_date = '2018-03-05', bookings.guest_id = 001 or use subqueries.



I tried:



SELECT bookings.guest_id, bookings.room_number, bookings.check_in_date, bookings.check_out_date
FROM bookings
GROUP BY bookings.guest_id, bookings.room_number, bookings.check_in_date, bookings.check_out_date
HAVING count(distinct bookings.check_in_date) >= 2;


However, it isn't giving me the result I'm looking for but rather I'm looking for this as a result:



guest_id, room_number, check_in_date, check_out_date
1, 203, 2018-03-01T00:00:00.000Z, 2018-03-07T00:00:00.000Z
1, 203, 2018-03-14T00:00:00.000Z, 2018-03-21T00:00:00.000Z


Schema (PostgreSQL v10.0)



CREATE TABLE guests (
id integer,
name text,
address text,
city text,
state text,
postal_code integer,
phone_number numeric(10)
);

CREATE TABLE rooms (
room_number integer,
floor_number integer,
view boolean,
bed_type text
);

CREATE TABLE bookings (
id integer,
guest_id integer,
room_number integer,
order_date date,
check_in_date date,
check_out_date date
);

INSERT INTO guests (id, name, address, city, state, postal_code, phone_number)
VALUES
(1, 'John', '5 8th Avenue', 'New York', 'New York', 10001, 2021110190),
(2, 'Jane', '13 Wall St', 'New York', 'New York', 10001, 1112020190),
(3, 'Jim', '12 Water St', 'New York', 'New York', 10001, 3332224444),
(4, 'Fang', '12 Broadway', 'New York', 'New York', 10001, 1234567890),
(5, 'Bruce', '123 1st Ave', 'New York', 'New York', 10001, 9871112222);

INSERT INTO rooms (room_number, floor_number, view, bed_type)
VALUES
(101, 1, FALSE, 'King'),
(102, 1, FALSE, 'Queens'),
(203, 2, FALSE, 'Queens'),
(204, 2, TRUE, 'King'),
(409, 4, TRUE, 'King');

INSERT INTO bookings (id, guest_id, room_number, order_date, check_in_date, check_out_date)
VALUES
(18001, 001, 203, '2018-03-05', '2018-03-01', '2018-03-07'),
(18002, 001, 203, '2018-03-05', '2018-03-14', '2018-03-21'),
(18003, 001, 409, '2018-03-05', '2018-11-27', '2018-11-30'),
(18004, 001, 204, '2018-03-05', '2018-11-27', '2018-11-30'),

(18007, 002, 409, '2018-05-05', '2018-05-05', '2018-05-12'),
(18002, 002, 203, '2018-03-05', '2018-03-14', '2018-03-21'),
(18006, 002, 409, '2018-06-05', '2018-07-04', '2018-07-07'),

(18005, 003, 409, '2018-06-05', '2018-08-05', '2018-08-12'),

(18008, 004, 409, '2018-09-05', '2018-09-05', '2018-09-12');









share|improve this question

























  • Why aren't you allowed to use sub-queries?

    – Vlam
    Nov 27 '18 at 1:26











  • My school want us to learn how to do queries without them. I honestly prefer them because they're more readable to me.

    – lassiter
    Nov 27 '18 at 1:34











  • What does it mean "booked one room on separate dates"? Is that "booked the same room", "booked a room for one person", or just "booked one room and not two"?

    – Gordon Linoff
    Nov 27 '18 at 1:42











  • It means, I have hotel room #1 booked from today through tomorrow. I also have hotel room #1 booked next week through next week +1 day.

    – lassiter
    Nov 27 '18 at 1:47











  • Why does your fiddle use MySQL but you tag Postgres here?

    – Parfait
    Nov 27 '18 at 2:15


















0















I have a DB Fiddle as an example: https://www.db-fiddle.com/f/vX7yFVk8j6FPTSm2QVoPNE/2



I'm trying to figure out how to find all bookings for a guest who has booked one room several times on different dates. I can't use a static value/variable like bookings.check_in_date = '2018-03-05', bookings.guest_id = 001 or use subqueries.



I tried:



SELECT bookings.guest_id, bookings.room_number, bookings.check_in_date, bookings.check_out_date
FROM bookings
GROUP BY bookings.guest_id, bookings.room_number, bookings.check_in_date, bookings.check_out_date
HAVING count(distinct bookings.check_in_date) >= 2;


However, it isn't giving me the result I'm looking for but rather I'm looking for this as a result:



guest_id, room_number, check_in_date, check_out_date
1, 203, 2018-03-01T00:00:00.000Z, 2018-03-07T00:00:00.000Z
1, 203, 2018-03-14T00:00:00.000Z, 2018-03-21T00:00:00.000Z


Schema (PostgreSQL v10.0)



CREATE TABLE guests (
id integer,
name text,
address text,
city text,
state text,
postal_code integer,
phone_number numeric(10)
);

CREATE TABLE rooms (
room_number integer,
floor_number integer,
view boolean,
bed_type text
);

CREATE TABLE bookings (
id integer,
guest_id integer,
room_number integer,
order_date date,
check_in_date date,
check_out_date date
);

INSERT INTO guests (id, name, address, city, state, postal_code, phone_number)
VALUES
(1, 'John', '5 8th Avenue', 'New York', 'New York', 10001, 2021110190),
(2, 'Jane', '13 Wall St', 'New York', 'New York', 10001, 1112020190),
(3, 'Jim', '12 Water St', 'New York', 'New York', 10001, 3332224444),
(4, 'Fang', '12 Broadway', 'New York', 'New York', 10001, 1234567890),
(5, 'Bruce', '123 1st Ave', 'New York', 'New York', 10001, 9871112222);

INSERT INTO rooms (room_number, floor_number, view, bed_type)
VALUES
(101, 1, FALSE, 'King'),
(102, 1, FALSE, 'Queens'),
(203, 2, FALSE, 'Queens'),
(204, 2, TRUE, 'King'),
(409, 4, TRUE, 'King');

INSERT INTO bookings (id, guest_id, room_number, order_date, check_in_date, check_out_date)
VALUES
(18001, 001, 203, '2018-03-05', '2018-03-01', '2018-03-07'),
(18002, 001, 203, '2018-03-05', '2018-03-14', '2018-03-21'),
(18003, 001, 409, '2018-03-05', '2018-11-27', '2018-11-30'),
(18004, 001, 204, '2018-03-05', '2018-11-27', '2018-11-30'),

(18007, 002, 409, '2018-05-05', '2018-05-05', '2018-05-12'),
(18002, 002, 203, '2018-03-05', '2018-03-14', '2018-03-21'),
(18006, 002, 409, '2018-06-05', '2018-07-04', '2018-07-07'),

(18005, 003, 409, '2018-06-05', '2018-08-05', '2018-08-12'),

(18008, 004, 409, '2018-09-05', '2018-09-05', '2018-09-12');









share|improve this question

























  • Why aren't you allowed to use sub-queries?

    – Vlam
    Nov 27 '18 at 1:26











  • My school want us to learn how to do queries without them. I honestly prefer them because they're more readable to me.

    – lassiter
    Nov 27 '18 at 1:34











  • What does it mean "booked one room on separate dates"? Is that "booked the same room", "booked a room for one person", or just "booked one room and not two"?

    – Gordon Linoff
    Nov 27 '18 at 1:42











  • It means, I have hotel room #1 booked from today through tomorrow. I also have hotel room #1 booked next week through next week +1 day.

    – lassiter
    Nov 27 '18 at 1:47











  • Why does your fiddle use MySQL but you tag Postgres here?

    – Parfait
    Nov 27 '18 at 2:15














0












0








0








I have a DB Fiddle as an example: https://www.db-fiddle.com/f/vX7yFVk8j6FPTSm2QVoPNE/2



I'm trying to figure out how to find all bookings for a guest who has booked one room several times on different dates. I can't use a static value/variable like bookings.check_in_date = '2018-03-05', bookings.guest_id = 001 or use subqueries.



I tried:



SELECT bookings.guest_id, bookings.room_number, bookings.check_in_date, bookings.check_out_date
FROM bookings
GROUP BY bookings.guest_id, bookings.room_number, bookings.check_in_date, bookings.check_out_date
HAVING count(distinct bookings.check_in_date) >= 2;


However, it isn't giving me the result I'm looking for but rather I'm looking for this as a result:



guest_id, room_number, check_in_date, check_out_date
1, 203, 2018-03-01T00:00:00.000Z, 2018-03-07T00:00:00.000Z
1, 203, 2018-03-14T00:00:00.000Z, 2018-03-21T00:00:00.000Z


Schema (PostgreSQL v10.0)



CREATE TABLE guests (
id integer,
name text,
address text,
city text,
state text,
postal_code integer,
phone_number numeric(10)
);

CREATE TABLE rooms (
room_number integer,
floor_number integer,
view boolean,
bed_type text
);

CREATE TABLE bookings (
id integer,
guest_id integer,
room_number integer,
order_date date,
check_in_date date,
check_out_date date
);

INSERT INTO guests (id, name, address, city, state, postal_code, phone_number)
VALUES
(1, 'John', '5 8th Avenue', 'New York', 'New York', 10001, 2021110190),
(2, 'Jane', '13 Wall St', 'New York', 'New York', 10001, 1112020190),
(3, 'Jim', '12 Water St', 'New York', 'New York', 10001, 3332224444),
(4, 'Fang', '12 Broadway', 'New York', 'New York', 10001, 1234567890),
(5, 'Bruce', '123 1st Ave', 'New York', 'New York', 10001, 9871112222);

INSERT INTO rooms (room_number, floor_number, view, bed_type)
VALUES
(101, 1, FALSE, 'King'),
(102, 1, FALSE, 'Queens'),
(203, 2, FALSE, 'Queens'),
(204, 2, TRUE, 'King'),
(409, 4, TRUE, 'King');

INSERT INTO bookings (id, guest_id, room_number, order_date, check_in_date, check_out_date)
VALUES
(18001, 001, 203, '2018-03-05', '2018-03-01', '2018-03-07'),
(18002, 001, 203, '2018-03-05', '2018-03-14', '2018-03-21'),
(18003, 001, 409, '2018-03-05', '2018-11-27', '2018-11-30'),
(18004, 001, 204, '2018-03-05', '2018-11-27', '2018-11-30'),

(18007, 002, 409, '2018-05-05', '2018-05-05', '2018-05-12'),
(18002, 002, 203, '2018-03-05', '2018-03-14', '2018-03-21'),
(18006, 002, 409, '2018-06-05', '2018-07-04', '2018-07-07'),

(18005, 003, 409, '2018-06-05', '2018-08-05', '2018-08-12'),

(18008, 004, 409, '2018-09-05', '2018-09-05', '2018-09-12');









share|improve this question
















I have a DB Fiddle as an example: https://www.db-fiddle.com/f/vX7yFVk8j6FPTSm2QVoPNE/2



I'm trying to figure out how to find all bookings for a guest who has booked one room several times on different dates. I can't use a static value/variable like bookings.check_in_date = '2018-03-05', bookings.guest_id = 001 or use subqueries.



I tried:



SELECT bookings.guest_id, bookings.room_number, bookings.check_in_date, bookings.check_out_date
FROM bookings
GROUP BY bookings.guest_id, bookings.room_number, bookings.check_in_date, bookings.check_out_date
HAVING count(distinct bookings.check_in_date) >= 2;


However, it isn't giving me the result I'm looking for but rather I'm looking for this as a result:



guest_id, room_number, check_in_date, check_out_date
1, 203, 2018-03-01T00:00:00.000Z, 2018-03-07T00:00:00.000Z
1, 203, 2018-03-14T00:00:00.000Z, 2018-03-21T00:00:00.000Z


Schema (PostgreSQL v10.0)



CREATE TABLE guests (
id integer,
name text,
address text,
city text,
state text,
postal_code integer,
phone_number numeric(10)
);

CREATE TABLE rooms (
room_number integer,
floor_number integer,
view boolean,
bed_type text
);

CREATE TABLE bookings (
id integer,
guest_id integer,
room_number integer,
order_date date,
check_in_date date,
check_out_date date
);

INSERT INTO guests (id, name, address, city, state, postal_code, phone_number)
VALUES
(1, 'John', '5 8th Avenue', 'New York', 'New York', 10001, 2021110190),
(2, 'Jane', '13 Wall St', 'New York', 'New York', 10001, 1112020190),
(3, 'Jim', '12 Water St', 'New York', 'New York', 10001, 3332224444),
(4, 'Fang', '12 Broadway', 'New York', 'New York', 10001, 1234567890),
(5, 'Bruce', '123 1st Ave', 'New York', 'New York', 10001, 9871112222);

INSERT INTO rooms (room_number, floor_number, view, bed_type)
VALUES
(101, 1, FALSE, 'King'),
(102, 1, FALSE, 'Queens'),
(203, 2, FALSE, 'Queens'),
(204, 2, TRUE, 'King'),
(409, 4, TRUE, 'King');

INSERT INTO bookings (id, guest_id, room_number, order_date, check_in_date, check_out_date)
VALUES
(18001, 001, 203, '2018-03-05', '2018-03-01', '2018-03-07'),
(18002, 001, 203, '2018-03-05', '2018-03-14', '2018-03-21'),
(18003, 001, 409, '2018-03-05', '2018-11-27', '2018-11-30'),
(18004, 001, 204, '2018-03-05', '2018-11-27', '2018-11-30'),

(18007, 002, 409, '2018-05-05', '2018-05-05', '2018-05-12'),
(18002, 002, 203, '2018-03-05', '2018-03-14', '2018-03-21'),
(18006, 002, 409, '2018-06-05', '2018-07-04', '2018-07-07'),

(18005, 003, 409, '2018-06-05', '2018-08-05', '2018-08-12'),

(18008, 004, 409, '2018-09-05', '2018-09-05', '2018-09-12');






sql postgresql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 27 '18 at 2:31







lassiter

















asked Nov 27 '18 at 1:07









lassiterlassiter

738




738













  • Why aren't you allowed to use sub-queries?

    – Vlam
    Nov 27 '18 at 1:26











  • My school want us to learn how to do queries without them. I honestly prefer them because they're more readable to me.

    – lassiter
    Nov 27 '18 at 1:34











  • What does it mean "booked one room on separate dates"? Is that "booked the same room", "booked a room for one person", or just "booked one room and not two"?

    – Gordon Linoff
    Nov 27 '18 at 1:42











  • It means, I have hotel room #1 booked from today through tomorrow. I also have hotel room #1 booked next week through next week +1 day.

    – lassiter
    Nov 27 '18 at 1:47











  • Why does your fiddle use MySQL but you tag Postgres here?

    – Parfait
    Nov 27 '18 at 2:15



















  • Why aren't you allowed to use sub-queries?

    – Vlam
    Nov 27 '18 at 1:26











  • My school want us to learn how to do queries without them. I honestly prefer them because they're more readable to me.

    – lassiter
    Nov 27 '18 at 1:34











  • What does it mean "booked one room on separate dates"? Is that "booked the same room", "booked a room for one person", or just "booked one room and not two"?

    – Gordon Linoff
    Nov 27 '18 at 1:42











  • It means, I have hotel room #1 booked from today through tomorrow. I also have hotel room #1 booked next week through next week +1 day.

    – lassiter
    Nov 27 '18 at 1:47











  • Why does your fiddle use MySQL but you tag Postgres here?

    – Parfait
    Nov 27 '18 at 2:15

















Why aren't you allowed to use sub-queries?

– Vlam
Nov 27 '18 at 1:26





Why aren't you allowed to use sub-queries?

– Vlam
Nov 27 '18 at 1:26













My school want us to learn how to do queries without them. I honestly prefer them because they're more readable to me.

– lassiter
Nov 27 '18 at 1:34





My school want us to learn how to do queries without them. I honestly prefer them because they're more readable to me.

– lassiter
Nov 27 '18 at 1:34













What does it mean "booked one room on separate dates"? Is that "booked the same room", "booked a room for one person", or just "booked one room and not two"?

– Gordon Linoff
Nov 27 '18 at 1:42





What does it mean "booked one room on separate dates"? Is that "booked the same room", "booked a room for one person", or just "booked one room and not two"?

– Gordon Linoff
Nov 27 '18 at 1:42













It means, I have hotel room #1 booked from today through tomorrow. I also have hotel room #1 booked next week through next week +1 day.

– lassiter
Nov 27 '18 at 1:47





It means, I have hotel room #1 booked from today through tomorrow. I also have hotel room #1 booked next week through next week +1 day.

– lassiter
Nov 27 '18 at 1:47













Why does your fiddle use MySQL but you tag Postgres here?

– Parfait
Nov 27 '18 at 2:15





Why does your fiddle use MySQL but you tag Postgres here?

– Parfait
Nov 27 '18 at 2:15












1 Answer
1






active

oldest

votes


















1














Just counting the number of bookings per guest_id should get your the guests who have more than one booking



select a.*
from bookings a
join (
select guest_id,room_number,count(*)
from bookings
group by guest_id,room_number
having count(*)>1
)b
on a.guest_id=b.guest_id
And a.room_ number=b.room_number





share|improve this answer


























  • Yes, but that uses a subquery and returns guests that have a single booking.

    – lassiter
    Nov 27 '18 at 2:09











  • If using Postgres, this derived table subquery can become a CTE.

    – Parfait
    Nov 27 '18 at 2:15











  • Single booking? count(*)>1

    – George Joseph
    Nov 27 '18 at 2:21











  • I had to look up CTE, we haven't been introduced to that. How do you go about doing that approach?

    – lassiter
    Nov 27 '18 at 2:33











  • @GeorgeJoseph I guess I don't understand your query. However, it's not returning the result of: guest_id, room_number, check_in_date, check_out_date 1, 203, 2018-03-01T00:00:00.000Z, 2018-03-07T00:00:00.000Z 1, 203, 2018-03-14T00:00:00.000Z, 2018-03-21T00:00:00.000Z

    – lassiter
    Nov 27 '18 at 2:35














Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53491378%2fhow-to-select-rows-where-one-column-value-is-the-same-on-separate-dates-without%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Just counting the number of bookings per guest_id should get your the guests who have more than one booking



select a.*
from bookings a
join (
select guest_id,room_number,count(*)
from bookings
group by guest_id,room_number
having count(*)>1
)b
on a.guest_id=b.guest_id
And a.room_ number=b.room_number





share|improve this answer


























  • Yes, but that uses a subquery and returns guests that have a single booking.

    – lassiter
    Nov 27 '18 at 2:09











  • If using Postgres, this derived table subquery can become a CTE.

    – Parfait
    Nov 27 '18 at 2:15











  • Single booking? count(*)>1

    – George Joseph
    Nov 27 '18 at 2:21











  • I had to look up CTE, we haven't been introduced to that. How do you go about doing that approach?

    – lassiter
    Nov 27 '18 at 2:33











  • @GeorgeJoseph I guess I don't understand your query. However, it's not returning the result of: guest_id, room_number, check_in_date, check_out_date 1, 203, 2018-03-01T00:00:00.000Z, 2018-03-07T00:00:00.000Z 1, 203, 2018-03-14T00:00:00.000Z, 2018-03-21T00:00:00.000Z

    – lassiter
    Nov 27 '18 at 2:35


















1














Just counting the number of bookings per guest_id should get your the guests who have more than one booking



select a.*
from bookings a
join (
select guest_id,room_number,count(*)
from bookings
group by guest_id,room_number
having count(*)>1
)b
on a.guest_id=b.guest_id
And a.room_ number=b.room_number





share|improve this answer


























  • Yes, but that uses a subquery and returns guests that have a single booking.

    – lassiter
    Nov 27 '18 at 2:09











  • If using Postgres, this derived table subquery can become a CTE.

    – Parfait
    Nov 27 '18 at 2:15











  • Single booking? count(*)>1

    – George Joseph
    Nov 27 '18 at 2:21











  • I had to look up CTE, we haven't been introduced to that. How do you go about doing that approach?

    – lassiter
    Nov 27 '18 at 2:33











  • @GeorgeJoseph I guess I don't understand your query. However, it's not returning the result of: guest_id, room_number, check_in_date, check_out_date 1, 203, 2018-03-01T00:00:00.000Z, 2018-03-07T00:00:00.000Z 1, 203, 2018-03-14T00:00:00.000Z, 2018-03-21T00:00:00.000Z

    – lassiter
    Nov 27 '18 at 2:35
















1












1








1







Just counting the number of bookings per guest_id should get your the guests who have more than one booking



select a.*
from bookings a
join (
select guest_id,room_number,count(*)
from bookings
group by guest_id,room_number
having count(*)>1
)b
on a.guest_id=b.guest_id
And a.room_ number=b.room_number





share|improve this answer















Just counting the number of bookings per guest_id should get your the guests who have more than one booking



select a.*
from bookings a
join (
select guest_id,room_number,count(*)
from bookings
group by guest_id,room_number
having count(*)>1
)b
on a.guest_id=b.guest_id
And a.room_ number=b.room_number






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 27 '18 at 5:11

























answered Nov 27 '18 at 1:57









George JosephGeorge Joseph

1,590510




1,590510













  • Yes, but that uses a subquery and returns guests that have a single booking.

    – lassiter
    Nov 27 '18 at 2:09











  • If using Postgres, this derived table subquery can become a CTE.

    – Parfait
    Nov 27 '18 at 2:15











  • Single booking? count(*)>1

    – George Joseph
    Nov 27 '18 at 2:21











  • I had to look up CTE, we haven't been introduced to that. How do you go about doing that approach?

    – lassiter
    Nov 27 '18 at 2:33











  • @GeorgeJoseph I guess I don't understand your query. However, it's not returning the result of: guest_id, room_number, check_in_date, check_out_date 1, 203, 2018-03-01T00:00:00.000Z, 2018-03-07T00:00:00.000Z 1, 203, 2018-03-14T00:00:00.000Z, 2018-03-21T00:00:00.000Z

    – lassiter
    Nov 27 '18 at 2:35





















  • Yes, but that uses a subquery and returns guests that have a single booking.

    – lassiter
    Nov 27 '18 at 2:09











  • If using Postgres, this derived table subquery can become a CTE.

    – Parfait
    Nov 27 '18 at 2:15











  • Single booking? count(*)>1

    – George Joseph
    Nov 27 '18 at 2:21











  • I had to look up CTE, we haven't been introduced to that. How do you go about doing that approach?

    – lassiter
    Nov 27 '18 at 2:33











  • @GeorgeJoseph I guess I don't understand your query. However, it's not returning the result of: guest_id, room_number, check_in_date, check_out_date 1, 203, 2018-03-01T00:00:00.000Z, 2018-03-07T00:00:00.000Z 1, 203, 2018-03-14T00:00:00.000Z, 2018-03-21T00:00:00.000Z

    – lassiter
    Nov 27 '18 at 2:35



















Yes, but that uses a subquery and returns guests that have a single booking.

– lassiter
Nov 27 '18 at 2:09





Yes, but that uses a subquery and returns guests that have a single booking.

– lassiter
Nov 27 '18 at 2:09













If using Postgres, this derived table subquery can become a CTE.

– Parfait
Nov 27 '18 at 2:15





If using Postgres, this derived table subquery can become a CTE.

– Parfait
Nov 27 '18 at 2:15













Single booking? count(*)>1

– George Joseph
Nov 27 '18 at 2:21





Single booking? count(*)>1

– George Joseph
Nov 27 '18 at 2:21













I had to look up CTE, we haven't been introduced to that. How do you go about doing that approach?

– lassiter
Nov 27 '18 at 2:33





I had to look up CTE, we haven't been introduced to that. How do you go about doing that approach?

– lassiter
Nov 27 '18 at 2:33













@GeorgeJoseph I guess I don't understand your query. However, it's not returning the result of: guest_id, room_number, check_in_date, check_out_date 1, 203, 2018-03-01T00:00:00.000Z, 2018-03-07T00:00:00.000Z 1, 203, 2018-03-14T00:00:00.000Z, 2018-03-21T00:00:00.000Z

– lassiter
Nov 27 '18 at 2:35







@GeorgeJoseph I guess I don't understand your query. However, it's not returning the result of: guest_id, room_number, check_in_date, check_out_date 1, 203, 2018-03-01T00:00:00.000Z, 2018-03-07T00:00:00.000Z 1, 203, 2018-03-14T00:00:00.000Z, 2018-03-21T00:00:00.000Z

– lassiter
Nov 27 '18 at 2:35






















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53491378%2fhow-to-select-rows-where-one-column-value-is-the-same-on-separate-dates-without%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Tonle Sap (See)

I get strange results when I access the Sqlitedatabase with Unity C# via XAMPP

Guatemaltekische Davis-Cup-Mannschaft