SQL 2005: How to use GROUP BY with a sub query - sql

The following very simple query
select distinct guid, browser_agent
from tblMyGlossary
where browser_agent is not null
provides the following results:
guid browser_agent
367DE2B8-88A5-4DA9-ACBB-C0864493DC1F Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727)
5DCB918E-DA56-4545-A4E3-D09B1B803422 Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727)
998B8F37-2C9A-49EB-AA0B-CF88C4CC7BDF Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5
A0DD3BCB-E8A9-4434-A869-C343FB21F993 Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727)
Ii want to be able to count the number of unique browser_agent strings, so I am performing the following query:
select browser_agent, count(browser_agent) as 'count'
from
(
select distinct guid, browser_agent
from tblMyGlossary
where browser_agent is not null
)
group by browser_agent
order by 'count' desc;
Problem is SQL 2005 is complaining:
Msg 156, Level 15, State 1, Line 8
Incorrect syntax near the keyword 'group'.
Can anyone shed any light on how to resolve this please? I've run out of ideas.
Many thanks,
Mark

You need to alias your derived table.
select browser_agent, count(browser_agent) as 'count'
from
(
select distinct guid, browser_agent
from tblMyGlossary
where browser_agent is not null
) a
group by browser_agent
order by 'count' desc;

Related

extracting data from unstructured JSON in big query

I have JSON as a string in a big query field:
[{"name":"user_group","value":"regular"},{"name":"checkout_version","value":"2.2"},{"name":"currency","value":"EUR"},{"name":"currency_exchange_rate","value":"1"},{"name":"currency_symbol","value":"€"},{"name":"variant","value":"default"},{"name":"snowplow_id","value":"XXXXXXX"},{"name":"ip_address","value":"XXXX"},{"name":"user_agent","value":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36"},{"name":"is_test_order","value":"false"}]
I'm going crazy trying to extract the value ("default") from this section:
{"name":"variant","value":"default"}
the part I want will always follow "name":"variant","value":" and have a " at the end.
I have tried json_extract but regexp_extract seems the best option, I tried this: (
select REGEXP_EXTRACT(json_string_field, r'/\{"value":"([^"]+)"/') as variant
from source_table
)
but I'm just getting nulls back...would appreciate ideas...
consider below query
WITH json_data AS (
SELECT '[{"name":"user_group","value":"regular"},{"name":"checkout_version","value":"2.2"},{"name":"currency","value":"EUR"},{"name":"currency_exchange_rate","value":"1"},{"name":"currency_symbol","value":"€"},{"name":"variant","value":"default"},{"name":"snowplow_id","value":"XXXXXXX"},{"name":"ip_address","value":"XXXX"},{"name":"user_agent","value":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36"},{"name":"is_test_order","value":"false"}]' json
)
SELECT JSON_VALUE(kv, '$.value') AS value
FROM json_data, UNNEST(JSON_QUERY_ARRAY(json)) kv
WHERE JSON_VALUE(kv, '$.name') = 'variant';

Karate: Is there a way to pass variable as string in scenario outline and examples table [duplicate]

This question already has an answer here:
Passing variables into Examples section [duplicate]
(1 answer)
Closed 1 year ago.
I am using latest karate v1.1.0. My feature looks like this:
Feature: Scenario outline with examples table
Background:
* url 'http://localhost:8080'
* def dummy = Java.type(karatetests.attributes)
* def test = new attributes()
* def userid = test.getuserid()
Scenario Outline: pass userid with string as parameter
Given path '<path>'
And Header Host = 'hostname'
And Header User-Agent = '<Ua-string>'
When method POST
Then status 200
Examples:
| path | Ua-string |
| api | AppleWebKit/537.36 (KHTML, like Gecko, userid)|
In cucumber: I was able to realise the variable value of 'userid' in Ua-string table with AppleWebKit/537.36 (KHTML, like Gecko, ${userid})
In karate: I tried with 'userid', "userid", "#(userid)", and '#(userid)' unfortunately was not succesfull.
Examples:
| path | Ua-string |
| api | AppleWebKit/537.36 (KHTML, like Gecko, userid)| => Result: userid string is passed not its value
| api | AppleWebKit/537.36 (KHTML, like Gecko, 'userid')| => Result: syntax error
| api | AppleWebKit/537.36 (KHTML, like Gecko, "userid")| => Result: "userid" string is passed not its value
| api | AppleWebKit/537.36 (KHTML, like Gecko, "#(userid)")| => Result: "#(userid)" string is passed not its value
| api | AppleWebKit/537.36 (KHTML, like Gecko, '#(userid)')| => Result: '#(userid)' syntax error
How can I replace the userid with its value, while passing it to Ua-string header?
Thanks
Works perfectly for me. Try this example that you can cut and paste into any feature and see that it works for yourself:
Scenario Outline:
* url 'https://httpbin.org/anything'
* header User-Agent = userAgent
* method get
Examples:
| userAgent |
| foo |
| bar |
But I think you are expecting functions and variables to work within Examples: - but sorry, that is not supported: https://stackoverflow.com/a/60358535/143475
EDIT I still don't understand what you mean by "variable" in your comment, but if you are seeing some problems with commas, try this, and refer the docs: https://github.com/intuit/karate#scenario-outline-enhancements
Scenario Outline:
* url 'https://httpbin.org/anything'
* header User-Agent = userAgent
* method get
Examples:
| userAgent! |
| 'foo, bar' |
| 'baz, ban' |
And finally, if you really want to do some "magic" using variables, you can always do that in the scenario body as shown here: https://stackoverflow.com/a/60358535/143475
Or just use string concatenation. Is that so hard ?

SQL Extract from JSON "object"

So I have a requirement to extract some data from JSON store and for the most part its pretty simple with JSONVALUE(). However some of the stores seem to use a deeper storage method.
Some example code;
DECLARE #json NVARCHAR(MAX) = '{
"N.data.-60d8249a-ed12-4f41-98f4-01c910c6b2f4": null,
"R.title": "{\"description\":\"Mr\",\"alternates\":{},\"position\":0}",
"R.gender": "{\"description\":\"Male\",\"alternates\":{},\"position\":1}",
"R.jobTitle": "{\"description\":\"Operations\",\"alternates\":{},\"position\":2}"
}'
What I need to do is extract the "description" aspect from each key. For example, "R.title" would give me "Mr" etc.
Ideally I could extract a specific key each time using a parameter approach:
DECLARE #id nvarchar(200) = 'R.title'
SELECT
*
FROM OPENJSON(#json, concat('$."',#id,'"')) AS a
Is there a correct way to do this without doing nasty substring() methods?
Thanks!
SQL Server version:
Microsoft SQL Server 2017 (RTM) - 14.0.1000.169 (X64) Aug 22 2017
17:04:49 Copyright (C) 2017 Microsoft Corporation Express Edition
(64-bit) on Windows Server 2012 R2 Datacenter 6.3 (Build 9600: )
(Hypervisor)
It's probably a late answer, but I hope it helps. You need to parse the input JSON with two OPENJSON() calls and additional APPLY operator. The first OPENJSON() call uses the default schema to get the names for all keys in the input JSON.
Statement:
DECLARE #json NVARCHAR(MAX) = '{
"N.data.-60d8249a-ed12-4f41-98f4-01c910c6b2f4": null,
"R.title": "{\"description\":\"Mr\",\"alternates\":{},\"position\":0}",
"R.gender": "{\"description\":\"Male\",\"alternates\":{},\"position\":1}",
"R.jobTitle": "{\"description\":\"Operations\",\"alternates\":{},\"position\":2}"
}'
SELECT j.[key], v.description
FROM OPENJSON(#json) j
CROSS APPLY OPENJSON(j.[value]) WITH (description nvarchar(max) '$.description') v
Result:
-----------------------
key description
-----------------------
R.title Mr
R.gender Male
R.jobTitle Operations

Impala AnalysisException: Subqueries are not supported in the HAVING clause

I have a query where I am selecting destination host names where a user agent string matches and grouping by where there is a distinct srchostname using Impala.
select desthostname
from proxy_table
where useragentstring = "Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/538.1 (KHTML, like Gecko) Google Earth Pro/7.3.2.5491 Safari/538.1"
group by desthostname
having count(*) = (select count(distinct srchostname) from proxy_table);
But I am running into the error:
AnalysisException: Subqueries are not supported in the HAVING clause.
Do you know how I can fix this?
Run this:
select desthostname from
(select desthostname,count(*) as cnt
from proxy_table
where useragentstring = "Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/538.1 (KHTML, like Gecko) Google Earth Pro/7.3.2.5491 Safari/538.1"
group by desthostname) A where A.cnt in (select count(distinct srchostname) from proxy_table);

Fetch email address from NVARCHAR2 DATATYPE

I have a table in which there is a column of NVARCHAR2 datatype which holds a string.
The string contains some Email Ids which I require to fetch in a comma separated manner.
Below is the test data --
create table nvarchar2_email (email_reject nvarchar2(1000));
insert into nvarchar2_email values ('com.wm.app.b2b.server.ServiceException: javax.mail.SendFailedException: Invalid Addresses; nested exception is:
com.sun.mail.smtp.SMTPAddressFailedException: 550 5.1.1 <manoj.dalai#gmail.com>: Recipient address rejected: User unknown in virtual alias table;
nested exception is:
com.sun.mail.smtp.SMTPAddressFailedException: 550 5.1.1 <santoshi.k#gmail.com>: Recipient address rejected: User unknown in virtual alias table
nested exception is:
com.sun.mail.smtp.SMTPAddressFailedException: 550 5.1.1 <biswajit-kumar.p#gmail.com>: Recipient address rejected: User unknown in virtual alias table');
insert into nvarchar2_email values ('com.wm.app.b2b.server.ServiceException: javax.mail.SendFailedException: Invalid Addresses; nested exception is:
com.sun.mail.smtp.SMTPAddressFailedException: 550 5.1.1 <manoj.dalai#gmail.com>: Recipient address rejected: User unknown in virtual alias table;
nested exception is:
com.sun.mail.smtp.SMTPAddressFailedException: 550 5.1.1 <santoshi.k#gmail.com>: Recipient address rejected: User unknown in virtual alias table');
I am trying to use the below SQL but it is repeating the Email Ids !!
select email_rejetc, listagg(REGEXP_substr (email_rejetc,'[A-Za-z0-9._%+-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}', 1,level), ',') within group (order by email_rejetc) invalid_email
from nvarchar2_email
connect by level <= REGEXP_count (email_rejetc,'[A-Za-z0-9._%+-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}')
group by EMAIL_REJETC
Here the required output is like
manoj.dalai#gmail.com,santosh.k#gmail.com,biswajit-kumar#gmail.com
Number of emails can VARY in different rows of the table;
My DB is :
Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
select (select listagg (regexp_substr(cast(e.email_reject as varchar2(1000)),'<(.*?#.*?)>',1,level,'',1),',')
within group (order by e.email_reject)
from dual
connect by level <= regexp_count (e.email_reject,'<.*?#.*?>')
) as emails
from nvarchar2_email e
;
P.s.
There seem to be an issue with regexp_substr and nvarchar that causes each character in the result to be preceded by \0.
Tested on Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production
According to your example, it would appear that the e-mail address is always presented as <aaaa#bbbb>, meaning a <, a string with a # in the middle, and a > sign.
You could try something like this (cannot check syntax, so you might need to do some tests):
SUBSTR(<input string> ,
INSTR(<input string>,'<') + 1 ,
(INSTR(<input string>,'>') - INSTR(<input string>,'<') - 2
) ;
This will yield the FIRST e-mail address within the string. You may use the same concept (providing a string without the first section that contains the first e-mail address) in a loop to extract additional addresses within the same string.
I can't see a way to do this through a single "SELECT" statement because each string may have several (and not all string the same number of) addresses.
One option to investigate is to implement a recursive select (Oracle supports this), but it will be much more complex.
Personally, I would go with the approach suggested above.