SQL values from one table into multiple columns [duplicate] - sql

This question already has answers here:
Convert Rows to columns using 'Pivot' in SQL Server
(9 answers)
Closed 2 years ago.
I have made a code as followed:
select
PMGCONTACTPERSON.RECID AS 'Recid'
,PMGCONTACTPERSON.ROLEID AS 'Rol'
,DIRPARTYTABLE.NAME
from PMGCONTACTPERSON
left join DIRPARTYTABLE
ON DIRPARTYTABLE.PARTYID = PMGCONTACTPERSON.PARTYID
What I would like te get is columns named after the rolid and values filled with the names.
Any ideas?

I think you are looking to Pivot the table i.e. rows to columns. You can use PIVOT clause in SQL SERVER.
SELECT * FROM (
SELECT ROLE_NAME, NAME FROM TEST_NAME) T
PIVOT(MAX(NAME) FOR ROLE_NAME IN ([MANAGER], [ENGINEER])) AS PIVOT_TABLE;

Related

How to get min value by comparing multiple columns in SQL [duplicate]

This question already has answers here:
What's the best way to select the minimum value from several columns?
(20 answers)
Closed 4 months ago.
I'm trying to get the min value from the table by comparing four adjacent columns (Date-1 to Date-4) in that table and updating the min value in the final column. But I'm receiving only NULL as the min value instead of getting the min value from four columns.
I'm using case for comparing the columns.
As SQL Server does not yet support LEAST in its current version SQL Server 2019, you can apply a lateral cross join on an aggregation query on the values instead.
select *
from mytable t
cross apply
(
select min(dt) as final_min_date
from (values (t.date_1), (t.date_2), (t.date_3), (t.date_4)) all_dates (dt)
) min_date;

Add a Column from another table based on a select query from a separate table [duplicate]

This question already has answers here:
SQL - Getting a column from another table to join this query
(2 answers)
Closed 4 years ago.
Looking for some help here on adding a column of data to my query results from another table.
So the key on both tables is customer_number.
I have a query that is the following:
SELECT [CUSTOMER_NUMBER]
,[CUSTOMER_NAME]
,count(*) 'YEARS'
FROM [DB].[dbo].[tablefromxlsimport$]
group by [CUSTOMER_NUMBER]
,[CUSTOMER_NAME]
This returns the information I am looking for from the tableformxlsimport$ but I would like to add a column of data from another table fullcustomerlist$ based on the results of the query that will append a column of data that is customer_Current_title.
Hopefully I explained this correctly
Please see the below query:
SELECT
A.[CUSTOMER_NUMBER]
,A.[CUSTOMER_NAME]
,A.YEARS
B.customer_Current_title
FROM
( SELECT
A.[CUSTOMER_NUMBER]
,A.[CUSTOMER_NAME]
,count(*) 'YEARS'
FROM [DB].[dbo].[tablefromxlsimport$] AS A
group by [CUSTOMER_NUMBER]
,[CUSTOMER_NAME]
) AS A
INNER JOIN dbo.fullcustomerlist$ AS B on A.CUSTOMER_NUMBER = B.CUSTOMER_NUMBER

Want to add multiple values in "NOT Like" in where condition in sql [duplicate]

This question already has answers here:
SQL Query with NOT LIKE IN
(9 answers)
Closed 4 years ago.
I have a query in which I want to check for multiple values which should not be in where clause. I want to exclude those records which have those values
Select * from Order where order_number NOT LIKE ('%asd%','%ass%','%asdd%')
I guess you'll have to go with a typical AND:
Select * from Order where
order_number NOT LIKE '%asd%'
AND order_number NOT LIKE '%ass%'
AND order_number NOT LIKE '%asdd%'

SQL Select all rows with specific value [duplicate]

This question already has an answer here:
How can I select a value from one row in SQL Server?
(1 answer)
Closed 6 years ago.
Trying to select all rows with the value 33487MO in the column ORMSTRID
This is my current query -
SELECT * from MyTable
Where ORMSTRID = "33487MO"
But it gives me an error that is saying Invalid column name '33487MO'
SELECT * from MyTable
shows me all of the rows and columns from the table, and ORMSTRID is definitely the column name.
You should use single quote with string data
According your example your query should be:
select * from mytable
where ORMSTRID like '33487MO'

PostgreSQL ORDER BY clause on numerical portion of text column [duplicate]

This question already has answers here:
SQL multiple column ordering
(9 answers)
PostgreSQL ORDER BY issue - natural sort
(9 answers)
Closed 7 years ago.
I have a table that has a column as primary key and the values are
G1,G2,G3,...Gn
I would like to order the data but the problem is when I use the ORDER BY clause it displays data as:
G1,G10,G11... G2,G20,G21, ... G3,G30,G31....
The query I use is:
select * from myTable order by id asc;
Your id column is obviously of some text data type so the ordering is alphabetical, not by the number. To get it to work, strip the 'G' from the id column when ordering:
SELECT * FROM mytable
ORDER BY right(id, -1)::integer;