mySQL, two-dimensional into one-dimensional - mysql

I've forgotten whatever I used to know about pivots, but this seems to me the reverse. Suppose I have a set of items A, B, C, D, … and a list of attributes W, X, Y, Z. I have in a spreadsheet something like
A B C D
W 1 P 3 Q
X 5 R 7 S
Y T 2 U 4
Z D 6 F 7
where the value of attribute X for item B is 'P'. In order to do some statistics on comparisons, I'd like to change it from table to list, i.e.,
W A 1
X A 5
Y A T
Z A D
W B P
X B R
Y C U
Z C F
W D Q
X D S
Y B 2
Z B 6
Etc.
I can easily write a nested loop macro in the spreadsheet to do it, but is there an easy way to import it into mySQL in the desired format? Queries to get the statistics needed are simple in SQL (and formulas not very hard in a spreadsheet) if the data is in the second format.
Since there apparently isn't a "spreadsheet" tag, I used "excel." :-)
There are a lot of questions that looked similar at first glance, but the five I looked at all wanted to discard one of the indices (A-D or W-Z), i.e. creating something like
W 1
W P
X 5
X R

EDITED
You can use PowerQuery to unpivot tables. See the answer by teylyn for the following question. I have Office 365 and didn't need to install the plugin first. The functionality was already available.
Convert matrix to 3-column table ('reverse pivot', 'unpivot', 'flatten', 'normalize')
Another way to unpivot data without using VBA is with PowerQuery, a free add-in for Excel 2010 and higher, available here: http://www.microsoft.com/en-us/download/details.aspx?id=39379
...
Click the column header of the first column to select it. Then, on the Transform ribbon, click the Unpivot Columns drop-down and select Unpivot other columns.
...
OLD ANSWER
If you import the spreadsheet as is, you can run a query to output the correct format.
For a fixed, small number of items, you can use UNION for each of the columns.
SELECT attr, 'A' AS 'item', A AS 'value'
FROM sheet
UNION
SELECT attr, 'B' AS 'item', B AS 'value'
FROM sheet
UNION
SELECT attr, 'C' AS 'item', C AS 'value'
FROM sheet
UNION
SELECT attr, 'D' AS 'item', D AS 'value'
FROM sheet;
Working example: http://sqlfiddle.com/#!9/c274e7/7

Related

How to join columns of same table under different query conditon

I am explaining here I have table name TB which contains three columns and data like this
Cl1. Cl2 Cl3
0. X. A
0. Y. B
0. Z. C
1. X. a
1. X. b
1. X. c
1. Z. d
And I output like this
Cl1. Cl2. Cl3. No
0. X. A. 3
0. Y. B. 0
0. Z. C. 1
Here no column shows repetition of Cl2. Value when Cl1.=1 with respective value of Cl2. When Cl1. = 0 means for Cl1. = 1 here we can see for Cl.=1 and Cl2.=X it's value is repeated twice for Cl1.=1 similar when no match found for Cl1.=1 and Cl2.=Y no gives 0
I hope i have described my problem
I did many attempts but I didn't get any solution. My problem goes out of mind.
My few attempts are like
Select Cl1. ,Cl2. ,Cl3. ,IF(Cl2.=(Select Cl2. From TB where Cl1.=1),Cl2. ,0) as no.
From TB
where Cl2.=0
I also try join type statement for the same table but it also didn't help me.
Please guys help me. I am new on stack overflow if I did any mistake in describing my question then sorry
Try this, #Rajesh
SELECT
t1.cl1,
t1.cl2,
t1.cl3,
IFNULL(cnt, 0) as No
FROM
(
SELECT cl1, cl2, cl3 FROM tb WHERE cl1=0
) AS t1
LEFT OUTER JOIN
(
SELECT cl2, COUNT(cl2) AS cnt FROM tb WHERE cl1 <> 0 GROUP BY cl2
) AS t2
ON t1.cl2=t2.cl2

Transposing Crosstab in SAP business Object Design Studio

This is my crosstab :
ColumnA ColumnB
X D
Y E
Z F
I want it to be converted to :
ColumnA X Y Z
ColumnB D E F
I checked all the properties and checked for CSS but did not find anything on this topic!
Select your original table in webi report, right click the block, select "Turn into " option, then select "Horizontal Table" option. You will get the expected result.

How to use like operator for remove whitespace and similar words at the same time?

Below is my query from which I'm excluding some characters from query. It's working for me.
SELECT
b.TRANS_DETAILS_ID,
b.DEBIT_AMOUNT,
b.ENTITY_NAME,
REPLACE(b.DESCRIPTION, 'SP Z O.O.', '') AS DESCRIPTION,
DATE_FORMAT(a.TRANSACTION_DATE_TIME, '%d-%m-%Y') AS TRANS_DATE
FROM bank_book_transaction_master a,
bank_book_transaction_details b
WHERE a.TRANSACTION_DATE_TIME
BETWEEN '$from_date_srch' AND '$to_date_srch'
AND DEBIT_CREDIT_FLAG = 2
AND a.ORG_ID = '$org_id'
AND a.BANK_ID = '$bank_id'
AND a.TRANSACTION_ID = b.TRANS_MASTER_ID
But my problem is in my database SP Z O.O. these characters are storing differently sometimes. In some columns it's SP Z O.o. or sp z O.O. or SP Z O.O.. My query removes only if charaters are similar like SP Z O.O.. But I want to add all conditions like small letters whitespace etc.
I tried with LIKE %SP Z O.O.% but it shows me result in 1 and 0.

Duplicates issue

I have a problem with duplicates.
Actually what I need is only the see duplicates but my table has many variables something like the below:
a b c d e
32 ayi dam som kem
32 ayi dam som tws
32 ayi dam tsm tws
12 mm ds de ko
12 mm tmm to ko
I am trying to keep if 'a' 'b' 'c' and 'd' variables are same. So I need only first 2 columns. I try to do this
proc sort data=al nodupkey dupout=dups;
by a b c d;
run;
any idea if this works?
In SAS 9.3+ you can do this very easily with the new nouniquekey option.
proc sort data=have nouniquekey out=want;
by a b c d;
run;
That removes any rows which are unique and leaves duplicates.
If you have an earlier version of SAS, you can do something fairly simple after a regular sort.
So, after sorting as in your example above but without nodupkey:
data want;
set have;
by a b c d;
if not (first.d and last.d);
run;
That removes records that are the first AND last record based on a b c d.
Another option could be Proc SQL, no pre-sort needed:
proc sql;
create table want as
select * from have
group by a, b, c, d
having count(*)>1; /*This is to tell SAS only to keep those dups*/
quit;

MySQL - return records for clients unless they have a specific value and only that value

Trying to wrap my head around how to do this query - I want to return a list of client records and need to exclude clients if they had only a specific value and no other values.
For example
c# value
1 X
1 Y
2 X
3 Y
I want all the records for clients 1 and 3, since they had a value other than X. I do not want client 2, because that client had ONLY X.
I for example want returned in this case:
1 X
1 Y
3 Y
Of course, I could have lots of other records with other client id's and values, but I want to eliminate only the ones that have a single "X" value and no other values.
Maybe using a sub-query?
Try this:
SELECT client, value FROM myTable where `client` in
(select distinct(client) from myTable where value !='X');
Returns:
Client Value
1 X
1 Y
3 Y
Something like this
SELECT ABB2.*
FROM
mytable AS ABB2
JOIN
(SELECT c
FROM mytable
WHERE value <> "X"
GROUP BY c) AS ABB1 ON ABB1.c = ABB2.c
GROUP BY ABB2.c, ABB2.value
It's faster than using a WHERE clause to identify the sub query results (as in Mike's answer)