I currently have a stored procedure which returns data and displayed in my report viewer but my issue is that i check to see if students attended to class or not and i have
COALESCE(A.Attended, 0)AS Attended
This returns 1 if they attended and 0 if not - in my report it only shows 1 or 0 even though they could have attended more than once. How can i cast this to an int to get the right total.
thanks
WHOLE QUERY:
SELECT
P.PartyId,
COUNT(COALESCE(A.Attended, 0))AS Attended,
COUNT(DISTINCT H.HearingId) AS Hearings,
O.OfficeName As OfficeName,
CO.Name,
P.FirstName AS FirstName,
P.LastName AS LastName,
P.BirthDate AS DOB
FROM Activity A
INNER JOIN ActivityType AT On A.ActivityTypeId = AT.ActivityTypeId
INNER JOIN ActivityEntry AE ON A.ActivityEntryId = AE.ActivityEntryId
INNER JOIN HearingEntry HE ON CAE.HearingEntryId = HE.HearingEntryId
INNER JOIN Hearing H ON HE.HearingEntryId = H.HearingEntryId
INNER JOIN [Case] C ON H.CaseId = C.CaseId
INNER JOIN CaseOffice CO ON C.CaseId = CO.CaseId AND AE.OfficeId = CO.OfficeId
INNER JOIN Office O ON CO.OfficeId = O.OfficeId
INNER JOIN Attended A ON H.HearingId = A.HearingId
INNER JOIN Party P ON A.PartyId = P.PartyId
WHERE HP.PartyId = P.PartyId AND AE.OfficeId = #OfficeId AND(H.HearingDate >= #BeginDate AND (H.HearingDate <= #EndDate OR H.HearingDate IS NULL)) AND HE.HearingEntryId = CAE.HearingEntryId
GROUP BY P.PartyId, A.Attended, O.OfficeName,CO.Name,P.FirstName, P.LastName,P.BirthDate
I guess you mean that A.Attented is a bit and you want it to be a int, so that you can aggregate later?
You can cast to a bit to an int like this:
CAST(A.Attented AS INT)
Or in this case:
COALESCE(CAST(A.Attended AS INT), 0) AS Attended
You need to SUM by Attended .
SELECT
P.PartyId,
(SUM(COALESCE(A.Attended, 0)))AS Attended,
COUNT(DISTINCT H.HearingId) AS Hearings,
O.OfficeName As OfficeName,
CO.Name,
P.FirstName AS FirstName,
P.LastName AS LastName,
P.BirthDate AS DOB
FROM Activity A
INNER JOIN ActivityType AT On A.ActivityTypeId = AT.ActivityTypeId
INNER JOIN ActivityEntry AE ON A.ActivityEntryId = AE.ActivityEntryId
INNER JOIN HearingEntry HE ON CAE.HearingEntryId = HE.HearingEntryId
INNER JOIN Hearing H ON HE.HearingEntryId = H.HearingEntryId
INNER JOIN [Case] C ON H.CaseId = C.CaseId
INNER JOIN CaseOffice CO ON C.CaseId = CO.CaseId AND AE.OfficeId = CO.OfficeId
INNER JOIN Office O ON CO.OfficeId = O.OfficeId
INNER JOIN Attended A ON H.HearingId = A.HearingId
INNER JOIN Party P ON A.PartyId = P.PartyId
WHERE HP.PartyId = P.PartyId AND AE.OfficeId = #OfficeId AND(H.HearingDate >= #BeginDate AND (H.HearingDate <= #EndDate OR H.HearingDate IS NULL)) AND HE.HearingEntryId = CAE.HearingEntryId
GROUP BY P.PartyId, O.OfficeName,CO.Name,P.FirstName, P.LastName,P.BirthDate
Related
I am looking for a way to perform this query in DQL :
SELECT
t0_.product, t0_.creation_date, t0_.id pvId,
t0_.product AS product,
t0_.file_path AS file_path_3,
t0_.product AS product_11
FROM table0 t0_
join (
select max(creation_date) as createdDate, id, product
from table0
group by product
) t0b on (t0b.createdDate = t0_.creation_date and t0_.product = t0b.product)
LEFT JOIN table1 t1_ ON t0_.product = t1_.id
LEFT JOIN table2 t2_ ON t1_.id = t2_.otherId
LEFT JOIN table3 t3_ ON t2_.id = t3_.otherId
LEFT JOIN table4 t4_ ON t2_.id = t4_.otherId
LEFT JOIN table5 t5_ ON t4_.id = t5_.otherId
LEFT JOIN table6 t6_ ON t4_.id = t6_.otherId
LEFT JOIN table7 t7_ ON t6_.id = t7_.otherId
WHERE t6_.id = :identifier
GROUP BY p0_.product;
explication : I have in table 'table0' several rows linked to table 'table1', I want to keep only the most recent one (column creation_date)
tableA.ID
tableA.propertyTableB.ID
tableA.createdAt
1
1
-4days
2
3
-3days
3
1
yesterday
4
3
today
5
1
today
6
1
-5 days
7
2
yesterday
8
2
today
I need to keep the rows : 4, 5, 8,
I need to get a queryBuilder object in order to do other operations behind it.
Has anyone already succeeded in this feat?
Thanks in advance <3
I need to keep the most recents rows from tableA related to tableB
EDIT : Maybe I find another way
no Select in the Join, just another JOIN like this :
FROM table0 t0
LEFT JOIN table0Bis AS t0B ON t0B.identifier = t0.identifier AND t0B.creation_date > t0.creation_date
LEFT JOIN table1 t1 ON t1.id = t0.foo
LEFT JOIN table2 t2 ON t1.id = t2.bar
LEFT JOIN table3 t3 ON t2.id = t3.rab
LEFT JOIN table4 t4 ON t2.id = t4.oof
LEFT JOIN table5 t5 ON t5.id = t4.toto
LEFT JOIN table6 t6 ON t5.id = t6.lorem
LEFT JOIN table7 t7 ON t6.id = t7.ipsum
where t7.id = :id AND t0B.creation_date IS NULL
group by t0.product;
$queryBuilder = $this->createQueryBuilder('t0');
$queryBuilder->leftJoin(
'App\Entity\Table0',
't0b',
Join::WITH,
't0b.product = t0.product AND t0b.creationDate > t0.creationDate')
->leftJoin('t0.foo', 't1')
->leftJoin('t1.bar', 't2')
->leftJoin('t2.bar', 't3')
->leftJoin('t3.rab', 't4')
->andWhere('t4.id = :ident')->setParameter('ident', $user->getId())
->andWhere('t0b.creationDate IS NULL')
->groupBy('t0.id', 't0.foobar')
->orderBy('t0.foobar', 'DESC')
;
I am looking to do some querying on two partitions (different partition date) on BQ.
Table format is the following
crm_id, attribute#1, attribute#2,attribute#3
For the IDs that are common from week 1 and week 2, I want to see how many changed attribute#1, attribute#2 and attribute# 3
I started with the inner join using:
WITH t1 AS
(SELECT crm_id AS w1_crm
FROM `table`
WHERE DATE(_PARTITIONTIME) = "date1"
)
SELECT crm_id
FROM `table`
WHERE DATE(_PARTITIONTIME) = "date2"
INNER JOIN
t1
ON
w1_crm = crm_id
I am getting an error on the INNER JOIN
Try below
WITH t1 AS (
SELECT crm_id AS w1_crm
FROM `table`
WHERE DATE(_PARTITIONTIME) = "date1"
), t2 AS (
SELECT crm_id
FROM `table`
WHERE DATE(_PARTITIONTIME) = "date2"
)
SELECT crm_id
FROM t2
INNER JOIN t1
ON w1_crm = crm_id
My current code snippet is given below. I want to use "NOT IN" query instead of left join.
from Complaints C
inner join Employees E on E.ID = C.EmployeeID
left join ComplaintInfractionMapping CIM on CIM.ComplaintID = C.ID
How to use "NOT IN" Query in stored procedure . In below code there is syntax error.
from Complaints C
inner join Employees E on E.ID = C.EmployeeID
NOT IN SELECT InfractionComment,InfractionDate FROM ComplaintInfractionMapping where ComplaintID = C.ID
you can do like this way :-
from Complaints C
inner join Employees E on E.ID = C.EmployeeID
Where Not Exists ( SELECT InfractionComment
,InfractionDate
FROM ComplaintInfractionMapping
where ComplaintID = C.ID
)
Hy everbody , i am having some trouble in transforming a query that has a set of subqueries in the ON clause.
The initial code was created in SQL Server and i have changed to work on Vertica.
This is the code :
SELECT
DISTINCT DocFinanceiro.AutoId AS AutoId,
DocFinanceiro.Classe AS ClasseDoc,
EspecPrestador.Seq AS Sequencia,
PrestadorServico.Codigo AS CodigoPrest,
PessoaPrest.Nome AS NomePrestador,
DocFinanceiro.CompFinanceira AS CompFinanceira,
EspecialidadeServico.Nome AS Especialidade,
ClassePrestador.Nome AS ClsPrestador,
RegistroPessoa.Numero AS RegistroINSS,
RegistroPessoa.Tipo AS TipoRegPessoa,
DocFinanceiro.DataVencimento AS DataVencimentoDocFin
FROM DocFinanceiro
INNER JOIN ClasseDocFinanceiro
ON DocFinanceiro.Classe = ClasseDocFinanceiro.Codigo
INNER JOIN ItemFinanceiro ON DocFinanceiro.AutoId = ItemFinanceiro.Documento
INNER JOIN ClasseApropriacaoFinan
ON ItemFinanceiro.ClasseApro = ClasseApropriacaoFinan.Codigo
INNER JOIN ContratoFinanceiro
ON DocFinanceiro.ContratoFinanceiro = ContratoFinanceiro.AutoId
INNER JOIN Pessoa ON ContratoFinanceiro.Pessoa = Pessoa.AutoId
INNER JOIN GeradorItemFinan
ON ItemFinanceiro.AutoId = GeradorItemFinan.ItemFinan
INNER JOIN PrestadorServico
ON ContratoFinanceiro.AutoId = PrestadorServico.ContratoFinanceiro
INNER JOIN Pessoa PessoaPrest ON PrestadorServico.Pessoa = PessoaPrest.AutoId
INNER JOIN TipoValorFinan
ON ClasseApropriacaoFinan.TipoValor = TipoValorFinan.Codigo
INNER JOIN ClasseContratoFinanceiro
ON ContratoFinanceiro.Classe = ClasseContratoFinanceiro.AutoId
INNER JOIN ClassePrestador
ON PrestadorServico.Classe = ClassePrestador.Codigo
LEFT JOIN EspecPrestador
ON EspecPrestador.Prestador = PrestadorServico.AutoId
AND EspecPrestador.Seq = 1
LEFT JOIN EspecialidadeServico
ON EspecialidadeServico.AutoId = EspecPrestador.Especialidade
LEFT JOIN RegistroPessoa ON PessoaPrest.AutoId = RegistroPessoa.Pessoa
--This is the part where i getr stuck
AND
(
RegistroPessoa.AutoId =
(
SELECT
RegistroPessoa.AutoId
FROM RegistroPessoa
WHERE RegistroPessoa.Pessoa = PessoaPrest.Autoid
AND (RegistroPessoa.Tipo = 1 OR RegistroPessoa.Tipo = 1) order by 1 limit 1
)
)
--until here
WHERE (GeradorItemFinan.TipoGerador = 1)
AND (DocFinanceiro.CompFinanceira = 1)
AND (PrestadorServico.AutoId = 1)
AND DocFinanceiro.CompSeq = 1
AND (DocFinanceiro.Numero IS NOT NULL)
AND (DocFinanceiro.Classe <> 1)
ORDER BY AutoId, Sequencia
;
If anybody has any ideas it would be great !!
try replace
RegistroPessoa.AutoId = (SELECT...)
to
RegistroPessoa.AutoId IN (SELECT...)
and use aliases for table short names
I have three tables. I have to retrieve the data using Linq statement. My three table names are A,B,C. I connected join for connecting two tables A and B based on the id's like:
select ol, fN, LN, ci, co
from member
join details
on member_id = details_id
where details_id in
(select contacts_id from contacts where
contacts_id1 = 1 and contacts_usr_id = 1)
I am able to write the query up to the where condition, how can I write the query for the inner while condition?
you can try this:
var idlist = (from tbl in table3
where tbl.usr_id == 1 && tbl.contacts_id == 1
select tbl.contacts_id ).ToList();
var x = from A in table1
from B in table2 where A.user_id == B.user_id
&& idlist.Contains(A.user_id)
select new { a = A.a, b = A.b, c = A.c, d = B.d, e = B.e };
check and let me know if that work.
Try flipping the query upside down. How about the following:
var query =
from t3 in table3
where t3.user_id = 1 && t3.contacts_id = 1
join t2 in table2 on t3.contacts_id equals t2.usr_id
join t1 in table1 on t2.usr_id equals t1.userid
select new {t2.a, t2.b, t2.c, t1.d, t1.e};