Hiding new column (alias column) - mysql

I have a mysql table that looks like this:
id col2 col3 col4
1 value1 value2 3534
2 value1 value1 8456
3 value1 value2 3566
4 value1 value3 7345
5 value2 value3 6734
What I wanted to do is, select distinct col2+col3 values
id col2 col3 col4
1 value1 value2 3534
2 value1 value1 8456
4 value1 value3 7345
5 value2 value3 6734
I used below query, I get desired result as shown here.
SELECT distinct(CONCAT(col2, col3)) as "dummy column", id, col2, col3, col4
FROM yourtable
GROUP BY CONCAT(col2, col3);
My question is,
As I don't want to display column "dummy column" in output table, how can I hide the same?
This question is related to SO older question
Selecting distinct 2 columns combination in mysql

Why aren't you trying the following?
SELECT id, col2, col3, col4
FROM yourtable
GROUP BY col2, col3

Related

Selecting distinct 5 columns combination in mysql

I have a mysql table that looks like this: Col 1 is UNIQUE.
1 value1 value2 0 2
2 value1 value2 1 3
3 value3 value4 3 2
4 value4 value5 4 1
5 value3 value4 3 1
I need a query to select all the rows with distinct column 1 and 2, for example the output I want for this example will look like this:
1 value1 value2 0 2
3 value3 value4 3 2
4 value4 value5 4 1
I need distinct col 1 and 2 but altogether all columns combination will be distinct always. I want to display distinct col 1,2 and 3 without col 2,3 repeating.
I've found a few samples on how to do it but they all select distinct on each column individually. I tried many stackoverflow answers too. But my question is different.
One method that performs well is a correlate subquery:
select t.*
from t
where t.col1 = (select min(t2.col1)
from t t2
where t2.col2 = t.col2 and t2.col3 = t.col3
);
For best performance, you want an index on (col2, col3, col1).
I strongly advise having a primary key on all tables, but if you did not have one, then row_number() would be the way to go:
select t.*
from (select t.*,
row_number() over (partition by col2, col3 order by col2) as seqnum
from t
) t
where seqnum = 1;
This incurs a tad more overhead because row numbers need to be assigned to all rows before they are filtered for only first one.
It could be achieved by using ROW_NUMBER:
SELECT *
FROM (SELECT *, ROW_NUMBER() OVER(PARTITION BY col2, col3 ORDER BY col1) AS rn
FROM tab) sub
WHERE rn=1

SQL - if col1 have value expect 0 and col2 have value except 0 with same value for col3 then values in one row

I have one table with these value
col1 col2 col3
---------------------- ------------------- -------------------------
1 0 ADD SERVICE ACTIVITY 1
0 1 ADD SERVICE ACTIVITY 1
0 8 Docment testing 2 (C07)
I want result like:
col1 col2 col3
---------------------- ------------------- -------------------------
1 1 ADD SERVICE ACTIVITY 1
0 8 Docment testing 2 (C07)
It seems that you want to group your data by column col3. I'm not sure whether results for col1 and col2 are sums or max values.
--for MAX:
SELECT MAX(COL1) as col1, MAX(COL2) as col2, COL3
/*-- for SUM:
SELECT SUM(COL1) as col1, SUM(COL2) as col2, COL3
*/
FROM test
GROUP BY COL3;

sql database maths operation on data

I have a database with rows and columns of data, the row,col data is some summed data (ie. 1 3 5 7 from original data = 1 2 2 2).
I want to get the original data by subtraction eg. val=r1c2-r1c1 etc across the rows and columns. Is this possible in sql without having to do lots of individual select statements for each row/column?
I would like to do this for all rows and columns in database, like the following pseudocode
a[ 1,3,5,7;
2,5,6,7 ];
for(i=0;i<size(a,1); i++)
for(j=0;j<size(a,2)-1; j++)
b(i,j)=a(i,j+1)-a(i,j);
Try this.
SELECT col1,
col2 - col1 col2,
col3 - col2 col3,
col4 - col3 col4
FROM (SELECT 1 col1,
3 col2,
5 col3,
7 col4) a
Update: Same query will work for more than one row
SELECT col1,
col2 - col1 col2,
col3 - col2 col3,
col4 - col3 col4
FROM (SELECT 1 col1,3 col2,5 col3,7 col4
UNION ALL
SELECT 1 col1,3 col2,9 col3,11 col4
UNION ALL
SELECT 1 col1,3 col2,5 col3,7 col4) a

SQL query to create many-to-one tables from one table?

I have a table like so:
column1 column2
------- -------
key1 value1
key1 value2
key1 value3
key2 value4
key2 value5
key2 value6
I would like to create the following two tables:
id column1
-- -------
1 key1
2 key2
key_id column2
------ -------
1 value1
1 value2
1 value3
2 value4
2 value5
2 value6
That is, I would like to split a table into a many-to-one relation between two new tables.
How would I write an SQL query to do this?
Assuming the id column in your first new table is an identity column:
INSERT INTO NewTable1
(column1)
SELECT DISTINCT column1
FROM OldTable;
INSERT INTO NewTable2
(key_id, column2)
SELECT n1.id, o.column2
FROM OldTable o
INNER JOIN NewTable1 n1
ON o.column1 = n1.column1;
Why don’t u use php my admin and enter the data manually. It will write the php for you.
Also your id field on each table must always be unique and auto incremented.
you cannot use 111 222 in a primary key

Grouping problem

When you use Group By how do you keep the other fields in sync when using aggregates
Here I am trying to find the min value of col4 when col2 = xxx
select col1, col2, col3, min(col4)
from table
where col2 = 'xxx'
group by col3
I can get the minimum value in col4 but col1 is not correct but col2, col3, col4 are.
Can anyone show me how to do this ?
Thanks
You are using non-standard MySQL extension to GROUP BY.
This query in fact reads as "for each distinct value of col3, select the minimal value of col4 along with the values of col1 and col2 from a single row of table having this value of col3 in no particular order"
Like, if we have the following data:
col1 col2 col3 col4
---- --- --- ----
A A 1 1
B B 1 2
C C 2 3
D D 2 4
, this query:
SELECT col1, col2, col3, MIN(col4)
FROM mytable
GROUP BY
col3
will return either of the following:
col1 col2 col3 col4
---- --- --- ----
A A 1 1
C C 2 3
col1 col2 col3 col4
---- --- --- ----
B B 1 1
C C 2 3
col1 col2 col3 col4
---- --- --- ----
A A 1 1
D D 2 3
col1 col2 col3 col4
---- --- --- ----
B B 1 1
D D 2 3
i. e. it can return any value of col1 and col2 found in the rows that contribute to the corresponding group.
This is equivalent of FIRST_VALUE analytic function, but in no particular order.
Update:
To select values of col1 and col2 corresponding to the minimal value of col4 within each group, use this:
SELECT col1, co2, col3, col4
FROM (
SELECT col1, col2, col3, col4,
COALESCE(#col3 = col3, FALSE) AS grp,
#col3 := col3 AS nv
FROM (
SELECT #col3 := NULL
) vars, table
WHERE col2 = 'xxx'
ORDER BY
col3, col4
) q
WHERE NOT grp
select a.col3, a.col2, a.col1, a.col4
from table as a natural join
(select col3, min(col4) as col4 from table
where col2='xxx'
group by col3 ) as b
where a.col2 = 'xxx' -- sometimes this helps the optimizer even though it's redundant
you can get into a bit of trouble here when there may be multiple rows with the same col3, col4, and col2, but different col1s -- pretty straightforward to fix with rownums and such, but that gets db-specific.
I guess you want the col1-3 corresponding to the min(col4) for each
col3?
Something like:
select X.col1, X.col2, X.col3, X.col4 from table X
join (select col3, min(col4) as mcol4 from table where col2='xxx' group by col3) as Y
on X.col3=Y.col3 and X.col4=Y.mcol4
where X.col2='xxx';