SQL query with left join with where condition - sql

please help me to correct this query, I need the data with this that senderId
select count(tg."transferGroupId"),
tg."transferGroupId",
tg."senderId",
tg."receiverId",
tg."createdAt",
u."firstName"
from kit.kit_transfers as tg
where tg."senderId"='30b81071-0167-4736-a162-103a2165be3d'
left join users as u on tg."receiverId"=u.id
group by tg."transferGroupId", tg."senderId",tg."receiverId",tg."createdAt",u."firstName"

select count(tg."transferGroupId"),
tg."transferGroupId",
tg."senderId",
tg."receiverId",
tg."createdAt",
u."firstName"
from kit.kit_transfers as tg
left join users as u on tg."receiverId"=u.id
where tg."senderId"='30b81071-0167-4736-a162-103a2165be3d'
group by tg."transferGroupId", tg."senderId",tg."receiverId",tg."createdAt",u."firstName"

Related

LEFT JOIN DUPLICATING VALUES (QUERY WITH WHERE IN)

I need to get rid of duplicated values return from a LEFT JOIN that I did, I'm such a beginner in SQL so I do hope you guys help me out!
SELECT *
,tm.TEAM_NAME
,up.USER_NAME OWNER_NAME
FROM NTTUS_INIT_TM_TASK_HEADER th
LEFT JOIN NTTUS_TEAM tm
ON th.TEAM_ID = tm.TEAM_ID
LEFT JOIN NTTUS_USER_PROFILE up
ON th.OWNER_ID = up.USER_ID
WHERE 1 = 1
AND INIT_ID IN(${arr.ids})
Actually from your description, it's hard to understand your table. But try this. If this doesn't work let me know
SELECT *
,tm.TEAM_NAME
,up.USER_NAME OWNER_NAME
FROM
NTTUS_INIT_TM_TASK_HEADER as th
LEFT JOIN
NTTUS_TEAM as tm ON th.TEAM_ID = tm.TEAM_ID
LEFT JOIN
NTTUS_USER_PROFILE as up ON th.OWNER_ID = up.USER_ID
WHERE
INIT_ID IN(${arr.ids})
Group By
th.*
,tm.TEAM_NAME
,up.USER_NAME OWNER_NAME
Having count(*) = 1

Looking to add in a Count query with Group by INTO an existing working query

Goal:
I wish to get the Count of how many times a WorkItem was re-assigned
From what I understand the proper query is the following:
SELECT
WorkItemDimvw.Id,
COUNT(WorkItemAssignedToUserFactvw.WorkItemAssignedToUser_UserDimKey) AS Assignments
FROM WorkItemDimvw INNER JOIN WorkItemAssignedToUserFactvw
ON WorkItemDimvw.WorkItemDimKey = WorkItemAssignedToUserFactvw.WorkItemDimKey
GROUP BY WorkItemDimvw.Id
The EXISTING query is below and I'm wondering / forgeting if I should:
Just add in COUNT(WorkItemAssignedToUserFactvw.WorkItemAssignedToUser_UserDimKey) AS Assignments since joins are existing, except it is group by WorkItemDimvw.Id
Should it instead be a subquery in the Select below?
Query:
SELECT
SRD.ID,
SRD.Title,
SRD.Description,
SRD.EntityDimKey,
WI.WorkItemDimKey,
IATUFact.DateKey
FROM
SLAConfigurationDimvw
INNER JOIN SLAInstanceInformationFactvw
ON SLAConfigurationDimvw.SLAConfigurationDimKey = SLAInstanceInformationFactvw.SLAConfigurationDimKey
RIGHT OUTER JOIN ServiceRequestDimvw AS SRD
INNER JOIN WorkItemDimvw AS WI
ON SRD.EntityDimKey = WI.EntityDimKey
LEFT OUTER JOIN WorkItemAssignedToUserFactvw AS IATUFact
ON WI.WorkItemDimKey = IATUFact.WorkItemDimKey
AND IATUFact.DeletedDate IS NULL
The trick is to aggregate the data on a sub query, before you join it.
SELECT
SRD.ID,
SRD.Title,
SRD.Description,
SRD.EntityDimKey,
WI.WorkItemDimKey,
IATUFact.DateKey,
IATUFact.Assignments
FROM
SLAConfigurationDimvw
INNER JOIN
SLAInstanceInformationFactvw
ON SLAConfigurationDimvw.SLAConfigurationDimKey = SLAInstanceInformationFactvw.SLAConfigurationDimKey
RIGHT OUTER JOIN
ServiceRequestDimvw AS SRD
ON <you're missing something here>
INNER JOIN
WorkItemDimvw AS WI
ON SRD.EntityDimKey = WI.EntityDimKey
LEFT OUTER JOIN
(
SELECT
WorkItemDimKey,
DateKey,
COUNT(WorkItemAssignedToUser_UserDimKey) AS Assignments
FROM
WorkItemAssignedToUserFactvw
WHERE
DeletedDate IS NULL
GROUP BY
WorkItemDimKey,
DateKey
)
IATUFact
ON WI.WorkItemDimKey = IATUFact.WorkItemDimKey

Multiple join with SQL

I have a problem with my SQL query.
I want to have a multiple join, but the error is not helpful.
The following is my query:
SELECT bn_ms_bm_bankmaster.CMP_CUSTCODE AS Id_client
,BN_CS_MP_MASTERPROFILE.CMP_NAME AS Nom_prenom
,PR_GN_AD_ENTITYADDRESS.PMP_MUNCIPCODE
FROM bn_ms_bm_bankmaster
INNER JOIN BN_CS_MP_MASTERPROFILE ON bn_ms_bm_bankmaster.CMP_CUSTCODE = BN_CS_MP_MASTERPROFILE.CMP_CUSTCODE
INNER JOIN PR_GN_AD_ENTITYADDRESS ON bn_ms_bm_bankmaster.CMP_CUSTCODE = PR_GN_AD_ENTITYADDRESS.CMP_CUSTCODE
This query give back to me the flowing error:
Impossible to add table bn_ms_bm_bankmaster with Microsoft Query
Any help will welcome.
Can you try with below.
select * from(Select bn_ms_bm_bankmaster.CMP_CUSTCODE AS Id_client,BN_CS_MP_MASTERPROFILE.CMP_NAME as Nom_prenom,PR_GN_AD_ENTITYADDRESS.PMP_MUNCIPCODE FROM bn_ms_bm_bankmaster INNER JOIN BN_CS_MP_MASTERPROFILE ON bn_ms_bm_bankmaster.CMP_CUSTCODE=BN_CS_MP_MASTERPROFILE.CMP_CUSTCODEINNER JOIN PR_GN_AD_ENTITYADDRESS ON bn_ms_bm_bankmaster.CMP_CUSTCODE=PR_GN_AD_ENTITYADDRESS.CMP_CUSTCODE);
Please try this.
Select A.CMP_CUSTCODE AS Id_client,
B.CMP_NAME as Nom_prenom,
C.PMP_MUNCIPCODE
FROM bn_ms_bm_bankmaster AS A
INNER JOIN BN_CS_MP_MASTERPROFILE AS B ON A.CMP_CUSTCODE= B.CMP_CUSTCODE
INNER JOIN PR_GN_AD_ENTITYADDRESS AS C ON B.CMP_CUSTCODE = C.CMP_CUSTCODE

Return single row from SQL Query of many tables with common UserId key

Is the a better way of returning a single row from a SQL Query that has multiple tables with a common UserId Key? I also referred to LINK but result was not what I'm looking for.
Also, they are many users which are assigned with many ModuleAccess, many PageAccess, many catalogAccess, Only 1 UserType, And Only 1 SystemAccess. User table has go many users.
I tried this But did no work:
SELECT us.userId, us.username, us.email, us.isAdministrator, us.status, us.FullName, ut.userTypeId, ut.typeName, ut.levelName, sys.sysAccessId, sys.adminDashboard, sys.accessName, sys.standardDashboard,
sys.marginChart, sys.expiringChart, sys.increasingChart, sys.viewCatalogue, sys.importList, sys.exportList, sys.masterDataMain, sys.changesNeed, md.moduleId, md.moduleName, md.moduleUrl,
pg.pageId, pg.pageName, pg.pageUrl, pg.pagePermission, pg.pageAccess,cat.catAccId, cat.HasAccess
FROM dbo.mp_Users AS us
INNER JOIN dbo.mp_UserType AS ut ON us.userId = ut.userId
INNER JOIN dbo.mp_PageAccess AS pg ON us.userId = pg.userId
INNER JOIN dbo.mp_ModuleAccess AS md ON us.userId = md.userId
INNER JOIN dbo.mp_SystemAccess AS sys ON us.userId = sys.userId
INNER JOIN dbo.mp_CatalogAccess AS cat ON us.userId = cat.userId
What I want is something link this:
This is my current result query :
Any One has a better way of Query this above SQL?
Thank you
_________________After few attempt_______________Query error
SELECT
dbo.mp_Users.userId, dbo.mp_Users.username, dbo.mp_Users.email, dbo.mp_Users.isAdministrator, dbo.mp_Users.status, dbo.mp_Users.FullName, dbo.mp_UserType.userTypeId,
dbo.mp_UserType.typeName, dbo.mp_UserType.levelName, dbo.mp_SystemAccess.sysAccessId, dbo.mp_SystemAccess.adminDashboard, dbo.mp_SystemAccess.accessName, dbo.mp_SystemAccess.standardDashboard,
dbo.mp_SystemAccess.marginChart, dbo.mp_SystemAccess.expiringChart, dbo.mp_SystemAccess.increasingChart, dbo.mp_SystemAccess.viewCatalogue, dbo.mp_SystemAccess.importList,
dbo.mp_SystemAccess.exportList, dbo.mp_SystemAccess.masterDataMain, dbo.mp_SystemAccess.changesNeed,
sum(dbo.mp_ModuleAccess.moduleId),
max(dbo.mp_ModuleAccess.moduleName),
max(dbo.mp_ModuleAccess.moduleUrl),
sum(dbo.mp_PageAccess.pageId),
max(dbo.mp_PageAccess.pageName),
max(dbo.mp_PageAccess.pageUrl),
max(dbo.mp_PageAccess.pagePermission),
max(dbo.mp_PageAccess.pageAccess),
sum(dbo.mp_CatalogAccess.catAccId),
max(dbo.mp_CatalogAccess.HasAccess)
FROM dbo.mp_Users
INNER JOIN dbo.mp_UserType ON dbo.mp_Users.userId = dbo.mp_UserType.userId
INNER JOIN dbo.mp_PageAccess ON dbo.mp_Users.userId = dbo.mp_PageAccess.userId
INNER JOIN dbo.mp_ModuleAccess ON dbo.mp_Users.userId = dbo.mp_ModuleAccess.userId
INNER JOIN dbo.mp_SystemAccess ON dbo.mp_Users.userId = dbo.mp_SystemAccess.userId
INNER JOIN dbo.mp_CatalogAccess ON dbo.mp_Users.userId = dbo.mp_CatalogAccess.userId
GROUP BY dbo.mp_Users.userId
You can add
group by userId
in the end of your code, and then, at the select section,for all fields except userID, you will have to use
max(fieldname)
instead of fieldname eg
max(us.username), max(us.email)
Be careful, though. I proposed max() because I see that for every userID, the multiple values of your fields have the same value - however, you must be sure this is the case. If there is some field for which there are multiple values, you will have to identify in what way we should select one of these to present in the SELECT section.
Here are al the fields with max(), use with caution:
SELECT
us.userId,
max(us.username),
max(us.email),
max(us.isAdministrator),
max(us.status),
max(us.FullName),
max(ut.userTypeId),
max(ut.typeName),
max(ut.levelName),
max(sys.sysAccessId),
max(sys.adminDashboard),
max(sys.accessName),
max(sys.standardDashboard),
max(ys.marginChart),
max(sys.expiringChart),
max(sys.increasingChart),
max(sys.viewCatalogue),
max(sys.importList),
max(sys.exportList),
max(sys.masterDataMain),
max(sys.changesNeed),
max(md.moduleId),
max(md.moduleName),
max(md.moduleUrl),
max(pg.pageId),
max(pg.pageName),
max(pg.pageUrl),
max(pg.pagePermission),
max(pg.pageAccess),
max(cat.catAccId),
max(cat.HasAccess)
FROM dbo.mp_Users AS us
INNER JOIN dbo.mp_UserType AS ut ON us.userId = ut.userId
INNER JOIN dbo.mp_PageAccess AS pg ON us.userId = pg.userId
INNER JOIN dbo.mp_ModuleAccess AS md ON us.userId = md.userId
INNER JOIN dbo.mp_SystemAccess AS sys ON us.userId = sys.userId
INNER JOIN dbo.mp_CatalogAccess AS cat ON us.userId = cat.userId
GROUP BY us.userId
I think you can :
1 use LIMIT 1 when just one row of data
2 use a fairly typed column in the Join table and index it

there are INNER JOIN limits?

There are a limit to use INNER JOIN?
Original Query (Work just fine) I got 77 rows affected
SELECT atendimento.id, atendimento.responsavel, atendimento.ocorrencia,
atendimento.idcontrato, cliente.nome as clinome, cliente.id as cliid,
atendimento.d_ini, usuario.apelido, tipos.descricao, tipos.id as tip
FROM atendimento
INNER JOIN cliente ON atendimento.cliente=cliente.id
INNER JOIN usuario ON atendimento.usuario=usuario.id
INNER JOIN tipos ON atendimento.status=tipos.id
WHERE 1=1 AND (atendimento.status=10 OR atendimento.status=11)
ORDER BY tipos.id, atendimento.d_ini ASC
New Try: (Not working very well) it is limited to only 17 rows affected. The result here is the same except for the only 17 rows returned in my query.
SELECT atendimento.id, atendimento.responsavel, atendimento.ocorrencia,
atendimento.idcontrato, atend_os.protocolo, atend_os.tecnico,
cliente.nome as clinome, cliente.id as cliid, atendimento.d_ini,
usuario.apelido, tipos.descricao, tipos.id as tip
FROM atendimento
INNER JOIN atend_os ON atendimento.id=atend_os.protocolo
INNER JOIN cliente ON atendimento.cliente=cliente.id
INNER JOIN usuario ON atendimento.usuario=usuario.id
INNER JOIN tipos ON atendimento.status=tipos.id
WHERE 1=1 AND (atendimento.status=10 OR atendimento.status=11)
ORDER BY tipos.id, atendimento.d_ini ASC
What is going wrong here? does someone know why the result change?
Thanks a lot for any info!
The difference between the two queries is that the new query is accessing the atend_os table as well. Thus, any values of atendimento.cliente that doesn't also exist as a cliente.id is being filtered out.
An INNER JOIN requires that the value exist in both tables or the row is discarded.
try
SELECT atendimento.id, atendimento.responsavel, atendimento.ocorrencia,
atendimento.idcontrato, atend_os.protocolo, atend_os.tecnico,
cliente.nome as clinome, cliente.id as cliid, atendimento.d_ini,
usuario.apelido, tipos.descricao, tipos.id as tip
FROM atendimento
INNER JOIN cliente ON atendimento.cliente=cliente.id
INNER JOIN usuario ON atendimento.usuario=usuario.id
INNER JOIN tipos ON atendimento.status=tipos.id
LEFT JOIN atend_os ON atendimento.id=atend_os.protocolo
WHERE 1=1 AND (atendimento.status=10 OR atendimento.status=11)
ORDER BY tipos.id, atendimento.d_ini ASC
and see how many rows have atend_os.protocolo = NULL
Apparently this table atend_os does not contain a record for every atendimento. Perhaps you need a left join?