Jump to content

Recommended Posts

Hello Everyone,

 

I have a problem with a query

 

I have the following table

 

tb1:

id name pricechart ordernr   srvid

---------------------------------------------

1 rick hotel   GRP             2     1

2 rick hotel   PRI               1       1

3 Jonas Hotel GRP              2      2

4 Jonas Hotel PRI             1     2

5 Allan Hotel   PRI             1       1

 

 

now what i want to have as result are all the distinct names with a specific pricechart, the pricechart has to be either PRI or GRP and followed up by some sequence.  like for example if GRP pricechart is not available then display the PRI.

 

a result for the above described sequence (GRP -> PRI ) will result in the following:

 

id name pricechart ordernr   srvid

---------------------------------------------

1 rick hotel   GRP             2     1

3 Jonas Hotel GRP              2      2

5 Allan Hotel   PRI             1       1

 

OR if the sequence is turned around  like display first PRI if the pricechart is there else use the GRP    (PRI -> GRP) the folllwoing result would be displayed:

 

id name pricechart ordernr   srvid

---------------------------------------------

 

2 rick hotel   PRI               1       1

4 Jonas Hotel PRI             1     2

5 Allan Hotel   PRI             1       1

 

 

Can anyone help me?  I tried it now multiple times myself but without any luck :(

 

Thanks in advance!

Rick

 

 

 

 

 

This probably isn't the best way, but it works in this case.

 

mysql> SELECT * FROM hotels;
+------+-------------+------------+---------+-------+
| id   | name        | pricechart | ordernr | srvid |
+------+-------------+------------+---------+-------+
|    1 | Rick Hotel  | PRI        |       1 |     1 |
|    2 | Rick Hotel  | GRP        |       2 |     1 |
|    3 | Jonas Hotel | GRP        |       2 |     2 |
|    4 | Jonas Hotel | PRI        |       1 |     2 |
|    5 | Allan Hotel | PRI        |       1 |     1 |
+------+-------------+------------+---------+-------+
5 rows in set (0.00 sec)

mysql> SELECT hotels.* FROM hotels JOIN (SELECT name,LEFT(GROUP_CONCAT(pricechart ORDER BY pricechart='GRP' DESC SEPARATOR ''),3) AS pc FROM hotels GROUP BY name) AS h2 ON (hotels.name=h2.name AND hotels.pricechart=h2.pc);
+------+-------------+------------+---------+-------+
| id   | name        | pricechart | ordernr | srvid |
+------+-------------+------------+---------+-------+
|    2 | Rick Hotel  | GRP        |       2 |     1 |
|    3 | Jonas Hotel | GRP        |       2 |     2 |
|    5 | Allan Hotel | PRI        |       1 |     1 |
+------+-------------+------------+---------+-------+
3 rows in set (0.00 sec)

mysql> SELECT hotels.* FROM hotels JOIN (SELECT name,LEFT(GROUP_CONCAT(pricechart ORDER BY pricechart='PRI' DESC SEPARATOR ''),3) AS pc FROM hotels GROUP BY name) AS h2 ON (hotels.name=h2.name AND hotels.pricechart=h2.pc);
+------+-------------+------------+---------+-------+
| id   | name        | pricechart | ordernr | srvid |
+------+-------------+------------+---------+-------+
|    1 | Rick Hotel  | PRI        |       1 |     1 |
|    4 | Jonas Hotel | PRI        |       1 |     2 |
|    5 | Allan Hotel | PRI        |       1 |     1 |
+------+-------------+------------+---------+-------+
3 rows in set (0.00 sec)

Hi all,

 

I have been busy with the code you all have provided, and i believe i got it to work now.

 

I adapted the query so that it can handle more than 2 pricecharts in a sequence. and only returns the specified pricecharts.

 

SELECT table4.* FROM table4 JOIN 
(
        SELECT 
        srvid,
        MIN(field(pricecart,'GRP','PRI')) as tc
        FROM table4 GROUP BY srvid
) AS h2 ON (
      (table4.pricecart='PRI'   OR  table4.pricecart='GRP') AND    
      table4.srvid=h2.srvid AND field(table4.pricecart,'GRP','PRI')=h2.tc
);

 

Thanks for all the help!

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.