Jump to content
php.lv forumi

permutācija sql


NBS

Recommended Posts

Sveiki, man ir šāda problēma.

Ir vairāki mainīgie: 1110, 1121, 1122, 1130.

Gribās no šiem mainīgajiem permutāciju.

Lai noselektējās šādi:

1110, 1121

1110, 1122

1110, 1130

1121, 1122

1121, 1130

1110, 1121, 1122

1110, 1121, 1130

utt.

 

MS SQL izmantojot.

Edited by NBS
Link to comment
Share on other sites

Diezgan patizli izskatās, bet kaut kas uz to pusi ir:

WITH v AS ( 
 SELECT '1110' AS n 
 union ALL 
 SELECT '1121'
 union ALL 
 SELECT '1122'
 union ALL 
 SELECT '1130'
 union ALL 
 SELECT ''
)
SELECT DISTINCT (CASE WHEN v1.n = '' THEN ''
           WHEN LEN(v2.n + v3.n + v4.n + v5.n) > 0 THEN v1.n + ','
           ELSE v1.n
      END + 
      CASE WHEN v2.n = '' THEN ''
           WHEN LEN(v3.n + v4.n + v5.n) > 0 THEN v2.n + ','
           ELSE v2.n
      END + 
      CASE WHEN v3.n = '' THEN ''
           WHEN LEN(v4.n + v5.n) > 0 THEN v3.n + ','
           ELSE v3.n
      END + 
      CASE WHEN v4.n = '' THEN ''
           WHEN LEN(v5.n) > 0 THEN v4.n + ','
           ELSE v4.n
      END + 
      CASE WHEN v5.n = '' THEN ''
           ELSE v5.n
      END)
AS t
FROM v v1
CROSS JOIN 
v v2 
CROSS JOIN 
v v3 
CROSS JOIN 
v v4 
CROSS JOIN 
v v5
order by t

 

Gints Plivna

http://datubazes.wordpress.com

Link to comment
Share on other sites

Kaut kā galīgi ne tā.

Pašreiz sanāk šādi ar tavu kodu:

1110

1110,1110

1110,1110,1110

1110,1110,1110,1110

1110,1110,1110,1110,1110

1110,1110,1110,1110,1121

1110,1110,1110,1110,1122

1110,1110,1110,1110,1130

1110,1110,1110,1121

...

 

 

Bet vajag šādi:

1110, 1121

1110, 1122

1110, 1130

1121, 1122

1121, 1130

1122, 1130

1110, 1121, 1122

1110, 1121, 1130

1110, 1122, 1130

1110, 1121, 1122, 1130

Link to comment
Share on other sites

Nu, ja godīgi esmu gatavs piekrist, ka SQLs nav varbūt labākais risinājums šai problēmai :)

BET cross join ka zināms ir Dekarta reizinājums. Tātad katrs ar katru. Ja no tā kaut ko vajag ierobežot, tad to var izdarīt gluži tāpat kā, ja laistu cauri kaut kādus ciklus ciklā vai kā citādi ģenerētu šādus variantus.

 

Piemēram šādi:

WITH v AS ( 
 SELECT '1110' AS n 
 union ALL 
 SELECT '1121'
 union ALL 
 SELECT '1122'
 union ALL 
 SELECT '1130'
 union ALL 
 SELECT ''
)
SELECT DISTINCT (CASE WHEN v1.n = '' THEN ''
           WHEN LEN(v2.n + v3.n + v4.n + v5.n) > 0 THEN v1.n + ','
           ELSE v1.n
      END + 
      CASE WHEN v2.n = '' THEN ''
           WHEN LEN(v3.n + v4.n + v5.n) > 0 THEN v2.n + ','
           ELSE v2.n
      END + 
      CASE WHEN v3.n = '' THEN ''
           WHEN LEN(v4.n + v5.n) > 0 THEN v3.n + ','
           ELSE v3.n
      END + 
      CASE WHEN v4.n = '' THEN ''
           WHEN LEN(v5.n) > 0 THEN v4.n + ','
           ELSE v4.n
      END + 
      CASE WHEN v5.n = '' THEN ''
           ELSE v5.n
      END)
AS t
FROM v v1
CROSS JOIN 
v v2 
CROSS JOIN 
v v3 
CROSS JOIN 
v v4 
CROSS JOIN 
v v5
WHERE v1.n <> v2.n AND v1.n <> v3.n AND v1.n <> v4.n AND v1.n <> v5.n
 AND (v2.n <> v3.n AND v2.n <> v4.n AND v2.n <> v5.n OR len(v2.n + v3.n + v4.n + v5.n) = 0)
 AND (v3.n <> v4.n AND v3.n <> v5.n OR len(v3.n + v4.n + v5.n) = 0)
 AND (v4.n <> v5.n OR len(v4.n + v5.n) = 0)
order by  t

t
------------------------
1110
1110,1121
1110,1121,1122
1110,1121,1122,1130
1110,1121,1130
1110,1121,1130,1122
1110,1122
1110,1122,1121
1110,1122,1121,1130
1110,1122,1130
1110,1122,1130,1121
1110,1130
1110,1130,1121
1110,1130,1121,1122
1110,1130,1122
1110,1130,1122,1121
1121

 

Nezinu, vai tas ir tas ko vajag, jo īsti jau tā skaidri neaprakstīji, kas tas ir, bet nu pielabojot WHERE klauzas teorētiski noteikti var dabūt to, kas nepieciešams. Vai tas ir arī praktiski labs risinājums - nu tas jau ir cits jautājums ;)

Galu galā mēs zinām, ka SQLā mierīgi var zīmēt arī pulksteni, piemēram

 

Gints Plivna

http://datubazes.wordpress.com

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...