Group By then concat items with commas - azure-stream-analytics

Using event hubs, I'm pushing my data to azure stream analytics. SearchWords, ItemId, UserId are some my data property.
What I want to do is, grouping my searchwords and concat the items. For example:
Data:
SearchWords ItemId UserID
wordA - 100 - 1
wordA - 102 - 1
wordC - 103 - 2
wordD - 102 - 3
wordA - 105 - 5
Output:
wordA - 100,102,105
wordC - 103
wordD - 102
How can i do this in stream analytics query?

You can use user defined aggregates to do this.
https://learn.microsoft.com/en-us/azure/stream-analytics/stream-analytics-javascript-user-defined-aggregates

Related

How to form Management Hierarchy Formation in SQL Server?

how to form the management hierarchy for below records?
Input Data:
Id
Sub ID
Name
Description
101
NULL
Page Reference
Page Reference
102
1
Page 1
Page 1
103
2
Ashok
Ashok
104
3
Kumar
Kumar
105
4
Page 2
Page 2
106
5
Arvind
Arvind
107
4
Page 11
Page 11
108
6
Gova
Gova
109
7
Gokul
Gokul
110
8
Kannan
Kannan
I tried with Recursion CTE, I am not able to find exact solution. Need an below format,
Conditions are
New Leaf ID --> If Sub ID IS NULL , then it will be 1 , If contains page row, it will be 2, if contains other than that it will be 3.
Page --> Whenever Page row starts, from the next row original information showing for that page. we need to form based on the lead rows.
Output Data:
Id
Sub ID
Name
Page
New Leaf ID
101
NULL
Page Reference
1
102
1
Page 1
Page 1
2
103
2
Ashok
Page 1
3
104
3
Kumar
Page 1
3
107
4
Page 11
Page 11
2
108
6
Gova
Page 11
3
109
7
Gokul
Page 11
3
110
8
Kannan
Page 11
3
105
4
Page 2
Page 2
2
106
5
Arvind
Page 2
3
The new leaf id is conditional logic. As for the page, it is a bit trickier: it seems like the ordering of the rows defines the dependencies, so basically you want to relate each level 3 leaf to the preceding level 2.
Here is an approach using a window count to identify leafs that belong to the same page:
select id, subid, name,
case when sub_id is not null
then max(case when name like 'Page %' then name end) over(order by id)
end as page,
case
when subid is null then 1
when name like 'Page %' then 2
else 3
end as new_leaf_id
from (
select t.*,
sum(case when name like 'Page %' then 1 else 0 end) over(order by id) grp
from mytable t
) t

Pentaho Data Integration (PDI) lookup for latest record

Say my lookup table looks something like
Table_1
Key Id incremental _count date
1 1 1 2015-05-20
2 1 2 2015-05-20
3 1 4 2015-05-22
4 2 1 2015-05-22
5 1 6 2015-05-22
For each Id how do I limit PDI lookup to return only the most recent record?
OUTPUT
Key Id incremental _count date
4 2 1 2015-05-22
5 1 6 2015-05-22
it should work with this transformation setup:
1) Sort Rows Step:
sort by (ascending): Id -> Inc -> Date
2) 1. Group by Step:
Group field:
date
id
Aggregates:
Subject: key - Type: last value
Subject: inc -Type:last value
3) 2. Group by Step:
Group field:
1: id
Aggregates:
Subject: date - Type: last value
Subject: key - Type: last value
Subject: inc - Type: last value
Hope this will help.
With best regards,
S.R.
Did similar thing - Group By over Group By with aggregate as Last Value
Thanks S.R

Updation in tables involving multiple conditions (one to many relationship)

I have created three tables like Product, Brands and ProductBrands in ms sql server
These tables contains data as the following
Products (PId - PName - PDescription)
1 - Mobiles - blah blah
2 - T.V - blah blah
3 - A.C - blah blah
Brands (BId - BName)
1 - Samsung
2 - Sony
3 - L.G
4 - Apple
ProductsBrands (PBId - PId - BId)
1 - 1 - 1
2 - 1 - 2
3 - 1 - 3
4 - 2 - 1
5 - 2 - 3
...
...
Now I have to perform update operation on Product tables on the following conditions
If user will select product "let's say Mobile", and he will select Samsung and L.G only
so for this requirement, the updation in ProductsBrands should be like this
ProductsBrands (PBId - PId - BId)
1 - 1 - 1
{2 - 1 - 2} should be deleted
3 - 1 - 3
...
...
If user will select product "let's say Mobile", and he will select Apple
so for this requirement, the updation in ProductsBrands should be like this
ProductsBrands (PBId - PId - BId)
1 - 1 - 1
2 - 1 - 2
3 - 1 - 3
4 - 2 - 1
5 - 2 - 3
6 - 1 - 4 {new entry is made}
If user will select product "let's say Mobile", and he will select Samsung, L.G and Apple
so for this requirement, the updation in ProductsBrands should be like this
ProductsBrands (PBId - PId - BId)
1 - 1 - 1
{2 - 1 - 2} should be deleted
3 - 1 - 3
4 - 2 - 1
5 - 2 - 3
6 - 1 - 4 {new entry is made}
Now I am very very confused how to write stored procedure for all above conditions
Please help me!!!
Your scenario was a little ambiguous for me. So lets simplify it as follow, and check if I got it correct or not:
You want to Insert or Delete or even Update a record or a set of records in a table(let say tbl1), whenever a condition(let say con1) is correct(let say if con1 is correct then Insert should me made, otherwise Delete or Update should be made).
If this is what you are looking for, then you could make a use of MERGE statement(Read More).
If this is what you are looking for, and you have problem with MERGE statement, please tell us to clarify it.
Other links:Here and Here

SQL - Count with distinct on ID

Here is an example of my table structure:
Key - Country - Store
1 - Germany- YYY
1 - Germany- YYY
2 - France- XXX
2 - France- XXX
2 - France- XXX
3 - United Kingdom- YYY
3 - United Kingdom- YYY
4 - Germany- YYY
5 - France- YYY
5 - France- YYY
I would like to start a query on this table to get the following result:
Country- XXX - YYY
Germany - 0 - 2
France - 1 - 1
United Kingdom - 0 - 1
The problem for me ist the ID/Key which does not get used once but several times, thats why I cant just use a count query.
What should my query look like?
You should be able to use the TRANSFORM function to get this result:
TRANSFORM Count(store)
SELECT Country
FROM
(
select distinct key, country, store
from yourtable
) d
GROUP BY Country
PIVOT Store

Select Best value from Text Field

I need to modify a query that is designed to populate our Online ordering site with the products it can sell. I get my data from a view. That veiw has all of the products for sale. Sometimes products can be sold in kits of multiple products. This kits have their own product ID but the veiw renders them with a record for each product so the data looks something like:
1 - item1 - item1desc - 1 - true
2 - item2 - item2desc - 1 - true
3 - item3 - item3desc - 1 - true
4 - item4 - item1desc - 3 - true
4 - item4 - item2desc - 3 - true
4 - item4 - item3desc - 3 - true
What i would like to see is
1 - item1 - item1desc - 1 - true
2 - item2 - item2desc - 1 - true
3 - item3 - item3desc - 1 - true
4 - item4 - kit includes item1desc, item2desc, item3desc - 3 - true
or
4 - item4 - kit - 1 - true
This is the query I have but it still returns 3 rows for a 3 item kit
SELECT [CustomerProductID]
,[CustomerProductName] AS CustomerItemName
,MAX([ProductDescription]) AS CustomerItemDescription
,COUNT([ProductNameID]) AS ProductCount
,[IsActive]
,[ModifiedDate]
FROM [dbo].[vw_ProductList]
where CustomerID in (#tbl)
GROUP BY [CustomerProductID],
[CustomerProductName],
[IsActive],
[ModifiedDate]
Any ideas how to fix this?
It is probably [ModifiedDate] that is different for each row in a "3 item kit".
Remove [ModifedDate] from GROUP BY use MAX([ModifedDate]) in the field list.