I am getting an issue with my query written, am I able to use a LOWER and a MIN In the same query?
SELECT DISTINCT
LOWER(id_5) AS "ID"
,MIN(visittime) AS "First Date"
FROM table
WHERE (date >= '2022-11-21')
AND (
server = 'pets.com' AND page = 'pets:dogs:puppy')
MIN is aggregation function which can be used only with aggregation or as window function or scenarios reducible to this like in queries when only one row is expected:
select max(LOWER(id_5)) AS "ID" -- or any other aggregation function
, min(Status) AS "First Date"
from dataset;
which is equivalent to group by true.
Note that DISTINCT works on all values in the select. If you want to find first visit per unique id_5 then just use group by:
SELECT LOWER(id_5) AS "ID"
, MIN(visittime) AS "First Date"
FROM table
WHERE (date >= '2022-11-21')
AND (
server = 'pets.com' AND page = 'pets:dogs:puppy')
group by LOWER(id_5)
Related
I have a question regarding the max-statement in a select -
Without the MAX-statemen i have this select:
SELECT stockID, DATE, close, symbol
FROM ta_stockprice JOIN ta_stock ON ta_stock.id = ta_stockprice.stockID
WHERE stockid = 8648
ORDER BY close
At the end i only want to have the max row for the close-column so i tried:
Why i didnĀ“t get date = "2021-07-02" as output?
(i saw that i allways get "2021-07-01" as output - no matter if i use MAX / MIN / AVG...)
The MAX() turns the query into an aggregation query. With no GROUP BY, it returns one row. But the query is syntactically incorrect, because it mixes aggregated and unaggregated columns.
Once upon a time, MySQL allowed such syntax in violation of the SQL Standard but returned values from arbitrary rows for the unaggreged columns.
Use ORDER BY to do what you want:
SELECT stockID, DATE, close, symbol
FROM ta_stockprice JOIN ta_stock ON ta_stock.id = ta_stockprice.stockID
WHERE stockid = 8648
ORDER BY close DESC
LIMIT 1;
I am trying to use columns that I created in this query to create another column.
Let me first my messy query. The query looks like this:
SELECT tb.team, tb.player, tb.type, tb.date, ToChar(Current Date-1, 'DD-MON-YY') as yesterday,
CASE WHEN to_date(tb.date) = yesterday then 1 else 0 end dateindicator,
FROM (
COUNT DISTINCT(*)
FROM TABLE_A, dual
where dateindicator = 1
Group by tb.team
)
What I am trying to do here is:
creating a column with "Yesterday's date"
Using the "Yesterday" column to create another column called dateindicator indicating each row is yesterday's data or not.
then using that dateindicator, I want to count the distinct number of player for each team that has 1 of the dateindicator column.
But I am getting the "invalid identifier" error. I am new to this oracle SQL, and trying to learn here.
You cannot use an Alias in your Select statement.
see here: SQL: Alias Column Name for Use in CASE Statement
you need to use the full toChar(.. in the CASE WHEN.
Also:
Your WHERE-condition (Line 5) doesnt belong there.. it should be:
SELECT DISTINCT .>. FROM .>. WHERE. you have to specify the table first. then you can filter it with where.
If I follow your explanation correctly: for each team, you want to count the number of players whose date column is yesterday.
If so, you can just filter and aggregate:
select team, count(*) as cnt
from mytable
where mydate >= trunc(sysdate) - 1 and mydate < trunc(sysdate)
group by team
This assumes that the dates are stored in column mydate, that is of date datatype.
I am unsure what you mean by counting distinct players; presumably, a given player appears just once per team, so I used count(*). If you really need to, you can change that to count(distinct player).
Finally: if you want to allow teams where no player matches, you can move the filtering logic within the aggregate function:
select team,
sum(case when mydate >= trunc(sysdate) - 1 and mydate < trunc(sysdate) then 1 else 0 end) as cnt
from mytable
group by team
My DB contains userID in the first column and the timestamp of some action (login or clicking the button). The first record (userID, time) is always user logging in, sometimes there is also second which means user also clicked the button - but that's not always the case. There are never more than 2 rows with the same userID.
"userID" "time"
"0" "1526474096000"
"1" "1526474097000"
"2" "1526474098000"
"3" "1526474099000"
"4" "1526474100000"
"1" "1526474106000"
"0" "1526474107000"
For example, here only users with ID 0 and 1 clicked the button.
I want to calculate histogram (or some other approximation) of time differences because there are too many users to for my RAM to import time difference for every user.
My SQL query looks like this (n is some histogram bin size):
SELECT COUNT(*), (MAX(time)-MIN(time)/n) as time_difference
FROM table_name
GROUP BY userID, time_difference
It doesn't work because "aggregate functions are not allowed in the GROUP BY clause".
Just use a subquery. I'm not totally sure what you are trying to do with that '/n', but this at least resolves your syntax error.
SELECT time_Difference,
count(*)
FROM (SELECT (MAX(time)-MIN(time)/n) as time_difference
FROM table_name
GROUP
BY userID
) TMP
GROUP
BY time_difference;
Are you trying to do this?
SELECT userID, (MAX(time)-MIN(time)/n) as time_difference
FROM table_name
GROUP BY userID
I have a database function that I am able to get the correct results from, but when I try to use SUM to total the results it returns a much higher value.
SELECT
SUM([DATABASE].[dbo].[fn_GetCharges]([TABLE1_DATE],[TABLE1_CUST],[TABLE1_SITE],[TABLE1_SERV])) AS [GROSS_REVENUE]
FROM [DATABASE].[dbo].[TABLE1]
WHERE
[TABLE1_ROUT] = '1234'
AND [TABLE1_DATE] = '2018-05-08'
This returns a SUM value of about 15,740.
If I remove the SUM and GROUP BY the function then it shows each of the returned values, which I manually totalled to about 750.
I'm using Microsoft SQL Server 2014.
We don't know what is in your function, but you could probably output your raw data first and then sum it next.
;WITH cte AS (
SELECT
[DATABASE].[dbo].[fn_GetCharges]([TABLE1_DATE],[TABLE1_CUST],[TABLE1_SITE],[TABLE1_SERV]) AS [GROSS_REVENUE]
FROM [DATABASE].[dbo].[TABLE1]
WHERE
[TABLE1_ROUT] = '1234'
AND [TABLE1_DATE] = '2018-05-08'
GROUP BY whatever_you_grouped_by_that_gave_correct_results
)
SELECT SUM(gross_revenue) AS gross_revenue
FROM cte
Wondering if you used group by clause in the query since I don't see one posted.
Group By (columns) Having (condition). What you specified in the 'where' goes in the 'Having' clause
I have a question about writing query in sql (in continue of my previous question: subtract values of two rows and inserting it into a new column (not subsequent rows)):
I want to write a query that calculate the number of times that a user had won a competition before the current time, the condition of winning is place=1 ; I want the result in a new column (win-frequency) and the value of [win-frequency] changes when a new winning happens. in the following picture, I calculated win-frequency manually.
http://www.8pic.ir/images/54691148512772358477.jpg
I write the following query, but I got error:
SELECT [user-name],
submissions,
[date],
place,
recency,
[win-recency],
COUNT( SELECT [date] FROM [top-design1]] td1
WHERE td1.[user-name] = [top-design1].[user-name]
AND place=1
AND [date]< [top-design1].[date]
ORDER BY [date] DESC) as win-frequency
)
FROM [top-design1]
this is the sql fiddle:
http://sqlfiddle.com/#!3/0ec5f
You have to fix the brackets ) and [, remove the ORDER BY from the correlated subquery, and escape the column name [win-frequency]. It should be this way:
SELECT [user-name],
submissions,
[date],
place,
recency,
[win-recency],
(SELECT COUNT([date])
FROM [top-design1] td1
WHERE td1.[user-name] = [top-design1].[user-name]
AND place = 1
AND [date] < [top-design1].[date]
) as [win-frequency]
FROM [top-design1];
Updated SQL Fiddle Demo.