I have below data set and I want to have it joined into a single column like below from two different columns. Here is the data set.
Actual Data set:
Station_id |Market | Households | Over Air
1 |Houston |2117641 |220221
2 |Topeka |182443 |4562
3 |Columbus |922291 |167671
3 |Boston |1200 |13400
From the above data set I want to have below result.
Station_id | Market | Value
1 | Houston | 2117641
2 | Topeka | 182443
3 | Columbus | 167671
3 | Boston | 13400
please help me how to write the query to get the result. I am using Mysql database.
Thanks & Regards
Arfater Rahman.
Please try using the following query:
SELECT Station_id, Market,
CASE
WHEN Station_id=1 THEN Households
WHEN Station_id=2 THEN Households
WHEN Station_id=3 THEN Over_Air
END AS Value
FROM DATASET
Is this what you want?
CASE
WHEN Station_id = 3 then `Over Air`
ELSE `Households`
END
based on:
Station_id | Market | Households | Over Air
1 Houston 2117641 220221
2 Topeka 182443 4562
3 Columbus 922291 167671
3 Boston 1200 13400
Related
So, Im trying to make a query that will count how many days belong in different seasons. Its for site that's about booking apartments and it can have multiple seasons one after another. For example:
| Season_id | From | Till |
| 1 | 2015-09-01 | 2015-09-30 |
| 2 | 2015-09-30 | 2015-10-20 |
| 3 | 2015-10-30 | 2015-12-30 |
So when someone is searching to book for date ex.:
2015-09-25 - 2015-10-10.
I want to get result like:
Seasons: 1, 2
Count days: 5, 10
Is it possible to do that with one query or it must be multiple??
Thank you in advance.
I have a table apartment as below
aid | aname
1 | dream home
2 | My hub
3 | Lake view
another table apartment_details
id | aid | bhk | size | facing
1 | 1 | 2 | 1200 | east
2 | 1 | 2 | 1200 | west
3 | 1 | 2 | 1000 | south
4 | 1 | 2 | 1000 | north
I have written the query as
SELECT distinct ap.aid, ap.aname, al.bhk, (select group_concat(distinct concat(al.bhk,'BHK - ',al.size)) from apartment_details as al where al.id = ap.aid) as details
When I tried to display details using foreach I get the output as
2BHK - 1200
2BHK - 1200
2BHK - 1000
2BHK - 1000
In this query it is considering bhk, size, facing in distinct and the output obtained is based on facing. This looks something like I am displaying duplicate data or something the same data is repeating as there is no facing displayed. How can I display only distinct values based on bhk, size and not facing so that I get the output as
2BHK - 1200
2BHK - 1000
Can anyone help me in solving this issue? Thanks in advance
To my way of thinking, in general, there is no problem in SQL for which GROUP_CONCAT is the solution. So, with that in mind, let's start with this:
SELECT DISTINCT bhk,size FROM apartment_details
well I eventually got to a max() on one table pulling correctly using max() - took me sometime to understand what was going on and reading the limits of mysql when using it
I have spent a some time doing some demo data on sqlfiddle (one below is with just the max on the one table
http://sqlfiddle.com/#!2/fff224/1
what i would like (and to absorb as ive tried for the last 2 hours on getting this to work) is how to incorporate another tables that dont need to use max (i have included these in the sqlfiddle
the result i would be after would be
case_number full_address case owner client compiled date(max()) recommendation
1000 1 high street bob london 14/12/2012 let
1001 2 high street ken Compton 13/12/2013 sell
1002 3 high street ken Leeds 14/12/2013 sell
completing the inner joins between from client\staff\ to case process im fine with its just this nested select max im falling over on
SELECT p.case_number
, p.full_address
, s.case_owner_name 'case owner'
, c.client_name client
, r.compiled_date
, r.recommendation
FROM case_process p
JOIN staff s
ON s.case_owner_number = p.case_owner_number
JOIN client c
ON c.client_number = p.client_number
JOIN reporting r
ON r.case_number = p.case_number
JOIN (SELECT case_number,MAX(compiled_date) max_compiled_date FROM reporting GROUP BY case_number) x
ON x.case_number = r.case_number
AND x.max_compiled_date = r.compiled_date;
+-------------+---------------+------------+---------+---------------+----------------+
| case_number | full_address | case owner | client | compiled_date | recommendation |
+-------------+---------------+------------+---------+---------------+----------------+
| 1000 | 1 high street | Bob | London | 2012-12-14 | let |
| 1001 | 2 high street | Ken | Compton | 2013-12-13 | sell |
| 1002 | 3 high street | Ken | Leeds | 2012-12-14 | sell |
+-------------+---------------+------------+---------+---------------+----------------+
3 rows in set (0.00 sec)
fiddle of same... http://sqlfiddle.com/#!2/fff224/7
Assume I have the following tables:
tableA
a_name | age | country
Jordan | 5 | Germany
Molly | 6 | Spain
Paris | 7 | France
tableB
b_name | age | country
Kyle | 5 | Germany
Bob | 6 | Spain
Bob | 7 | Spain
Stephen | 7 | France
Kyle | 9 | France
Mario | 2 | Mexico
I want to make it such that I can produce a tableC that contains:
id (auto increment primary key) | age | country | country_marker
1 | 5 | Germany | 1
2 | 6 | Spain | 2
3 | 7 | France | 3
4 | 7 | Spain | 2
5 | 8 | France | 3
6 | 9 | France | 3
7 | 2 | Mexico | 4
For the new table:
takes any unique "age, country" pair only and putting them into tableC with "country_marker" automatically assigning incrementing unique numbers based on distinct "country"
Note there is no countries table, as the "country" in tableC is just based on whatever countries are in tableA, and tableB and country_marker is just a system generated identifier to indicate the unique countries in the table. Output tableC "id and country marker" ordering does not matter as long as it meets the bullet above.
I have painfully tried to produce this using MySQL cursors and want to know if its possible/what is some SQL query or set of queries I could use that would make this faster than using cursors.
I'd approach this by first creating table C in something like phpmyadmin. Then I'd write a query to pull the information from tables A and B that I want to put into C. Then, within phpmyadmin, I'd export the results of the query to a .sql file on my local machine. I'd open the .sql file to make the necessary modifications to the INSERT statements so that I can import the data in the .sql file to table C.
This isn't exactly the most efficient way, but it seems like the least technical way that would definitely work.
I am having a table with data stored in row basis as shown below.
UID | DetailsID | Data|
----------------------|
1 | 1 | A |
1 | 2 | 200|
1 | 3 | 2010-10-11 08:32 |
2 | 1 | B |
2 | 2 | 600|
2 | 3 | 2011-05-20 14:56 |
From this I need the output as follows
UID|1|2|3
------------
1|A|200|2010-10-11 08:32
2|B|600|2011-05-20 14:56
Here main thing is, the number of entries of DetailsID values is not known.
I wanted this one in MySQL.
Please help me out of this.
Not quite what you want, but other than loads of left joins i can only suggest:
SELECT UID,GROUP_CONCAT(DetailsID SEPARATOR ",") "DetailsIDs",GROUP_CONCAT(Data SEPARATOR ",") "Data" FROM data_table GROUP BY UID;
Do that transformation in your coding language, not in SQL.
you didnt say where you need the output. If you need the output in PHP pages it is simple only by creating the loop for the entries in columns wise.