I amdealing with a simple query from data imported by GoogleAnalytics Premium.
When running this query I get an error, "table not found MYPROJECTNAME:MYPROPERTYID.ga_sessions_ "
SELECT
trafficSource.source
, trafficSource.medium
, COUNT(*) as VisitCount
FROM (
TABLE_DATA_RANGE([MYPROPERTYID.ga_sessions_], TIMESTAMP('2016-05-01') ,TIMESTAMP('2016-05-08'))
)
GROUP BY trafficSource.source, trafficSource.medium
ORDER BY 2, 3
What could be the issue?
I really appreciate any help.
Roberto
Try as below
SELECT
trafficSource.source
, trafficSource.medium
, COUNT(*) as VisitCount
FROM (
TABLE_DATE_RANGE([MYPROPERTYID.ga_sessions_], TIMESTAMP('2016-05-01') ,TIMESTAMP('2016-05-08'))
)
GROUP BY trafficSource.source, trafficSource.medium
ORDER BY 2, 3
You misspelled TABLE_DATE_RANGE
Related
I am converting legal SQL Query to Standard SQL Query in Bigquery to calculate google analytics bounce rate. But on Converting the query there is some difference in the output result.
Legacy SQL Query
SELECT session_bounceRate,
t1.source as source,t1.medium as medium,
total_session
FROM (
SELECT
IFNULL(t1.session_bounceSessionCount, 0) / t2.session_sessionId_distinct_count AS session_bounceRate,
t1.source,t1.medium,
t2.total_session_distinct_count AS total_session
FROM (
SELECT
INTEGER(session_bounceSession_distinct_count) AS session_bounceSessionCount,
source,
medium
FROM (
SELECT
COUNT(DISTINCT session_sessionId, 10000000) AS session_bounceSession_distinct_count, --Changes done (Count(x) in legacy sql stands for approx ,
source, --,so replaced it with APPROX_COUNT_DISTINCT to get approx count
medium from (
SELECT
SUM(IF(session_hitsType = 'event'
AND session_isInteraction_first = 1, 1, 0)) AS session_isEventInteraction_sum,
session_sessionId AS session_sessionId,
SUM(session_pageViews_sum) AS session_pageViews_sum_sum,
source,
medium
FROM (
SELECT
hits.type AS session_hitsType,
sessionId AS session_sessionId,
SUM(totals.pageviews) AS session_pageViews_sum,
FIRST(hits.isInteraction) AS session_isInteraction_first,
trafficSource.source AS source,
trafficSource.medium AS medium,
FROM
TABLE_DATE_RANGE([[test:test.session_streaming_], TIMESTAMP('2018-04-01'), TIMESTAMP('2018-04-30')) AS session_streaming
GROUP BY
source,
medium,
session_hitsType,
session_sessionId )
GROUP BY
source,
medium,
session_sessionId )
WHERE
(session_isEventInteraction_sum = 0
AND session_pageViews_sum_sum = 1)
GROUP BY
source,
medium ) ) AS t1
JOIN EACH (
SELECT
COUNT(DISTINCT sessionId, 10000000) AS session_sessionId_distinct_count,
trafficSource.source AS source,
trafficSource.medium AS medium,
COUNT(DISTINCT sessionId, 10000000) AS total_session_distinct_count
FROM
TABLE_DATE_RANGE([test:Test.session_streaming_], TIMESTAMP('2018-04-01'), TIMESTAMP('2018-04-30')) AS session_streaming
GROUP BY
source,
medium ) AS t2
ON
t1.source = t2.source
and t1.medium=t2.medium)
where t1.medium='zadv_display'
ORDER BY
total_session DESC
Standard SQL Query
SELECT
IFNULL(t1.session_bounceSessionCount, 0) / t2.session_sessionId_distinct_count AS session_bounceRate,
t1.source,
t1.medium,
t2.total_session_distinct_count AS total_session
FROM (
SELECT
CAST(session_bounceSession_distinct_count AS INT64) AS session_bounceSessionCount,
source,
medium
FROM (
SELECT
APPROX_COUNT_DISTINCT(DISTINCT session_sessionId) AS session_bounceSession_distinct_count,
source,
medium
FROM (
SELECT
SUM(IF(session_hitsType = 'event'
AND session_isInteraction_first = 1, 1, 0)) AS session_isEventInteraction_sum,
session_sessionId AS session_sessionId,
SUM(session_pageViews_sum) AS session_pageViews_sum_sum,
source,
medium
FROM (
SELECT
session_hitsType,
session_sessionId,
source,
medium,
CASE
WHEN session_isInteraction_first = TRUE THEN 1
ELSE 0
END AS session_isInteraction_first,
SUM(session_pageViews_sum) AS session_pageViews_sum
FROM (
SELECT
hits.Type AS session_hitsType,
sessionId AS session_sessionId,
trafficSource.source AS source,
trafficSource.medium AS medium,
totals.pageviews AS session_pageViews_sum,
FIRST_VALUE(hits.isInteraction) OVER(PARTITION BY sessionId ORDER BY TIMESTAMP_SECONDS(hits.Time)) AS session_isInteraction_first
FROM
`test.Test.session_streaming_*`,
unNEST(hits) hits
WHERE
_table_suffix BETWEEN '20180401'
AND '20180430' )
GROUP BY
session_hitsType,
session_sessionId,
source,
medium,
session_isInteraction_first )
GROUP BY
source,
medium,
session_sessionId)
WHERE
(session_isEventInteraction_sum = 0
AND session_pageViews_sum_sum = 1)
GROUP BY
source,
medium)) AS t1
JOIN (
SELECT
APPROX_COUNT_DISTINCT(DISTINCT sessionId) AS session_sessionId_distinct_count,
trafficSource.source AS source,
trafficSource.medium AS medium,
APPROX_COUNT_DISTINCT(DISTINCT sessionId) AS total_session_distinct_count
FROM
`test.Test.session_streaming_*`
WHERE _table_suffix BETWEEN '20180401' AND '20180430'
GROUP BY
source,
medium
) AS t2
ON
t1.source = t2.source
AND t1.medium=t2.medium
where t1.medium='zadv_display'
order by total_session desc
We have replace First function in value with First_value in standard sql that is the noticeable change made in the query.
Could Someone guide me is there some issue on conversion as standard sql query query output should match with legacy output?
I'm not really sure you really need all those nested queries.
But you should make use of sub-queries on arrays - a lot. E.g. the first event interaction information in a session goes like this:
SELECT
date,
visitStartTime,
(SELECT isInteraction FROM t.hits WHERE type='EVENT' ORDER BY hitNumber ASC LIMIT 1) AS isInteraction
FROM
`project.dataset.ga_sessions_20180624` AS t
LIMIT
1000
So simply treat (struct-)arrays like smaller tables within a bigger table.
If you only have aggregations to session level, you shouldn't flatten the table at all.
Here's also a document explaining the migration to standard sql: https://cloud.google.com/bigquery/docs/reference/standard-sql/migrating-from-legacy-sql
I have 2 tables in oracle . users and profile table.
i am running following query to get results
SELECT fu.user_name ,papf.SSN
FROM apps.fnd_user fu, apps.per_all_people_f papf
WHERE fu.employee_id = papf.person_id
AND (TO_CHAR(papf.EFFECTIVE_END_DATE) IS NULL
OR papf.EFFECTIVE_END_DATE > SYSDATE )
Thing is that ,
Profile table has multiple records for SSN.
So this query is returning dupes for SSN.
I just want to get one distinct record for SSN.
for eg result is like
ABC 123
ABC 123
How do i just display unquie values for SSN
Like
ABC 123
U tried distinct but did not work
Any suggestions ?
Proper use of Distinct for your case...
SELECT Distinct fu.user_name , papf.SSN
FROM apps.fnd_user fu
, apps.per_all_people_f papf
WHERE fu.employee_id = papf.person_id
AND (TO_CHAR(papf.EFFECTIVE_END_DATE) IS NULL
OR papf.EFFECTIVE_END_DATE > SYSDATE )
Query using group by can be like below to have a single record:
SELECT fu.user_name ,
papf.SSN
FROM apps.fnd_user fu,
apps.per_all_people_f papf
WHERE fu.employee_id = papf.person_id
AND ( papf.EFFECTIVE_END_DATE IS NULL
OR papf.EFFECTIVE_END_DATE > trunc(SYSDATE) --assuming effective_end_date doesnot have timestamp with it.
)
group by fu.user_name,
papf.SSN ;
I am using MS SQL 2008 R2. One of my table have more than 10 lac rows — 1 lac is 105 or 100,000, so 10 lac is 1,000,000).
I want to bind this to ASP Gridview. I tried custom paging with page size and index. But grid not binded. Timeout Error occured.
Tried directly execute stored procedure, but it takes a long time.
How can I optimize this procedure ?
My procedure
ALTER PROCEDURE SP_LOAN_APPROVAL_GET_LIST
#USERCODE NVARCHAR(50) ,
#FROMDATE DATETIME = NULL ,
#TODATE DATETIME = NULL ,
#PAGESIZE INT ,
#PAGENO INT ,
#TOTALROW BIGINT OUTPUT
AS
BEGIN
SELECT *
FROM ( SELECT DOC_NO ,
DOC_DATE_GRE ,
EMP_CODE ,
EMP_NAME_ENG as Name ,
LOAN_AMOUNT ,
DESC_ENG as Discription ,
REMARKS ,
ROW_NUMBER() OVER(
ORDER BY ( SELECT 1 )
) AS [ROWNO]
from VW_PER_LOAN
Where isnull( POST_FLAG , 'N' ) = 'N'
and ISNULl( CANCEL_FLAG , 'N' ) != 'Y'
and DOC_DATE_GRE between ISNULL(#FROMDATE , DOC_DATE_GRE )
and ISNULL(#TODATE , DOC_DATE_GRE )
and BRANCH in ( SELECT *
FROM DBO.FN_SSP_GetAllowedBranches(#USERCODE)
)
) T
WHERE T.ROWNO BETWEEN ((#PAGENO-1)*#PAGESIZE)+1 AND #PAGESIZE*(#PAGENO)
SELECT #TOTALROW=COUNT(*)
from VW_PER_LOAN
Where isnull(POST_FLAG,'N')= 'N'
and ISNULl(CANCEL_FLAG,'N')!='Y'
and DOC_DATE_GRE between ISNULL(#FROMDATE,DOC_DATE_GRE)and ISNULL(#TODATE,DOC_DATE_GRE)
and BRANCH in ( SELECT *
FROM DBO.FN_SSP_GetAllowedBranches(#USERCODE)
)
END
Thanks
The first thing to do is to look at your execution plan and discuss it with a DBA if you don't understand it.
The obvious thing that stands out is that your where clause has pretty much every column reference wrapped in some sort of function. That makes them expressions and make the SQL optimizer unable to use any covering indices that might exist.
It looks like you are calling a table-valued function as an uncorrelated subquery. That would worry me with respect to performance. I'd probably move that out of the query. Instead run it just once and populate a temporary table.
I am trying to assign 'A' to [Student Details].group based on this SELECT statement.
SELECT TOP (10) PERCENT [Person Id], [Given Names], Surname, Gpa, [Location Cd]
FROM [Student Details]
WHERE ([Location Cd] = 'PAR')
ORDER BY Gpa DESC
I can't figure out how to use a SELECT statement in an UPDATE statement.
Can someone please explain how to accomplish this?
I am using ASP .NET and MsSQL Server if it makes a difference.
Thanks
I'm assuming you want to update these records and then return them :
SELECT TOP (10) PERCENT [Person Id], [Given Names], Surname, Gpa, [Location Cd]
INTO #temp
FROM [Student Details]
WHERE ([Location Cd] = 'PAR')
ORDER BY Gpa DESC
update [Student Details] set group='A' where [person id] in(select [person id] from #temp)
select * from #temp
I'm also assuming person id is the PK of student details
Try this using CTE (Common Table Expression):
;WITH CTE AS
(
SELECT TOP 10 PERCENT [Group]
FROM [Student Details]
WHERE ([Location Cd] = 'PAR')
ORDER BY Gpa DESC
)
UPDATE CTE SET [Group] = 'A'
Is this you want?
Update top (10) Percent [Student Details] set [group] = 'A'
where [Location Cd] = 'PAR' AND [group] is null
Hey guys I hope you can help me I've tried every thing I can think of and it keeps telling me that my syntax near SELECT and that my syntax near AS is incorrect
CREATE PROCEDURE dbo.StoredProcedure2
SELECT
, Announcements.ID
, Announcement.CreateDate
, Announcements.Announcement
,aspnet_Users.UserName
,(SELECT Announcement_Read_State.Read_Date
FROM Announcement_Read_State
WHERE Announcement_Read_State.Announcement_ID = Announcements.ID
AND Announcement_Read_State.User_ID = 2) AS ReadState
FROM Announcements INNER JOIN aspnet_User ON Announcements .Sender_User_ID = aspnet_User.UserName
WHERE (Announcements.ID IN
( SELECT Max(Announcements.ID)
FROM Thread_Participant INNER JOIN Announcements ON
Thread_Participant.ThreadId = Announcements.Announcement_ThreadId
WHERE MessageThreadParticipant.UserID = 2
GROUP BY ThreadParticipant.AnnouncementThreadId
)
ORDER BY Message.CreateDate DESC;
It should be:
CREATE PROCEDURE dbo.StoredProcedure2
AS -- you were missing this
SELECT -- you had an extra comma here
Announcements.ID
, Announcement.CreateDate
, Announcements.Announcement
,aspnet_Users.UserName
,(SELECT Announcement_Read_State.Read_Date
FROM Announcement_Read_State
WHERE Announcement_Read_State.Announcement_ID = Announcements.ID
AND Announcement_Read_State.User_ID = 2) AS ReadState
FROM Announcements INNER JOIN aspnet_User ON Announcements .Sender_User_ID = aspnet_User.UserName
WHERE (Announcements.ID IN
( SELECT Max(Announcements.ID)
FROM Thread_Participant INNER JOIN Announcements ON
Thread_Participant.ThreadId = Announcements.Announcement_ThreadId
WHERE MessageThreadParticipant.UserID = 2
GROUP BY ThreadParticipant.AnnouncementThreadId
)
)-- you were missing this one
ORDER BY Message.CreateDate DESC;