How to select uncommon entry from 2 tables [closed] - sql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
TableA TableB
Id Name Id Role
1 abc 1 Test
2 xyz
3 zxc
I want output as all Ids from table A which are not present in table B
O/p
Id Name
2 xyz
3 zxc

You could use the not in operator:
SELECT *
FROM TableA
WHERE id NOT IN (SELECT id FROM TableB)

Try this:
SELECT * FROM TABLE_1 WHERE (Id,Name ) NOT IN (SELECT Id,Name FROM TABLE_2)
Edit (after question was formatted properly):
SELECT * FROM TABLE_1 WHERE Id NOT IN (SELECT Id FROM TABLE_2)

Related

Create CASE WHEN labels based on DISTINCT values in a particular column [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have data of the following form:
ID
Category
Amount
1
A
100
1
B
200
1
B
150
1
C
500
2
B
20
3
A
100
1
B
100
I wish to GROUP BY the column ID, find out the DISTINCT types of Category present for each ID group and create a new column where I can create the following classification labels for each grouped ID based on the unique or distinct categories present and also calculate the corresponding sum of amount for each grouped ID. So the output should be the follows:
ID
Classification
Sum of Amount
1
ALL
950
1
B only
20
1
A and B only
200
I tried the following SQL code but it doesn't work, most likely because DISTINCT() command within a CASE WHEN statement cannot consider multiple values.
My query:
SELECT
ID,
(CASE WHEN DISTINCT(CATEGORY) IN ("A") then "A Only" WHEN WHEN DISTINCT(CATEGORY) IN ("B") THEN "B only"..........)
SUM(AMOUNT)
FROM Table
GROUP BY 1,2
I have tried multiple ways of using the DISTINCT statement with CASE WHEN but none of them works.
Hmmm . . . How about this?
select id,
(case when min(category) = max(category)
then min(category) || ' only'
when count(distinct category) = 3
then 'All'
when min(category) is NULL
then 'None'
else min(category) || ' and ' || max(category)
end)
from t
group by id;

Is there a way to break out a row if it meets a certain value? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
For example, let's say there's a table like this:
ID Food
-- ----
1 Fruit
So whenever it says Fruit, I want to break it out into 3 rows:
ID Food
-- ----
1 Apple
1 Banana
1 Orange
Any tips?
Hmmm . . . You could do:
select t.id, coalesce(a.alt, t.food) as food
from t outer apply
(select alt
from (values ('Apple'), ('Banana'), ('Orange')) v(alt)
where t.food = 'Fruit'
) a;
select ID, Food FROM YourTable WHERE food <> 'Fruit'
UNION
select ID, f2 FROM YourTable JOIN
(SELECT'Apple' f2
union
SELECT 'Banana' f2
UNION
SELECT 'ORANGE' f2
) DT
ON food = 'Fruit'

inserting data into table from string with splitting [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am trying to insert the XML string like
1,2,3,4,5,6,7,8,9,,1,1,1,1,2,23,4
into a table
I wanted to insert the data into following table in a such manner that after every 3 values it should go into next row is it possible do this?
and the xml string is generated in the program.
Your question is quite unclear, but you might be looking for something like this:
DECLARE #Numbers VARCHAR(MAX)='1,2,3,4,5,6,7,8,9,,1,1,1,1,2,23,4';
WITH Splitted AS
(
SELECT A.B.value('.','int') AS Number
,ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS RowInx
FROM (SELECT CAST('<x>' + REPLACE(#Numbers,',','</x><x>') + '</x>' AS XML)) AS Casted(AsXml)
CROSS APPLY Casted.AsXml.nodes('/x') AS A(B)
)
,Extended AS
(
SELECT (RowInx -1 ) / 3 AS Rank3
,Number
,REPLACE('Field_' + CAST((RowInx % 3) AS VARCHAR(1)),'_0','_3') AS ColumnName
FROM Splitted
)
SELECT p.*
FROM Extended
PIVOT
(
MAX(Number) FOR ColumnName IN(Field_1,Field_2,Field_3)
) AS p
The result (btw: the empty value in your example "...8,9,,1,1" is taken as "0"):
Rank3 Field_1 Field_2 Field_3
0 1 2 3
1 4 5 6
2 7 8 9
3 0 1 1
4 1 1 2
5 23 4 NULL

Output of SQL using one column only [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have a table with column name "Val" which contains following values:
Val
1
2
3
4
My desired output is as follows:
1 1
2 1
2 2
3 1
3 2
3 3
4 1
4 2
4 3
4 4
Thanks!
Ajaysharma2061
You can do this with a self join:
select t.val, t2.val
from t join
t t2
on t2.val <= t.val
order by t.val;
You can join the table on itself as if its a two different tables using the SQL join keyword. Following your attached image your SQL query is expected to be like this
SELECT t.Val, c.Val
FROM Table t, Table c
WHERE t.id <= c.id
Here is a link on
tutorial point
that can help you as well

Select rows that if I sum their value = 0 from table [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Let's say I have a table like this:
ID. Location. Value.
1. AGF. 10.00
2. VHJ. -20.00
3. AGF. -20.00
4. AGF. 5.00
5. KLZ. 50.00
6. AGF. 10.00
I want to select the rows that have same Location AND whose Values sum to zero.
In this case the result should be:
1
3
6
because those rows are all in Location AGF and they sum to 0 (10 + -20 + 10).
Try:
Select ID from YourTable where Location IN(
Select location from YouTable
Group By Location
Having sum(Value) = 0
)
You need to find all locations with zero sum using grouping and group filters (group by and having clauses respectively). This can be done in a subquery. Then select all IDs with the just selected locations.
select ID
from YOUR_TABLE
where Location in (
select Location
from YOUR_TABLE
group by Location
having sum(Value) = 0
)
You could use GROUP BY and HAVING, like this:
Select ID from tablelocation where Location IN(
Select location from tablelocation
Group By Location
Having sum(Value) = 0
)