Output of SQL using one column only [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 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

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;

How do I get the max records of a max subset in SQL [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 4 years ago.
Improve this question
I have the following tables:
Training Occurrence
Id Training Id Due Date
5 1 09/01/2018
6 1 09/15/2018
7 2 09/01/2018
Training Occurrence User
Id Training Material Occurrence Id User Id
1 5 'Chad'
2 5 'Chad'
3 6 'Chad'
4 6 'Chad'
5 7 'Chad'
My query needs to get the newest Training Material Occurrence User record of the newest Training Material Occurrence table. So if I pass in the user 'Chad'
I would want to see:
Id Occurrence Id User Training Id Due Date
4 6 'Chad' 1 09/15/2018
5 7 'Chad' 2 09/01/2018
Here is my query:
SELECT tmou.*, tmo.TrainingMaterialId, tmo.DueDate
FROM dbo.TrainingMaterialOccurrenceUser as tmou
INNER JOIN dbo.TrainingMaterialOccurrence as tmo on
tmou.TrainingMaterialOccurrenceId = tmo.Id
AND tmou.Id IN (SELECT MAX(tmou.Id)
FROM dbo.TrainingMaterialOccurrenceUser as tmou
WHERE tmou.UserId = #UserId
AND tmou.TrainingMaterialOccurrenceId IN (SELECT MAX(tmo.Id) as occurrenceId
FROM dbo.TrainingMaterialOccurrence as tmo
WHERE tmo.Id IN (Select TrainingMaterialOccurrenceId FROM dbo.TrainingMaterialOccurrenceUser as tmou1 WHERE tmou1.UserId = #UserId)
GROUP BY tmo.TrainingMaterialId)
GROUP BY tmou.TrainingMaterialOccurrenceId)
As you can see this is a mess. Any ideas on how I can clean this up.
Rank your rows with ROW_NUMBER:
SELECT Id, TrainingMaterialOccurrenceId, UserId, TrainingMaterialId, DueDate
FROM
(
SELECT
tmou.Id,
tmou.TrainingMaterialOccurrenceId,
tmou.UserId,
tmo.TrainingMaterialId,
tmo.DueDate,
ROW_NUMBER() OVER(PARTITION BY tmo.TrainingMaterialId
ORDER BY tmo.DueDate DESC,
tmo.Id DESC,
tmou.TrainingMaterialOccurrenceId DESC,
tmou.Id DESC) AS rn
FROM dbo.TrainingMaterialOccurrenceUser as tmou
INNER JOIN dbo.TrainingMaterialOccurrence as tmo
ON tmo.Id = tmou.TrainingMaterialOccurrenceId
)
WHERE rn = 1;

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

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
)

How to select uncommon entry from 2 tables [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 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)