I have the following two SELECTs:
Declare #S_PWO varchar(300)
Declare #S_O varchar(300)
#S_PWO =
(
SELECT COUNT(PWO.Attr1)
FROM Person P
JOIN PersonInOrg PIO
ON P.UID_Person = PIO.UID_Person
JOIN Org O
ON O.UID_Org = PIO.UID_Org
JOIN PersonWantsOrg PWO
ON PIO.Attr2 = PWO.Attr3
WHERE
O.Attr4 like '%STRING%' AND
P.UID_Person = 'XXXXXXXXX'
)
#S_O =
(
SELECT Count(O.CCC_DisplayName)
FROM Person P
JOIN PersonInOrg PIO
ON P.UID_Person = PIO.UID_Person
JOIN Org O
ON O.UID_Org = PIO.UID_Org
JOIN PersonWantsOrg PWO
ON PIO.Attr2 = PWO.Attr3
WHERE
O.Attr4 like '%STRING%' AND
P.UID_Person = 'XXXXXXXXX'
)
For the person XXXXXXXXX they may result in an equal count, or in an unequal count.
I now need to find all persons where the count of those two Selects is unqueal.
How do i do that?
Your using the same Join/Where in both Selects, only the Count differs.
Simply combine both into one, Group By UID_Person and apply Having:
SELECT P.UID_Person, Count(PWO.Attr1) AS cntAttr1, Count(O.CCC_DisplayName) AS cntDisplayName
FROM Person P
JOIN PersonInOrg PIO
JOIN PersonInOrg PIO
ON P.UID_Person = PIO.UID_Person
JOIN Org O
ON O.UID_Org = PIO.UID_Org
JOIN PersonWantsOrg PWO
ON PIO.Attr2 = PWO.Attr3
WHERE
O.Attr4 LIKE '%STRING%'
GROUP BY P.UID_Person
HAVING Count(PWO.Attr1) <> Count(O.CCC_DisplayName)
Related
I have 2 selects. The first one:
select purs.t_orno as purchased
from ttisfc001201 sls
inner join twhinr110201 sfc on sfc.t_orno = sls.t_pdno
inner join twhltc100201 purs on purs.t_clot=sfc.t_clot
left join twhltc220201 items on items.t_clot = sfc.t_clot
left join twhltc210201 cert_num on cert_num.t_item = items.t_item
left join twhltc200201 cert on cert.t_ltft = cert_num.t_ltft
where sls.t_cprj = 'SLS004336' and purs.t_orno like N'PUR%'
and sfc.t_koor = 1 and sfc.t_kost = 5
Is giving me these results:
PUR007833
PUR008544
PUR008698
PUR008963
PUR009048
PUR009304
PUR009611
PUR009912
PUR009913
PUR010006
PUR010110
PUR010400
PUR010465
PUR010539
PUR010664
So basically these are results I must use in the second select in where clause. A field from table in second select must be equal to one of them. To understand me better it should look like this:
select distinct fac.t_isup
from ttfacp200201 fac
inner join ttfacp250201 mid on mid.t_ityp = fac.t_ttyp and mid.t_idoc=fac.t_ninv
where mid.t_orno ='PUR010400' or mid.t_orno='PUR009912'or mid.t_orno='PUR009913'or mid.t_orno='PUR010465'or mid.t_orno='PUR008544'or mid.t_orno='PUR008963'or mid.t_orno='PUR009048'or mid.t_orno='PUR010110'or mid.t_orno='PUR007833'or mid.t_orno='PUR009304'or mid.t_orno='PUR009611'or mid.t_orno='PUR010664'or mid.t_orno='PUR010006'or mid.t_orno='PUR010539'or mid.t_orno='PUR008698'or mid.t_orno='PUR010667'
All these ORs are results from the first select. How I can combine them (the first select to go in second select where clause) so I can get results at once?
You can use the IN clause for your second query
select distinct fac.t_isup
from ttfacp200201 fac
inner join ttfacp250201 mid on mid.t_ityp = fac.t_ttyp and mid.t_idoc=fac.t_ninv
where mid.t_orno IN (
select purs.t_orno
from ttisfc001201 sls
inner join twhinr110201 sfc on sfc.t_orno = sls.t_pdno
inner join twhltc100201 purs on purs.t_clot=sfc.t_clot
left join twhltc220201 items on items.t_clot = sfc.t_clot
left join twhltc210201 cert_num on cert_num.t_item = items.t_item
left join twhltc200201 cert on cert.t_ltft = cert_num.t_ltft
where sls.t_cprj = 'SLS004336' and purs.t_orno like N'PUR%'
and sfc.t_koor = 1 and sfc.t_kost = 5
)
Something like this
;with first_query_cte(purchased) as (
select purs.t_orno
from ttisfc001201 sls
inner join twhinr110201 sfc on sfc.t_orno = sls.t_pdno
inner join twhltc100201 purs on purs.t_clot=sfc.t_clot
left join twhltc220201 items on items.t_clot = sfc.t_clot
left join twhltc210201 cert_num on cert_num.t_item = items.t_item
left join twhltc200201 cert on cert.t_ltft = cert_num.t_ltft
where sls.t_cprj = 'SLS004336' and purs.t_orno like N'PUR%'
and sfc.t_koor = 1 and sfc.t_kost = 5)
select distinct fac.t_isup
from ttfacp200201 fac
inner join ttfacp250201 mid on mid.t_ityp = fac.t_ttyp and mid.t_idoc=fac.t_ninv
inner join first_query_cte fqc on mid.t_orno=fqc.purchased;
SELECT pims.icicimedicalexaminerreport.id,
pims.icicimerfemaleapplicant.adversemenstrualid,
pims.icicimerfemaleapplicant.pregnantid,
pims.icicimerfemaleapplicant.miscarriageabortionid,
pims.icicimerfemaleapplicant.breastdiseaseid,
pims.pimscase.tiannumber
FROM pims.pimscase
INNER JOIN pims.digitization
ON pims.pimscase.digitizationid = pims.digitization.id
INNER JOIN pims.medicalexaminerreport
ON pims.digitization.medicalexaminerreportid =
pims.medicalexaminerreport.id
INNER JOIN pims.icicimedicalexaminerreport
ON pims.medicalexaminerreport.id =
pims.icicimedicalexaminerreport.id
INNER JOIN pims.icicimerfemaleapplicant
ON pims.icicimedicalexaminerreport.id =
pims.icicimerfemaleapplicant.id
WHERE pims.pimscase.tiannumber = 'ICICI1234567890'
which gives me the following output
Now I want to use the above output values to select the rows from the table "YesNoAnswerWithObservation"
I imagine it should look something like this Select * from YesNoAnswerWithObservation Where Id in (22,27,26,...23)
Only instead of typing the values inside IN clause I want to use the values in each column resulting from above-mentioned query.
I tried the below code but it returns all the rows in the table rather than rows mentioned inside the In
SELECT pims.yesnoanswerwithobservation.observation,
graphitegtccore.yesnoquestion.description,
pims.yesnoanswerwithobservation.id ObservationId
FROM pims.yesnoanswerwithobservation
INNER JOIN graphitegtccore.yesnoquestion
ON pims.yesnoanswerwithobservation.yesnoanswerid =
graphitegtccore.yesnoquestion.id
WHERE EXISTS (SELECT pims.icicimedicalexaminerreport.id,
pims.icicimerfemaleapplicant.adversemenstrualid,
pims.icicimerfemaleapplicant.pregnantid,
pims.icicimerfemaleapplicant.pelvicorgandiseaseid,
pims.icicimerfemaleapplicant.miscarriageabortionid,
pims.icicimerfemaleapplicant.gynocologicalscanid,
pims.icicimerfemaleapplicant.breastdiseaseid,
pims.pimscase.tiannumber
FROM pims.pimscase
INNER JOIN pims.digitization
ON pims.pimscase.digitizationid =
pims.digitization.id
INNER JOIN pims.medicalexaminerreport
ON pims.digitization.medicalexaminerreportid =
pims.medicalexaminerreport.id
INNER JOIN pims.icicimedicalexaminerreport
ON pims.medicalexaminerreport.id =
pims.icicimedicalexaminerreport.id
INNER JOIN pims.icicimerfemaleapplicant
ON pims.icicimedicalexaminerreport.id =
pims.icicimerfemaleapplicant.id
WHERE pims.pimscase.tiannumber = 'ICICI1234567890')
Any help or a nudge in the right direction would be greatly appreciated
Presumably you want the ids from the first query:
SELECT awo.observation, ynq.description, ynq.id as ObservationId
FROM pims.yesnoanswerwithobservation awo JOIN
graphitegtccore.yesnoquestion ynq
ON awo.yesnoanswerid = ynq.id
WHERE ynq.id = (SELECT mer.id
FROM pims.pimscase c JOIN
pims.digitization d
ON c.digitizationid = d.id JOIN
pims.medicalexaminerreport mer
ON d.medicalexaminerreportid = mer.id JOIN
pims.icicimedicalexaminerreport imer
ON mer.id = imer.id JOIN
pims.icicimerfemaleapplicant ifa
ON imer.id = ifa.id
WHERE c.tiannumber = 'ICICI1234567890'
) ;
Notice that table aliases make the query much easier to write and to read.
I have the following issue:
I have several tables in my Database, in order to check for a specific criteria I have to join several tables, where I use the following statement:
SELECT *
FROM (SELECT * FROM (((((((((SELECT * FROM table1 WHERE tab1_id = 1) AS A
INNER JOIN (SELECT * FROM table2 WHERE tab2_variable IS NOT NULL) AS B
ON A.variable = B.variable)
INNER JOIN (SELECT table3.*, IIF(year='XXXX', 0, 1) AS flag FROM table3) AS C
ON A.h_name = C.h_name)
INNER JOIN (SELECT * FROM table4 WHERE s_flag = 0) AS D
ON C.v_type = D.v_type)
INNER JOIN table5
ON C.ng_type = table5.ng_type)
INNER JOIN table6
ON C.part = table6.part)
INNER JOIN table7
ON C.ifg = table7.ifg)
INNER JOIN table8
ON C.v_type = table8.v_type AND C.ng_typ = table8.ng_typ AND C.ntr_flag = table8.ntr_flag)
INNER JOIN table9
ON table8.ifg_sii_id = table9.ifg_sii_id)) AS F
LEFT JOIN table10
ON F.risk = table10.risk AND C.v_type = table10.v_type
AND F.series = table10.series
This statement fails. But when I remove one the following two join-conditions in the last Left JOIN, it works as intended:
F.risk = table10.risk and/or C.v_type = table10.v_type
They are both of type CHAR, whereas series is type TINYINT, I guess it has something to do with joining on multiple conditions with strings, but I'm not able to find a workaround, any ideas?
according to your current SQL, Table C is not visible as it's a sub query within F. instead of C.v_type you need to use F.c_v_type and the c_v_type field must come from C table like. select v_type as c_v_type from table3... as C
In the last part of your Query You could try to actually select the table and include the Where Clause Like so:
LEFT JOIN (Select * from table10 Where C.v_type = v_type AND F.series = series) as xx
ON F.risk = xx.risk
Hope this helps
I want to extract a ID , User_ID value from one of the Companies and Contract tables, depending on the ContorollerName value.
select P.TitleProject, P.StartDateProject, P.EndDateProject,P.ControllerID,P.RecordID,P.IsAllocated,P.ProjectStatus_ID,
CN.ControllerName,CN.PersianName,
PU.ProjectID,PU.UserID,PU.RoleID,
CASE
WHEN CN.ControllerName = 'Company' THEN
Companies.Id,Companies.[User_Id]
WHEN CN.ControllerName = 'Contract' THEN
Contracts.Id,Contracts.[User_Id]
END
from Projects P
left outer join Controllers CN ON P.ControllerID = CN.Id
left outer join ProjectUsers PU ON P.Id = PU.ProjectID
where P.IsAllocated = 1
For example, if ContorollerName is 'Company' , the select command is as follows :
select P.TitleProject, P.StartDateProject, P.EndDateProject,P.ControllerID,P.RecordID,P.IsAllocated,P.ProjectStatus_ID,
CN.ControllerName,CN.PersianName,
PU.ProjectID,PU.UserID,PU.RoleID,
Companies.Id,Companies.[User_Id]
You are on the right track -- using left join. But you need to add the tables to the from clause with the appropriate logic.
The logic for the join is quite unclear. The query looks something like this:
select . . .,
coalesce(c.id, co.id) as id,
coalesce(c.user_id, co.user_id) as user_id
from Projects P left join
Controllers CN
on P.ControllerID = CN.Id left join
ProjectUsers PU
on P.Id = PU.ProjectID left join
companies c
on c.? = ? and -- no idea what the right join conditions are
c.ControllerName = 'Company' left join
contracts co
on co.? = ? and -- no idea what the right join conditions are
co.ControllerName = 'Contract'
where P.IsAllocated = 1
In a database with several tables and relationships between them, I want to select only the most recent payment by the clients last year.
My query goes like this:
SELECT
P.Name,
EV.EventName,
FN.Installments,
FN.PurchaseValue,
FN.DueDate
FROM ClientPrivate PF
JOIN Client P ON P.PesControle = PF.PesControle
JOIN ClientClass CP ON P.PesControle = CP.PesControle
JOIN EVENT EV ON CP.EveControle = EV.EveControle
JOIN Class cc ON cc.CurControle = EV.CurControle
JOIN Finance FN ON FN.PesControle = P.PesControle
It returns the values I need, only I'd like to get only the most recent purchase by each client, instead of all of them.
I edited to help clarify. The 'Controle' columns are the keys.
Whatever you date column is put that in the ORDER BY Clause of the ROW_NUMBER() function and you are good to go.
;WITH CTE AS
(
SELECT
P.PesNome,
EV.EveDescri,
FN.FinTotParc,
FN.FinVlrLiquido,
FN.FinDiaVencto,
ROW_NUMBER() OVER (PARTITION BY P.PesNome ORDER BY [DateColumn] DESC) rn
FROM PessoaFisica PF
JOIN Pessoa P ON P.PesControle = PF.PesControle
JOIN CursoPessoa CP ON P.PesControle = CP.PesControle
JOIN EVENTO EV ON CP.EveControle = EV.EveControle
JOIN Curso cc ON cc.CurControle = EV.CurControle
JOIN Financeiro FN ON FN.PesControle = P.PesControle
)
SELECT *
FROM CTE
WHERE rn = 1
I'm guessing Financeiro.FinDiaVencto is your purchase date, and that Pessoa.PesControle is a unique identifier for a person. If not, you'll need to modify the query. I'm using Google to translate Portuguese, and it's still not clear to me.
SELECT
P.PesNome,
EV.EveDescri,
FN.FinTotParc,
FN.FinVlrLiquido,
FN.FinDiaVencto
FROM PessoaFisica PF
JOIN Pessoa P
ON P.PesControle = PF.PesControle
JOIN CursoPessoa CP
ON P.PesControle = CP.PesControle
JOIN EVENTO EV
ON CP.EveControle = EV.EveControle
JOIN Curso cc
ON cc.CurControle = EV.CurControle
JOIN Financeiro FN
on FN.PesControle = P.PesControle
WHERE EXISTS (
SELECT 1
FROM PessoaFisica PF_s
JOIN Pessoa P_s
ON P_s.PesControle = PF_s.PesControle
JOIN CursoPessoa CP_s
ON P_s.PesControle = CP_s.PesControle
JOIN EVENTO EV_s
ON CP_s.EveControle = EV_s.EveControle
JOIN Curso cc_s
ON cc_s.CurControle = EV_s.CurControle
JOIN Financeiro FN_s
on FN_s.PesControle = P_s.PesControle
WHERE P_s.PesControle = P.PesControle
HAVING MAX(FN_s.FinDiaVencto) = FN.FinDiaVencto
)
That should return the record with the most recent Financeiro.FinDiaVencto for each Pessoa.PesControle. If you need it by course or by event(?) you'll need to modify the WHERE clause to join on those fields, too.
I'm not sure if that join to PessoaFisica is necessary either, but I included it because it could be eliminating records. You can include a GROUP BY in the subquery, but it should be redundant.