I have a table that contains entries like this:
I would like to transfor it to something like this:
Can't find how to do so with a group by only. Am I missing anything?
Thanks in advance for your help
SELECT Entity,
MAX(CASE WHEN Type=Auto THEN Value ELSE NULL END) AS ValueAuto,
MAX(CASE WHEN Type=Manual THEN Value ELSE NULL END) AS ValueMaual
FROM tableName
GROUP BY Entity
above query returns the good values if we have only two types, If I don't know how many groups are there in the table, dynamically how can do with the case statement.
Use UN-PIVOT is the best options to solve the above requirements.
Related
I am using this code to check for data integrity of nvarchar/string data fields. Taking the sum from the view using code below, and comparing that to similar formula in EXCEL to see if I get the same total. Is there a better way? I am new at this.
--Aggregate Boolean fields e.g. nvarchar.
With table1 AS
(
Select
CASE WHEN (field_name) = 'Y' Then 1
WHEN field_name = 'N' Then 2
When field_name IS NULL THEN 3
ELSE field_name
END AS field_name_count
From mysqlview
)
Select SUM(field_name) AS Count
From table1
;
Or this approach
--Count characters in nvarchar column
Select
SUM(LEN(field_name)) AS Count
From mysqlview
;
I can't comment so I'll add my 2 cents here.
you seems to sum a Nvarchar field - can you update your question with the reasoning for this?
Also - in the first example you have sum on 'field_name' while the column name is 'field_name_count'
Do you try to see how many fields contain text and compare this number to your Excel data?
in that case you can count them easily by doing:
select count(1) from mysqlview where fieldname != '' and not fieldname is null
Hope I helped
Basically, i have a table that have a series of columns named:
ATTRIBUTE10, ATTRIBUTE11, ATTRIBUTE12 ... ATTRIBUTE50
I want a query that gives me all the columns from ATTRIBUTE10 to ATTRIBUTE50 not null
As others have commented we aren't exactly sure of your requirements, but if you want a list the UNPIVOT can do that...
SELECT attribute , value
FROM
(SELECT * from YourFile) p
UNPIVOT
(value FOR attribute IN
(attribute1, attribute2, attribute3, etc.)
)AS unpvt
May be you can use where condition for all columns Or use between operator as below.
For All Columns
where ATTRIBUTE10 is not null and ATTRIBUTE11 is not null ...... and ATTRIBUTE50 is not null
By using between operator
where ATTRIBUTE10 between ATTRIBUTE11 and ATTRIBUTE50
One way to approach the problem is to unfold your table-with-a-zillion-like-named-attributes into one in which you've got one attribute per row, with appropriate foreign keys back to the original table. So something like:
CREATE TABLE ATTR_TABLE AS
SELECT ID_ATTR, ID_TABLE_WITH_ATTRS, ATTR
FROM (SELECT ((ID_TABLE_WITH_ATTRS-1)*100)+1 AS ID_ATTR, ID_TABLE_WITH_ATTRS, ATTRIBUTE10 AS ATTR FROM TABLE_WITH_ATTRS UNION ALL
SELECT ((ID_TABLE_WITH_ATTRS-1)*100)+2, ID_TABLE_WITH_ATTRS, ATTRIBUTE11 FROM TABLE_WITH_ATTRS UNION ALL
SELECT ((ID_TABLE_WITH_ATTRS-1)*100)+3, ID_TABLE_WITH_ATTRS, ATTRIBUTE12 FROM TABLE_WITH_ATTRS);
This only unfolds ATTRIBUTE10, ATTRIBUTE11, and ATTRIBUTE12, but you should be able to get the idea - the rest of the attributes just requires a little cut-n-paste on your part.
You can then query this table to find your non-NULL attributes as
SELECT *
FROM ATTR_TABLE
WHERE ATTR IS NOT NULL
ORDER BY ID_ATTR
Hopefully the difficulty you're encountering in dealing with this table-with-a-zillion-repeated-fields teaches you a hard lesson about exactly why tables with repeated fields or groups of fields are a Bad Idea.
dbfiddle here
How can I make the values show up differently?
Example:
I have a table 'Feelings' with a column called 'Happy'
If I select * from 'Happy' it will bring back values 1, 2, 3 (with user IDs to show which user is which feeling)
1 stands for Yes, 2 stands for no, 3 stands for maybe
I want the table to not show 1,2,3 but instead show yes,no,maybe
How would I go about to making this?
CASE HAPPY
WHEN '1' THEN 'Yes'
WHEN '2' THEN 'No'
WHEN '3' THEN 'Maybe'
ELSE 'Other'
END AS 'Happy'
While you can use a CASE as other answers here you can also use a join -- to a table of lookup values or to a CTE, or VALUES clause. Even a temp table in a stored procedure.
That would look like this:
SELECT user_id, COALESCE(lookup.value,'Unknown') as happy
FROM feelings
LEFT JOIN (
VALUES
('1','Yes'),
('2','No'),
('3','Maybe')
) AS lookup(k,value) ON feelings.happy = lookup.k
Using a join and storing related information in a table is a much more SQL way of doing things and offers many benefits including easier maintenance and often faster execution.
If you just have a table called Feelings and a column called Happy with just IDs, you can try the following query:
SELECT
user_id,
CASE HAPPY
WHEN '1' THEN 'Yes'
WHEN '2' THEN 'No'
WHEN '3' THEN 'Maybe'
ELSE 'Other'
END AS 'feeling_happy'
FROM FEELINGS
Otherwise, if you have 2 tables, one called Feelings with column Happy that has the IDs and another table called Happy with the IDs in one column and the defined statuses in another column (which we'll call happy_x), then you can simple use the following query:
SELECT
f.user_id, h.happy_x
FROM Feelings f
INNER JOIN Happy AS h
ON h.id = f.happy
I am wondering whether is possible to assign a value to a casted column in SQL depending on real table values.
For Example:
select *, cast(null as number) as value from table1
where if(table1.id > 10 then value = 1) else value = 0
NOTE: I understand the above example is not completely Oracle, but, it is just a demonstration on what I want to accomplish in Oracle. Also, the above example can be done multiple ways due to its simplicity. My goal here is to verify if it is possible to accomplish the example using casted columns (columns not part of table1) and some sort of if/else.
Thanks,
Y_Y
select table1.*, (case when table1.id > 10 then 1 else 0 end) as value
from table1
Here's the scenario:
I have a table with 3 columns: 'KeyColumn', 'SubKeyColumn' and 'BooleanColumn', where the first two are the primary keys of the table.
For my query, I'd like to count the number of rows there are for any given value in 'KeyColumn', and I'd also like to know which ones have a value of true for 'BooleanColumn'. My initial thought was to create a query like this:
SELECT
COUNT(*)
,COUNT(CASE WHEN BooleanColumn = 1 THEN 1 ELSE 0 END)
FROM
MyTable
GROUP BY
KeyColumn
However, the 2nd part does not work (I'm not entirely sure why I thought it would to begin with). Is it possible to do something like this in one query? Or am I going to need to do multiple queries to make this happen?
Change COUNT to SUM in the 2nd part. ;)
... CASE WHEN BooleanColumn = 1 THEN 1 ELSE NULL END ...
COUNT counts the NON-NULL rows.
You could also do SUM(CAST(BooleanColumn AS TINYINT))