I have the following 3 tables:
Clients AS c
--------
ClientID,
ClientGUID,
InitializationDate,
LastCheckin
ClientServices AS cs
---------------
ClientID,
ServiceID,
Status,
Version
Services AS s
---------
ServiceID,
Name,
Description
I need to build a query that will give me the following results:
s.Name, cs.Version
I need it to act as a left join, in the sense that I'd like to have all Services display, regardless of whether that service is in ClientServices. The selected version will simply display as NULL in this case.
I tried doing some simple joins, but every combination of LEFT JOIN and JOIN I used resulted in ONLY the ClientServices that belonged to that client showing up. An example:
SELECT s.`Name`,
cs.`Version`
FROM `Services` s LEFT JOIN ClientServices cs ON
s.`ServiceID` = cs.`ServiceID`
JOIN Clients c ON
cs.`ClientID` = c.`ClientID`
WHERE `ClientGUID`='thisisanewguid'
I was able to finally get the desired result with this query:
SELECT s.`Name`,
cs.`Version`
FROM `Services` s LEFT JOIN ClientServices cs
ON s.`ServiceID` = cs.`ServiceID`
WHERE cs.ClientID = (
SELECT ClientID
FROM Clients
WHERE ClientGUID='thisisanewguid'
)
OR cs.ClientID IS NULL
but I feel as though its a little "hacky". Is there a better way to get the same result set, but without doing multiple selects in one query? (preferably with only joins, but I'm not sure if thats possible anymore)
Example Data Set:
Clients:
ClientID, ClientGUID, InitializationDate, LastCheckin
1, 'xxxxxxxxxxxxxxxx', 10/10/2017, 10/12/2017
2, 'thisisanewguid', 05/23/2017, 10/12/2017
ClientServices:
ClientID, ServiceID, Status, Version
1, 1, 1, '0.1'
2, 1, 1, '0.1'
2, 2, 1, '0.2'
Services:
ServiceID, Name, Description
1, InITManager, 'Manages and updates all InIT services.'
2, InITIAM, 'InIT's Inventory/Asset Management.'
3, InITTesting, 'testing'
Desired Result Set WHERE ClientGUID='thisisanewguid':
Name, Version
InITManager, '0.1'
InITIAM, '0.2'
InITTesting, NULL
for obatin the result you showed in question you could use left table with left join without where clause for column involved id left join
SELECT s.`Name`,
cs.`Version`
FROM `Services` s
LEFT JOIN ClientServices cs ON s.`ServiceID` = cs.`ServiceID`
LEFT JOIN Clients c ON c.ClientID = cs.ClientID AND c.`ClientGUID`='thisisanewguid'
Hi can you please try the following query
SELECT cs.Version, s.Name
FROM ClientServices cs
RIGHT JOIN Clients c ON c.ClientID = cs.ClientID
RIGHT JOIN Services s ON s.ServiceID = cs.ServiceID
Related
I've the following SQL Query which runs perfectly fine but now i want to calculate the count based on the following scenario:
SELECT d.vseverity, v.vulnstatus, v.vtitleid, d.vtitle
FROM vulnsummary v
JOIN project p ON v.projid = p.projid
AND v.stagename = p.currentstage
JOIN datasets d ON v.vtitleid = d.datasetid
The current Output is:
Now i want to show the count like this way:
High (Open) - 2
High (Closed) - 0
Medium (Open) - 1
Medium (Closed) - 0
Low (Open) - 3
Low (Closed) - 1
Please help me to solve this query, Thank You
You need to CROSS JOIN the distinct sets of severity and status values and then LEFT JOIN that to your table to allow you to count the values of each severity/status combination. Without sample data it's hard to be certain but something like this should work:
SELECT sv.vseverity, st.vulnstatus, COUNT(v.vseverity) AS count
FROM (
SELECT DISTINCT vseverity
FROM datasets
) sv
CROSS JOIN (
SELECT DISTINCT vulnstatus
FROM vulnsummary
) st
LEFT JOIN (
SELECT d.vseverity, v.vulnstatus
FROM vulnsummary v
JOIN project p ON v.projid = p.projid
AND v.stagename = p.currentstage
JOIN datasets d ON v.vtitleid = d.datasetid
) v ON v.vseverity = sv.vseverity AND v.vulnstatus = st.vulnstatus
GROUP BY sv.vseverity, st.vulnstatus
I don't have your full dataset, however, a RIGHT OUTER JOIN to a master volnstatus table will enable (the volnstatus table showing all options i.e. 'Open', 'Closed'). A rough draft example, with only the volnstatus table populated:
SELECT COUNT(s.vulnstatus) CountOf, t.vtype
FROM dbo.vusummary s
RIGHT OUTER JOIN
vusummarytype t
ON s.vulnstatus = t.vtype
GROUP BY t.vtype
I have two tables named chatMaster and chatMessages , i was trying to left join the two tables to get all data from master and latest message for each chat. I am using this query to join the two tables
SELECT c.id, c.p1, c.p2, m.toP, m.message, m.createdOn FROM chatMaster c
left join chatMessages m on c.id = m.id group by m.id ;
This query is for selecting only one row for each entry in chatMaster, but it is selecting the first message for every entry like,
1, 2018-11-24 00:40:08, HI!, 99e22056-ee7f-11e8-bc28-8acac6f59ef9, 1001
2, 2018-11-24 00:40:21, HI! There, 99e22056-ee7f-11e8-bc28-8acac6f59ef9, 1
3, 2018-11-24 01:33:12, HI!, e3345a17-ee7f-11e8-bc28-8acac6f59ef9, 2
this is the result of select * from chatMessages, There are two entries for chat id 99e22056-ee7f-11e8-bc28-8acac6f59ef9 and i want the send one which is send on 2018-11-24 00:40:21 but the result of my join query is
99e22056-ee7f-11e8-bc28-8acac6f59ef9, 1001, 1, 1001, HI!, 2018-11-24 00:40:08
e3345a17-ee7f-11e8-bc28-8acac6f59ef9, 2, 10001, 2, HI!, 2018-11-24 01:33:12
It is selecting the first message. How to select the latest message? What changes should i make to get the latest message from chatMessages instead of first message. please ask if you need more details
In a Derived Table, you can get the maximum values of createdOn (latest createdOn) for every chat-master id. You can then join this result-set to the main table, to get only the row corresponding to latest createdOn.
SELECT c.id, c.p1, c.p2, m.toP, m.message, m.createdOn
FROM chatMaster c
LEFT JOIN
( SELECT id, MAX(createdOn) AS latest_createdOn
FROM chatMessages
GROUP BY id
) AS dt
ON dt.id = c.id
LEFT JOIN chatMessages m
ON m.id = c.id AND
m.createdOn = dt.latest_createdOn
I have a query as follows:
SELECT
staff_names.staff_ID AS sid
staff_names.name AS name,
staff_names.rec_type AS rec_type,
prod_staff.specialized AS specialized,
compspec.name AS compspec_name
FROM staff_names JOIN prod_staff USING (staff_ID)
LEFT JOIN (prod_staff_compspec JOIN company_list USING (comp_ID)) compspec
USING (prod_ID, staff_ID, role_ID)
WHERE prod_staff.role_ID = 2
AND prod_staff.prod_ID = 27
AND prod_staff.asst = 'n'
AND episode IS NOT NULL
ORDER BY name
Running this as-is says there's an error near the 'compspec' alias. Removing that and changing 'compspec' to 'company_list' in the SELECT clause returns no rows, even though it should return 1 with the given values. The left join seems to be the problem, but I don't how it should be formatted.
The prod_staff table has prod_ID, staff_ID and role_ID fields. prod_staff_compspec has these and a comp_ID field. prod_staff may or may not have a matching prod_staff_compspec row, but prod_staff_compspec always has a matching company_list row.
What I want to do is retrieve a list of all staff names associated with a given role_ID and prod_ID in the prod_staff table, as well as a company name from the company_list table, if a link to such exists in the prod_staff_compspec table (only a small minority have one).
Switched to ON to define the table relations. LEFT JOIN (prod_staff_compspec JOIN company_list USING (comp_ID)) compspec is switched to 2 left join.
select a.staff_id sid, a.name, a.rec_type, b.specialized, d.name compspec_name
from staff_names a
join prod_staff b on a.staff_id = b.staff_id
left join prod_staff_compspec c on b.prod_id = c.prod_id and b.staff_id = c.staff_id and b.role_id = c.role_id
left join company_list d on c.comp_id = d.comp_id
where b.role_id = 2 and b.prod_id = 27 and b.asst = 'n' and episode is not null
order by a.name;
I'm realizing a project which raises me the next tables:
For being more specific: First table:
'docs' => doc_id, doc_type_type_id, clients_cli_id
where doc_type_type_id
invoices
reference guides
Second Table:
'client' => cli_id
What I try to do is to join Client with doc that My query is:
Show Client with his invoice and reference guide:
SELECT c.cli_name, d1.doc_file as f1 , d2.doc_file as f2 FROM clients c INNER JOIN docs d1 ON d1.client_cli_id = c.cli_id INNER JOIN docs d2 ON d2.client_cli_id = c.cli_id WHERE d1.doc_fec=d2.doc_fec
select * from docs
inner join client on docs.clients_cli_id = client.cli_id
where doc_type_type_id = 1
Something in this format should give you all invoices joined to client.
Help needed for the below query. Thanks
SELECT b.SchemeCode_Db,
'DBELE',
1,
SecurityCode_Db,
"DIRECT",
Qty,
Price,
((Commission + TransferCharge) / Qty) AS Charges,
"",
iif(Buy_sell_code = "1110",
((Qty * Price) + Commission + TransferCharge),
iif(Buy_sell_code = "1120", ((Qty * Price) - Commission
- TransferCharge
))) AS totalCost,
BrokerCode_Db,
"",
Deal,
Format(Tradedate, "dd/MM/yyyy"),
Format(Valuedate, "dd/MM/yyyy"),
'', 'BSE', 'CH', 'D', '',
iif(Buy_sell_code = "1110",
'PUR',
iif(Buy_sell_code = "1120", 'SAL')) AS txn,
'USD' AS cur
FROM tbl_EQUITYINPUT a
LEFT JOIN tbl_EQUITYMapping b
ON FundCode = SchemeCode_Client
LEFT JOIN tbl_EQUITYMapping c
ON Ticker = SecurityCode_Client
LEFT JOIN tbl_EQUITYMapping d
ON Broker = Brokercode_Client
The Access db engine requires parentheses in the FROM clause when your query includes more than one JOIN. I suspect this FROM clause version will be a step closer to something the db engine will accept.
FROM
((tbl_EQUITYINPUT a
LEFT JOIN tbl_EQUITYMapping b
ON FundCode = SchemeCode_Client)
LEFT JOIN tbl_EQUITYMapping c
ON Ticker = SecurityCode_Client)
LEFT JOIN tbl_EQUITYMapping d
ON Broker = Brokercode_Client
However, I'm uncertain whether the db engine will be confused sorting out which join field comes from which table source. I would prefix those field names with the proper table alias.
But I think your most direct route to joy for this may be to start with a new query in Design View in the query designer. Add tbl_EQUITYINPUT and 3 copies of tbl_EQUITYMapping and assign the aliases. Then set up your joins between them while still in Design View. The query designer understands the join rules which keep the engine happy, so will guide you to the correct join syntax. And it will also include the aliases with the field names in your joins.
I'm wondering if it's confused over which instance of tbl_EQUITYMapping to join against.
I'd expect to see your from block look a little more like this:
select *
FROM tbl_EQUITYINPUT a
LEFT JOIN tbl_EQUITYMapping b
ON a.FundCode = b.SchemeCode_Client
LEFT JOIN tbl_EQUITYMapping c
ON a.Ticker = c.SecurityCode_Client
LEFT JOIN tbl_EQUITYMapping d
ON a.Broker = d.Brokercode_Client
In this case, you are joining three different copies of tbl_EQUITYMapping with tbl_EQUITYINPUT. But I don't think that is what you want to do. If you are pulling data from tbl_EQUITYMapping, the select won't know which copy to pull it from.
I expect that SchemeCode_Client, SecurityCode_Client, and Brokercode_Client form a composite key for tbl_EQUITYMapping. In which case I would expect a from block that looked like this.
select *
FROM tbl_EQUITYINPUT a
LEFT JOIN tbl_EQUITYMapping b
ON (a.FundCode = b.SchemeCode_Client
AND a.Ticker = b.SecurityCode_Client
AND a.Broker = B.Brokercode_Client)
In this case, you'd get only one copy of tbl_EQUITYMapping
You could equally well do something like this:
select *
from tbl_EQUITYINPUT a,
tbl_EQUITYMapping b
where
(a.FundCode = b.SchemeCode_Client
AND a.Ticker = b.SecurityCode_Client
AND a.Broker = B.Brokercode_Client)
(the designer may or may not optimise that for you in the former version)