This Google data analysis course is killing me. I keep getting errors that I can't seem to solve with the course info. Here is the code and the error.
WITH longest_used_bike AS (
SELECT
bikeid
SUM(duration_minutes) AS trip_duration
FROM
`bigquery-public-data.austin_bikeshare.bikeshare_trips`
GROUP BY
bikeid
ORDER BY
trip_duration DESC
LIMIT 1
)
## find the station at which longest bikeshare ride started
SELECT
trips.start_station_id,
COUNT(*) AS trip_ct
FROM
longest_used_bike AS longest
INNER JOIN
`bigquery-public-data.austin_bikeshare.bikeshare_trips` AS trips
ON longest.bikeid = trips.bikeid
GROUP BY
trips.start_station_id
ORDER BY
trips_ct DESC
LIMIT 1
ERROR
Syntax error: Expected ")" but got "(" at [4:12]
Try putting a comma after the data below:
SELECT
bikeid,
SUM(duration_minutes) AS trip_duration
Hello I have trouble to get distinct count of column.
The result I get right now is total of all not different ones.
This select is as a sub select
select count (distinct tss.Deliverypostadre) from Shipment tss
where tss.DATAAREAID='wh' and tss.PARTITION=1234) as client_count
And if I just go using distinct
distinct s.Deliverypostadre as Client_count
Msg 156, Level 15, State 1, Line 105 Incorrect syntax near the keyword
'distinct'.
can't figure out how to make it work.
Please Check below query
Select ..in (select count(distinct tss.Deliverypostadre) as client_count from Shipment tss
where tss.DATAAREAID='wh' and tss.PARTITION=1234)
Select Code from [dbo].[Codes] where codeId in (select count (distinct Code) as code from [dbo].[Codes])
This should work:
select . . .
(select count(distinct tss.Deliverypostadre)
from Shipment tss
where tss.DATAAREAID = 'wh' and tss.PARTITION = 1234
) as client_count
from . . .
Some databases might have a problem with the space between count and (. but the syntax basically looks correct.
I know this has been asked before but I've looked at other questions and my query still won't work. I have a table with MLB batting stats from last 100 years or so, I am trying to find the playerid, homeruns and percentage of that year's (2012) total homerun's hit that player's hrs make up.
query:
select playerid, hr, hr/sum(hr) over (partition by playerid, yearid)*100 p
from mlbbattingstats
where yearid=2012 and p != 0
order by hr;
error:
Error at line 3:
ORA-00904: "P": invalid identifier
I have tried multiple different aliases and gotten the same error. Any help in what I am doing wrong would be appreciated and sorry if this has been answered previously.
You can't reference a column alias on the same query level (except for order by). You need to wrap the statement into a derived table:
select *
from (
select playerid, hr, hr/sum(hr) over (partition by playerid, yearid)*100 p
from mlbbattingstats
where yearid = 2012
)
where p <> 0
order by hr;
If p <> 0, then hr <> 0. So, your query would seem to be equivalent to:
select playerid, hr,
hr/sum(hr) over (partition by playerid, yearid)*100 as p
from mlbbattingstats
where yearid = 2012 and hr <> 0
order by hr;
Your original problem is that you cannot use a column alias defined in a select in the where clause as the same level.
In a dns log table, trying to use this query to get an avg. number of dns queries within a day:
select to_char(log_time, 'DD-MM-YYYY'),log_client,avg(count(*)) as nums from msint
where to_char(log_time, 'DD-MM-YYYY') = '25-09-2013' and log_client = '10.10.10.1';
and get an error "nested group function without GROUP BY"
but when I add group by log_client,log_time, get another error not a single-group group function
Maybe someone can help me with a solution. Thanks.
Please try below query, check whether the result is expected.
select
log_time,
log_client,
avg(nums) nums
From(
select
to_char(log_time, 'DD-MM-YYYY')log_time,
log_client,
count(*) over(partition by to_char(log_time, 'DD-MM-YYYY'), log_client) as nums
from msint
where
to_char(log_time, 'DD-MM-YYYY') = '25-09-2013' and
log_client = '10.10.10.1'
)x
group by log_time, log_client;
use group by along with having clause.
if possible please share your table structure so that query can be checked against it.
I have the following example code:
create table Details(
name varchar(20),
age int,
weight int,
recordDate Datetime)
--insert data
..query:
SELECT a.name,
a.age,
a.recordDate,
a.weight - (SELECT b.weight
FROM Details
WHERE b.recordDate = dateadd(dd, -1, a.recordDate) as subtable)
FROM Details a
GROUP BY WITH ROLLUP (a.recordDate, a.name, a.age)
I want to see the weight difference between RecordDates for each person and then record total weight different for that person and also for the age group and then grand weight gain/loss. This is not my actual table but just an example.
Problem:
It was complaining about subquery - then I had to use it as table variable: subtable.
Now it is complaining:
Msg 156, Level 15, State 1, Line 14
Incorrect syntax near the keyword 'as'.
Msg 319, Level 15, State 1, Line 18
Incorrect syntax near the keyword 'with'. If this statement is a common table expression or an xmlnamespaces clause, the previous statement must be terminated with a semicolon.
What am I missing?
Typo:
a.weight - (SELECT b.weight
FROM Details
WHERE b.recordDate = dateadd(dd, -1, a.recordDate)
..."b" is being used as a table alias, but it's not actually defined as one.
Next issue is that your GROUP BY doesn't include a.weight, and there's no aggregate function associated with it. Here's my re-write of your query:
SELECT a.name,
a.age,
a.recordDate,
SUM(a.weight - t.weight) 'weight'
FROM DETAILS a
JOIN (SELECT b.recordDate,
b.weight
FROM DETAILS b) t ON t.recordDate = DATEADD(dd, -1, a.recordDate)
GROUP BY (a.recordDate, a.name, a.age) WITH ROLLUP
Try it like this
SELECT
a.name,
a.age,
a.recordDate,
SUM(a.weight - b.weight) as WeightDiff
FROM Details a
JOIN Details b
ON (b.age = a.age
AND b.name = a.name
AND b.recordDate = dateadd(dd, -1, a.recordDate)
)
GROUP BY a.age, a.name, a.recordDate WITH ROLLUP
Don't use AS keyword. You can just directly write {(select * from blah) a}
OK, so the problem is that WITH ROLLUP isn't really the answer you're looking for. This is for creating subtotals not running totals which is what you're after, so using it will give you the total for different combinations of dates rather than a running total, which is what you're after. In the beginning, the query that you want to just get a total that gives you name, age, date and weight loss compared to yesterday is as follows:
select
a.name
,a.age
,a.recordDate
,(SELECT b.weight from Details b WHERE b.recordDate = dateadd(dd,-1,a.recordDate)) - a.weight as weightLossForToday
from details a
Keep in mind that this query will only work if you have exactly 1 record every day. If you have 2 records for a single day or the entries aren't exactly 1 day apart (ie. they include time), then it won't work. In order to get a running total working, you'll need to follow the approach from a question like this.
That should do the trick.
SELECT a.name,a.age,a.recordDate,a.weight-(SELECT b.weight
FROM Details
WHERE b.recordDate=dateadd(dd,-1,a.recordDate))
FROM Details a
GROUP BY (a.recordDate,a.name,a.age)
WITH ROLLUP