Create dynamic coloumns in sql server 2014 when duplicate values ​​appear

I am running the following sql:

SELECT DISTINCT 
    Anvendelseskoder.[Usage Code] AS [Byggeanvendelseskode],
    Anvendelseskoder.[Usage Code Value] AS [Byggeanvendelse],
    HeleDanmark_DAWA.KVHx
FROM Anvendelseskoder 
    RIGHT JOIN HeleDanmark_DAWA 
        ON Anvendelseskoder.KVHx = HeleDanmark_DAWA.KVHx
WHERE HeleDanmark_DAWA.postnr=6720 AND Anvendelseskoder.[Usage Code]>0

      

This gives me the following output: (new user, spam messages, sorry!)

enter image description here

And I want something more:

enter image description here

I checked this question about using PIVOT and JOIN together , but I can't seem to get it to work. Can anyone help me with this?

Update: The code I tried to do but cannot get it to do what I want: enter image description here

WITH Acode 
    AS (SELECT 
        HeleDanmark_DAWA.KVHx,
        Anvendelseskoder.[Usage Code],
        Anvendelseskoder.[Usage Code Value]
    FROM Anvendelseskoder
        RIGHT JOIN HeleDanmark_DAWA
            ON HeleDanmark_DAWA.KVHx = Anvendelseskoder.KVHx)
SELECT *
FROM Acode PIVOT( COUNT([Usage Code]) FOR [Usage Code Value] IN (
    [Stuehus til landbrugsejendom],
    [Fritliggende enfamilieshus (parcelhus).],
    [Række-, kæde- eller dobbelthus (lodret adskillelse mellem enhederne).],
    [Etageboligbebyggelse (flerfamiliehus, herunder tofamiliehus (vandret adskillelse mellem enhederne)).],
    [Kollegium],
    [Døgninstitution (plejehjem, alderdomshjem, børne- eller ungdomshjem).],
    [Anden enhed til helårsbeboelse],
    [Erhvervsmæssig produktion vedrørende landbrug, skovbrug, gartneri, råstofudvinding og lign.],
    [Erhvervsmæssig produktion vedrørende industri, håndværk m.v. (fabrik, værksted o. lign.)],
    [El-, gas-, vand- eller varmeværk, forbrændingsanstalt o. lign.],
    [Anden enhed til produktion og lager i forbindelse med landbrug, industri o. lign.],
    [Transport- og garageanlæg (fragtmandshal, lufthavnsbygning,banegårdsbygning o. lign.)],
    [Engroshandel og lager.],
    [Detailhandel m.v.],
    [Pengeinstitut, forsikringsvirksomhed m.v.],
    [Kontor og liberale erhverv bortset fra offentlig administration],
    [Offentlig administration.],
    [Hotel, restauration, vaskeri, frisør og anden servicevirksomhed.],
    [Anden enhed til handel, transport etc.],
    [Biograf, teater, erhvervsmæssig udstilling m.v.],
    [Bibliotek, museum, kirke o. lign.],
    [Undervisning og forskning (skole, gymnasium, forskningslaboratorium).],
    [Hospital, fødeklinik o. lign.],
    [Daginstitution.],
    [Anden institution, herunder kaserne, fængsel m.v.],
    [Sommerhus.],
    [Enhed til ferieformål m.v. bortset fra sommerhus (feriekoloni vandrehjem o. lign.)],
    [Enhed i forbindelse med idrætsudøvelse (klubhus, idrætshal, svømmehal o. lign.).],
    [Kolonihavehus.],
    [Anden enhed til fritidsformål.],
    [Ikke tidligere vurderet erhvervsenhed i nybyggeri],
    [Garage],
    [Carport],
    [Udhus]) ) AS pvt

      

Update Code17july (Which I can't work):

WITH a AS (
SELECT DISTINCT 
  Anvendelseskoder.[Usage Code] AS [Byggeanvendelseskode],
  Anvendelseskoder.[Usage Code Value] AS [Byggeanvendelse],
  HeleDanmark_DAWA.KVHx
FROM Anvendelseskoder 
RIGHT JOIN HeleDanmark_DAWA 
  ON Anvendelseskoder.KVHx = HeleDanmark_DAWA.KVHx
WHERE HeleDanmark_DAWA.postnr=6720 AND Anvendelseskoder.[Usage Code]>0), 

aa AS (SELECT a.*, row_number() over(partition by KVHx order by Byggeanvendelseskode) Nr
FROM a)

SELECT KVHx, Byggeanvendelseskode AS Kode1, Byggeanvendelse AS Anvend1,
    (SELECT Byggeanvendelseskode FROM aa AS s2
    WHERE s2.Nr = 2 AND s2.KVHx = aa.KVHx) AS Kode2,
    (SELECT Byggeanvendelse FROM aa AS s2 
    WHERE s2.Nr = 2 
    AND s2.KVHx = aa.KVHx) AS Anvend2,
    (SELECT Byggeanvendelseskode FROM aa AS s2
    WHERE s2.Nr = 3 AND s2.KVHx = aa.KVHx) AS Kode3,
    (SELECT Byggeanvendelse FROM aa AS s2
WHERE s2.Nr = 3 AND s2.KVHx = aa.KVHx) AS Anvend3
FROM aa AS s1
WHERE Nr = 1

      

+3


source to share


1 answer


It looks like you are actually quite close to your last try, just remove the alias as s1

from the from clause at the end of the query and you should get the result you want - or at least a result that matches the image you showed as the desired result ( except for one minor change, but the result does not seem to match the original data).

But your query uses multiple correlated subqueries that might be inefficient and could be replaced by case statements, and therefore your query can be boiled down to this (which gives the same result):



WITH cte AS (
    SELECT DISTINCT 
       Anvendelseskoder.[Usage Code] AS [Byggeanvendelseskode],
       Anvendelseskoder.[Usage Code Value] AS [Byggeanvendelse],
       HeleDanmark_DAWA.KVHx,
       Nr = ROW_NUMBER() OVER(PARTITION BY HeleDanmark_DAWA.KVHx 
                              ORDER BY Anvendelseskoder.[Usage Code])
    FROM Anvendelseskoder 
    RIGHT JOIN HeleDanmark_DAWA 
     ON Anvendelseskoder.KVHx = HeleDanmark_DAWA.KVHx
    WHERE HeleDanmark_DAWA.postnr=6720 AND Anvendelseskoder.[Usage Code]>0
)

SELECT KVHx, 
    MAX(CASE WHEN Nr = 1 THEN Byggeanvendelseskode END) AS Kode1, 
    MAX(CASE WHEN Nr = 1 THEN Byggeanvendelse END) AS Anvend1,
    MAX(CASE WHEN Nr = 2 THEN Byggeanvendelseskode END) AS Kode2, 
    MAX(CASE WHEN Nr = 2 THEN Byggeanvendelse END) AS Anvend2,
    MAX(CASE WHEN Nr = 3 THEN Byggeanvendelseskode END) AS Kode3, 
    MAX(CASE WHEN Nr = 3 THEN Byggeanvendelse END) AS Anvend3
FROM cte 
GROUP BY KVHx;

      

If it may be that you have a different number of columns, you might want to do a dynamic query. There are some good answers that show how for example this: Convert rows to columns efficiently in sql server

0


source







All Articles