I have the following two tables:
attributevalueassign
itm_id attr_value_id attr_id
----- ------------- -------
396 237 1
396 20 2
715 274 1
715 16 2
attributevalue
attr_id attr_value_id attr_value
------- ------------- ----------
1 237 3055020
1 274 2454518
2 16 Dunlop
2 20 Vogue
I need to be able to write a query that when I supply the item_id (ex:396), I get back the following result:
itm_id attr_value_id attr_id attr_value
------ ------------- ------- ----------
396 237 1 3055020
396 20 2 Vogue
When I try using INNER JOIN, I get more rows than I want:
SELECT dbo.select_attributevalue.attr_value, dbo.select_attributevalueassign.itm_id
FROM dbo.select_attributevalueassign INNER JOIN
dbo.select_attributevalue ON dbo.select_attributevalueassign.attr_id = dbo.select_attributevalue.attr_id
WHERE (dbo.select_attributevalueassign.itm_id = 396)
itm_id attr_value_id attr_id attr_value
------ ------------- ------- ----------
396 237 1 3055020
396 237 1 2454518
396 20 2 Dunlop
396 20 2 Vogue
So, my thought is that I probably shouldn't be using a JOIN, but I don't know what the alternative is. I've dug around on this site looking for a solution, and there are many questions on how to remove duplicate records using a join, but I didn't see one similar to what I was asking. My knowledge of SQL queries is basic, so I'm not sure what to do. Any help would be much appreciated!
You might need to do inner join by two columns attr_value_id and attr_id. Try this query:
SELECT dbo.select_attributevalue.attr_value, dbo.select_attributevalueassign.itm_id
FROM dbo.select_attributevalueassign
INNER JOIN dbo.select_attributevalue ON
dbo.select_attributevalueassign.attr_id = dbo.select_attributevalue.attr_id
AND dbo.select_attributevalueassign.attr_value_id = dbo.select_attributevalue.attr_value_id
WHERE (dbo.select_attributevalueassign.itm_id = 396)
Related
What I am looking for is the query that can return only selected packages filtered by the product id.
Even if I select only one product, query should return only record that is solely defined in where clause and should not return records if that specified product id is also involved in other packages as well.
Tables examples
Packages Table
id package_name
------ ------------
2 STANDARD
3 BUSINESS
5 BASIC
6 CLASSIC
package_product table
id package_id product_id
------ ---------- ------------
2 3 52
3 230 52
4 230 53
5 2 52
6 2 53
7 5 52
8 5 53
product table
id product_name
------ ----------------------
52 Logo Design
53 Website Design
54 Business Card Design
Query I am trying to use
SELECT p.id, p.package_name, p.package_price, pp.product_id
FROM package_product pp INNER JOIN packages p ON p.id = pp.package_id
WHERE pp.product IN (52, 53)
Result what i get
id package_name package_price product_id
------ ------------ ------------- ------------
3 BUSINESS 139 52
230 ULTIMATE 1149 52
230 ULTIMATE 1149 53
2 STANDARD 89 52
2 STANDARD 89 53
5 BASIC 265 52
5 BASIC 265 53
What I am looking for
id package_name package_price product_id
------ ------------ ------------- ------------
230 ULTIMATE 1149 52
230 ULTIMATE 1149 53
2 STANDARD 89 52
2 STANDARD 89 53
5 BASIC 265 52
5 BASIC 265 53
You may join to a subquery which imposes the restriction that a matching package have both 52 and 53 products:
SELECT
p.id,
p.package_name,
p.package_price,
pp.product_id
FROM package_product pp
INNER JOIN
(
SELECT package_id
FROM package_product
WHERE product_id IN (52, 53)
GROUP BY package_id
HAVING COUNT(DISTINCT product_id) = 2
) t
ON pp.package_id = t.package_id
INNER JOIN packages p
ON p.id = pp.package_id;
Demo
The subquery aliased as t above finds those packages having both the 52 and 53 products. Used as an inner join, it filters off non matching packages.
I have 3 tables and I want to query them and get results based on
Table 1 Team ID .
I think its simple but I failed to work this out.
All I'm trying to do is get their select their data from other tables and group them by TeamID.
The structure of tables is as follows:
Table 1
This table holds the teams entries by PersonID - referenced by PersonID Table, Each team is in one row
[TeamID] Tournament_id Person_1_ID Person_2_ID Person_3_ID Country_ID
--- --------- -------- ---------- --------- --------
1 77789 123 124 125 90
2 77789 126 127 128 95
3 77789 129 130 131 5
.........
Table 2
This is the person table PersonID = Primary Key
[PersonID] Name Dob Email Country_ID
--------- ------- -------- ---------- ------------
123 John 19/03/1992 John#live.com 90
124 Moe 20/10/1995 Moe#live.com 90
125 Sami 10/05/1989 Sami#example.com 90
126 Kim 30/01/1990 Kim#company.com 95
.......
Table 3
Participation table
[ParticipationID] PersonID tournament_id Country_id
----------------- ---------- ------------- -------------
9999901 123 77789 90
9999902 124 77789 90
9999903 125 77789 90
9999904 126 77789 95
9999905 127 77789 95
.......................
How do I get this output
TeamID Tournament_Id Name Country_ID
------ ----------- ----- ------
1 777789 John 90
1 777789 Moe 90
1 777789 Sami 90
2 777789 Kim 95
Something like this:
SELECT t.TeamID
,t.Tournament_Id
,pr.Name
,pr.Country_Id
FROM Team AS t
INNER JOIN Participation AS p ON p.tournament_id = t.Tournament_id
INNER JOIN Person AS pr ON pr.PersonID = p.PersonID
WHERE Tournament_Id = 777789
Need to fetch data from master table based on child table condition.
Master Table:-
ID Name Address
1 abc xyz
2 abs txt
3 aui tre
4 pop top
5 the tre
6 pot tos
7 pog sop
8 pat top
9 bat cric
10 not kot
Child Table:-
chid shootid imagename IDFK
101 234 123ab.jpg 3
102 234 54abcab.jpg 3
103 235 123abc.jpg 3
104 236 12390acb.jpg Null
105 235 12332aab.jpg 8
106 234 123786ab.jpg 4
107 234 54789abcab.jpg 10
108 235 122343abc.jpg 10
109 235 122123acb.jpg 4
110 234 12123aab.jpg 9
111 234 1223ab.jpg Null
112 233 5432abcab.jpg Null
113 235 1239abc.jpg Null
114 236 1238acb.jpg 2
115 236 12356aab.jpg 2
116 236 1235ab.jpg 2
117 236 545abcab.jpg Null
118 237 1233abc.jpg 1
119 237 1223acb.jpg 1
120 237 1123aab.jpg 1
In Child table IDFK is the foreign key and ID in Master table is the primary key of that.
Now i want to show those name from master table that doesn't exist on child table filter on shootid like where childtable.Shootid=234. I tried but not find the desired output.Every time it just return's the same out for different shootid as well.
Please help me and show me the right query for that.
I don't know if I am understand you well or not but I think this is what you want,
Select * from [master] m
where m.ID not in (Select IDFK from detail where shootid=234)
I think this is what you are looking for.
Select distinct m.name from master m LEFT OUTER JOIN child c
ON m.id = c.id and
c.shootid=234
where
c.id is null
My two tables are
Entry
event_id competitor_id place
101 101 1
101 102 2
101 201 3
101 301 4
102 201 2
103 201 3
second table lists prizes on offer for the events
Prize
event_id place money
101 1 120
101 2 60
101 3 30
102 1 10
102 2 5
102 3 2
103 1 100
103 2 60
103 3 40
From this I am looking to show all the information from the Entry table alongside the amount of money they won for their respected placing. If they failed to place in the money then a 0 will be displayed.
Any help would be appreciated.
try this:
SELECT a.Event_ID,
a.Competitor_ID,
a.Place,
COALESCE(b.money, 0) as `Money`
FROM entry a left join prize b
on (a.event_id = b.event_ID) AND
(a.place = b.Place)
hope this helps.
EVENT_ID COMPETITOR_ID PLACE MONEY
101 101 1 120
101 102 2 60
101 201 3 30
101 301 4 0 -- << this is what you're looking for
102 201 2 5
103 201 3 40
Try this:
select e.*, coalesce(p.money, 0) money from entry e
left join prize p
on e.event_id = p.event_id and e.place = p.place
You can play with the fiddle here.
SELECT * FROM Entry NATURAL LEFT JOIN Prize;
If you absolutely want 0 instead of NULL for the "money" when no prize was won (but how can you differentiate between a prize of 0 and no prize?):
SELECT Entry.*, COALESCE(money, 0) AS money FROM Entry NATURAL LEFT JOIN Prize;
See them both on sqlfiddle.
I want to left join 2 tables.
So all attributes of left table should be shown in result table, no matter if it can join with other table or not.
When I do the follwing, I dont get the expected result
week_table:
date kw note
---------- -- ----
2012-04-01 0 NULL
2012-04-02 0 NULL
fact_table:
id number_of_application number_of_cluster number_of_jvm number_of_node number_of_was fk_wasversion fk_date fk_domain fk_osname fk_osarch fk_osversion fk_stage
-- --------------------- ----------------- ------------- -------------- ------------- ------------- ---------- --------- --------- --------- ------------ --------
1 114 8 80 18 18 6.0 2012-04-01 domain1 Linux sparc 2 stage1
2 114 8 80 18 18 6.0 2012-04-02 domain1 Linux sparc 2 stage1
3 114 8 80 18 18 6.0 2012-04-01 domain1 AIX sparc 2 stage1
4 114 8 80 18 18 6.0 2012-04-02 domain1 Solaris sparc 2 stage1
When I do this:
select
w.date,
coalesce(sum(f.number_of_was), 0)
from
week_table w
left join fact_table f on (w.date = f.fk_date)
where f.fk_osname = "AIX"
group by w.date;
I only get :
date coalesce(sum(f.number_of_was), 0)
---------- ---------------------------------
2012-04-01 18
Expected:
date coalesce(sum(f.number_of_was), 0)
---------- --------------------------------
2012-04-02 18
Does someone know why?
Best Regards
Move the criteria on the outer joined table from the WHERE clause to the ON clause:
select
w.date,
coalesce(sum(f.number_of_was), 0)
from
week_table w
left join fact_table f on (w.date = f.fk_date and f.fk_osname = "AIX")
group by w.date;
where f.fk_osname = "AIX"
I guess there is only one record with this osname?
because of where f.fk_osname = "AIX"
if the data doesnt exist on your right table, it is NULL, and you are asking to retrieve only the rows where fk_osname is = "AIX"`
you can do WHERE (f.fk_osname = "AIX" OR f.fk_osname IS NULL)