I have a FULLVISITORID in my table with 2 different visitid.
Each visitid has multiple hits. I want to select the visitid where one hits.page.pagepath = 'somepage'
and another hits.eventInfo.eventCategory = 'some event'. Both the conditions should be happening for the same visitid.
For Example:
Select * from my_table where FullVisitorid = '1'
FullVisitorid Visitid ........... hits.page.pagepath .....hits.event.eventCategory
1 123 A abc
B cde
c efg
1 147 somePage ggg
D fff
E SomeEvent
I want the result to be VistiID = 147 becuase the visitid has both pagepath = 'somepage' and eventcategory = 'someevent'
Thanks for your help!
Using CTEs, you can use simple join logic to get your results.
with unnested as (
-- Get the fields you care about
select FullVisitorid, Visitid, h.page.pagepath, h.event.eventCategory
from `dataset.table`
left join unnest(hits) h
),
somepage as (
-- Get somepage hit Visitids
select FullVisitorid, Visitid
from unnested
where pagepath = 'somepage'
group by 1,2
),
someevent as (
-- Get someevent hit Visitids
select FulVisitorid, Visitid
from unnested
where eventCategory = 'someevent'
group by 1,2
),
joined as (
-- Join the CTEs to get common Visitids
select FulVisitorid, Visitid
from somepage
inner join someevent using(FullVisitorid, Visitid)
)
select * from joined
Related
I am trying to get the max date by account from 3 different tables and view those dates side by side. I created a separate query for each table, merged the results with UNION ALL, and then wrapped all that in a PIVOT.
The first 2 sections in the link/pic below show what I have been able to accomplish and the 3rd section is what I would like to do.
Query results by step
How can I get the results from 2 of the tables to repeat? Is that possible?
--define var_ent_type = 'ACOM'
--define var_ent_id = '52766'
--define var_dict_id = 113
SELECT
*
FROM
(
SELECT
E.ENTITY_TYPE,
E.ENTITY_ID,
'PERF_SUMMARY' as "TableName",
PS.DICTIONARY_ID,
to_char(MAX(PS.END_EFFECTIVE_DATE), 'YYYY-MM-DD') as "MaxDate"
FROM
RULESDBO.ENTITY E
INNER JOIN PERFORMDBO.PERF_SUMMARY PS ON (PS.ENTITY_ID = E.ENTITY_ID)
WHERE
1=1
-- AND E.ENTITY_TYPE = '&var_ent_type'
-- AND E.ENTITY_ID = '&var_ent_id'
AND PS.DICTIONARY_ID >= 100
AND (E.ACTIVE_STATUS <> 'N' )--and E.TERMINATION_DATE is null )
GROUP BY
E.ENTITY_TYPE,
E.ENTITY_ID,
'PERF_SUMMARY',
PS.DICTIONARY_ID
union all
SELECT
E.ENTITY_TYPE,
E.ENTITY_ID,
'POSITION' as "TableName",
0 as DICTIONARY_ID,
to_char(MAX(H.EFFECTIVE_DATE), 'YYYY-MM-DD') as "MaxDate"
FROM
RULESDBO.ENTITY E
INNER JOIN HOLDINGDBO.POSITION H ON (H.ENTITY_ID = E.ENTITY_ID)
WHERE
1=1
-- AND E.ENTITY_TYPE = '&var_ent_type'
-- AND E.ENTITY_ID = '&var_ent_id'
AND (E.ACTIVE_STATUS <> 'N' )--and E.TERMINATION_DATE is null )
GROUP BY
E.ENTITY_TYPE,
E.ENTITY_ID,
'POSITION',
1
union all
SELECT
E.ENTITY_TYPE,
E.ENTITY_ID,
'CASH_ACTIVITY' as "TableName",
0 as DICTIONARY_ID,
to_char(MAX(C.EFFECTIVE_DATE), 'YYYY-MM-DD') as "MaxDate"
FROM
RULESDBO.ENTITY E
INNER JOIN CASHDBO.CASH_ACTIVITY C ON (C.ENTITY_ID = E.ENTITY_ID)
WHERE
1=1
-- AND E.ENTITY_TYPE = '&var_ent_type'
-- AND E.ENTITY_ID = '&var_ent_id'
AND (E.ACTIVE_STATUS <> 'N' )--and E.TERMINATION_DATE is null )
GROUP BY
E.ENTITY_TYPE,
E.ENTITY_ID,
'CASH_ACTIVITY',
1
--ORDER BY
-- 2,3, 4
)
PIVOT
(
MAX("MaxDate")
FOR "TableName"
IN ('CASH_ACTIVITY', 'PERF_SUMMARY','POSITION')
)
Everything is possible. You only need a window function to make the value repeat across rows w/o data.
--Assuming current query is QC
With QC as (
...
)
select code, account, grouping,
--cash,
first_value(cash) over (partition by code, account order by grouping asc rows unbounded preceding) as cash_repeat,
perf,
--pos,
first_value(pos) over (partition by code, account order by grouping asc rows unbounded preceding) as pos_repeat
from QC
;
See first_value() help here: https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/FIRST_VALUE.html#GUID-D454EC3F-370C-4C64-9B11-33FCB10D95EC
I have Car table. Car has is_sold and is_shipped. A Car belongs to a dealership, dealership_id (FK).
I want to run a query that tells me the count of sold cars and the count of shipped cars for a given dealership all in one result.
sold_count | shipped_count
10 | 4
The single queries I have look like this:
select count(*) as sold_count
from car
where dealership_id=25 and is_sold=true;
and
select count(*) as shipped_count
from car
where dealership_id=25 and is_shipped=true;
How do I combine the two to get both counts in one result?
This will do:
select dealership_id,
sum(case when is_sold is true then 1 else 0 end),
sum(case when is_shipped is true then 1 else 0 end)
from cars group by dealership_id;
You can use the filter clause of the Aggregate function. (see demo)
select dealership_id
, count(*) filter (where is_sold) cars_sold
, count(*) filter (where is_shipped) cars_shipped
from cars
where dealership_id = 25
group by dealership_id;
You can also using cross join.
select 'hello' as col1, 'world' as col2;
return:
col1 | col2
-------+-------
hello | world
(1 row)
similarly,
with a as
(
select count(*) as a1 from emp where empid> 5),
b as (
select count(*) as a2 from emp where salary > 6000)
select * from a, b;
or you can even apply to different table. like:
with a as
(select count(*) as a1 from emp where empid> 5),
b as
(select count(*) as a2 from ab )
select * from a, b;
with a as
(
select count(*) as sold_count
from car
where dealership_id=25 and is_sold=true
),
b as
(
select count(*) as shipped_count
from car
where dealership_id=25 and is_shipped=true
)
select a,b;
further reading: https://www.postgresql.org/docs/current/queries-table-expressions.html.
https://stackoverflow.com/a/26369295/15603477
Hi I have a CTE with 5 inner joins and a where clause which is reducing by one.
the sample code looks like below. but the actual code has more complex logic
;With CTE_EG AS
(
select *,
-1 as offset from a
inner join a1 on a1.id=a.id
inner join a2 on a1.id=a2.id
inner join a3 on a1.id=a3.id
where a1.offset = a2.quarter-1
union all
select *,
-2 as offset from a
inner join a1 on a1.id=a.id
inner join a2 on a1.id=a2.id
inner join a3 on a1.id=a3.id
where a1.offset = a2.quarter-2
union all
...
)
this repeats till offset -4 and a1.offset = a2.quarter-4.
How can I avoid the same code to be repeated for so many times for only one where clause value. the actualy query has 5 inner joins and total 5 union all.
I can not remove the union all because that will generate in some calculation discrepancy.
I want something like when we pass an integer value n , the selects in between union all should repeat with the changing where clause like a1.offset = a2.quarter-2 to a1.offset = a2.quarter-n
Please suggest
This should just be:
;With Numbers(n) as (
select 1 union all select 2 union all
select 3 union all select 4
), CTE_EG AS
(
select *,
-n as offset from a
inner join a1 on a1.id=a.id
inner join a2 on a1.id=a2.id
inner join a3 on a1.id=a3.id
inner join numbers n on a1.offset = a2.quarter-n
)
I don't understand your point about not being able to remove the UNION ALL.
I need to update a field with concatenated results from a T-SQL query that uses an INNER JOIN and a LEFT JOIN. I was able to do this with the STUFF and FOR XML PATH functions with a simpler query, but my efforts at doing the same process with a more elaborate query have not been successful.
Here is the query that gives me the results I need with the ID field going to end up as the grouping and the Step field will be the one where the concatenated values need to be in the one field per one ID.
SELECT sc.ID, sc.STEP
FROM Table1 As sc
INNER JOIN Table2 As f
ON sc.STEP = f.Step AND sc.STEP_TYPE = f.StepType AND
sc.OldStep = f.OldStep
LEFT JOIN Table3 As l
ON sc.ID = l.ID
WHERE f.Group = l.Group AND sc.CompDate IS NULL
That will give me my results broken down into multiple fields per ID
•ID-----STEP
01 - 101
01 - 102
01 - 103
02 - 107
02 - 113
And what I need is:
•ID-----STEP
01 - 101, 102, 103
02 - 107, 113
Here is what i've tried so far:
;With OA As
( SELECT s.ID, STUFF((
SELECT ', ' + sc.STEP
FROM Table1 As sc
WHERE sc.ID = s.ID
ORDER BY sc.ID
FOR XML PATH('')),1,1,'') As Steps
FROM Table1 As s
INNER JOIN Table2 As f
ON s.STEP = f.Step AND s.STEP_TYPE = f.StepType
AND s.OldStep = f.OldStep
LEFT JOIN Table3 As l
ON s.ID = l.ID
WHERE f.Group = l.Group AND s.CompDate IS NULL
GROUP BY s.ID
)
SELECT * FROM OpenAuditSteps
The problem here is that I am getting a concatenation of all the reocrds, not just the ones grouped on the individual ID's. I've tried various ways of arranging the joins, but nothing has worked so far.
You are very nearly there: You already have your first query working. Assuming the results of that go into #Table1 then
SELECT Distinct
sc1.ID,
STUFF (( Select ',' + sc2.STEP
FROM #Table1 AS SC2
WHERE sc2.ID = sc1.ID
FOR XML PATH('')),1,1,'') AS STEPS
FROM #Table1 AS SC1
ORDER BY sc1.ID
So to combine it into one single query using WITH try this:
;WITH IDSteps AS (
SELECT sc.ID, sc.STEP
FROM Table1 As sc
INNER JOIN Table2 As f
ON sc.STEP = f.Step AND sc.STEP_TYPE = f.StepType AND
sc.OldStep = f.OldStep
LEFT JOIN Table3 As l
ON sc.ID = l.ID
WHERE f.Group = l.Group AND sc.CompDate IS NULL
)
SELECT Distinct
sc1.ID,
STUFF (( Select ',' + sc2.STEP
FROM IDSteps AS SC2
WHERE sc2.ID = sc1.ID
FOR XML PATH('')),1,1,'') AS STEPS
FROM IDSteps AS SC1
ORDER BY sc1.ID;
My table is like
ID FName LName Date(mm/dd/yy) Sequence Value
101 A B 1/10/2010 1 10
101 A B 1/10/2010 2 20
101 X Y 1/2/2010 1 15
101 Z X 1/3/2010 5 10
102 A B 1/10/2010 2 10
102 X Y 1/2/2010 1 15
102 Z X 1/3/2010 5 10
I need a query that should return 2 records
101 A B 1/10/2010 2 20
102 A B 1/10/2010 2 10
that is max of date and max of sequence group by id.
Could anyone assist on this.
-----------------------
-- get me my rows...
-----------------------
select * from myTable t
-----------------------
-- limiting them...
-----------------------
inner join
----------------------------------
-- ...by joining to a subselection
----------------------------------
(select m.id, m.date, max(m.sequence) as max_seq from myTable m inner join
----------------------------------------------------
-- first group on id and date to get max-date-per-id
----------------------------------------------------
(select id, max(date) as date from myTable group by id) y
on m.id = y.id and m.date = y.date
group by id) x
on t.id = x.id
and t.sequence = x.max_seq
Would be a simple solution, which does not take account of ties, nor of rows where sequence is NULL.
EDIT: I've added an extra group to first select max-date-per-id, and then join on this to get max-sequence-per-max-date-per-id before joining to the main table to get all columns.
I have considered your table name as employee..
check the below thing helped you.
select * from employee emp1
join (select Id, max(Date) as dat, max(sequence) as seq from employee group by id) emp2
on emp1.id = emp2.id and emp1.sequence = emp2.seq and emp1.date = emp2.dat
I'm a fan of using the WITH clause in SELECT statements to organize the different steps. I find that it makes the code easier to read.
WITH max_date(max_date)
AS (
SELECT MAX(Date)
FROM my_table
),
max_seq(max_seq)
AS (
SELECT MAX(Sequence)
FROM my_table
WHERE Date = (SELECT md.max_date FROM max_date md)
)
SELECT *
FROM my_table
WHERE Date = (SELECT md.max_date FROM max_date md)
AND Sequence = (SELECT ms.max_seq FROM max_seq ms);
You should be able to optimize this further as needed.