using xquery to count employees in department (same lvl tag) - xquery

I have a xml file like this:
<employees>
<emp_num>
<department>1</department>
<salary>1000</salary>
</emp_mum>
<emp_num>
<department>1</department>
<salary>2000</salary>
</emp_mum>
</employees>
I want to know the number of employees per department ordered by the average salary of the department.
My main problem to do this is that "department" and "salary" are at the same level and I don't know how to use FLWOR in this situation.

Try
for $emp in //emp_num
group by $d := $emp/department
order by avg($emp/salary)
return count($emp)

Related

Symfony order by the content of a specific field

I want to sort data in repository based on the content of a specific field
For example i have an entity person with the fields role,fistName and lastName.
I would like to sort using ->orderBy('p.role', ???) and get a list ordered based on this order : professors then directors then teachers and at last students .
Example of my database:
Wanted result:
Ps: i can not use ASC or DESC since my sorting order is neither ASC nor DESC;
it is a custom order
Very strage scenario, you could use a CASE statement in order to assing a custom value for each fields then sort on him.
You could use the HIDDEN keyword.
As example take a look at this DQL:
SELECT p, CASE
WHEN p.role = "professor" THEN 1
WHEN p.role = "director" THEN 2
WHEN p.role = "teacher" THEN 3
WHEN p.role = "student" THEN 4
ELSE 99 END
AS HIDDEN mySortRule
FROM Bundle\Entity\Person p
ORDER BY mySortRule ASC
Hope this help

displaying count values IN AN ARRAY

I have the following sql code:
$management_lcfruh_sql = "SELECT COUNT(schicht), codes.lcfruh, personal.status, dienstplan.kw, personal.perso_id, personal.sort_order, dienstplan.datum FROM dienstplan INNER JOIN codes ON dienstplan.schicht=codes.lcfruh INNER JOIN personal ON personal.perso_id=dienstplan.perso_id WHERE codes.lcfruh!='' AND personal.status='management' AND dienstplan.kw='$kw' ORDER BY personal.sort_order, dienstplan.datum";
$management_lcfruh_result= mysql_query($management_lcfruh_sql);
how can I get a list of counts instead of only one count, dienstplan.kw='$kw' is a week of the year which have seve days, so I should get seven result listed instead of a one count of all of the seven.
<?php
while($rows=mysql_fetch_array($management_lcfruh_result)){
?>
<? echo $rows['COUNT(schicht)']; ?>
<?php
}
?>
Um well you haven't been a big help on the extra info front, but may be this will get you going in the right direction.
Given Table1(WeekNo int, Category int, Schict int)
and Table2 (WeekNo int, Schict int, CategoryDate dateTime)
Then
Select t1.Catgeory, DayName(t2.CategoryDate), t1.Schict, Count(t1.schict)
From Table1 t1
Inner join Table2 t2 On t1.Schict = t2.schict and t1.WeekNo = t2.weekNo
Group by t1.Category,DayName(t2.CategoryDate), t1.Schict
Would get you a count by Category and schict across the two tables, but the number of records you get would be dependent on how many days of the week you had records for.
You could deal with that in Sql but it would be much easier to fill in the missing days in your array client side in PHP. Posibly even easier, if you were to use the mysql DayOfWeek
function instead of DayName.
Some clues anyway
Perhaps even easier would to simply add this date you won't tell us about to the select clause of your current query and then use PHP to get the day.

Cypher Order By Number of Paths

Let's say I have a graph of movies and directors, where movies are connected to each other by co-viewership. I want to find similar directors, i.e. directors whose films tend to be watched together.
START n=node:index(Name="Steven Spielberg") MATCH n-->m--l<--o RETURN o;
This gets me all of the related directors, but how do I order them by the number of paths that connect them? Bonus points if I can also take weight of the tie between films into consideration.
count(*) is the number of paths that start with n and end with o
START n=node:index(Name="Steven Spielberg")
MATCH n-->m--l<--o
RETURN o,count(*)
order by count(*) desc;
with weights on the relationships
START n=node:index(Name="Steven Spielberg")
MATCH path=n-->m--l<--o
RETURN o,sum(reduce(sum=0,r in rels(path) : sum+r.weight)) as weight
ORDER BY weight desc;
START n=node:index(Name="Steven Spielberg")
MATCH path=n-->m--l<--o
RETURN o
ORDER BY length(path);

How to fetch first occurance of most active row row in job table with specific deptid,

Eg : Emplid 001 most effective dated row (say 01/01/2013 )is active and belongs to deptid 101.Suppose If he has two more rows prior with same deptid say one on 10/12/2012 and 01/12/2012, Then i needs to retrieve 01/12/2012 rows.So it should be the first row of continous occurances, In case if i have row with 05/12/2012 with other deptid (102), In that case my query should return 10/12/2012 rows, Please help on this
Though not with a left join, this uses a sub-select on minimum effective date to get the data you need - the trick to get the min effdt by deptid is putting the deptid in the sub-select. Note that empl_rcd_nbr is usually used as a limiter in the sub-select (and job2.empl_rcd_nbr - job.empl_rcd_nbr) but you didn't have it in your original select. If you get dup rows, check your empl_rcd_nbr value since it is a primary key:
select job.emplid, job.effdt, job.deptid
from ps_job job
where job.effdt = (select Min(job2.effdt)
from ps_job job2
where job2.emplid = job.emplid
and job2.deptid = job.deptid)
order by job.emplid
To achieve what you need, you will have to pick up the min(effdt) row with deptid same as the deptid of the current max(effdt) row and also, there should not exist a row > the min(effdt) with deptid <> deptid of the min(effdt) row.
A query satisfying the above conditions should help you with your result set.

How pass a list of values to compare in a SQL Function in SQL Server 2008?

I have an SQL Function with the following SQL within:
SELECT StockID FROM (SELECT DISTINCT StockID,
ROW_NUMBER() OVER(ORDER BY DateAdded DESC) AS RowNum
FROM Stock
WHERE CategoryCode LIKE #CategoryID) AS Info
WHERE RowNum BETWEEN #startRowIndex AND (#startRowIndex + #maximumRows) - 1
I have a Parameter #CategoryID - however I need to take in a category ID such as "BA" and translated this to a list of Category IDs such as "IE","EG" etc so my WHERE clause looks like:
WHERE (CategoryCode LIKE 'IE' OR CategoryCode LIKE 'EG') AS Info
I have a Lookup Table which contains the "BA" code and then all the real category codes this means such as "IE" and "EG".
How do I have the CategoryID expand to multiple "OR" statements in my SQL Function?
I am unsure how to do this, can anyone solve this problem?
At the moment the query as shown can cope with one CategoryID such as "IE", this is done as I want a category page such as category.aspx where a parameter "BA" is passed such as category.aspx?category=BA and this page will list all items with the category codes "EG" and "IE".
The reason I need this is there is a "parent" category code which has multiple "children" category codes which are different to the parent code. I am using ASP.NET and .NET 3.5 on the front-end if this helps.
Try using
WHERE CategoryCode IN (
SELECT LookupCategoryCode
FROM LookupTable
WHERE LookupCategoryId = #CategoryId
)
Replacing "LookupCategoryCode", "LookupTable", and "LookupCategoryId" for the respective values in your lookup table.
Assuming the parameter is a common delimited list of categoryID's
Try
WHERE charindex(','+CategoryCode+',',','+#CatParam+',') > 0
Performance won't be great, but it should do the trick for you

Resources