SQL Sum one column based on various text in another - sql

First question here so not sure exactly how to describe my issue really.
Suppose I have a very large table called "soitem". From this table, I would like to see rows "soitem.productnum", "soitem.description", and "oitem.qtyfulfilled".
This will be filtered by "soitem.datelastfullfillment" which so far looks like this:
SELECT soitem.productnum, SUM(soitem.qtyfulfilled) AS Total_Sold
FROM soitem
WHERE soitem.datelastfulfillment BETWEEN '9/1/15' AND '9/30/15'
GROUP BY soitem.productnum
I would like to take this a step further and have it only sum the qtyfulfilled when a different column, "qbclassid", equals (2, 3, 9, 12, 14).
If you notice, my query also does not show soitme.description yet since it usually gives me an error. If anyone can get that to work too, even better!

Try this:
SELECT soitem.productnum, soitem.description, SUM(soitem.qtyfulfilled) AS Total_Sold
FROM soitem
WHERE soitem.datelastfulfillment BETWEEN '9/1/15' AND '9/30/15'
AND soitem.qbclassid IN (2, 3, 9, 12, 14)
GROUP BY soitem.productnum, soitem.description

Related

Big query Pivoting with a specific requirement

I have used pivot in big query, but here is a specific use case and the data that I need to show in looker. I am trying the similar option in looker but wanted to know if I can just show this in big query.
This is how my data (Sample) in BIG QUERY table is:
The output should be as below:
If you look at it, it's pivoting but I need to assign the column names as shown (for the specific range) and for the range 6 and more, I need to add the pivot columns data into one.
I don't see pivot index or something like this in BIG_QUERY. Was thinking if there is a way to sum up the column data after pivot index 6 or so? Any suggestions how to achieve this?
Hope below approach would be helpful,
SELECT * FROM (
SELECT Node, bucket, total_code
FROM sample, UNNEST([RANGE_BUCKET(data1, [1, 2, 3, 4, 5, 6, 7])]) bucket
) PIVOT (SUM(total_code) `range` FOR bucket IN (1, 2, 3, 4, 5, 6, 7));
output:
RANGE_BUCKET - https://cloud.google.com/bigquery/docs/reference/standard-sql/mathematical_functions#range_bucket

Possible to do subselect in Google Sheets

I have the following data in the movies data range:
I would like to do a subselect to get the highest-grossing movie for that director. I can do it by adding a new column like this:
Note that I've used the hacky 'nested-query' notation to remove the header row and just return a single scalar value:
=QUERY(QUERY(movies, "SELECT MAX(C) WHERE A='"&A2&"' GROUP BY A", 0), "SELECT * OFFSET 1", 0)
However, I was wondering if I could just do a single query on the director|movie|boxoffice columns with a subselect within the query statement, I suppose it would come out to something like:
=QUERY(movies, "SELECT A, B, C, (SELECT MAX(C) WHERE A='"&A2&"' GROUP BY A)", 0)
I believe the answer to this is a straight 'no', but I was curious if there's any sort of sub-query composability within the google sheets query language, or if I just need to sort of figure out workarounds here?
https://developers.google.com/chart/interactive/docs/querylanguage
try:
=INDEX(IFNA(VLOOKUP(A2:A, SORT(A2:C, 3, ), 3, )))
or whole:
=INDEX({A1:C, {"highestgrossing"; IFNA(VLOOKUP(A2:A, SORT(A2:C, 3, ), 3, ))}})

How to get data in a column in order by using SQL in operator

There is a data set as shown below;
When input for event_type is 4, 1, 2, 3 for example, I would like to get 3, 999, 3, 9 from cnt_stamp in this order. I created a SQL code as shown below, but it seems like it always returns 999, 3, 9, 3 regardless the order of the input.
How can I fix the SQL to achieve this? Thank you for taking your time, and please let me know if you have any question.
SELECT `cnt_stamp` FROM `stm_events` WHERE `event_type` in (4,1,2,3)
Add ORDER BY FIELD(event_type, 4, 1, 2, 3) in your query. It should look like:
SELECT cnt_stamp FROM stm_events WHERE event_type in (4,1,2,3) ORDER BY FIELD(event_type, 4, 1, 2, 3);
its cannot because as default the data sort by ascending, if u want result like u want,, better u create 1 column for indexing

Doing a concat over a partition in SQL?

I have some data ordered like so:
date, uid, grouping
2018-01-01, 1, a
2018-01-02, 1, a
2018-01-03, 1, b
2018-02-01, 2, x
2018-02-05, 2, x
2018-02-01, 3, z
2018-03-01, 3, y
2018-03-02, 3, z
And I wanted a final form like:
uid, path
1, "a-a-b"
2, "x-x"
3, "z-y-z"
but running something like
select
a.uid
,concat(grouping) over (partition by date, uid) as path
from temp1 a
Doesn't seem to want to play well with SQL or Google BigQuery (which is the specific environment I'm working in). Is there an easy enough way to get the groupings concatenated that I'm missing? I imagine there's a way to brute force it by including a bunch of if-then statements as custom columns, then concatenating the result, but I'm sure that will be a lot messier. Any thoughts?
You are looking for string_agg():
select a.uid, string_agg(grouping, '-' order by date) as path
from temp1 a
group by a.uid;

SQL Dynamic Column Query [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Is there a way to create a SQL Server function to "join" multiple rows from a subquery into a single delimited field?
I have a table of "Events", and each event has a a list of 1-4 (essentially variable #) "Users". So let's say I get all events for today, and then I want to list the users as a dynamic number of columns, rather than repeated rows.
Right now I have
SELECT E.EventID, E.Time, U.Name FROM Events
INNER JOIN Users U ON E.UserID = U.UserID
WHERE Date = '12/20/2010'
This brings me results like:
EventID, Time, Name
211, '4:00am', 'Joe'
211, '4:00am', 'Phil'
211, "4:00am', 'Billy'
218, '7:00am', 'Sally'
218, '7:00am', 'Susan'
I can work with this and it's acceptable, however the duplication for EventID and Time (there are more columns in my actual query) seems wasteful to me. What I would really like in the output is this:
EventID, Time, Name1, Name2, Name3
211, '4:00am', 'Joe', 'Phil', 'Billy'
218, '7:00am', 'Sally', 'Susan', NULL
I have tried looking at tutorials for PIVOTs (I have SQL 2008), but I don't know if they conceptually match what I'm trying to do. Most of them are using "MIN" or "MAX".
Maybe this can't be done? My other alternative is to get a list of Events for today, and then loop through that, finding a recordset of Users for that Event. I would prefer to grab it all in one query though.
Any ideas?
Returning a variable number of columns would be harder to deal with in code. The result set you are currently getting is easy to transform into an object that you can worth with in code.
What are you trying to do with the output?