SELECT DISTINCT AND GET ALL VALUES FOR EACH COLUMN SQL ACCESS - sql

I have a table that have grouped all samples returned from the lab analysis of my work.
The problem is that some labs send different elements via different files, and now I'm facing problems.
I need to make a query that grab all values that are stored in different columns and assign it in just one row for each sample. Example:
Batch Source_File Value Value 2
SAMPLE A 1 A 150 null
SAMPLE A 1 B null 100
SAMPLE B 2 C null 300
SAMPLE B 2 D 100 null
OUTPUT
Batch Source_File Value Value 2
SAMPLE A 1 A,B 150 100
SAMPLE B 2 C,D 100 300

You can use plain SQL:
SELECT
Samples.Sample,
Samples.Batch,
Min([Source_File]) & "," & Max([Source_File]) AS Source_Files,
Sum(Samples.Value) AS Sum1,
Sum(Samples.Value2) AS Sum2
FROM
Samples
GROUP BY
Samples.Sample,
Samples.Batch;
Output:

Related

Oracle SQL - Need to eliminate data if at least one of the particular condition is not satisfied

My question is related to Oracle sql. I have a two tables say, study table and another one is study part table. Stdyno is the primary key in study table and (stydyno + sqncno) is the primary key in studypart table.
EG: studypart table has data as below.
studyNo sqnc part approvalIN
--------------------------------
123 1 fgh Y
123 2 jhf N
123 3 rty N
456 1 wer N
456 2 wdg N
456 3 ghg N
I need query in such a way that my output from studypart table gives result
as study number which has all the approvalIn as N. If it has at least one of the approvalIn as 'Y'
then that studyno should be excluded from the result.
Desired output:
studyno: 456
I tried this implementation in stored procedure taking Y and N approvalIn count separately ie,
if a studyno has both the count then exclude it and
if it has only one count say either N or Y the include it.
But i would like to know how to achieve this is query.
You can do it by excluding those rows whose count of "approvalIN = 'N'" does not match the total count of "approvalIN" values.
SELECT STUDYNO
FROM tab
GROUP BY STUDYNO
HAVING SUM(CASE WHEN approvalIN = 'N' THEN 1 END) = COUNT(approvalIN)
Check the demo here.

Select top n (variable) for each criteria in a table based on another table

I want a VBA code to make a query to show Equip with Top ActiveTime for each ModelID (from 1st table) based on TopN for each ModelID (from the 2nd table), I know i have to use QueryDef and Sql VBA but I can't figure how to write the code
Just and Example to illustrate
My 1st table is
EquipID
Equip
ActimeTime
ModelID
1
DT1
10
1
2
DT2
6
1
3
DT3
13
1
4
DT4
15
1
5
DT5
16
2
6
DT6
12
2
7
DT7
6
2
8
DT8
13
2
My 2nd Table is
ModelID
Model
TopN
1
775
3
2
789
2
So the query result should be like (Showing the Top 3 of 775 Model and the Top 2 of 789)
Equip
ActimeTime
Model
DT4
15
775
DT3
13
775
DT1
10
775
DT5
16
789
DT8
13
789
Thanks a lot in advance, I'm really stuck at this one and solving this will help me a lot in my project
[Table1][1]
[1]: https://i.stack.imgur.com/geMca.png
[Table2][2]
[2]: https://i.stack.imgur.com/lMPDP.png
[Query Result][3]
[3]: https://i.stack.imgur.com/cGf6k.png
You can do it in straight SQL - but oooh is it ugly to follow and construct
I created 4 queries with the final one resulting in what you're looking for.
The key was to get a RowID based on the sorted order you're looking for (Model and ActimeTime). You can get a pseudo Row ID using Dcount
Here's the 4 queries - I'm sure you can make one mashup if you're daring
My tables are Table3 and Table4 - you can change them in the first query to match your database. Build these queries in order as they are dependent on the one before them
qListModels
SELECT Table3.Equip, Table3.ActimeTime, Table4.Model, Table4.TopN, "" & [Model] & "-" & Format([ActimeTime],"000") AS [Model-ActTime]
FROM Table3 INNER JOIN Table4 ON Table3.ModelID = Table4.ModelID
ORDER BY Table4.Model, Table3.ActimeTime DESC;
qListModelsInOrder
SELECT qListModels.*, DCount("[Model-ActTime]","[qListModels]","[Model-ActTime]>=" & """" & [Model-ActTime] & """") AS row_id
FROM qListModels;
qListModelStartRows
SELECT qListModelsInOrder.Model, Min(qListModelsInOrder.row_id) AS MinOfrow_id
FROM qListModelsInOrder
GROUP BY qListModelsInOrder.Model;
qListTopNModels
SELECT qListModelsInOrder.Equip, qListModelsInOrder.ActimeTime, qListModelsInOrder.Model
FROM qListModelsInOrder INNER JOIN qListModelStartRows ON qListModelsInOrder.Model = qListModelStartRows.Model
WHERE ((([row_id]-[MinOfrow_id])<[TopN]))
ORDER BY qListModelsInOrder.Model, qListModelsInOrder.ActimeTime DESC;
This last one can be run anytime to get the results you want
Example Output:

Create a new record based on multiple records

I have a table with Accounts that, if there are multiple matching ones, I need to combine to create a new one in a procedure.
The table looks like:
ACCT ID QTY LEI
A_1 2 200 NULL
A_2 3 200 NULL
A_3 3 200 0
A_1 3 100 NULL
BB_1 2 200 NULL
BB_2 2 100 NULL
BB_3 3 200 0
BB_1 3 100 NULL
What I am trying to do is:
Find the ones I need to combine based on ACCT; The data above basically has two ACCTS, A and BB, the "_" are just to identify them as individual sub accounts.
For column QTY: SUM of QTY based on the ID and ACCT
For column LEI: If any record in the group of ACCT and ID is 0 and rest are NULL then replace with 0, if all are NULL then NULL
If there's only one record (no other record to merge with), that whole line will be used (see first record in table) .
Create a new record based on the above, rename ACCT to _X and delete the existing records it has used
End result of the above looks like this:
ACCT ID QTY LEI
A_X 2 200 NULL
A_X 3 500 0
BB_X 2 300 NULL
BB_X 3 300 0
Not sure what the best way of approaching this is, any ideas on this?
use string operations (like SubString and IndexOf) to parse the name of the account
use group by and aggregate functions (i.e. Sum) to calculate the results
insert the results into a temp table, delete the original data, insert back into the original table

How to merge two tables with a different amount and order of columns in SSMS?

I have one large table with ~10,000 rows of data and 100 columns that I want to continuously update. The problem is that the files I will use to update (.csv) often are in different orders or contain extra/missing columns. If there are extra columns in the update I am fine discarding them, but I want the remaining columns to match up exactly, even if some are missing or out of order.
I know that there is a solution in creating a select and simply listing all columns, but I am looking for something more elegant/foolproof. Many of the examples I have seen work well enough using MERGE, UNION, or JOIN but I can't get them to work for this much larger dataset, which is why it has been giving me so much trouble. I am not very experienced with SQL so I would appreciate some additional padding to the explanation.
Where ABCD are columns and 1 is data: Here is the master table
a b c d
1 1 1 1
Here is the update table:
b c d e
1 _ 1 1
Only imagine that there are 100 columns and 100 rows to append to the 10,000 stored.
Desired:
a b c d e
1 1 1 1
_ 1 _ 1 1
Or even
a b c d
1 1 1 1
_ 1 _ 1
e:
This answer is exactly what I want, but it doesn't seem possible in TSQL
https://stackoverflow.com/a/52524364/11777090
do union all
select a,b,c,d,0 from table
union all
select 0,b,c,d,e from table

Extract only variables which is greater than other table in influxDB

I am using influxDB and I would like to extract some values which is greater than certain threshold in other table.
For example, I have two tables as shown in below.
Table A
Time value
1 15
2 25
3 9
4 22
Table B
Time threshold
1 16
2 12
3 13
4 15
Give above two tables, I would like to extract three values which is greater than first row in Table B. Therefore what I want to have is as below.
Time value
2 25
4 22
I tried it using below sql query, but it didn't give any correct result.
select * from data1 where value > (select spec from spec1 limit1);
Look forward to your feedback.
Thanks.
Integrate the condition in an inner join:
select * from tableA as a
inner join tableB as b on a.id=b.id and a.value > b.threshold
When your time column doesn't only include integer values, you have to format the time and join on a time range. Here is an example:
SQL join on time range