Nested select statements - stored-procedures

I am trying to return results based on the number of cases if greater than 0 but when i try to execute the stored procedure i get an error stating: Operand type clash: uniqueidentifier is is incompatible with tinyint. And that is for my nested select statement in where clause.
SELECT
O.OfficeId,
O.OfficeName AS Name,
AT.Description AS CaseActivity,
SUM(A.Duration) AS [CaseMinutes],
CAST(SUM(A.Duration) AS FLOAT) / 60 AS [CaseHours],
COUNT(A.ActivityId) AS Activities,
COUNT(DISTINCT A.CaseId) AS Cases,
MIN(CAST(A.Duration AS FLOAT) / 60) AS [Case Min Time],
MAX(CAST(A.Duration AS FLOAT) / 60) AS [Case Max Time],
SUM(CAST(A.Duration AS FLOAT) / 60) / COUNT(A.ActivityId) AS [Case Avg Time],
SUM(CAST(A.Duration AS FLOAT) / 60) AS [Case TotalHours]
FROM Activity A
INNER JOIN ActivityType AT ON A.ActivityTypeId = AT.ActivityTypeId
INNER JOIN ActivityEntry AE ON A.ActivityEntryId = AE.ActivityEntryId
INNER JOIN [Case] C ON A.CaseId = C.CaseId
INNER JOIN [Office] O ON AE.OfficeId = O.OfficeId
INNER JOIN [User] U ON C.CreatedByUserId = U.UserId
WHERE A.CaseId in(select A.CaseId from Activity where A.CaseId > 1 AND .dbo.DateOnly(AE.ActivityDate) BETWEEN #BeginDate AND #EndDate)
GROUP BY
O.OfficeId,
O.OfficeName,
AT.Description
**Desired GOAL from stored procedure**
I want to return results from this stored procedure where the case count is greater than 0. Currently this stored procedure returns all activities with cases 0 or greater. I am only interested in getting the activities where the cases are greater than 0. In my where clause i am trying to insert another select statement that will filter the results to cases > 0.

The error message is telling you.
One of the comparisons is comparing things from two columns with different types.
Look at the table structures and see which columns you're comparing has uniqueidentifier and which have ints and then look at the sql and see which ones you're comparing.

Related

Distinct join between 2 tables

this is my code :
SELECT DISTINCT Emps.name, Degrees.Name AS degree, Degrees.Date AS degree_date
FROM Emps INNER JOIN
Degrees ON Emps.id = Degrees.empId
but distict dosn't work , i want this result
i want distict name with degree which has max id or max date
thanks for all i found the solution
SELECT e.name, d.Name AS degree
FROM Emps AS e
full JOIN (
SELECT t.*, ROW_NUMBER() OVER (PARTITION BY t.empId ORDER BY t.id DESC) AS rn FROM Degrees AS t
) AS d ON e.id = d.empId
WHERE d.rn = 1

Joins On Subqueries

I'm still kinda new to SQL; please spare me. I have an issue with trying to join a table or another view to my existing query below.
The last question I had asked refers to joining the two subqueries and giving them an alias in order for the join to work (something I still don't understand?) I'm trying to join a table and another subquery to the query below. But I'm not quite sure how I could continue adding subqueries to it.
I don't really understand how the alias names allowed the subqueries to be joined, and if I add another subquery, then I'll add another alias?
Any assistance you could provide would be much appreciated. Apologize in advanced for the formatting :/
Select
Sub4.DT as Date, Sub2.USOH, Sub2.USOHP, Sub2.OVXH, Sub2.OVXHP, Sub4.USOPP, Sub4.OVXPP
From
(Select
*
From
(Select
x.ID, x.Date, x.USOH, x.OVXH, convert(varchar, x.Date, 1) AS DT,
Abs(Cast((((x.USOH / NullIf((y.USOH), 0)) - 1) * 100) AS Decimal(10, 2))) AS USOHP2,
Format(Abs(((x.USOH / NullIf((y.USOH), 0))) - 1), 'P') AS USOHP,
Abs(Cast((((x.OVXH / NullIf((y.OVXH), 0)) - 1) * 100) AS Decimal(10, 2))) AS OVXHP2,
Format(Abs(((x.OVXH / NullIf((y.OVXH),0)))-1),'P') AS OVXHP
From
(Select
a.Date as aDate, Max(b.Date) As aPrevDate
From
MACDHistogram A
Inner Join
MACDHistogram b on a.Date > b.Date
Group By a.Date) Sub1 -- Group Date > Previous Date SUB1
Inner Join
MACDHistogram x on Sub1.aDate = x.Date
Inner Join
MACDHistogram y on Sub1.aPrevDate = y.Date) T2) Sub2 --Histogram Percent SubQuery SUB2
Inner Join
(Select
*
From
(Select
z.ID, z.ID2, z.Date, z.USO as USOP, z.OVX as OVXP,
convert(varchar, z.Date, 1) as DT,
Cast(((z.USO / NullIf((q.USO),0)- 1) * 100) as Decimal(10,2)) AS USOPP2,
Format(((z.USO / NullIf((q.USO),0))-1),'P') AS USOPP,
Cast(((z.OVX / NullIf((q.OVX),0)- 1) * 100) as Decimal(10,2)) AS OVXPP2,
Format(((z.OVX / NullIf((q.OVX),0))-1),'P') AS OVXPP
From
(Select
c.Date as cDate, Max(d.Date) As cPrevDate
From
Prices C
Inner Join
Prices d on c.Date > d.Date Group By c.Date) Sub3 -- Group Date > Previous Date SUB3
Inner Join
Prices z on Sub3.cDate = z.Date
Inner Join
Prices q on Sub3.cPrevDate = q.Date) T4) Sub4 -- Price Percent Subquery SUB4
On Sub2.Date = Sub4.Date
Order By
sub4.Date Desc

Find top 3 nodes with maximum relationships

The structure of my data base is:
( :node ) -[:give { money: some_int_value } ]-> ( :Org )
One node can have multiple relations.
I need to find top 3 nodes with the most number of relations :give with their property money holding: vx <= money <= vy
Using ORDER BY and LIMIT should solve your problem:
Match ( n:node ) -[r:give { money: some_int_value } ]-> ( :Org )
RETURN n
ORDER BY count(r) DESC //Order by the number of relations each node has
LIMIT 3 //We only want the top 3 nodes
Instead of using the label 'node', maybe use something more descriptive like Person for the label so the datamodel is more clear:
MATCH (p:Person)-[r:give]->(o:Org)
WITH count(r) AS num, sum(r.money) AS total, p
RETURN p, num, total ORDER BY num DESC LIMIT 3;
I'm not sure what you mean by "their property money holding: vx <= money <= vy". If you could clarify I can update my answer accordingly. You can calculate the total of the money properties using the sum() function.
Edit
To only include relationships with money property with value greater than 10 and less 25:
MATCH (p:Person)-[r:give]->(o:Org)
WHERE r.money >= 10 AND r.money <= 25
WITH count(r) AS num, sum(r.money) AS total, p
RETURN p, num, total ORDER BY num DESC LIMIT 3;

INNER and OUTER JOIN on 3 tables in PIG

I am trying to convert the following join statement into PIG.
from EXCH e
JOIN (select a,p, count(buyer) from EXCH group by a, p) sq on e.a = sq.a and e.p = sq.p
left outer join myimplocal i on e.a = i.a and e.p = i.p and e.f = i.f and i.imp > 0 and e.iswin = 1
Currently, I have this:
EXCH2 = FOREACH EXCH GENERATE a, p, buyer;
EXCH2_groupby = GROUP EXCH2 BY (a, p);
EXCH_alias_sq = FOREACH EXCH2_groupby GENERATE EXCH2.a, EXCH2.p, COUNT(EXCH2.buyer);
join_EXCH_and_EXCH_alias_sq = JOIN EXCH by (a, p), EXCH_alias_sq by (a, p);
myimplocal_filterby_imp_notZero = FILTER myimplocal BY imp > 0;
Should I filter e.iswin=1 before doing the left outer join? Should I apply the filter to join_EXCH_and_EXCH_alias_sq, then do an outer join on myimplocal_filterby_imp_notZero
and my filter alias?
Any help would be greatly appreciated.
In my question - as e.iswin is performed during the outerjoin on the result of the inner join tables, I had to use Union to give me the correct result.
This is what I did:
Data Set 1:
1) inner join EXCH_and_EXCH_alias_sq
2) filter the result with e.iswin =1,
3)left outer join the result in step 2 with myimplocal
Data set 2:
1) inner join EXCH_and_EXCH_alias_sq (same as (1)above)
2) filter the result with e.iswin =0
UNION ONSCHEMA (alias for step 3 from Data set 1) (alias for step 2 from Data set 2)
This gave me the correct result.

Compute sum SQLPlus

I'm struggling to figure out the issue with my SQL table using compute sum.
All that is displayed where the sum of the column should be is a blank box!
Code Below:
TTITLE CENTER ==================== SKIP 1-
CENTER 'U T O O L' skip 1-
CENTER ==================== SKIP 1 -
LEFT 'Tool Report 1.03' SKIP 1 -
LEFT ============ SKIP 2-
RIGHT 'Page:' -
FORMAT 999 SQL.PNO SKIP 2
set pagesize 50
column MEMBERNAME HEADING 'Member Name' format a20
compute sum of TOTAL on Rental_ID
Break on RENTAL_ID
select Member.Member_ID, SUBSTR(Member.FName,0,10) || SUBSTR(' ',0,10) ||
SUBSTR(Member.SName,0,15) as MEMBERNAME,
Rental.Rental_ID,
Tool.Name,
Rental_Line.Qty,
Rental_Line.Price,
TO_Char(Rental_Line.Qty*Rental_Line.Price,'L9,999.99') TOTAL
from Rental_Line
INNER JOIN Rental
on Rental.Rental_ID = Rental_Line.Rental_ID
INNER JOIN Member
on Rental.Member_ID = Member.Member_ID
INNER JOIN Tool_Instance
on Rental_Line.Tool_Instance_ID = Tool_Instance.Tool_Instance_ID
INNER JOIN Tool
on Tool_Instance.Tool_ID = Tool.Tool_ID
where Rental.Rental_ID = '&Rental_ID';
may be this help you, as I understood you need SUM(Rental_Line.Qty) OVER (PARTITION BY Rental.Rental_ID)
select Member.Member_ID,
SUBSTR(Member.FName, 0, 10) || SUBSTR(' ', 0, 10) ||
SUBSTR(Member.SName, 0, 15) as MEMBERNAME,
Rental.Rental_ID,
Tool.Name,
Rental_Line.Qty,
Rental_Line.Price,
TO_Char(Rental_Line.Qty * Rental_Line.Price, 'L9,999.99') TOTAL,
SUM(Rental_Line.Qty) OVER (PARTITION BY Rental.Rental_ID) TOTAL_QTY,
SUM(Rental_Line.Qty * Rental_Line.Price) OVER (PARTITION BY Rental.Rental_ID) TOTAL_SUM
from Rental_Line
INNER JOIN Rental on Rental.Rental_ID = Rental_Line.Rental_ID
INNER JOIN Member on Rental.Member_ID = Member.Member_ID
INNER JOIN Tool_Instance on Rental_Line.Tool_Instance_ID =
Tool_Instance.Tool_Instance_ID
INNER JOIN Tool on Tool_Instance.Tool_ID = Tool.Tool_ID
where Rental.Rental_ID = '&Rental_ID';

Resources