I have two tables:
LLOAN
LOANID SOURCEID LOAN_COMPANY ETC
1 1 3
2 1 3
3 1 1
4 2 1
5 2 1
6 2 1
7 3 1
8 3 1
COMPANY
CompanyID CountryID CompanyIDLLAS
1 1 1
2 1 2
3 1 3
4 2 1
5 3 1
6 4 1
And I want to join them. The SourceID refers to the CountryID and the LOAN_COMPANY refers to the CompanyID. Only country '1' has multiple companies, all the others just have one.
How can I join these two tables correctly? I've tried many different things, of which this came the closest:
SELECT Count(c.CompanyID) FROM dbo.LLOAN As l
LEFT JOIN dbo.Company As c ON c.CountryID = l.SourceID AND c.CompanyID = l.LOAN_COMPANY
But it leaves many rows blank. What is the correct way to join two tables with two conditions?
Try below Query:
SELECT Count(c.CompanyID)
FROM dbo.LLOAN As LL
LEFT JOIN dbo.Company As C
ON (C.CountryID = LL.SourceID)
AND (C.CompanyID = LL.LOAN_COMPANY)
You can group the condition using paranthesis like this:
SELECT Count(c.CompanyID)
FROM dbo.LLOAN As l
LEFT JOIN dbo.Company As c ON (c.CountryID = l.SourceID) AND (c.CompanyID = l.LOAN_COMPANY)
Related
Now this question got me so bad i am even not sure about the question headline.
I have 4 tables (topic t, user u, entry e, user_fav u_f), respectively
topic_id topic_name
1 abba
2 queen
3 rolling stones
user_id user_name
1 a
2 b
3 c
4 d
entry_id entry topic_fk(FK to t.topic_id) user_fk(FK to u.user_id)
1 ..... 1 1
2 ..... 1 2
3 ..... 2 2
4 ..... 2 3
5 ..... 3 4
user_fav_id user_name entry_fk(FK to e.entry_id)
1 a 1
2 a 2
3 a 3
4 c 4
5 d 5
and my question is how i can reach to t.topic_name using u_f.user_name?
An example to be more precise: if i want to use user a,
i need to get u_f.entry_fk (which are 1, 2 & 3)
then i need to get e.topic_fk(which are 1, 1 & 2)
and finally, i need to get t.topic_name (which are abba, abba & queen) (pay attention i need 2 abba's with different entry_ids with them)
You can use sql joins. Try this:
SELECT t.topic_name, e.entry_id
FROM `user` u
LEFT JOIN entry e ON u.user_id=e.user_id
INNER JOIN topic t ON e.topic_id=t.topic_id
WHERE u.user_name='a';
I'm very inexperienced. I've prepared a select statement which gives the information I need to populate a matches table. However it is not suitable because it contains a where clause. Is there a different way to use it, or how can I change it so that it is suitable for INSERT INTO.
The tables are as follows:-
match_order
match_order_id||match_descrip||first_player||second_player
1 1v2 1 2
2 1v3 1 3
3 2v3 2 3
4 1v4 1 4
5 2v4 2 4
6 3v4 3 4
entries
entry_id||round_id||league_id||box_id||box_position
1 1 1 1 1
2 1 1 1 2
3 1 1 1 3
4 1 2 1 4
5 1 2 1 2
6 1 2 1 1
7 1 2 1 1
matches
match_id||round_id||league_id||box_id||match_order_id||player1||player2
I need to insert new rows every month for a new round of matches. League size, box size & positions change each month.
This is the statement which gives the correct rows.
SELECT e.round_id, e.league_id, e.box_id, mo.match_order_id, e.entry_id as player1, e1.entry_id as player2
FROM match_order mo
LEFT JOIN entries e ON mo.first_player = e.box_position
LEFT JOIN entries e1 ON mo.second_player = e1.box_position
WHERE e.round_id = e1.round_id AND e.league_id = e1.league_id AND e.box_id = e1.box_id
ORDER BY round_id, league_id, box_id, match_order_id
Any help & advise would be greatly appreciated.
Thank you
Assuming match_id is an auto-increment column, you have the data for the other columns. You can just add the INSERT statement before your SELECT.
INSERT INTO matches(round_id, leage_id, box_id, match_order_id, player1, player2)
SELECT e.round_id, e.league_id, e.box_id, mo.match_order_id, e.entry_id as player1, e1.entry_id as player2
FROM match_order mo
LEFT JOIN entries e ON mo.first_player = e.box_position
LEFT JOIN entries e1 ON mo.second_player = e1.box_position
WHERE e.round_id = e1.round_id AND e.league_id = e1.league_id AND e.box_id = e1.box_id
Heres my query
SELECT
fsi_courier_assignment_print_master_listing.master_listing_id,
fsi_master_listing.transmittal_id,
fsi_transmittals.product_name,
fsi_transmittals.transmittal_id
FROM fsi_courier_assignment_print_master_listing
LEFT JOIN fsi_master_listing ON fsi_courier_assignment_print_master_listing.master_listing_id = fsi_master_listing.master_listing_id
LEFT JOIN fsi_transmittals ON fsi_master_listing.transmittal_id = fsi_transmittals.transmittal_id
WHERE dispatch_code_id=".$this->db->escape($dispatch_code_id)."
UNION ALL
SELECT
fsi_courier_assignment_print_master_listing_undelivered.master_listing_id,
fsi_master_listing.transmittal_id,
fsi_transmittals.product_name,
fsi_transmittals.transmittal_id
FROM fsi_courier_assignment_print_master_listing_undelivered
LEFT JOIN fsi_master_listing ON fsi_courier_assignment_print_master_listing_undelivered.master_listing_id = fsi_master_listing.master_listing_id
LEFT JOIN fsi_transmittals ON fsi_master_listing.transmittal_id = fsi_transmittals.transmittal_id
WHERE dispatch_code_id=".$this->db->escape($dispatch_code_id)."
fsi_courier_assignment_print_master_listing table
master_listing_id dispatch_code_id
2 2
5 2
36 2
37 2
134 2
135 2
136 2
137 2
138 2
139 2
140 2
fsi_courier_assignment_print_master_listing_undelivered table
master_listing_id dispatch_code_id
1 2
fsi_master_listing table
master_listing_id transmittal_id
1 1
2 1
5 2
36 2
37 2
134 3
135 3
136 3
137 3
138 3
139 3
140 3
fsi_transmittals table
transmittal_id product_name
1 Name 1
2 Name 2
3 Name 3
What Im trying to do is to get the combined result of product from fsi_courier_assignment_print_master_listing and fsi_courier_assignment_print_master_listing_undelivered where dispatch_code_id='2' and count them
My desire Output would be
Product Name Product Count
Name 1 2
Name 2 3
Name 3 7
Thanks in advance, hope somebody can help me to this..
Your query is fine, you just need to:
Add COUNT with GROUP BY product_name and put your query as a subquery.
The transmittal_id is specified two times in the two union queries, either remove one of them or give them different names (It might work fine in MySQL, but it is recommended not to do so).
So your query will be something like this:
SELECT
t.product_name,
COUNT(*) AS ProductCount
FROM
(
SELECT
ml.master_listing_id,
m.transmittal_id,
t.product_name
FROM fsi_courier_assignment_print_master_listing AS ml
LEFT JOIN fsi_master_listing AS m
ON ml.master_listing_id = m.master_listing_id
LEFT JOIN fsi_transmittals AS t
ON m.transmittal_id = t.transmittal_id
UNION ALL
SELECT
u.master_listing_id,
m.transmittal_id,
t.product_name
FROM fsi_courier_assignment_print_master_listing_undelivered as u
LEFT JOIN fsi_master_listing AS m
ON u.master_listing_id = m.master_listing_id
LEFT JOIN fsi_transmittals AS t
ON m.transmittal_id = t.transmittal_id
) AS t
GROUP BY t.product_name;
This will give you:
I have two tables, network_permissions and device_permissions like this:
NETWORK_PERMISSIONS
user_id network_id perm
1 1 3
1 2 4
2 3 3
2 2 1
3 2 1
4 2 3
DEVICES_PERMISSIONS
user_id network_id device_id perm
1 2 2 1
2 2 2 1
Now, I would take only user_id that for the net=2 are in the network_permissions table (within users with perm=3), but not in the device_permissions.
I would in this case the result is: 3 because is the user that have perm!=3 in the network_permission table for the net=2, but is not present in the device_permissions table.
Sorry if it's confused... Thank you for the help.
SELECT n.user_id
FROM network_permissions AS n
Left JOIN devices_permissions AS d ON n.user_id = d.user_id
WHERE d.user_id is null and n.network_id=2 and n.perm=3
Select NETWORK_PERMISSIONS.network_id FROM NETWORK_PERMISSIONS LEFT JOIN DEVICES_PERMISSIONS on NETWORK_PERMISSIONS.network_id = DEVICES_PERMISSIONS.network_id AND NETWORK_PERMISSIONS.user_id = DEVICES_PERMISSIONS.user_id
I have the following 2 tables:
1) Companies
ID CompanyName Abbreviation Notes
1 CompanyA CA ...
2 CompanyB CB ...
3 CompanyC CC ...
2) PlannedDeployments
ID CompanyID TypeID DepDate NumDeployed
1 1 2 09/2010 5
2 1 2 10/2010 5
3 1 3 09/2010 3
4 1 3 10/2010 3
5 1 4 10/2010 4
6 2 2 12/2010 10
7 2 4 10/2010 1
8 3 2 11/2010 6
Note that TypeID is a number between 1 and 5 describing what type of person is being deployed. For the purposes of this query, I'm interested in Type2 employees for each company and then the sum of Types 3 & 4 for each date. What I eventually want to end up with is a crosstab that looks like the following:
Crosstab
Date/Company CompanyA CompanyB CompanyC SumOfTypes3and4
09/2010 5 3
10/2010 5 8
11/2010 6
12/2010 10
The problem is that final column - the sum of Type 3 and Type 4 employees. The current crosstab that I have includes everything except that sum column and looks like the following:
TRANSFORM Sum(PlannedDeployments.NumDeployed) AS ["NumDeployed"]
SELECT PlannedDeployments.DepDate
FROM PlannedDeployments LEFT JOIN Companies ON Companies.ID=PlannedDeployments.CompanyID
WHERE PlannedDeployments.TypeID=2 AND (PlannedDeployments.DepDate Between FormFieldValue("Form", "Control") AND FormFieldValue("Form", "Control"))
GROUP BY PlannedDeployments.DepDate
PIVOT Companies.CompanyName;
The second part of that WHERE clause is just limiting the data by some form controls. Anyway - I'm having a lot of trouble getting that final column. Anyone have any ideas?
Edit: Building on the solution provided by Remou below, here's what the final query ended up looking like:
TRANSFORM Sum(PlannedDeployments.NumDeployed) AS ["NumDeployed"]
SELECT PlannedDeployments.DepDate, q.SumOfNumDeployed
FROM (SELECT PlannedDeployments.DepDate, Sum(PlannedDeployments.NumDeployed) AS SumOfNumDeployed
FROM PlannedDeployments
WHERE (((PlannedDeployments.[TypeID]) In (3,4)))
GROUP BY PlannedDeployments.DepDate) AS q
RIGHT JOIN (PlannedDeployments
INNER JOIN Companies ON PlannedDeployments.CompanyID = Companies.ID)
ON q.DepDate = PlannedDeployments.DepDate
WHERE PlannedDeployments.TypeID=2
AND (PlannedDeployments.DepDate Between FormFieldValue("Form", "Control")
AND FormFieldValue("Form", "Control"))
GROUP BY PlannedDeployments.DepDate, q.SumOfNumDeployed
PIVOT Companies.CompanyName;
You can use a subquery:
TRANSFORM Sum(PlannedDeployments.NumDeployed) AS ["NumDeployed"]
SELECT PlannedDeployments.DepDate, Sum(q.SumOfNumDeployed) AS SumOfSumOfNumDeployed
FROM (SELECT PlannedDeployments.DepDate, Sum(PlannedDeployments.NumDeployed) AS SumOfNumDeployed
FROM PlannedDeployments
WHERE (((PlannedDeployments.[TypeID]) In (3,4)))
GROUP BY PlannedDeployments.DepDate) AS q
RIGHT JOIN (PlannedDeployments
INNER JOIN Companies ON PlannedDeployments.CompanyID = Companies.ID)
ON q.DepDate = PlannedDeployments.DepDate
WHERE PlannedDeployments.TypeID=2
AND (PlannedDeployments.DepDate Between FormFieldValue("Form", "Control")
AND FormFieldValue("Form", "Control"))
GROUP BY PlannedDeployments.DepDate
PIVOT Companies.CompanyName;