Two query functions in Google Sheets - arrays

I have created a Google Sheet query that is pulling data from four other sources.
=QUERY({'1ST A'!A4:BF;'1ST B'!A4:BF;'1ST C'!A4:BF;'1ST D'!A4:BF},"select * where Col1 <>''")
It works well. I am trying to add an additional query that enables the data to be sorted in the master spreadsheet according to a value in the specific column. I am trying to add something like this...
=query(A4:BF,"select * order by N asc")
How do I combine the two criteria and am I writing the command correctly?

try:
=QUERY({'1ST A'!A4:BF; '1ST B'!A4:BF; '1ST C'!A4:BF; '1ST D'!A4:BF},
"where Col1 <>'' order by Col14 asc")

Related

Need to Add Values to Certain Items

I have a table that I need to add the same values to a whole bunch of items
(in a nut shell if the item doesn't have a UNIT of "CTN" I want to add the same values i have listed to them all)
I thought the following would work but it doesn't :(
Any idea what i am doing wrong ?
INSERT INTO ICUNIT
(UNIT,AUDTDATE,AUDTTIME,AUDTUSER,AUDTORG,CONVERSION)
VALUES ('CTN','20220509','22513927','ADMIN','AU','1')
WHERE ITEMNO In '0','etc','etc','etc'
If I understand correctly you might want to use INSERT INTO ... SELECT from original table with your condition.
INSERT INTO ICUNIT (UNIT,AUDTDATE,AUDTTIME,AUDTUSER,AUDTORG,CONVERSION)
SELECT 'CTN','20220509','22513927','ADMIN','AU','1'
FROM ICUNIT
WHERE ITEMNO In ('0','etc','etc','etc')
The query you needs starts by selecting the filtered items. So it seems something like below is your starting point
select <?> from dbo.ICUNIT as icu where icu.UNIT <> 'CTN' order by ...;
Notice the use of schema name, terminators, and table aliases - all best practices. I will guess that a given "item" can have multiple rows in this table so long as ICUNIT is unique within ITEMNO. Correct? If so, the above query won't work. So let's try slightly more complicated filtering.
select distinct icu.ITEMNO
from dbo.ICUNIT as icu
where not exists (select * from dbo.ICUNIT as ctns
where ctns.ITEMNO = icu.ITEMNO -- correlating the subquery
and ctns.UNIT = 'CTN')
order by ...;
There are other ways to do that above but that is one common way. That query will produce a resultset of all ITEMNO values in your table that do not already have a row where UNIT is "CTN". If you need to filter that for specific ITEMNO values you simply adjust the WHERE clause. If that works correctly, you can use that with your insert statement to then insert the desired rows.
insert into dbo.ICUNIT (...)
select distinct icu.ITEMNO, 'CTN', '20220509', '22513927', 'ADMIN', 'AU', '1'
from ...
;

SQL query based on list from another query

I am trying to build a query that will generate a list of records based on the results of a very similar query.
Here are the details and examples
Query 1: Generate a list if part #'s in a specific location of the warehouse.
Query 2: Use the list of part #'s generated in #1 to show all locations for the list of part #'s, assuming they will be in both the location specified in #1 and other locations.
Query 1 looks like this:
Select
ItemMaster.ItemNo, BinInfo.BIN, ItemDetail.Qty, ItemDetail.Whouse_ID
From
((ItemDetail
Left Join
ItemMaster on ItemMaster.ID=ItemDetail.Item_ID)
Left Join
BinInfo on BinInfo.ID = ItemDetail.Bin_ID)
Where
ItemDetail.Whouse_ID = '1'
And BinInfo.Bin = 'VLM';
Query 2 needs to be almost identical except the ItemMaster.ItemNo list will come from query #1.
Any help here would be great. I don't know if I need to learn Unions, Nested Queries, or what.
make sure that your first query returns the list of ids that you need.
then write the second query with the WHERE id IN (...) syntax:
SELECT * FROM table1 WHERE id IN
(SELECT id FROM table2 WHERE...) -- first query

Converting two columns table into one column table in Google Sheets

In column A there is a list of tasks.
In column B each task has an associated group.
How to, using built-in formulas, generate sequence like in column D?
Here is a screenshot :
try:
=ARRAYFORMULA(TRIM(TRANSPOSE(SPLIT(QUERY(TRANSPOSE(QUERY(QUERY(IF(A2:B="",,A2:B&"♦"),
"select max(Col1) where Col1 is not null group by Col1 pivot Col2", 0)
,,999^99)),,999^99), "♦"))))
This should work as well as player0s. I keep trying to get him to use FLATTEN() :)
=QUERY(FLATTEN(TRANSPOSE(QUERY(A2:B,"select Max(A) group by A pivot B"))),"where Col1<>''")

SQL How to compare if a variable is the same as a value in a database column?

The problem I have is the following:
I work with a ticket system that uses plugins to realize workflows.
In this case I use SQL to presort incoming emails.
The SQL query looks like this:
SELECT Count(Case when {MSG_CC_002_DeviceReg_MailBody} LIKE '%You have received an invoice from%' Then 1 END);
What I want to do now is instead of using LIKE and then a certain phrase like above, I want to compare this to a column in a database table that contains all necessary phrases.
The table has only two columns, phraseID and phrase.
{MSG_CC_002_DeviceReg_MailBody} is the variable that needs to compared against the values of the column.
So if the variable matches with an entry in the column it should just return 1.
[Edit:]
This is just one of the things I want to use this for, I also have a variable {MSG_CC_002_DeviceReg_MailSender} that will provide the email address that I want to compare to a similar table that contains email addresses.
Is this possible?
If so - how?
You can use join or a subquery:
select count(*)
from t
where exists (select 1
from othertable ot
where {MSG_CC_002_DeviceReg_MailBody} LIKE '%' + ot.phrase + '%'
) ;
This will be dog-slow if you have a lot of phrases or email addresses, but it'll give you what you want.
SELECT COUNT(*) AS RetValue
FROM PhraseTable
WHERE {MSG_CC_002_DeviceReg_MailBody} LIKE '%' + phrase + '%';
Yes it is possible, you can achieve this with using dynamic query. Basically you need to construct your query as a string then execute it.
You can find examples and more information about dynamic query within the following link;
https://www.mssqltips.com/sqlservertip/1160/execute-dynamic-sql-commands-in-sql-server/

Transpose results of several SUM queries. Is this possible?

I built a report based off a query that summed multiple values from a single table. It's essentially a Year To Date report, in this case, Pesticides received year to date. While the report works as a quick reference check, these totals need to be plugged (copy/pasted) into a master excel sheet that keeps a running "available balance" inventory. When I export the totals to an excel from the report, the data is not in the format it needs to be. It's horizontal instead of vertical. I can copy the data, paste it to Excel, transpose it and then copy/paste the data to where it belongs,but was wondering if it could be transposed when the query outputs the totals.
I tried the crosstab method but that appears to apply to a single table, and this query displays the sums of the columns. Perhaps I did it wrong using the crosstab method, but I tried many different ways and did not get the desired result.
Here's is the SUM query
SELECT
Agnique_MMF_Recieved.[SumOfAgnique MMF],
Altosid_30_Day_Recieved.[SumOfAltosid 30 Day],
Altosid_SR-20_Recieved.[SumOfAltosid Liquid SR-20],
Altosid_Pellets_Recieved.[SumOfAltosid Pellets],
Altosid_WSP_Recieved.[SumOfAltosid WSP],
Altosid_XR_Recieved.[SumOfAltosid XR],
Altosid_XRG_Recieved.[SumOfAltosid XRG],
Aquabac_200G_Recieved.[SumOfAquabac 200G],
BVA_2_Recieved.[SumOfBVA 2 Oil],
Four_Star_BTI_Received.[SumOfFourStar BTI],
Golden_Bear_Recieved.[SumOfGolden Bear],
Metalarv_S-9_Recieved.[SumOfMetalarv S-9],
Sustain_MGB_Recieved.[SumOfSustain MGB],
Vectobac_GS_Received.[SumOfVectobac - GS],
Vectobac_12AS_Recieved.[SumOfVectobac 12AS]
FROM
Agnique_MMF_Recieved,
Altosid_30_Day_Recieved,
Altosid_Pellets_Recieved,
Altosid_SR-20_Recieved,
Altosid_WSP_Recieved,
Altosid_XR_Recieved,
Altosid_XRG_Recieved,
Aquabac_200G_Recieved,
BVA_2_Recieved,
Four_Star_BTI_Received,
Golden_Bear_Recieved,
Metalarv_S-9_Recieved,
Sustain_MGB_Recieved,
Vectobac_12AS_Recieved,
Vectobac_GS_Received;
I would like the data to end up looking like
SumofAgniqueMMF as the row header and next to it a totals column with the value from the sum.
Anyway, is this possible? Thanks!
Can I assume that each of those tables in your FROM clause are single-record tables/views with just a single SUM in them?
if so:
SELECT 'SumOfAgnique MMF' AS [Description],
Agnique_MMF_Recieved.[SumOfAgnique MMF] AS [Sum]
FROM [Agnique_MMF_Recieved]
UNION ALL
SELECT 'SumOfAltosid 30 Day' AS [Description],
Altosid_30_Day_Recieved.[SumOfAltosid 30 Day] AS [SUM]
FROM [Altosid_30_Day_Recieved]
...etc
Consider: t==> a table, f ==> a field, r ==> a result set.
select r1.s1, r2.s2 ... rn.sn
from
(
(select sum(f1) as s1 from t1) r1,
(select sum(f2) as s2 from t2) r2,
...
...
(select sum(fn) as sn from tn) rn
)

Resources