DISTINCT with CAST and GROUP BY - sql

I'm trying to get a DISTINCT of the column FeedbackDT but i can't seem to figure why doesn't it work..
SQL Query:
SELECT COUNT(FeedbackID) as FeedbackID,
(SELECT DISTINCT CAST(feedbackDateTime AS DATE)) as FeedbackDT
FROM Feedback
WHERE feedBackDateTime <= GETDATE()
GROUP BY (feedbackDateTime)
The result of the executed query
I searched high and low but to no avail..
Appreciate any help, thanks..

Because your current query doesn't make much sense. When you use GROUP BY, you get the distinct values of the column you are grouping by (or the combination of columns, if you are using more than one). There's no need for the SELECT DISTINCT subquery that you are using.
It seems to me that you need to use a simple GROUP BY:
SELECT CAST(feedbackDateTime AS DATE) FeedbackDT,
COUNT(FeedbackID) as FeedbackID
FROM Feedback
WHERE feedBackDateTime <= GETDATE()
GROUP BY CAST(feedbackDateTime AS DATE)
;

Related

using date of datetime in group by and order by in single SELECT versus using subquery

I (suddenly?) have trouble getting this very simple query to work on Google BigQuery without using a subquery and not sure why.
The data contains a datetime column and I just want to check up on the number of rows per day.
However, it keeps complaining I'm using the datetime column 'which is neither grouped or aggregated'.
SELECT date(datetime_col) as row_date, count(*) as count
FROM table1
GROUP BY date(datetime_col)
ORDER BY count DESC
Without the ORDER BY it works just fine. When I add the ORDER BY it suddenly complains the
'SELECT list expression references column 'datetime_col' which is
neither grouped nor aggregated'
If I remove the count and group by and order by on the date then it does work.
Now if I use a subquery to do the date casting in there it does work:
SELECT row_date, count(row_date) as count FROM
(SELECT date(datetime_col) as row_date FROM table1)
GROUP BY row_date
ORDER BY count DESC
So I'm wondering what is going on why the first single select query is not working and if that can be fixed without using the subquery?
Try putting row_date into GROUP BY:
SELECT date(datetime_col) as row_date, count(*) as count
FROM table1
GROUP BY row_date
ORDER BY count DESC

T-SQL How can I get the most common value from a result set

Trying to get a single result for the most common date in a result set :
SELECT col1, col2,
(SELECT MIN(CONVERT(VARCHAR,[date], 103)) FROM TABLE ) AS [Date]
FROM TABLE WHERE [Date] BETWEEN '20160101' AND '20160131'
Results :
So I just want 15/01/2016. I know I need to use a subquery and a count for this and have tried many different solutions, being a newbie I'm finding subqueries harder to get my head around than JOINs especially correlated nested subqueries.
Group by date, order by count descending and select top 1:
select top 1 with ties [date]
from table
where [date] between '20160101' and '20160131'
group by [date]
order by count(*) desc
with ties will select multiple dates if max count can be the same for some dates.

SQL - Can I have a Group By clause after a nestled Select?

For example:
Select max(date)
From table A
Where max(date) < any (select..
...)
Group By Book_Name,Client_Name
So the max(date) field could be compared to the Nestled Select return, as if the grouping of the greater Select was already made.
What you want is typically done with the HAVING clause.
Select Book_Name,Client_Name, max(date)
From table A
Group By Book_Name,Client_Name
HAVING max(date) < any (select..
...)
I removed reference to the other answer. I don't think it was correct and doesn't really help because I think HAVING is what you need.

SQL 2012 locating duplicate column entries in a table

I am using SQL 2012 and trying to identify rows where the SourceDataID column has two unique entries in the PartyCode column, and I'm having difficulties.
SELECT PartyCode, SourceDataID, count (*) as CNT
FROM CustomerOrderLocation (nolock)
GROUP BY PartyCode, SourceDataID
HAVING (Count(PartyCode)>1)
ORDER BY PartyCode
Results are returning as such:
W3333 948_O 31
(party code/sourcedataid/CNT)
This is showing me the total entries where the Partycode and the SourceDataID are listed together in the table. However, I need it to show a count of any instances where W333 lists 948_O as the SourceDataID more than once.
I'm not having luck structuring the query to pull the results I am looking to get. How can I do this?
A CTE coupled with the PARTITION BY function is helpful in finding duplicates of this manner. Code below:
WITH CTE AS(
SELECT PartyCode, SourceDataID,
ROW_NUMBER()OVER(PARTITION BY SourceDataID ORDER BY SourceDataID) RN
FROM CustomerOrderLocation (NOLOCK))
SELECT * FROM CTE WHERE RN > 1
This should return every duplicate PartyCode attached to a SourceDataID.
If you want to see the entire result, change the last SELECT statement to:
SELECT * FROM CTE ORDER BY PartyCode, RN
Thanks for the help everyone. I did not do the best job of describing the issue but this is the query I ended up creating to get my result set.
;with cte1 (sourcedataid, partycode) as (select sourcedataid, partycode from customerorderparty (nolock) group by PartyCode, SourceDataID)
select count(sourcedataid), sourcedataid from cte1 group by sourcedataid having count(sourcedataid) >1

Order by not working in Oracle subquery

I'm trying to return 7 events from a table, from todays date, and have them in date order:
SELECT ID
FROM table
where ID in (select ID from table
where DATEFIELD >= trunc(sysdate)
order by DATEFIELD ASC)
and rownum <= 7
If I remove the 'order by' it returns the IDs just fine and the query works, but it's not in the right order. Would appreciate any help with this since I can't seem to figure out what I'm doing wrong!
(edit) for clarification, I was using this before, and the order returned was really out:
select ID
from TABLE
where DATEFIELD >= trunc(sysdate)
and rownum <= 7
order by DATEFIELD
Thanks
The values for the ROWNUM "function" are applied before the ORDER BY is processed. That why it doesn't work the way you used it (See the manual for a similar explanation)
When limiting a query using ROWNUM and an ORDER BY is involved, the ordering must be done in an inner select and the limit must be applied in the outer select:
select *
from (
select *
from table
where datefield >= trunc(sysdate)
order by datefield ASC
)
where rownum <= 7
You cannot use order by in where id in (select id from ...) kind of subquery. It wouldn't make sense anyway. This condition only checks if id is in subquery. If it affects the order of output, it's only incidental. With different data query execution plan might be different and output order would be different as well. Use explicit order by at the end of the main query.
It is well known 'feature' of Oracle that rownum doesn't play nice with order by. See http://www.adp-gmbh.ch/ora/sql/examples/first_rows.html for more information. In your case you should use something like:
SELECT ID
FROM (select ID, row_number() over (order by DATEFIELD ) r
from table
where DATEFIELD >= trunc(sysdate))
WHERE r <= 7
See also:
http://www.orafaq.com/faq/how_does_one_select_the_top_n_rows_from_a_table
http://www.oracle.com/technetwork/issue-archive/2006/06-sep/o56asktom-086197.html
http://asktom.oracle.com/pls/asktom/f?p=100:11:507524690399301::::P11_QUESTION_ID:127412348064
See also other similar questions on SO, eg.:
Oracle SELECT TOP 10 records
Oracle/SQL - Select specified range of sequential records
Your outer query cant "see" the ORDER in the inner query and in this case the order in the inner doesn't make sense because it (the inner) is only being used to create a subset of data that will be used on the WHERE of the outer one, so the order of this subset doesn't matter.
maybe if you explain better what you want to do, we can help you
ORDER BY CLAUSE IN Subqueries:
the order by clause is not allowed inside a subquery, with the exception of the inline views. If attempt to include an ORDER BY clause, you receive an error message
An inline View is a query at the from clause.
SELECT t.*
FROM (SELECT id, name FROM student) t