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
I have the following table in Postgresql:
sch_code sch_level start_date end_date flag
1234 P 01-01-2018 31-01-2018 V
1234 S 01-01-2018 31-01-2018 V
5678 S 01-01-2018 31-01-2018 V
8965 P 01-01-2018 31-01-2018 V
The result which I require is as follows.
sch_code start_P end_P start_S end_S
1234 01-01-2018 31-01-2018 01-01-2018 31-01-2018
5678 00-00-0000 00-00-0000 01-01-2018 31-01-2018
8965 01-01-2018 31-01-2018 00-00-0000 00-00-0000
The queries which i tried with UNION did not provide a result. I have also tried using looped select statements.
You need a FULL OUTER JOIN (see guide here).
And since you can't use a WHERE filter due to potential NULL values, you have to pre-filter the 2 data sets using CTEs (see guide here).
WITH pdata AS (
SELECT * FROM mytable WHERE sch_level='P'
),
sdata AS (
SELECT * FROM mytable WHERE sch_level='S'
)
SELECT
COALESCE(pdata.sch_code, sdata.sch_code) AS sch_code,
pdata.start_date AS start_P,
pdata.end_date AS end_P,
sdata.start_date AS start_S,
pdata.end_date AS end_S
FROM pdata
FULL OUTER JOIN sdata ON pdata.sch_code = sdata.sch_code
If you don't want NULL values in your date fields, simply use COALESCE and provide default values in whichever data type you might be using.
I am wanting to derive a SQL that can bring me the following result, but I just can't seem to get my head around this particular scenario.
I want to see how many individual combination of ROLE-combination by USER there are, and report the count and list out role-names.
Here is the example table:
USER ROLE
AAA Report
AAA Enquiry
AAA Manager
BBB Report
BBB Enquiry
BBB Manager
CCC Enquiry
CCC Report
DDD Report
EEE Report
EEE Enquiry
EEE Admin
FFF Report
FFF Enquiry
GGG Report
GGG Enquiry
GGG Manager
GGG PAYROLL
HHH Report
III Report
III Enquiry
There are AAA, and BBB with role combination of "Report-Enquiry-Manager", therefore count of 2 recored.
There is only CCC with Enquiry-Report.
There are DDD and HHH with role of Report.
Therefore, the desired output would be
COUNT ROLE-COMBINATION
2 Report-Enquiry-Manager
3 Enquiry-Report
2 Report
1 Report-Enquiry-Admin
1 Report-Enquiry-Manager-PAYROLL
Could someone point me to the right direction please.
Thanks heaps,
You can user GROUP BY to get Count and use STUFF to get - separated role values.
create table test
(
[USER] varchar(10)
,[Role] varchar(100)
)
insert into test values
('AAA','Report')
,('AAA','Enquiry')
,('AAA','Manager')
,('BBB','Report')
,('BBB','Enquiry')
,('BBB','Manager')
,('CCC','Enquiry')
,('CCC','Report')
,('DDD','Report')
,('EEE','Report')
,('EEE','Enquiry')
,('EEE','Admin')
,('FFF','Report')
,('FFF','Enquiry')
,('GGG','Report')
,('GGG','Enquiry')
,('GGG','Manager')
,('GGG','PAYROLL')
,('HHH','Report')
,('III','Report')
,('III','Enquiry')
select COunt(1) as [COUNT], [Role-Combination] from
(
select Count(1) as [Count], t.[User]
,STUFF((SELECT
'-' + cm.[role] AS [text()]
FROM
test cm
WHERE
cm.[user] = t.[user]
order by [role] desc
FOR XML PATH('')
, root('user'), TYPE).value('.','varchar(max)'), 1, 1, '' )AS [Role-Combination]
from test t
GROUP BY t.[User]
) result
group by result.[Role-Combination]
DROP TABLE test
DECLARE #USER TABLE
(
xUSER VARCHAR(30),
xROLE VARCHAR(30)
)
INSERT INTO #USER (xUSER,xROLE)
VALUES
('AAA','Report'),
('AAA','Enquiry'),
('AAA','Manager'),
('BBB','Report'),
('BBB','Enquiry'),
('BBB','Manager'),
('CCC','Enquiry'),
('CCC','Report'),
('DDD','Report'),
('EEE','Report'),
('EEE','Enquiry'),
('EEE','Admin'),
('FFF','Report'),
('FFF','Enquiry'),
('GGG','Report'),
('GGG','Enquiry'),
('GGG','Manager'),
('GGG','PAYROLL'),
('HHH','Report'),
('III','Report'),
('III','Enquiry')
; WITH x AS
(
SELECT DISTINCT
xUSER,
STUFF((
SELECT '- ' + xRole
FROM #User
WHERE xUser = a.xUSer
ORDER BY xRole
FOR XML PATH ('')
), 1, 1, '') Groups
FROM #User a
)
SELECT
[Cnt] = COUNT(xUser), Groups as [Role-Cmbine]
FROM x
GROUP BY Groups
ORDER BY [Cnt] DESC
I have an employees table where all employees are located. I need to extract a subset of the employees with their corresponding supervisor. The table looks similar to this:
Emp_id | F_name | L_name | Superv_id | Superv_flg
---------------------------------------------------
123 john doe 456 N
456 jane doe 278 Y
234 Jack smith 268 N
My query looks like this so far:
with cte as
(
select f_name + ' ' l_name as supervisor, superv_id, emp_id
from [dbo].[SAP_worker_all]
where supvr_flag = 'Y'
)
SELECT distinct w.[first_name]
,w.[last_name]
,cte.supervisor
FROM [dbo].[SAP_worker_all] w
join cte
on w.[superv_id] = cte.[superv_id];
I am getting duplicate values and the supervisors returned are not the correct values. What did I do wrong?
if empID is unique you should not have duplicates
SELECT w.*, s.*
FROM [SAP_worker_all] w
JOIN [SAP_worker_all] s
ON s.[Emp_id] = w.[Superv_id]
AND s.[Superv_flg] = 'Y'
I have the two tables.
The first table tbl1:
name nvarchar(255)
number nvarchar(255)
The second table dbo.phone_codes:
country nvarchar(255)
code nvarchar(4)
The first query:
Select Name, Number
from dbo.tbl1
I am getting this result:
User1 375xxxxxxxx
User1 7xxxxxxxxxx
User2 49xxxxxxxxx
The second query:
select country, code
from dbo.phone_codes
I am getting result:
Belarus 375
Russian 7
Germany 49
Poland 48
What should I use the query if I want get this result:
User1 37552222222 Belarus 375
User1 77333333333 Russian 7
User2 49111111111 Germany 49
The first table:
name - nvarchar(255)
number - nvarchar(255)
The second table:
country - nvarchar(255)
code - nvarchar(4)
Try this
SELECT
t.Name, t.Number, p.country, p.code
FROM dbo.tbl1 t
INNER JOIN dbo.phone_codes p
ON t.Number LIKE p.code + '%'
SELECT
t.Name, t.Number, p.Country, p.Cpde
FROM
dbo.tbl1 t, dbo.phone_codes p
WHERE
charindex(p.Code, t.Number) = 1
Assuming your phone number and country code consists only of numbers, no spaces, brackets, dashes or plus sign. You can try something like this:
SELECT *
FROM(
SELECT T.Name ,
T.Number ,
P.country ,
P.code ,
RANK() OVER( PARTITION BY T.Number
ORDER BY ISNULL(CAST(P.code AS int), 1) DESC)RNK
FROM
dbo.tbl1 T LEFT JOIN dbo.phone_codes P
ON T.Number LIKE P.Code + '%'
)A
WHERE A.RNK = 1;
If you have special characters, you need to use replace function to remove any non-numeric characters.
Rank function is used to resolve the cases like Bermuda (1441) and US(1).