MySQL Pivot Tables Without Numbers - mysql

I trying to do a pivot table from this table.
I want a table like this:
I want my headers in a column.
How can I achieve this?
Thanks a lot.

You can use union all:
select 'field1' as field, field1 from t
union all
select 'field2' as field, field2 from t
union all
select 'field3' as field, field3 from t
union all
select 'field4' as field, field4 from t;

Related

Pivoting table sql

I have this table:
|............id.............|............a............. |.............b............|
|...........123qwe.... |...........0............. |.............13...........|
and I need to pivot it like this:
|.........id................|........indicator.... |.............value........ |
|...........123qwe.... |...........a............. |.............0.............|
|...........123qwe.... |...........b............. |.............13........... |
There are more thatn 100 columns with indicators as headers (a,b,c,d,e,f,...) so a sort of loop would be needed.
SELECT id, 'a', SUM(a)
FROM yourtable
GROUP BY id
UNION
SELECT id, 'b', SUM(b)
FROM yourtable
GROUP BY id
UNION
...
As usual, the right answer is to normalize you schema.
Do it like this:
SELECT id, 'a' AS indicator, a AS value
FROM test
UNION
SELECT id, 'b' AS indicator, b AS value
FROM test;
and if you want loop, you'd better write it in the program...

Return somehting when no record exists, infobright db

I have a query like this and using infobright database --
select field1, field2
from foo
where filed1 in (1,2,3)
I want something to return even if there is no record in table. For example, there is record for filed1 = 2
and filed1 = 2 but nothing exists for filed1 = 3.
How could I make this query so I get something returning from the table for field1 = 3 ?
I could use ifnull in case there is a null value in the table for field1 = 3, but what I am trying find if there is absolutely nothing exists.
Although this is a short list of ID values you want, you can create a select/union to create the IDs, then left-join to the data table something like
select
AllIDs.ID,
foo.field1,
foo.field2
from
( select 1 ID union select 2 union select 3 ) as AllIDs
left join foo
on AllIDs.ID = foo.field1
If field1 is unique you can do this:
SELECT
ISNULL(MAX(field1), 'Default F1') as field1,
ISNULL(MAX(field2), 'Default F2') as field2
FROM foo
WHERE field1 in (1,2,3)
GROUP by field1
Otherwise you can use UNION like this:
SELECT field1, field2
FROM foo
WHERE filed1 in (1,2,3)
UNION
SELECT 'Default F1' as field1, 'Default F2' as field2
WHERE (SELECT COUNT(*) FROM foo WHERE filed1 in (1,2,3)) = 0

Optimizing sql query to find number of non unique values in a column

Does anyone have an idea how to optmize the query below as it is taking quite some time:
select count(*) from
(select field1
from table
group by field1
having count(distinct field1) <> count(field1)) AS Q1
The query is used to find the number of non unique values in a column.
If you want the number of non-unique values, use:
select count(*)
from (select field1
from table
group by field1
having count(*) > 1
) t
And, yes, an index on table.field1 will speed this up.
If you want the values, use:
select field1
from table
group by field1
having count(*) > 1
Add an INDEX on column field1, eg
ALTER TABLE tableNAME ADD INDEX (field1)
How MySQL Uses Indexes

Deselecting a data with different column value

Hi guys I've got this table structure
field 1 field 2
---------------------------------------------
1 1
1 2
2 1
Then I want it to be like this when selecting Key Field2 = 1
field 1 field 2
---------------------------------------------
2 1
I don't want to return field1 = 1 because It contains different values field1 IN (1,2)
Thanks you so much.
Your post seems unclear because I think you mixed up column names in certain parts of your description. However, judging by your sample output, I'm going to assume you mean the following:
Select rows from the table where field2 contains identical values for the same field1.
If you only need to output field1 and field2, you could do the following:
SELECT field1, MAX(field2) AS field2
FROM atable
GROUP BY field1
HAVING COUNT(DISTINCT field2) = 1
You can omit DISTINCT if your table cannot hold duplicate pairs of (field1, field2).
However, if there are more columns in the table and some or all of them need to be returned too, you could first just get the field1 values like above, then join that row set back to atable to get complete rows, like this:
SELECT t.* /* or specify the necessary columns explicitly */
FROM atable AS t
INNER JOIN (
SELECT field1
FROM atable
GROUP BY field1
HAVING COUNT(DISTINCT field2) = 1
) s ON t.field1 = s.field1
Again, DISTINCT can be omitted, as explained above.
Since you are using SQL Server 2008, you could also use windowed aggregating. If your table doesn't contain duplicates of (field1, field2), you could use the following:
;
WITH counted AS (
SELECT
*,
cnt = COUNT(*) OVER (PARTITION BY field1)
FROM atable
)
SELECT
field1,
field2,
…
FROM counted
WHERE cnt = 1
But if the duplicates are allowed, you'll need to use a slightly different approach, because there's no windowing counterpart for COUNT(DISTINCT …). Here's what you could try:
;
WITH counted AS (
SELECT
*,
f2min = MIN(field2) OVER (PARTITION BY field1),
f2max = MAX(field2) OVER (PARTITION BY field1)
FROM atable
)
SELECT
field1,
field2,
…
FROM minmaxed
WHERE f2min = f2max
That is, you are getting the minimum and the maximum value of field2 for every field1 value. Then you are filtering out rows where f2min is not the same as f2max, because that would imply that there are different field2 values in the group.
SELECT * FROM table WHERE field_2 = 1 AND field_1 <> 1

Getting all unique values between two columns in mysql

I have a mysql table that has two fields that store the same type of information. I want to retrieve all unique values in those two fields.
If it were just one field I could do:
SELECT distinct FIELD1 FROM table
How can I get all unique values from FIELD1 and FIELD2
Clarification:
I don't mean unique pairs.
Say field1 contains 1,13,5,25,13,8
and field2 contains 6,10,1,30,13
I want a query that returns 1,13,5,25,8,6,10,30
Try this
select distinct * FROM (
select distinct field1 as n from table
union
select distinct field2 as n from table) as t;
select field1 ,field2 FROM tablename group by field1 ,field2
select field1 from table
union
select field2 from table
You mean all unique pairs? Then this might do (a bit dirty, though):
SELECT distinct CONCAT(FIELD1, '|', FIELD2) FROM table
You can do a union of two sql statements:
Select distinct FIELD1 From table
UNION
Select distinct FIELD2 From table