SELECT DISTINCT
searchQuery, COUNT(searchQuery) hits2, p2.domainid
FROM
table1. p2 WITH (NOLOCK)
JOIN
[DSAg].[dbo].[454343-32432432] a WITH (NOLOCK) ON a.domain = p2.domain AND p2.ip = a.ip
GROUP BY
p2.domain, a.block_reason_code, p2.searchQuery, a.last_click, searchQuery
HAVING
a.block_reason_code NOT IN ('j', 'a', 'c')
AND p2.searchQuery IS NOT NULL
AND p2.searchQuery != ''
AND a.last_click BETWEEN '1/12/23' AND '1/19/23'
I would like to group by only by searchQuery column but instead i get multiple searchQuery with the same name
aaa bbb 1 5465654
aaa bbb 2 5465654
aaa bbb 3 5465654
aaa bbb 1 5465654
aaa ccc 1 1234
aaa ccc 5 1234
while I want to get
aaa bbb 7 5465654
aaa ccc 6 1234
Related
I have a case table as follows:
caseId
caseType
last_name
first_Name
aCaseStatus
bCaseStatus
created_by
1
P
test
a
0
0
1
2
M
test1
b
1
2
2
3
M
test2
c
1
3
1
aCaseStatus
id
descr
1
aaa
2
bbb
3
ccc
bCaseStatus
id
descr
1
xxx
2
yyy
3
zzz
I have a query like below
select c.case_id caseId, c.last_name lastName, c.first_name firstName,
caseType caseType,
acaseStatus||','||bCaseStatus as caseStatus
from cases c
join aCaseStatus acs
on acs.id = c.aCaseStatus
left outer join bCaseStatus bcs
on bcs.id = c.bCaseStatus
where (c.created_by = 1 ) and ( c.aCaseStatus in (1) or c.bCaseStatus in (0)) and c.caseType='M'
UI has a display like below
caseId
Name
caseStatus
2
b,test1
aaa, yyy
3
c,test2
aaa, zzz
When you look at the UI display caseStatus is a combination of aCaseStatus, bCaseStatus.
Also, the UI table has a search functionality wherein caseStatus is a multi-select dropdown field.
This dropdown has a combination of aCaseStatus and bCaseStatus.
Dropdown is as below:
CaseStatus
aaa
bbb
ccc
xxx
yyy
zzz
Note: aCaseStatus alone is a mandatory field so I have given an inner join, however, bCaseStatus is not so I have given a left outer join.
When the user selects aaa, UI should display:
caseId
Name
caseStatus
2
b,test1
aaa
3
c,test2
aaa
when the user selects aaa, xxx UI should display:
caseId
Name
caseStatus
2
b,test1
aaa, xxx
3
c,test2
aaa
But when I use the above query for aaa selection I get:
caseId
Name
caseStatus
2
b,test1
aaa, xxx
3
c,test2
aaa
I do not want the xxx to be displayed even though caseId has xxx caseStatus. How should I change the query to achieve this? Should I write a union query one for aCaseStatus and the other for bCaseStatus or is there any other way this can be achieved?
Suggestions, please?
I have a test table having below details:
ID Key_COLUMN final_Value
1 aaa 1234
2 bbb 2345
3 bbb NULL
4 ccc 456
5 ccc 145
Desired Output:
--final_value updated from NULL to 2345 based key_column (bbb)
ID Key_COLUMN final_Value
1 aaa 1234
2 bbb 2345
3 bbb 2345
4 ccc 456
5 ccc 145
Identify KEY column having NULL and value and update NULL with the value.
this update requied on huge amount of data
Please assist.
You can use window functions:
select t.*,
coalesce(final_value, max(final_value) over (partition by key_column)) as imputed_final_value
from t;
If you wanted an update -- to actually change the data -- you can use a correlated subquery:
update t
set final_value = (select t2.final_value
from t t2
where t2.key_column = t.key_column and
t2.final_value is not null and
rownum = 1
)
where final_value is null;
TableA
------
IntId EvId Name Phone1
===== ==== ==== ======
100 aaa xxx 11111
101 bbb yyy 22222
102 ccc zzz 33333
103 bbb asd 44444
104 bbb sdf 55555
TableB
------
IntId ASId Grp Phone2
===== ==== ==== ======
201 bbb yyy 6666
202 ccc zzz 7777
203 bbb asd 8888
204 bbb kkf 9999
205 ddd esd 0000
206 eee ffr 1001
I want to join TableA with TableB using TableA.EvId = TableB.ASId to output {IndId, EvId, Name, Phone1, Grp, Phone2} (using outerjoin as I want all records in TableA with matching TableB columns)
I could do it using below query, but it is giving me duplicates since EvId and ASId have duplicates.
SELECT a.IntId, a.EvId, a.Name, a.Phone1, b.Grp, b.Phone2
FROM TableA a
LEFT OUTER JOIN TableB b
ON a.EvId = b.ASId
If EvId are duplicates, consider that record which has max IntId. Same rule for TableB
My final output should be like this - all unique EvId's in TableA using outerjoin on B
IntId EvId Name Phone1 Phone2 Grp
100 aaa xxx 11111 null null
104 bbb sdf 55555 9999 kkf
102 ccc zzz 33333 7777 zzz
Can you please help me with the query?
You can use ROW_NUMBER():
SELECT a.IntId, a.EvId, a.Name, a.Phone1, b.Grp, b.Phone2
FROM TableA a LEFT OUTER JOIN
(SELECT b.*,
ROW_NUMBER() OVER (PARTITION BY ASId ORDER BY IntID DESC) as seqnum
FROM TableB b
) b
ON a.EvId = b.ASId AND b.seqnum = 1;
I have a query where I need to extract data in a regular table and put two rows of data into a single row.
I have rows that consist of
StudentID AUDIT_ACTN Audit_Date .....
aaa A 01/01/2010
aaa A 03/04/2011
aaa A 02/02/2013
aaa D 09/10/2010
aaa D 05/06/2011
aaa D 06/07/2013
aaa A 11/12/2014~
bbb A 01/01/2010
bbb A 03/04/2011
bbb A 02/02/2013
bbb D 09/10/2010
bbb D 05/06/2011
bbb D 06/07/2013
bbb A 11/12/2014~
I want output like this
StudentID AUDIT_ACTN Audit_Date StudentID AUDIT_ACTN Audit_Date
aaa A 01/01/2010 aaa D 09/10/2010
aaa A 03/04/2011 aaa D 05/06/2011
aaa A 02/02/2013 aaa D 06/07/2013
aaa A 11/12/2014 NULL NULL NULL
bbb A 01/01/2010 bbb D 09/10/2010
bbb A 03/04/2011 bbb D 05/06/2011
bbb A 02/02/2013 bbb D 06/07/2013
bbb A 11/12/2014 NULL NULL NULL
The A & D data rows are related, a= add the something to the record and d = delete something from the record (something is an indicator). These is logical in that you must add something before you delete it and you cannot add it twice, without deleting it first.
My current script is probably going down the wrong track but here goes;
select a.StudentId,a.Audit_Date,a.AUDIT_ACTN,d.StudentId,Audit_Date,d.AUDIT_ACTN,
from table a
join
(select *
from
(Select StudentId, Audit_Date,AUDIT_ACTN
from table b
Where b.AUDIT_ACTN='D'
order by Audit_Date
)
where rownum=1
) d on a.StudentId = d.StudentId
and a.AUDIT_ACTN='A'
and Select * from (Select Audit_Date
Order by a.StudentId, a.Audit_Date
I know this is wrong but where do I go from here. If anyone can help and point me in the right direction. It would be appreciated.
My current attempts bring me zero rows, when I take out the rownum it brings me a x join returning 12 rows in this case.
thanks
Roger
I think you need aggregate function like below
select
A.StudentId,A.Audit_Date,A.AUDIT_ACTN,
D.StudentId,D.Audit_Date,D.AUDIT_ACTN
from
(Select StudentId, Audit_Date,AUDIT_ACTN,ROW_NUMBER()
OVER (PARTITION BY StudentId order by audit_date) rn
from table b
Where b.AUDIT_ACTN='D'
) D
FULL OUTER JOIN
(Select StudentId, Audit_Date,AUDIT_ACTN,ROW_NUMBER()
OVER (PARTITION BY StudentId order by audit_date) rn
from table b
Where b.AUDIT_ACTN='A'
) A
on A.rn = D.rn and A.StudentId = B.StudentId
I hope this will work
Suppose I have two tables Table1 and Table2 with the following data.
Column1 Column2 Column3
AAA KKK 9
BBB LLL 7
CCC MMM 9
DDD MMM 5
EEE MMM 7
FFF NNN 9
GGG OOO 1
Column4 Column1
TTT DDD
TTT BBB
UUU EEE
VVV BBB
WWW AAA
WWW BBB
XXX DDD
YYY EEE
YYY DDD
YYY CCC
YYY FFF
The query is of selecting "select value(s) from column4 which matches the tuple result of column1 when column2 has the value 'MMM'('CCC','DDD','EEE') this result should match with all results from column4"
the result is 'YYY'
The error message is
SELECT DISTINCT t2.Column4
FROM Table1 t1
LEFT JOIN Table2 t2 ON t1.Column1 = t2.Column1
WHERE Column2 = 'MMM'
GROUP BY t1.Column2
HAVING COUNT(t1.Column1) = COUNT(t2.Column1)
*
ERROR at line 1:
ORA-00904: "T1"."Column1": invalid identifier
SELECT t2.Column4
FROM Table1 t1
LEFT JOIN Table2 t2 ON t1.Column1 = t2.Column1
WHERE Column2 = 'MMM'
GROUP BY t2.Column4
HAVING COUNT(t1.Column1) = COUNT(t2.Column4)
What about this query?