Group by Returns Multiple Rows
up vote
0
down vote
favorite
My query below returns multiple lines instead of aggregating the count. What am I doing wrong.
select case when s.Customer is not null then 'Paid' else 'Free' END as Paid_Free,
Status,case when CompleteDate < '4/1/2018' then '1H' else '2H' end as Busy,
count(*) userCount
from CompletesCur OS
left outer join Sales s on S.Cust = ID
WHERE s.ReturnYear = 2017
and s.MediaType = 'Online'
and os.ReturnYearFiled = 2017
and s.ReturnYear = s.TransactionTaxYear2
and s.ProductGroup in ('Fed','State','Phone Support', 'Import')
GROUP by s.Customer,Status,CompleteDate
Results:
Paid_Free Status Busy Usercount
Paid Returning Yr3+ 2H 2
Paid Returning Yr3+ 2H 1
Paid Returning Yr3+ 1H 1
Paid Returning Yr3+ 1H 1
Paid Returning Yr2+ 2H 2
Paid Returning Yr2+ 2H 2
Results Requested:
Paid_Free Status Busy UserCount
Paid Returning Yr3+ 2H 3
Paid Returning Yr3+ 1H 2
Paid Returning Yr2+ 2H 4
sql
add a comment |
up vote
0
down vote
favorite
My query below returns multiple lines instead of aggregating the count. What am I doing wrong.
select case when s.Customer is not null then 'Paid' else 'Free' END as Paid_Free,
Status,case when CompleteDate < '4/1/2018' then '1H' else '2H' end as Busy,
count(*) userCount
from CompletesCur OS
left outer join Sales s on S.Cust = ID
WHERE s.ReturnYear = 2017
and s.MediaType = 'Online'
and os.ReturnYearFiled = 2017
and s.ReturnYear = s.TransactionTaxYear2
and s.ProductGroup in ('Fed','State','Phone Support', 'Import')
GROUP by s.Customer,Status,CompleteDate
Results:
Paid_Free Status Busy Usercount
Paid Returning Yr3+ 2H 2
Paid Returning Yr3+ 2H 1
Paid Returning Yr3+ 1H 1
Paid Returning Yr3+ 1H 1
Paid Returning Yr2+ 2H 2
Paid Returning Yr2+ 2H 2
Results Requested:
Paid_Free Status Busy UserCount
Paid Returning Yr3+ 2H 3
Paid Returning Yr3+ 1H 2
Paid Returning Yr2+ 2H 4
sql
Could you post some sample data
– Sami
Nov 9 at 15:38
The s table conditions in the WHERE clause makes the LEFT JOIN to return regular INNER JOIN result. Move those conditions to the ON clause to get true LEFT JOIN result. (Or switch to INNER JOIN?)
– jarlh
Nov 9 at 15:41
And I don't thinkCompleteDatedatatype isDATEbecause of'4/1/2018'
– Sami
Nov 9 at 15:42
@sami if completedate is a date sqlserver will convert '4/1/2018' string to a date
– Caius Jard
Nov 9 at 15:45
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
My query below returns multiple lines instead of aggregating the count. What am I doing wrong.
select case when s.Customer is not null then 'Paid' else 'Free' END as Paid_Free,
Status,case when CompleteDate < '4/1/2018' then '1H' else '2H' end as Busy,
count(*) userCount
from CompletesCur OS
left outer join Sales s on S.Cust = ID
WHERE s.ReturnYear = 2017
and s.MediaType = 'Online'
and os.ReturnYearFiled = 2017
and s.ReturnYear = s.TransactionTaxYear2
and s.ProductGroup in ('Fed','State','Phone Support', 'Import')
GROUP by s.Customer,Status,CompleteDate
Results:
Paid_Free Status Busy Usercount
Paid Returning Yr3+ 2H 2
Paid Returning Yr3+ 2H 1
Paid Returning Yr3+ 1H 1
Paid Returning Yr3+ 1H 1
Paid Returning Yr2+ 2H 2
Paid Returning Yr2+ 2H 2
Results Requested:
Paid_Free Status Busy UserCount
Paid Returning Yr3+ 2H 3
Paid Returning Yr3+ 1H 2
Paid Returning Yr2+ 2H 4
sql
My query below returns multiple lines instead of aggregating the count. What am I doing wrong.
select case when s.Customer is not null then 'Paid' else 'Free' END as Paid_Free,
Status,case when CompleteDate < '4/1/2018' then '1H' else '2H' end as Busy,
count(*) userCount
from CompletesCur OS
left outer join Sales s on S.Cust = ID
WHERE s.ReturnYear = 2017
and s.MediaType = 'Online'
and os.ReturnYearFiled = 2017
and s.ReturnYear = s.TransactionTaxYear2
and s.ProductGroup in ('Fed','State','Phone Support', 'Import')
GROUP by s.Customer,Status,CompleteDate
Results:
Paid_Free Status Busy Usercount
Paid Returning Yr3+ 2H 2
Paid Returning Yr3+ 2H 1
Paid Returning Yr3+ 1H 1
Paid Returning Yr3+ 1H 1
Paid Returning Yr2+ 2H 2
Paid Returning Yr2+ 2H 2
Results Requested:
Paid_Free Status Busy UserCount
Paid Returning Yr3+ 2H 3
Paid Returning Yr3+ 1H 2
Paid Returning Yr2+ 2H 4
sql
sql
edited Nov 9 at 15:51
asked Nov 9 at 15:36
Jake Wagner
259315
259315
Could you post some sample data
– Sami
Nov 9 at 15:38
The s table conditions in the WHERE clause makes the LEFT JOIN to return regular INNER JOIN result. Move those conditions to the ON clause to get true LEFT JOIN result. (Or switch to INNER JOIN?)
– jarlh
Nov 9 at 15:41
And I don't thinkCompleteDatedatatype isDATEbecause of'4/1/2018'
– Sami
Nov 9 at 15:42
@sami if completedate is a date sqlserver will convert '4/1/2018' string to a date
– Caius Jard
Nov 9 at 15:45
add a comment |
Could you post some sample data
– Sami
Nov 9 at 15:38
The s table conditions in the WHERE clause makes the LEFT JOIN to return regular INNER JOIN result. Move those conditions to the ON clause to get true LEFT JOIN result. (Or switch to INNER JOIN?)
– jarlh
Nov 9 at 15:41
And I don't thinkCompleteDatedatatype isDATEbecause of'4/1/2018'
– Sami
Nov 9 at 15:42
@sami if completedate is a date sqlserver will convert '4/1/2018' string to a date
– Caius Jard
Nov 9 at 15:45
Could you post some sample data
– Sami
Nov 9 at 15:38
Could you post some sample data
– Sami
Nov 9 at 15:38
The s table conditions in the WHERE clause makes the LEFT JOIN to return regular INNER JOIN result. Move those conditions to the ON clause to get true LEFT JOIN result. (Or switch to INNER JOIN?)
– jarlh
Nov 9 at 15:41
The s table conditions in the WHERE clause makes the LEFT JOIN to return regular INNER JOIN result. Move those conditions to the ON clause to get true LEFT JOIN result. (Or switch to INNER JOIN?)
– jarlh
Nov 9 at 15:41
And I don't think
CompleteDate datatype is DATE because of '4/1/2018'– Sami
Nov 9 at 15:42
And I don't think
CompleteDate datatype is DATE because of '4/1/2018'– Sami
Nov 9 at 15:42
@sami if completedate is a date sqlserver will convert '4/1/2018' string to a date
– Caius Jard
Nov 9 at 15:45
@sami if completedate is a date sqlserver will convert '4/1/2018' string to a date
– Caius Jard
Nov 9 at 15:45
add a comment |
5 Answers
5
active
oldest
votes
up vote
1
down vote
accepted
you can try like below
with cte
(
select case when s.Customer is not null then 'Paid' else 'Free' END as Paid_Free,
Status,case when CompleteDate < '4/1/2018' then '1H' else '2H' end as Busy,
count(*) userCount
from CompletesCur OS
left outer join Sales s on S.Cust = ID
WHERE s.ReturnYear = '2017'
and MediaType = 'Online'
and os.ReturnYearFiled = 2017
and s.ReturnYear = s.TransactionTaxYear2
and s.ProductGroup in ('Fed','State','Phone Support', 'Import')
GROUP by s.Customer,Status,CompleteDate
) select Paid_Free,Status,Busy, sum(userCount) from cte
group by Paid_Free,Busy,Status
add a comment |
up vote
1
down vote
Group by what you're expecting in your output
select case
when s.Customer is not null then 'Paid'
else 'Free'
end as Paid_Free
, Status
, case
when CompleteDate < '4/1/2018' then '1H'
else '2H'
end as Busy
, count(*) userCount
from CompletesCur OS
left outer join Sales s on S.Cust = ID
where s.ReturnYear = '2017'
and MediaType = 'Online'
and os.ReturnYearFiled = 2017
and s.ReturnYear = s.TransactionTaxYear2
and s.ProductGroup in ('Fed', 'State', 'Phone Support', 'Import')
group by case
when s.Customer is not null then 'Paid'
else 'Free'
end
, Status
, case
when CompleteDate < '4/1/2018' then '1H'
else '2H'
end
add a comment |
up vote
1
down vote
You need to group by the non-aggregated expressions in the select (and generally only those). Repeating complex expressions can get cumbersome, which is why I like defining new fields in subqueries or using `apply:
select v.Paid_Free, Status, v.Busy,
count(*) as userCount
from CompletesCur OS left join
Sales s
on S.Cust = ID cross apply
(values ( case when s.Customer is not null then 'Paid' else 'Free' END,
case when CompleteDate < '2018-04-01' then '1H' else '2H' end
)
) v(Paid_Free, Busy)
WHERE s.ReturnYear = '2017' and -- is this really a string?
MediaType = 'Online' and -- what table is this from?
os.ReturnYearFiled = 2017 and
s.ReturnYear = s.TransactionTaxYear2 and
s.ProductGroup in ('Fed', 'State', 'Phone Support', 'Import')
GROUP by v.Paid_Free, Status, v.Busy;
Other notes:
- You should qualify all column names, including
StatusandMediaType. - Is
ReturnYeara number or a string? If a number, do not use quotes for the comparison value. - I changed the date constant to a standard YYYY-MM-DD format.
add a comment |
up vote
0
down vote
You should
group by
case when s.Customer is not null then 'Paid' else 'Free' END,
Status,
case when CompleteDate < '4/1/2018' then '1H' else '2H' end
Otherwise you are still creating groups made of distinct customers and dates.
add a comment |
up vote
0
down vote
What am i doing wrong?
While the other answers explain what to do to fix the problem, you specifically asked what you did wrong and I hope this answer will help you understand that..
Make this change to your select, and you'll see:
select
case when s.Customer is not null then 'Paid' else 'Free' END as Paid_Free,
Status,
case when CompleteDate < '4/1/2018' then '1H' else '2H' end as Busy,
--my additions
s.customer,
CompleteDate,
count(*) userCount
No more duplicate rows! (I didn't squish the duplicates, I added the columns that were actually grouped, so you'll still get all the unexpected rows, but it will be easy to see why they appeared.)
The combination of customer/status/completedate is unique but you transform the data after it has been grouped. The result of transforming this data is that it becomes less precise - there are thousands of dates before 4/1/2018 but you effectively converted it to a boolean value (is-before or is-after). All your rows that were dates of 3/29/2018, 3/28/2018 etc.. and were formerly unique, now all became the same value, hence the apparent duplicates
Hiding info/losing precision in this way, creates duplicates
add a comment |
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',
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53228785%2fgroup-by-returns-multiple-rows%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
you can try like below
with cte
(
select case when s.Customer is not null then 'Paid' else 'Free' END as Paid_Free,
Status,case when CompleteDate < '4/1/2018' then '1H' else '2H' end as Busy,
count(*) userCount
from CompletesCur OS
left outer join Sales s on S.Cust = ID
WHERE s.ReturnYear = '2017'
and MediaType = 'Online'
and os.ReturnYearFiled = 2017
and s.ReturnYear = s.TransactionTaxYear2
and s.ProductGroup in ('Fed','State','Phone Support', 'Import')
GROUP by s.Customer,Status,CompleteDate
) select Paid_Free,Status,Busy, sum(userCount) from cte
group by Paid_Free,Busy,Status
add a comment |
up vote
1
down vote
accepted
you can try like below
with cte
(
select case when s.Customer is not null then 'Paid' else 'Free' END as Paid_Free,
Status,case when CompleteDate < '4/1/2018' then '1H' else '2H' end as Busy,
count(*) userCount
from CompletesCur OS
left outer join Sales s on S.Cust = ID
WHERE s.ReturnYear = '2017'
and MediaType = 'Online'
and os.ReturnYearFiled = 2017
and s.ReturnYear = s.TransactionTaxYear2
and s.ProductGroup in ('Fed','State','Phone Support', 'Import')
GROUP by s.Customer,Status,CompleteDate
) select Paid_Free,Status,Busy, sum(userCount) from cte
group by Paid_Free,Busy,Status
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
you can try like below
with cte
(
select case when s.Customer is not null then 'Paid' else 'Free' END as Paid_Free,
Status,case when CompleteDate < '4/1/2018' then '1H' else '2H' end as Busy,
count(*) userCount
from CompletesCur OS
left outer join Sales s on S.Cust = ID
WHERE s.ReturnYear = '2017'
and MediaType = 'Online'
and os.ReturnYearFiled = 2017
and s.ReturnYear = s.TransactionTaxYear2
and s.ProductGroup in ('Fed','State','Phone Support', 'Import')
GROUP by s.Customer,Status,CompleteDate
) select Paid_Free,Status,Busy, sum(userCount) from cte
group by Paid_Free,Busy,Status
you can try like below
with cte
(
select case when s.Customer is not null then 'Paid' else 'Free' END as Paid_Free,
Status,case when CompleteDate < '4/1/2018' then '1H' else '2H' end as Busy,
count(*) userCount
from CompletesCur OS
left outer join Sales s on S.Cust = ID
WHERE s.ReturnYear = '2017'
and MediaType = 'Online'
and os.ReturnYearFiled = 2017
and s.ReturnYear = s.TransactionTaxYear2
and s.ProductGroup in ('Fed','State','Phone Support', 'Import')
GROUP by s.Customer,Status,CompleteDate
) select Paid_Free,Status,Busy, sum(userCount) from cte
group by Paid_Free,Busy,Status
answered Nov 9 at 15:38
Zaynul Abadin Tuhin
11.1k2831
11.1k2831
add a comment |
add a comment |
up vote
1
down vote
Group by what you're expecting in your output
select case
when s.Customer is not null then 'Paid'
else 'Free'
end as Paid_Free
, Status
, case
when CompleteDate < '4/1/2018' then '1H'
else '2H'
end as Busy
, count(*) userCount
from CompletesCur OS
left outer join Sales s on S.Cust = ID
where s.ReturnYear = '2017'
and MediaType = 'Online'
and os.ReturnYearFiled = 2017
and s.ReturnYear = s.TransactionTaxYear2
and s.ProductGroup in ('Fed', 'State', 'Phone Support', 'Import')
group by case
when s.Customer is not null then 'Paid'
else 'Free'
end
, Status
, case
when CompleteDate < '4/1/2018' then '1H'
else '2H'
end
add a comment |
up vote
1
down vote
Group by what you're expecting in your output
select case
when s.Customer is not null then 'Paid'
else 'Free'
end as Paid_Free
, Status
, case
when CompleteDate < '4/1/2018' then '1H'
else '2H'
end as Busy
, count(*) userCount
from CompletesCur OS
left outer join Sales s on S.Cust = ID
where s.ReturnYear = '2017'
and MediaType = 'Online'
and os.ReturnYearFiled = 2017
and s.ReturnYear = s.TransactionTaxYear2
and s.ProductGroup in ('Fed', 'State', 'Phone Support', 'Import')
group by case
when s.Customer is not null then 'Paid'
else 'Free'
end
, Status
, case
when CompleteDate < '4/1/2018' then '1H'
else '2H'
end
add a comment |
up vote
1
down vote
up vote
1
down vote
Group by what you're expecting in your output
select case
when s.Customer is not null then 'Paid'
else 'Free'
end as Paid_Free
, Status
, case
when CompleteDate < '4/1/2018' then '1H'
else '2H'
end as Busy
, count(*) userCount
from CompletesCur OS
left outer join Sales s on S.Cust = ID
where s.ReturnYear = '2017'
and MediaType = 'Online'
and os.ReturnYearFiled = 2017
and s.ReturnYear = s.TransactionTaxYear2
and s.ProductGroup in ('Fed', 'State', 'Phone Support', 'Import')
group by case
when s.Customer is not null then 'Paid'
else 'Free'
end
, Status
, case
when CompleteDate < '4/1/2018' then '1H'
else '2H'
end
Group by what you're expecting in your output
select case
when s.Customer is not null then 'Paid'
else 'Free'
end as Paid_Free
, Status
, case
when CompleteDate < '4/1/2018' then '1H'
else '2H'
end as Busy
, count(*) userCount
from CompletesCur OS
left outer join Sales s on S.Cust = ID
where s.ReturnYear = '2017'
and MediaType = 'Online'
and os.ReturnYearFiled = 2017
and s.ReturnYear = s.TransactionTaxYear2
and s.ProductGroup in ('Fed', 'State', 'Phone Support', 'Import')
group by case
when s.Customer is not null then 'Paid'
else 'Free'
end
, Status
, case
when CompleteDate < '4/1/2018' then '1H'
else '2H'
end
answered Nov 9 at 15:38
JamieD77
11.9k1718
11.9k1718
add a comment |
add a comment |
up vote
1
down vote
You need to group by the non-aggregated expressions in the select (and generally only those). Repeating complex expressions can get cumbersome, which is why I like defining new fields in subqueries or using `apply:
select v.Paid_Free, Status, v.Busy,
count(*) as userCount
from CompletesCur OS left join
Sales s
on S.Cust = ID cross apply
(values ( case when s.Customer is not null then 'Paid' else 'Free' END,
case when CompleteDate < '2018-04-01' then '1H' else '2H' end
)
) v(Paid_Free, Busy)
WHERE s.ReturnYear = '2017' and -- is this really a string?
MediaType = 'Online' and -- what table is this from?
os.ReturnYearFiled = 2017 and
s.ReturnYear = s.TransactionTaxYear2 and
s.ProductGroup in ('Fed', 'State', 'Phone Support', 'Import')
GROUP by v.Paid_Free, Status, v.Busy;
Other notes:
- You should qualify all column names, including
StatusandMediaType. - Is
ReturnYeara number or a string? If a number, do not use quotes for the comparison value. - I changed the date constant to a standard YYYY-MM-DD format.
add a comment |
up vote
1
down vote
You need to group by the non-aggregated expressions in the select (and generally only those). Repeating complex expressions can get cumbersome, which is why I like defining new fields in subqueries or using `apply:
select v.Paid_Free, Status, v.Busy,
count(*) as userCount
from CompletesCur OS left join
Sales s
on S.Cust = ID cross apply
(values ( case when s.Customer is not null then 'Paid' else 'Free' END,
case when CompleteDate < '2018-04-01' then '1H' else '2H' end
)
) v(Paid_Free, Busy)
WHERE s.ReturnYear = '2017' and -- is this really a string?
MediaType = 'Online' and -- what table is this from?
os.ReturnYearFiled = 2017 and
s.ReturnYear = s.TransactionTaxYear2 and
s.ProductGroup in ('Fed', 'State', 'Phone Support', 'Import')
GROUP by v.Paid_Free, Status, v.Busy;
Other notes:
- You should qualify all column names, including
StatusandMediaType. - Is
ReturnYeara number or a string? If a number, do not use quotes for the comparison value. - I changed the date constant to a standard YYYY-MM-DD format.
add a comment |
up vote
1
down vote
up vote
1
down vote
You need to group by the non-aggregated expressions in the select (and generally only those). Repeating complex expressions can get cumbersome, which is why I like defining new fields in subqueries or using `apply:
select v.Paid_Free, Status, v.Busy,
count(*) as userCount
from CompletesCur OS left join
Sales s
on S.Cust = ID cross apply
(values ( case when s.Customer is not null then 'Paid' else 'Free' END,
case when CompleteDate < '2018-04-01' then '1H' else '2H' end
)
) v(Paid_Free, Busy)
WHERE s.ReturnYear = '2017' and -- is this really a string?
MediaType = 'Online' and -- what table is this from?
os.ReturnYearFiled = 2017 and
s.ReturnYear = s.TransactionTaxYear2 and
s.ProductGroup in ('Fed', 'State', 'Phone Support', 'Import')
GROUP by v.Paid_Free, Status, v.Busy;
Other notes:
- You should qualify all column names, including
StatusandMediaType. - Is
ReturnYeara number or a string? If a number, do not use quotes for the comparison value. - I changed the date constant to a standard YYYY-MM-DD format.
You need to group by the non-aggregated expressions in the select (and generally only those). Repeating complex expressions can get cumbersome, which is why I like defining new fields in subqueries or using `apply:
select v.Paid_Free, Status, v.Busy,
count(*) as userCount
from CompletesCur OS left join
Sales s
on S.Cust = ID cross apply
(values ( case when s.Customer is not null then 'Paid' else 'Free' END,
case when CompleteDate < '2018-04-01' then '1H' else '2H' end
)
) v(Paid_Free, Busy)
WHERE s.ReturnYear = '2017' and -- is this really a string?
MediaType = 'Online' and -- what table is this from?
os.ReturnYearFiled = 2017 and
s.ReturnYear = s.TransactionTaxYear2 and
s.ProductGroup in ('Fed', 'State', 'Phone Support', 'Import')
GROUP by v.Paid_Free, Status, v.Busy;
Other notes:
- You should qualify all column names, including
StatusandMediaType. - Is
ReturnYeara number or a string? If a number, do not use quotes for the comparison value. - I changed the date constant to a standard YYYY-MM-DD format.
answered Nov 9 at 15:40
Gordon Linoff
752k34286395
752k34286395
add a comment |
add a comment |
up vote
0
down vote
You should
group by
case when s.Customer is not null then 'Paid' else 'Free' END,
Status,
case when CompleteDate < '4/1/2018' then '1H' else '2H' end
Otherwise you are still creating groups made of distinct customers and dates.
add a comment |
up vote
0
down vote
You should
group by
case when s.Customer is not null then 'Paid' else 'Free' END,
Status,
case when CompleteDate < '4/1/2018' then '1H' else '2H' end
Otherwise you are still creating groups made of distinct customers and dates.
add a comment |
up vote
0
down vote
up vote
0
down vote
You should
group by
case when s.Customer is not null then 'Paid' else 'Free' END,
Status,
case when CompleteDate < '4/1/2018' then '1H' else '2H' end
Otherwise you are still creating groups made of distinct customers and dates.
You should
group by
case when s.Customer is not null then 'Paid' else 'Free' END,
Status,
case when CompleteDate < '4/1/2018' then '1H' else '2H' end
Otherwise you are still creating groups made of distinct customers and dates.
answered Nov 9 at 15:42
theo_vvv
233
233
add a comment |
add a comment |
up vote
0
down vote
What am i doing wrong?
While the other answers explain what to do to fix the problem, you specifically asked what you did wrong and I hope this answer will help you understand that..
Make this change to your select, and you'll see:
select
case when s.Customer is not null then 'Paid' else 'Free' END as Paid_Free,
Status,
case when CompleteDate < '4/1/2018' then '1H' else '2H' end as Busy,
--my additions
s.customer,
CompleteDate,
count(*) userCount
No more duplicate rows! (I didn't squish the duplicates, I added the columns that were actually grouped, so you'll still get all the unexpected rows, but it will be easy to see why they appeared.)
The combination of customer/status/completedate is unique but you transform the data after it has been grouped. The result of transforming this data is that it becomes less precise - there are thousands of dates before 4/1/2018 but you effectively converted it to a boolean value (is-before or is-after). All your rows that were dates of 3/29/2018, 3/28/2018 etc.. and were formerly unique, now all became the same value, hence the apparent duplicates
Hiding info/losing precision in this way, creates duplicates
add a comment |
up vote
0
down vote
What am i doing wrong?
While the other answers explain what to do to fix the problem, you specifically asked what you did wrong and I hope this answer will help you understand that..
Make this change to your select, and you'll see:
select
case when s.Customer is not null then 'Paid' else 'Free' END as Paid_Free,
Status,
case when CompleteDate < '4/1/2018' then '1H' else '2H' end as Busy,
--my additions
s.customer,
CompleteDate,
count(*) userCount
No more duplicate rows! (I didn't squish the duplicates, I added the columns that were actually grouped, so you'll still get all the unexpected rows, but it will be easy to see why they appeared.)
The combination of customer/status/completedate is unique but you transform the data after it has been grouped. The result of transforming this data is that it becomes less precise - there are thousands of dates before 4/1/2018 but you effectively converted it to a boolean value (is-before or is-after). All your rows that were dates of 3/29/2018, 3/28/2018 etc.. and were formerly unique, now all became the same value, hence the apparent duplicates
Hiding info/losing precision in this way, creates duplicates
add a comment |
up vote
0
down vote
up vote
0
down vote
What am i doing wrong?
While the other answers explain what to do to fix the problem, you specifically asked what you did wrong and I hope this answer will help you understand that..
Make this change to your select, and you'll see:
select
case when s.Customer is not null then 'Paid' else 'Free' END as Paid_Free,
Status,
case when CompleteDate < '4/1/2018' then '1H' else '2H' end as Busy,
--my additions
s.customer,
CompleteDate,
count(*) userCount
No more duplicate rows! (I didn't squish the duplicates, I added the columns that were actually grouped, so you'll still get all the unexpected rows, but it will be easy to see why they appeared.)
The combination of customer/status/completedate is unique but you transform the data after it has been grouped. The result of transforming this data is that it becomes less precise - there are thousands of dates before 4/1/2018 but you effectively converted it to a boolean value (is-before or is-after). All your rows that were dates of 3/29/2018, 3/28/2018 etc.. and were formerly unique, now all became the same value, hence the apparent duplicates
Hiding info/losing precision in this way, creates duplicates
What am i doing wrong?
While the other answers explain what to do to fix the problem, you specifically asked what you did wrong and I hope this answer will help you understand that..
Make this change to your select, and you'll see:
select
case when s.Customer is not null then 'Paid' else 'Free' END as Paid_Free,
Status,
case when CompleteDate < '4/1/2018' then '1H' else '2H' end as Busy,
--my additions
s.customer,
CompleteDate,
count(*) userCount
No more duplicate rows! (I didn't squish the duplicates, I added the columns that were actually grouped, so you'll still get all the unexpected rows, but it will be easy to see why they appeared.)
The combination of customer/status/completedate is unique but you transform the data after it has been grouped. The result of transforming this data is that it becomes less precise - there are thousands of dates before 4/1/2018 but you effectively converted it to a boolean value (is-before or is-after). All your rows that were dates of 3/29/2018, 3/28/2018 etc.. and were formerly unique, now all became the same value, hence the apparent duplicates
Hiding info/losing precision in this way, creates duplicates
edited Nov 9 at 16:01
answered Nov 9 at 15:55
Caius Jard
9,12711136
9,12711136
add a comment |
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53228785%2fgroup-by-returns-multiple-rows%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Could you post some sample data
– Sami
Nov 9 at 15:38
The s table conditions in the WHERE clause makes the LEFT JOIN to return regular INNER JOIN result. Move those conditions to the ON clause to get true LEFT JOIN result. (Or switch to INNER JOIN?)
– jarlh
Nov 9 at 15:41
And I don't think
CompleteDatedatatype isDATEbecause of'4/1/2018'– Sami
Nov 9 at 15:42
@sami if completedate is a date sqlserver will convert '4/1/2018' string to a date
– Caius Jard
Nov 9 at 15:45