I have two tables given below
service table
serviceid serviceName title isrestricted
--- -------------- ------ ----------
1 abc t1 0
2 asd t2 1
3 def t3 0
serviceRestricted table
sampletype serviceid
-------- ---------
a 2
output
serviceid serviceName title isrestricted
--- -------------- ------ ----------
1 abc t1 0
3 def t3 0
See the output .For Service id 2 isrestricted=1 and there is entry in the servicerestricted table. So serviceid 2 is not visible in the output
Left Join should solve your problem as:
select S.serviceid,S.serviceName,S.title,S.isrestricted
from #service S
Left Join #serviceRestricted SR on SR.serviceid = S.serviceid
where S.isrestricted = 1
Sample code here..
I am able to achive it by below query
select S.serviceid,S.serviceName,S.title,S.isrestricted
from service S
WHERE
((S.IsRestricted =1 and S.serviceid NOT IN (SELECT SERVICEID FROM servicerestricted WHERE SampleType=#SampleType) )
OR (S.IsRestricted is null OR S.isrestricted =0))
I have 2 tables like this:
Table_1 Table_2
--------------- -------------------
id bk_title bk_no bk_isbn
--------------- ---------------------
1 A_Book 1 ISBN0001
2 B_Book 1 ISBN0002
2 ISBN0003
I want to fetch records as:
BK_Title Num_Copies
----------------------------
A_Book 2
B_Book 1
Any SQL example would be of great help for me.
This is a simple INNER JOIN and GROUP BY with a COUNT:
Select T1.bk_title, Count(*) As Num_Copies
From Table_1 T1
Join Table_2 T2 On T1.id = T2.bk_no
Group by T1.bk_title
I have 3 tables just like:-
Table_1 Table_2 Table_3
------------------------ -------------------- --------------------
id bk_title strm_id bk_no bk_isbn s_id strm_name
----------------------- --------------------- -----------------------
1 A_Book 3 1 ISBN0001 3 Science
2 B_Book 4 1 ISBN0002 4 History
2 ISBN0003
I want to fetch records as
BK_Title Num_Copies Stream
---------------------------------------
A_Book 2 Science
B_Book 1 History
How do i do so.Please advice.
Try this:
SELECT BK_Title, COUNT(Table_2.bk_isbn) AS Num_Copies, Table_2.strm_name AS Stream
FROM Table_1
JOIN Table_2 on (Table_1.id = Table_2.bk_no)
JOIN Table_3 on (Table_1.strm_id = s_id)
GROUP BY BK_Title, strm_name
ORDER BY BK_Title
Heck, maybe 'joining' isn't even involved. I'm way out of my sql league here. Could someone please help me out w/ the following:
Table A
ItemId ItemLookup Price
------- ---------- -----
1 123456 10.00
2 234567 7.00
3 345678 6.00
Table B
ItemId Location Qty QtyOnHold
------- ---------- ----- ---------
1 1 26 20
2 1 0 0
3 1 12 6
1 2 4 0
2 2 2 1
3 2 16 8
What I'm hoping to get is something that looks like
ItemLookup, Price, (qty minus qtyonhold for loc1), (qty minus qtyonhold for loc2)
or 123456, 10.00, 6, 4
Thank you very much for any direction you can provide.
You can use conditional aggregation and a join:
select a.ItemLookup,
sum(case when Location = 1 then Qty - QtyOnHold end) as Location1,
sum(case when Location = 2 then Qty - QtyOnHold end) as Location2
from tableb b join
tablea a
on b.ItemId = a.ItemId
group by a.ItemLookup;
Somthing like this
select tablea.* ,
(select (qty- QtyOnHold) as qty from tableb where ItemId = tablea.ItemId ans Location = 1 ) as qtyl1,
(select (qty- QtyOnHold) as qty from tableb where ItemId = tablea.ItemId ans Location = 2) as qtyl2
from tablea
This assumes that there's only one row in TableB for each ItemID + Location combination. This is basically just a "pivot", you can learn various ways to do this in MySQL here.
SELECT ItemLookup, Price,
MAX(IF(Location = 1, Qty-QtyOnHold, 0)) avail1,
MAX(IF(Location = 2, Qty-QtyOnHold, 0)) avail2
FROM TableA AS a
JOIN TableB AS b ON a.ItemId = b.ItemId
GROUP BY a.ItemId
It seems to me that it may be possible to have a variable number of locations for each item. If this is the case, you need an aggregate function to convert/concatenate multiple rows into a column.
Here's an example with MySQL's group_concat function:
select a.itemlookup,a.price,group_concat('loc ',location,'=',b.x order by location) as qty_minus_qtyonhold
from tablea a,(select itemid,location,qty-qty_onhold x from tableb
group by itemid,location) as b
where a.itemid = b.itemid
group by 1
You'll get a result like this:
itemlookup price qty_minus_qtyonhold
---------- ------ ------------------
123456 10.00 loc 1=6,loc 2=4
234567 7.00 loc 1=0,loc 2=1
345678 6.00 loc 1=6,loc 2=8
Not sure what DBMS you're using but there are similar alternatives for Oracle and SQL Server
i've been trying to build this query. hoping someone can help.
I have 2 tables.
1 table contains
Code | name | Value | Period
1 name1 1 2010
2 name2 2 2010
table 2 contains
code | name |
1 name1
2 name2
3 name3
4 name4
what i want to be displayed is
1 name1 1
2 namw2 2
3 name3 0
4 name4 0
In some instances table 1 may have a value for all name variables in table 2
but where there are only 1,2,3 names i want it to display the other one but with a value of 0 or blank.
Try this:
select
T2.*,
isnull(T1.code, 0) as code -- or value
from
table2 T2
left outer join table1 T1 on T1.name = T2.name
You can replace isnull(T1.code, 0) as code with isnull(T1.value, 0) as value. I'm not sure what you're after ...