Comparing Column1 to Column 2 and writing to Column3 if match - sql

I have an excel worksheet linked to a SQL query in column [Raw Data]. After adding a few columns with formulas to clean up the raw data, i need to find if the value in column [ProcDataQ] exists in column [ProcDataO], all columns comprise to make Table1.
ProcDataQ | ProcDataO | Stat
--------- | --------- | ----
C1234 | C7126 | Ordered
C8372 | C6152 | No Order
C7126 | C1234 | Ordered
I am able to do this with the below formula but i have more than 20,000 records and it takes on or around 30 seconds to load or refresh the table and i figured i could speed this up using a little vba that I'll trigger to run on the query refresh.
=IF(AND(LEFT([#[Raw Data]],1)="q", (NOT(ISERROR(MATCH([#ProcDataQ],[ProcDataO], 0))))),"Ordered", "No Order Placed")
fyi, i am running excel 2010 on PC.

Just use an IF and COUNTIF statement:
=IF(COUNTIF(range, item to look up)>0,"Ordered","Not ordered")

Related

Create aggregated SQL report where the columns are based on a table list

Not sure this is possible but I'm hoping someone can point me in the right direction.
I have a database that audit logs when a record changes stage. What I need to do is consolidate this back up into a report that gives me the earliest audit record for each stage by Record ID. i.e. I end up with a report that looks like this.
+----+---------+---------+---------+
| ID | Stage1 | Stage2 | Stage3 |
+----+---------+---------+---------+
| 1 | 1/10/20 | 1/10/20 | 3/10/20 |
| 2 | 1/10/20 | 2/10/20 | 4/10/20 |
| 3 | 2/10/20 | 2/10/20 | 3/10/20 |
+----+---------+---------+---------+
I can do this with multiple select queries but as my stages are stored in a table I was wondering if there is a way to build this dynamically. This would also minimise maintenance in the future.
My "Audit_Log" table has the following columns:
RecordID
Stage
Event_Date
My "Stages" are stored in a table called "Stages" column "Stage".
Any suggestions would be really appreciated.
Thanks
You can try to use operator TRANSFORM.
TRANSFORM <aggregate-function-expression>
<select-statement>
PIVOT <expression>
[IN (<column-value-list>)]
where <aggregate-function-expression> is an expression created with one of the aggregate functions,
<select-statement> contains a GROUP BY clause, <column-value-list> is a list of required values expected to be returned by the PIVOT expression, enclosed in quotes and separated by commas. (You can use the IN clause to force the output sequence of the columns.)
If there's one event_date's value to RecordID and Stage:
TRANSFORM Max(Audit_Log.Event_Date)AS stage_date
SELECT Audit_Log.RecordID
FROM Audit_Log
GROUP BY Audit_Log.RecordID
PIVOT Audit_Log.Stage

Update part of the table for a specific category

Let's imagine I have a similar table to this one:
ID | Country | time | location 1 | location 2 | count_clients
------------------------------------------------------------------
1 | PL |2019-01-01 | JAK | ADD3 | 23
2 | PL |2019-03-01 | GGF | ADD5 | 34
3 | PL |2019-01-01 | J3K | 55D3 | 67
4 | NL |2019-04-01 | FDK | AGH3 | 2
5 | NL |2019-01-01 | GGK | AFF3 | 234
It's an aggregated table. Source contains one row per client, in my table it's aggregated showing no. of clients per country, time, location 1 and location 2. It's updated by loading new rows only (new dates). First they are loaded to stage table then, after some modifications, to final table. The values loaded to stage table are already aggregated and stage table contains only new rows.
BUT I just learned that rows in source table can be deleted - it means the "count_clients" value can change or can be deleted. What's also important - I know which COUNTRY, location 1 and location 2 are affected but I don't know WHEN they were changed (was it before or after last load? I don't know).
Do you know any smart ways to handle it? I currently load new rows + rows that were affected by change to stage tables, then remove affected rows from final table and load the stage rows to final table.
The source table is huge. I'm looking for a solution that will allow me to update only part of the table affected by the updates. Please remember that in stage table I have only new rows that needs to be inserted + the rows that was changed. I wanted to use the MERGE statement but to do that I would need to use a part of the table as a target not the whole table. I tried to do it but it didn't work.
I tried to do something like:
MERGE INTO (select country, time, location1, location 2, count from myFinalTable join stage table on country=country and location=location) --target = only rows affected by change
USING myStageTable
ON country = country and location=location
WHEN MATCHED THEN
UPDATE
SET count = count
WHEN NOT MATCHED BY TARGET then INSERT --insert new uploads
WHEN NOT MATCHED BY SOURCE then DELETE
but it looks like I can't use the 'select' statement in target..?

Concatenating rows using SQL in Excel from Access Database

I have an Excel 2010 spreadsheet which gathers data from an Access database, one of the tables I need to get data from is set up like so:
|UniqueID | PaymentID | ClaimID |
1 | 1234 | 5556 |
2 | 1234 | 5557 |
3 | 1235 | 5558 |
4 | 1236 | 5559 |
5 | 1236 | 5560 |
What I need though is for the ClaimID's to be concatenated onto the same row based on the PaymentID so it should look something like this:
|UniqueID | PaymentID | ClaimID |
| 1 | 1234 | 5556, 5557 |
| 2 | 1235 | 5558
| 3 | 1236 | 5559, 5560 |
I've tried this using VBA code which worked, however it took too long to process. I have tried PowerQuery which also worked but the rest of the business don't have it installed, so won't work in the long run. My final thought was to use the SQL in connection options to manipulate the data, but I'm not sure how. I've searched and tried the following functions: ConcatADO, Concatlist, Stuff and Group_Concat but have not had any luck (maybe i'm using them wrong?).
Other information that might be helpful:
There is about 40,000 rows of data coming from the database on this particular table. While I use Excel 2010 some users will be using Excel 2007, 2010, 2013 and 2016.
Is there a way to achieve the concatenation of ClaimsID rows based on the PaymentID using SQL or a different method that I may not have thought about?
Thanks in Advance.
In normal sql something like:
SELECT
PaymentID ,
GROUP_CONCAT(ClaimID) GroupedName
FROM table_name
GROUP BY PaymentID
Should Work, In Postgres SQl something Like:
SELECT
PaymentID ,
array_agg(ClaimID)
FROM table_name
GROUP BY PaymentID
Should Work
For anyone else who may have this issue, I couldn't get the SQL way to work in the end so I had to go for something far less elegant.
I pulled the data through using SQL and added two helper columns onto the Excel data table. Before applying formulas - make sure the data is arranged in (for my instance) PaymentID order lowest to highest - in the first helper (named "ClaimIDConCat") column I placed this formula:
=IF([#PaymentID]=OFFSET([#PaymentID],-1,0),OFFSET([#ClaimIDConCat],-1,0)&", "&[#ClaimID],[#ClaimID])
This provides me with an ever growing list of ID's related to the payment. In the second helper column (named "FinalRow") i used this formula:
=IF([#PaymentID]<>OFFSET([#PaymentID],1,0),"FinalRow","NotFinal")
This will simply look to see if the row underneath is the same as the current row - if it is the same "NotFinal" is placed in the cell if it is "FinalRow" is used.
Finally my final piece was to pull the data into another table using a simple INDEX() MATCH() array formula as such:
{=IFERROR(INDEX(Tablename[ClaimIDConCat],MATCH(1,([#RelatedID]=ClaimIDData[PaymentID])*("FinalRow"=Tablename[FinalRow]),0)),"N/A")}
This provided me with the overall required output.

How to spool three columns from a table and check the summation of the third column in UNIX shell script

I have created a query which results three columns. I am able to fetch the details in the spool file and based on that i am checking a condition that the sum of all the values (numerical) from the third column is 0 or not. If not 0, then in the mail the complete result should come.
Issues i am facing are:
1) When i am writing simple SELECT query for the three columns, the results are not coming as three columns and single row for single record. But it is displaying as one row for each column value.
i.e. in TOAD the result is as:
|Column_name_1 | Column_name_2 | Column_name_3 |
+--------------+-----------------+----------------+
| text_1 | text_2 | num_1 |
| text_3 | text_4 | num_2 |
But in the spool file, i am getting result as--
|text_1 |
|text_2 |
|num_1 |
| text_3 |
| text_4 |
| num_2 |
2) The other issue is i am not getting any header in the spool file.
Can anyone please look into this and let me know how to proceed.
Try adding SET RECSEP OFF to fix the issue 1, which will solve your problem for record seperation.
Add SET HEADING ON to print the column headers.
See this link for a learning.

Transforming a 2 column SQL table into 3 columns, column 3 lagged on 2

Here's my problem: I want to write a query (that goes into a larger query) that takes a table like this;
ID | DATE
A | 1
A | 2
A | 3
B | 1
B | 2
and so on, and transforms it into;
ID | DATE1 | DATE2
A | 1 | 2
A | 2 | 3
A | 3 | NOW
B | 1 | 2
B | 2 | NOW
Where the numbers are dates, and NOW() is always appended to the most recent date. Given free rein I would do this in Python, but unfortunately this goes into a larger query. We're using SyBase's SQL Anywhere 12, I think? I interact with the database using SQuirreL SQL.
I'm very stumped. I thought (SQL query to transform a list of numbers into 2 columns) would help, but I'm afraid I don't know enough to make it work. I was thinking of JOINing the table to itself, but I don't know how to SELECT for only the A-1-2 rows instead of the A-1-3 rows as well, for instance, or how to insert the NOW() value into it. Does anyone have any ideas?
I made a an sqlfiddle.com to outline a solution for your example. You were mentioning dates, but using integers so I chose to do an integer example, but it can be modified. I wrote it in postgresql so the coalesce() function can be substituted with nvl() or similar. Also, the parameter '0' can be substituted with any value, including now(), but you must change the data type of the "i" column in the table to be a date as well. Please let me know if you need further help on this.
select a.id, a.i, coalesce(min(b.i),'0') from
test a
left join test b on b.id=a.id and a.i<b.i
group by a.id,a.i
order by a.id, a.i
http://sqlfiddle.com/#!15/f1fba/6