I have data in sql table which looks something like this.
WORKFLOW_ID|SLOT_NAME |VALUE |PROCESS_ID|
-----------|--------------|-------------------|----------|
47|pm_id |6355 | 212331|
47|entry_id |7722 | 212331|
47|tecn_no |T0212331 | 212331|
47|issue_date |2020-10-24 | 212331|
47|entry_datetime|2020-10-20 15:09:22| 212331|
I want to convert SLOT_NAME column values into COLUMNS NAMES and VALUE column into the COLUMN VALUES. So the data will look something like this.
WORKFLOW_ID|SLOT_NAME |pm_id |entry_id| tecn_no| issue_date| entry_datetime| PROCESS_ID|
-----------|--------------|-------------------|--------|------------|-----------------|------------------------|--------------|
47|pm_id |6355 | 7722| T0212331| 2020-10-24| 2020-10-20 15:09:22| 212331|
Keeping in mind there might be more SLOT NAMES or rows.
You might want to explore the dynamic pivot function written by Anton from AMIS. The source can be found here
https://technology.amis.nl/wp-content/uploads/images/antonsPivoting.zip
and some samples of its use here
https://technology.amis.nl/it/dynamic-sql-pivoting-stealing-antons-thunder/
Internally it is executing a standard query against your data, and then fetching the results in order to build an ANYDATA table in order to give you the dynamic list of columns back.
If you're on a more recent release, you could look at polymorphic table functions to do it, which is covered here
https://blog.sqlora.com/en/dynamic-pivot-with-polymorphic-table-function/
and if you are really on a recent release, ie, 21c you could use some SQL macros to do it
https://blog.sqlora.com/en/dynamic-pivot-with-sql-macros-in-oracle-20c/
Related
I am trying to use a LATERAL JOIN on a particular data set however i cannot seem to get the syntax correct for the query.
What am i trying to achieve:
Take the first column in the dataset (See picture) and use that as the Table headers (rows) and populate the rows with the data from the StringValue column
Currently it appears like this:
cfname | stringvalue |
----------------------------------------
customerrequesttype | newformsubmission|
Assignmentgroup | ITDEPT |
and I would like to have it appear as this:
customerrequesttype| Assignmentgroup|
-------------------------------------
newformsubmission | ITDEPT
As mentioned i am very new to SQL i know limited basics
+---------+
| Language|
+---------+
|Spanish |
|spanish |
|venezla |
|venezuala|
|irish |
|Irish |
+---------+
Best approach for normalising data in a sql column? I was thinking of converting to lower case and then using multiple replace functions. Is this the only way? Any insight appreciated thanks :)
There are many ways in sql to do it my friend, it totally depends on the scenario and how you want to utilize it.
Looking at the above ask, you can use LOWER function and then extract the DISTINCT values to give unique values instead of putting multiple REPLACE functions every time you see a new mismatched value.
Or you can delete duplicate values by applying ROW_NUMBER and LOWER function if you want to play around with 1 table only.
Let me know your feedback and i can revert with more inputs.
How would I go about joining results from multiple SQL queries so that they are side by side (but unrelated)?
The reason I am thinking of this is so that I can run 1 query in Google Big Query and it will return 1 single table which I can import into Excel and do some charts.
e.g. Query 1 looks at dataset TableA and returns:
**Metric:** Sales
**Value:** 3,402
And then Query 2 looks at dataset TableB and returns:
**Name:** John
**DOB:** 13 March
They would both use different tables and different filters, etc.
What would I do to make it look like:
---Sales----------John----
---3,402-------13 March----
Or alternatively:
-----Sales--------3,402-----
-----John-------13 March----
Or is there a totally different way to do this?
I can see the use case for the above, I've used something similar to create a single table from multiple tables with different metrics to query in Data Studio so that filters apply to all data in the dataset for example. However in that case, the data did share some dimensions that made it worthwhile doing.
If you are going to put those together with no relationship between the tables, I'd have 4 columns with TYPE describing the data in that row to make for easier filtering.
Type | Sales | Name | DOB
Use UNION ALL to put the rows together so you have something like
"Sales" | 3402 | null | null
"Customer Details" | null | John | 13 March
However, like the others said, make sure you have a good reason to do that otherwise you're just creating a bigger table to query for no reason.
I haven't been able to figure out exactly how to put together this SQL string. I'd really appreciate it if someone could help me out. I am using Access 2016, so please only provide answers that will work with Access. I have two queries that both have different fields except for one in common. I need to find the minimum absolute difference between the two similar columns. Then, I need to be able to pull the data from that corresponding record. For instance,
qry1.Col1 | qry1.Col2
-----------|-----------
10245.123 | Have
302044.31 | A
qry2.Col1 | qry2.Col2
----------------------
23451.321 | Great
345622.34 | Day
Find minimum absolute difference in a third query, qry3. For instance, Min(Abs(qry1!Col1 - qry2!Col1) I imagine it would produce one of these tables for each value in qry1.Col1. For the value 10245.123,
qry3.Col1
----------
13206.198
335377.217
Since 13206.198 is the minimum absolute difference, I want to pull the record corresponding to that from qry2 and associate it with the data from qry1 (I'm assuming this uses a JOIN). Resulting in a fourth query like this,
qry4.Col1 (qry1.Col1) | qry4.Col2 (qry1.Col2) | qry4.Col3 (qry2.Col2)
----------------------------------------------------------------------
10245.123 | Have | Great
302044.31 | A | Day
If this is all doable in one SQL string, that would be great. If a couple of steps are required, that's okay as well. I just would like to avoid having to time consumingly do this using loops and RecordSet.Findfirst in VBA.
You can use a correlated subquery:
select q1.*,
(select top 1 q2.col2
from qry2 as q2
order by abs(q2.col1 - q1.col1), q2.col2
) as qry2_col2
from qry1 as q1;
I have a query that produces a 2 field result: Email and Interest.
The result is millions of records. But there are about 100 distinct Interests.
I would like to run the query to produce a result that is 101 fields wide like this:
Email | Books | Cats | Dogs | ETC
Where the metric is the count of each.
With my knowledge of SQL thus far I'd have to use CASE WHEN. But I'd have to write 100 lines of code.
Is there a better way?
You could use the PIVOT statement but sounds like terradata does not support that. Pivot would require typing in all column names as well. Don't think you can avoid that