I'm new to Kusto/KQL but experienced in T-SQL. I am trying to get a list of exceptions, group them by type, add a count, and order by that count descending.
In SQL it would be:
SELECT Type, COUNT(Type)
FROM exceptions
GROUP BY Type
ORDER BY COUNT(Type) Desc
I've managed everything but the sort.
exceptions
| summarize count() by type
I cannot work out how to sort by an aggregate. I've tried | sort by count() desc, | sort by count() by type desc, | as c | sort by c desc, | extend c = summarize count() by type | sort by c desc
The default column name for count() aggregation is count_, so:
exceptions
| summarize count() by type
| sort by count_ desc
Or, with explicitly naming the column:
exceptions
| summarize CountExceptions = count() by type
| sort by CountExceptions desc
Related
I have a query that creates a result with two columns of ints.
from_id | to_id
---------------
51 | 100
3 | 21
... | ...
Let's call this result map_ids
Then, can I do something like
SELECT * FROM some_table st WHERE st.id IN map_ids.to_id
to only select stuff from some_table that has an ID that matches the result of the to_id in the first query?
I want to avoid using a subquery because the query that generate map_ids is quite long and is actually used twice in the real select from some_table.
It sounds like you're looking for subquery factoring. You can put your first query into a with clause and then join to that in the main body:
WITH map_ids (from_id, to_id) as (
... you first query ...
)
SELECT columns
FROM map_ids mi
JOIN some_table st ON st.id = mi.to_id
You can refer to map_ids again later, e.g. in another branch of a union.
You can do something like where st.id in (select to_id from map_ids) but joining is probably clearer (and maybe more efficient, depending on what the optimiser does with the real query).
I need help in linking two tables together to bring back data as I need.
I have two queries:
The first is as follows:
SELECT
POI.PO_ID as PO_ID, SUM(POI.INV_QTY) AS INV_QTY, POI.INV_NUMBER,
PO.SUP_SUP_ID AS SUPPLIER
FROM TABLE1 POI, PUR_ORDS PO
WHERE POI.PO_ID = '56886' AND POI.PO_ID = PO.PO_ID
GROUP BY POI.PO_ID, POI.INV_NUMBER, PO.SUP_SUP_ID
ORDER BY POI.INV_NUMBER ASC
The Results of this query are as follows:
PO_ID|INV_QTY|INV_NUMBER|SUPPLIER
---------------------------------
56886| 105|INV1 |SUP1
56886| 106|INV2 |SUP1
The second query I have is this:
SELECT
DIL.PO_PO_ID, sum(DIL.ACPTD_QTY) as ACPTD_QTY,
DIL.DLVD_DLVY_NUMB AS DEL_NUM
FROM TABLE2 DIL
where DIL.PO_PO_ID = '56886'
GROUP BY PO_PO_ID, DIL.DLVD_DLVY_NUMB
order by del_num
The Results of this query are as follows:
PO_PO_ID|ACPTD_QTY|DEL_NUM
--------------------------
56886| 105| 1
56886| 106| 2
Now I am attempting to join the two tables, but I get multiple values appearing, using the following:
SELECT DISTINCT(PO_ID), INV_NUMBER, INV_QTY, SUPPLIER, ACPTD_QTY, DEL_NUM
FROM
(SELECT
SELECT POI.PO_ID as PO_ID, SUM(POI.INV_QTY) AS INV_QTY,
POI.INV_NUMBER, PO.SUP_SUP_ID AS SUPPLIER
FROM TABLE1 POI, PUR_ORDS PO
WHERE POI.PO_ID = '56886'
AND POI.PO_ID = PO.PO_ID
GROUP BY POI.PO_ID, POI.INV_NUMBER, PO.SUP_SUP_ID
ORDER BY POI.INV_NUMBER ASC) POINET
INNER JOIN
(SELECT DIL.PO_PO_ID, sum(DIL.ACPTD_QTY) as ACPTD_QTY,
DIL.DLVD_DLVY_NUMB AS DEL_NUM
FROM TABLE 2 DIL
where DIL.PO_PO_ID = '56886'
GROUP BY PO_PO_ID, DIL.DLVD_DLVY_NUMB
order by DEL_NUM) DILV
ON DILV.PO_PO_ID = POINET.PO_ID
GROUP BY PO_ID, INV_NUMBER, INV_QTY, SUPPLIER, ACPTD_QTY, DEL_NUM
ORDER BY INV_NUMBER
However the dataset I get is this:
PO_ID|INV_NUMBER |INV_QTY|SUPPLIER|ACPTD_QTY|DEL_NUM
-----------------------------------------------------
56886|K-101/2014-15| 105|SUP1 | 105| 1
56886|K-101/2014-15| 105|SUP1 | 106| 2
56886|K-107/2014-15| 106|SUP1 | 105| 1
56886|K-107/2014-15| 106|SUP1 | 106| 2
However I need it show the following:
PO_ID|INV_NUMBER |INV_QTY|SUPPLIER|ACPTD_QTY|DEL_NUM
------------------------------------------------------
56886|K-101/2014-15| 105|SUP1 | 105| 1
56886|K-107/2014-15| 106|SUP1 | 106| 2
What do I need to do, to tweak my query?
Any help would be much appreciated.
I think you just need to modify your join statement to join on both columns
ON DILV.PO_PO_ID = POINET.PO_ID
AND DILV.INV_QTY = POINET.ACPTD_QTY
Actually, you can probably leave out the PO_ID, because I it's the same in both subqueries:
i.e. Just do this (on the third last line):
ON DILV.INV_QTY = POINET.ACPTD_QTY
I have a list of integers: ids. There is also collection, IdNames which comes from an sql table. For each integer in ids I want to find the matching id in, IdNames. Then for each record in IdNames that has a matching id I'd like to select the value in the Name and DisplayName columns and the id.
So here is the table IdNames
Id | Name | DisplayName
--------------------------------
1 | fistName | firstDisplayName
2 | secondName | secondDisplayName
3 | thirdName | thirdDisplayName
If ids contained the integers 2 and 3, I'd want this collection to be returned
Id | Name | DisplayName
--------------------------------
2 | secondName | secondDisplayName
3 | thirdName | thirdDisplayName
How would I write this as a linq query?
I stared writing it like this: IdNames.Select(x => x.Id == ids.Any()), but obviously it's not right.
var idNames = from idName in DataContext.IdNames
where ids.Contains(idName.Id)
select idName;
Good enough?
Use Join in Linq-To-Objects("I have a list of integers: ids. There is also collection, IdNames"):
var query = from id in ids
join idName in IdNames
on id equals idName.Id
select idName;
Why is LINQ JOIN so much faster than linking with WHERE?
I need the corresponding query in Criteria language to this one (to retrieve all categories from my table but to distinct them):
SELECT DISTINCT categoryName
FROM Category
WHERE CategoryID IN (
SELECT CategoryID
FROM FoodCategory
)
ORDER BY categoryName
I have table FoodCategory table
id | FoodID | CategoryID
--------|---------------|------------
| |
| |
| |
Actually CategoryID is a foreign key that is pointing to this table here. This is table for Category:
CategoryID | categoryName | otherField
---------------|------------------|------------
| |
| |
| |
And this is table for Food:
FoodID | FoodName | otherField
---------------|------------------|------------
| |
| |
| |
Something like that should do the trick :
public List<String> retrieveFoodCategoryNames() {
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
CriteriaQuery<String> criteriaQuery = builder.createQuery(String.class);
Root<FoodCategory> root = criteriaQuery.from(FoodCategory.class);
// by default an inner join so it will only get the categories which have their id in the FoodCategory table
Join<FoodCategory, Category> joinCategory = root.join(FoodCategory_.category);
Fetch<FoodCategory, Category> fetchCategory = root.fetch(FoodCategory_.category);
Path<String> categoryNamePath = fetchCategory.get(Category_.categoryName);
criteriaQuery.select(categoryNamePath).distinct(true);
criteriaQuery.orderBy(builder.asc(categoryNamePath));
return entityManager.createQuery(criteriaQuery).getResultList();
}
This is not the exact same SQL request because you used a subquery where I'm using a join but it seemed more suited to this particular case. The subquery syntax is a bit more complex and I will not try to write it without compiling! ^^
If something is unclear let me know :-)
I have HQL query that is giving me a headache and am hoping a friendly HQL wizard is out there who can help.
I think I'm trying to do something really simple but can't fathom it all.
I want to group and count some data to present it in a table and think I need a nested query of some sort but can't figure out the way to do it.
Basically I have a table (clients) which has relations to other tables that are used as lookups (clienttype) and (clientsex). The relationships is 1-m. I want to group the clients by type and then have a column counting males, and another counting females. e.g
Type | males | females
type A | x | x
type B | x | x
I can do the query just to get males or females but can't figure out how to get a second column.
Hope this makes sense, is possible and that someone is able to help.
Many thanks,
Craig
Query to get just males is:
SELECT a.clientStatus, COUNT(c.sex)
FROM
Tblclientstatus AS a RIGHT OUTER JOIN a.tblclients AS b
LEFT OUTER JOIN b.tblsex AS c
WHERE c.sex = 'male'
GROUP BY a.clientStatus
You can have a case when to add up "1" for each sex.
Something like this :
SELECT a.clientStatus,
COUNT(
case when c.sex = 'male' then 1
else 0 end
) as males,
COUNT(
case when c.sex = 'female' then 1
else 0 end
) as females,
FROM
Tblclientstatus AS a RIGHT OUTER JOIN a.tblclients AS b
LEFT OUTER JOIN b.tblsex AS c
GROUP BY a.clientStatus