How to get the Unmatched data from two table - sql-server-2008

How to get the Unmatched data from two table.I want to display the data which is not matched with CostomerMaster table. here is my table structure is
CustomerMaster Table:
CusID int Unchecked
CName varchar(MAX) Checked
Caddress varchar(50) Checked
Cloacation varchar(50) Checked
CMobile varchar(50) Checked
DailyDispatch Table:
DailyDispatchID int Unchecked
DcNo varchar(50) Checked
CustID varchar(50) Checked
Name varchar(50) Checked
OrderDate varchar(50) Checked
Gas varchar(50) Checked
I tried this:
SELECT * FROM customermaster C WHERE NOT EXISTS (SELECT 1 FROM dailydispatch D WHERE C.CNAME = D.NAME)
i am getting the output,but i need to display the output like
Name Gas OrderDate
The problem is how to display in the format,i used joins but not worked out any ideas for sort it out.

Try this.
SELECT DD.Name, DD.Gas, DD.OrderDate
FROM dailydispatch DD full outer join customermaster C on C.CNAME = DD.NAME)

Try this...
SELECT D.Name, D.Gas, D.OrderDate FROM DailyDispatch D
Left Join CustomerMaster C On C.CNAME = D.NAME
And C.CustID Is Null

Related

How to using where and join in mysql?

I have two tables;
database tftube;
table tftube_video;
database tfmember;
table member;
create table member(
no int primary key AUTO_INCREMENT,
member_no int,
name varchar(15) not null
);
create table tftube_reply(
no int primary key AUTO_INCREMENT,
member_no int,
video_name varchar(100)
);
I need to
1.tfmember.member.member_no equals tftube.tftube_reply.member_no
2.video_name=10.
3.Result variables are name, no, member_no,video_name.
So I try to this way.
select a.name from tfmember.member a join tftube.tftube_reply b on a.no = b.member_no where b.video_name=10;
but it's error
please help me
p.s tfmember.member.member_no contains tftube.tftube_reply.member_no.
Correct query will be:
select a.name from member a join tftube_reply b on a.no = b.member_no and b.video_name='10';
You should learn more about query before posting to ST.
Refer below link:
https://www.tutorialspoint.com/sql/index.htm
select a.name from tfmember.member a join tftube.tftube_reply b on a.no = b.member_no where b.video_name=10;
You join with table a auto increment col with table b member_no and video_name is varchar type that's why use '10' in where condition
Correct Query
select a.name from tfmember.member a join tftube.tftube_reply b on a.member_no = b.member_no where b.video_name='10';

MySQL query to select rows where one column matches and one column does not

Here are my table setups
Results_class
CREATE TABLE appfilter.results_class (
activity varchar(120) NOT NULL,
class varchar(255) NOT NULL,
PRIMARY KEY (activity, class)
)
Users
CREATE TABLE appfilter.user (
user varchar(15) NOT NULL,
name varchar(100) NOT NULL,
activity_full varchar(200) NOT NULL,
activity varchar(125) NOT NULL,
class varchar(125) NOT NULL
)
The results i want is simple. I want to see all rows where activity matches and class does not.
Example
Results_class
activity class
com.google.android.apps.plus com.google.android.apps.circles.realtimechat.ConversationListActivity
com.google.android.apps.plus com.google.android.apps.plus.phone.ConversationListActivity
Users
activity class
com.google.android.apps.plus com.google.android.apps.circles.realtimechat.ConversationListActivity
I need it to select this missing activity/class which is
activity class
com.google.android.apps.plus com.google.android.apps.plus.phone.ConversationListActivity<br>
I have tried this and it gives me all the activity/classes. Now if i can just remove the ones already in users thats what i need.
SELECT
rc.activity, rc.class
FROM results_class rc
INNER JOIN user b
ON b.activity = rc.activity
Try this .. The below query emulates the MINUS operation ie matches all in class a which are not in b.
SELECT a.activity,a.class FROM results_class a
left join user b on (a.activity = b.activity and a.class = b.class)
where b.class is null
Working fiddle
AFTER EDIT
considering the missed information in the question
"If there is a activity in results_class and not in users its bringing it back too. I only need matching activities to bring non matching classes"
select a1.activity,a1.class from (select a.activity,a.class FROM results_class a
left join user b on (a.activity = b.activity and a.class = b.class)
where b.class is null) a1 inner join user b1 on a1.activity = b1.activity
Try using a subquery with exists
SELECT rc.activity, rc.class
FROM results_class rc
JOIN users c on c.activity = rc.activity
WHERE NOT EXISTS
( SELECT 1
FROM user b
WHERE b.class = rc.class
)
So basically you get all of the results_class activities when joined to users.. then filter out the classes that are in results_class but not in users.
WORKING FIDDLE

How do I achieve the following using JOIN statement in mysql?

I have
TABLE 1: r_profile_token
Columns:
r_sequence int(45) AI PK
r_profileid varchar(45)
r_token varchar(300)
r_deviceType int(1)
r_date date
r_time time
and
TABLE 2: r_token_arn
Columns:
r_token varchar(300) PK
r_arn varchar(300)
I need a result of the form -
r_profileid
r_arn
r_deviceType
where I can specify the r_profileid.
So far my SQL statement is:
SELECT
b.r_arn,
a.r_deviceType
FROM
coffee2.r_profile_token a INNER JOIN
coffee2.r_token_arn b
ON a.r_token=b.r_token;
Which returns r_arn and r_deviceType but for all r_profileid?
How do I modify the statement so that it returns me r_arn and r_deviceType only corresponding to a specific r_profileid?
Use a WHERE clause.
SELECT B.R_ARN, A.R_DEVICETYPE
FROM COFFEE2.R_PROFILE_TOKEN A
INNER JOIN
COFFEE2.R_TOKEN_ARN B
ON A.R_TOKEN=B.R_TOKEN
WHERE R_PROFILEID = 'SOME_VALUE';
If you want for a single profileid, then use
WHERE R_PROFILEID = 'SOME_VALUE';
If you want for a range of profileIds , then use
WHERE R_PROFILE_ID IN ('VALUE1','VALUE2','VALUE3');
You can try this Query against your requirements.
SELECT
b.r_arn,
a.r_deviceType ,
a.r_profileid
FROM
r_profile_token a
INNER JOIN
r_token_arn b
ON
a.r_token=b.r_token
where r_profileid='profile name';
You need to put a where condition in your MYSql query.
select b.r_arn, a.r_deviceType from coffee2.r_profile_token a
INNER JOIN coffee2.r_token_arn b on a.r_token=b.r_token
where r_profileid = "Specific value";
select b.r_arn, a.r_deviceType, a.r_profileid from r_profile_token a
INNER JOIN r_token_arn b on
a.r_token=b.r_token;

MySQL query -- retrieve data from two different years

I cannot seem to get this MySQL query right. My table contains yearly inventory data for retail stores. Here's the table schema:
CREATE TABLE IF NOT EXISTS `inventory_data` (
inventory_id int unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY,
store_id smallint unsigned NOT NULL,
inventory_year smallint unsigned NOT NULL,
shortage_dollars decimal(10,2) unsigned NOT NULL
)
engine=INNODB;
Every store is assigned to a district which in this table (some non-relevant fields removed):
CREATE TABLE IF NOT EXISTS `stores` (
store_id smallint unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY,
district_id smallint unsigned not null
)
engine=INNODB;
I want to be able to retrieve the shortage dollar amounts for two given years for all the stores within a given district. Inventory data for each store is only added to the inventory_data table when the inventory is completed, so not all stores within a district will all be represented all the time.
This query works to return inventory data for all stores within a given district for a given year (ex: stores in district 1 for 2012):
SELECT stores.store_id, inventory_data.shortage_dollars
FROM stores
LEFT JOIN inventory_data ON (stores.store_id = inventory_data.store_id)
AND inventory_data.inventory_year = 2012
WHERE stores.district_id = 1
But, I need to be able to get data for stores within a district for two years, such that the data looks something close to this:
store_id | yr2011 | yr2012
For the specific result format that you need, you may try the following query:
SELECT `s`.`store_id`, `i`.`shortage_dollars` AS `yr2011`, `i1`.`shortage_dollars` AS `yr2012`
FROM `stores` `s`
LEFT JOIN `inventory_data` `i` ON `s`.`store_id` = `i`.`store_id`
AND `i`.`inventory_year` = 2011
LEFT JOIN `inventory_data` `i1` ON `s`.`store_id` = `i1`.`store_id`
AND `i1`.`inventory_year` = 2012
WHERE `s`.`district_id` = 1
Alternatively, you may as well try the next simpler query.
SELECT `s`.`store_id`, `i`.`inventory_year`, `i`.`shortage_dollars`
FROM `stores` `s`
LEFT JOIN `inventory_data` `i` ON `s`.`store_id` = `i`.`store_id`
WHERE `s`.`district_id` = 1
AND `i`.`inventory_year` IN (2011, 2012)
ORDER BY `s`.`store_id`, `i`.`inventory_year`
Hope it helps!
SELECT
stores.store_id,
inventory_data.inventory_year
inventory_data.shortage_dollars
FROM
(SELECT * FROM stores district_id = 1) stores
LEFT JOIN
(SELECT * FROM inventory_data
WHERE inventory_year IN (2011,2012)) inventory_data
USING (store_id)
;
or
SELECT
stores.store_id,
GROUP_CONCAT(inventory_data.shortage_dollars) dollars_per_year
FROM
(SELECT * FROM stores district_id = 1) stores
LEFT JOIN
(SELECT * FROM inventory_data
WHERE inventory_year IN (2011,2012)) inventory_data
USING (store_id)
GROUP BY stores.id,inventory_year;

Mysql query to check if all sub_items of a combo_item are active

I am trying to write a query that looks through all combo_items and only returns the ones where all sub_items that it references have Active=1.
I think I should be able to count how many sub_items there are in a combo_item total and then compare it to how many are Active, but I am failing pretty hard at figuring out how to do that...
My table definitions:
CREATE TABLE `combo_items` (
`c_id` int(11) NOT NULL,
`Label` varchar(20) NOT NULL,
PRIMARY KEY (`c_id`)
)
CREATE TABLE `sub_items` (
`s_id` int(11) NOT NULL,
`Label` varchar(20) NOT NULL,
`Active` int(1) NOT NULL,
PRIMARY KEY (`s_id`)
)
CREATE TABLE `combo_refs` (
`r_id` int(11) NOT NULL,
`c_id` int(11) NOT NULL,
`s_id` int(11) NOT NULL,
PRIMARY KEY (`r_id`)
)
So for each combo_item, there is at least 2 rows in the combo_refs table linking to the multiple sub_items. My brain is about to make bigbadaboom :(
I would just join the three tables usually and then combo-item-wise sum up the total number of sub-items and the number of active sub-items:
SELECT ci.c_id, ci.Label, SUM(1) AS total_sub_items, SUM(si.Active) AS active_sub_items
FROM combo_items AS ci
INNER JOIN combo_refs AS cr ON cr.c_id = ci.c_id
INNER JOIN sub_items AS si ON si.s_id = cr.s_id
GROUP BY ci.c_id
Of course, instead of using SUM(1) you could just say COUNT(ci.c_id), but I wanted an analog of SUM(si.Active).
The approach proposed assumes Active to be 1 (active) or 0 (not active).
To get only those combo-items whose all sub-items are active, just add WHERE si.Active = 1. You could then reject the SUM stuff anyway. Depends on what you are looking for actually:
SELECT ci.c_id, ci.Label
FROM combo_items AS ci
INNER JOIN combo_refs AS cr ON cr.c_id = ci.c_id
INNER JOIN sub_items AS si ON si.s_id = cr.s_id
WHERE si.Active = 1
GROUP BY ci.c_id
By the way, INNER JOIN ensures that there is at least one sub-item per combo-item at all.
(I have not tested it.)
See this answer:
MySQL: Selecting foreign keys with fields matching all the same fields of another table
Select ...
From combo_items As C
Where Exists (
Select 1
From sub_items As S1
Join combo_refs As CR1
On CR1.s_id = S1.s_id
Where CR1.c_id = C.c_id
)
And Not Exists (
Select 1
From sub_items As S2
Join combo_refs As CR2
On CR2.s_id = S2.s_id
Where CR2.c_id = C.c_id
And S2.Active = 0
)
The first subquery ensures that at least one sub_item exists. The second ensures that none of the sub_items are inactive.