I'm using excel 15.32 on Mac and am doing a database connection to my company's MySQL database. It is the "database" option and not the "SQL server ODBC".
My query looks something like:
SELECT COUNT(requisition_item.qty)
FROM requisition_item
INNER JOIN requisition
ON requisition.id = requisition_item.requisitionId
WHERE requisition_item.itemId IN (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) AND requisition.requiredBy >= '2016-08-01'
And in parameters I map EACH "?" to a specific cell. Is there a way to make that cleaner? Can I map to a range for a count function? It's a pain to map each one especially as my list grows!
EDIT: I am counting how many times a group of numbers (located in ColA of an Excel spreadsheet) appears in my requisition_items table. I map each "?" (Excel names them Parameter1, Parameter2, etc.) to each ColA cell (A1, A2, etc.) The query simply outputs a count of how many times each value that I type in ColA appears in that table.
Related
I have brought a table from an Authority database into Excel via power query OBDC type, that includes fields like:
Date - various
Comments - mem_txt
Sequence - seq_num
The Comments field has a length restriction, and if a longer string is entered, it returns multiple rows with the Comments field being chopped into suitable lengths and the order returned in the Sequence field as per extract below. All other parts of the records are the same.
I want to collapse the rows based and concatenate the various Comments into a single entry. There is a date/time column just outside of the screen shot above that can be used to group the rows by (it is the same for the set of rows, but unique across the data set).
For example:
I did try bring the data in by a query connection, using the GROUP_CONCAT(Comments SEPARATOR ', ') and GROUP BY date, but that command isn't available in Microsoft Query.
Assuming the date/time column you refer to is named date_time, the M code would be:
let
Source = Excel.CurrentWorkbook(){[Name = "Table1"]}[Content],
#"Grouped Rows" = Table.Group(
Source,
{"date_time"},
{{"NewCol", each Text.Combine([mem_text])}}
)
in
#"Grouped Rows"
Amend the Source line as required.
I have an Excel table in which the end user enters reference numbers into the "RefNum" column. I also created a "RefNum_SQL" column with a formula that automatically formats the list with commas and parentheses for the WHERE clause, in case that makes the SQL/M portions easier. We will refer to this table as the "Input" table:
RefNum RefNum_Formatted
2123 (2123,
2456 2456,
2789 2789)
I then have a SQL query which pulls additional details tied to those reference number and loads them into a table on another sheet of the Excel workbook.
SELECT RefNum, LocationID, ShipDate
FROM database.dbo.Products (NOLOCK)
WHERE RefNum IN (2123, 2456, 2789)
The results of the query are then loaded to a table on another sheet in the workbook, which we will call the "Output" table:
RefNum LocationID ShipDate
2123 13321 12/3/2019
2456 16654 5/17/2019
2789 19987 8/24/2019
Can Power Query M code pull the values in the RefNum column directly into the WHERE clause of the SQL query, so that the end user does not have to manually adjust queries in the Advanced Editor as the list changes?
Here is the code I attempted based on the resources I have found thus far:
let
Source = Sql.Database("server", "database")
RefNum_Formatted = table.Column(Input, RefNum_Formatted)
Output = Value.NativeQuery( Source,
"SELECT RefNum, LocationID, ShipDate
FROM database.dbo.Products (NOLOCK)
WHERE RefNum = #RefNum_Formatted")
in
Output
When attempting to run the above M code, I receive the following error:
DataSource.Error: Microsoft SQL: Must declare the scalar variable "#RefNum_Formatted".
I do understand that as it's currently written, Power Query expects #RefNum_Formatted to be a declared variable in SQL as opposed to one from an Excel table; however, I cannot figure out how to create a connection to the "Input" table values that can be pulled into the query using M. Can this be done?
Similar questions are asked in the links below but I have not been able to apply any of the answers successfully:
Power Query to Filter a SQL view based on an Excel column list
Using Excel Columns in SQL Query for Where Clause
https://techcommunity.microsoft.com/t5/excel/create-dynamic-power-query-sql-by-using-excel-table-column/m-p/211504
there are few things incorrect here
"#RefNum_Formatted" is just that a string not a reference to
previous step
RefNum_Formatted step is most likely a list rather
than a string
try this code (you may need to adjust the table/column references)
let
Source = Sql.Database("server", "database"),
RefNum_Formatted = List.Accumulate(Input[RefNum_Formatted],"",(state, current) => state & current), //List.Accumulate converts list to string
SQL_Code = "SELECT RefNum, LocationID, ShipDate
FROM database.dbo.Products (NOLOCK)
WHERE RefNum IN " & RefNum_Formatted, //that's how you pass a reference to a step, it cannot be done inside a string
Output = Value.NativeQuery( Source, SQL_Code)
in
Output
EDIT: added Source step
I currently have this SUMIFS statement in Excel:
=SUMIFS( ORDERS!$I:$I,ORDERS!$D:$D, C4, ORDERS!$G:$G, "Change", ORDERS!$L:$L, "")
The first criterion is to match the identifier in cell C4 to the identifier located in "Orders". The second criterion is to ensure that the type was "Change". The third criterion is to ensure that I haven't flagged the order as excludable.
The two sheets I have are an order data sheet and a customer sheet.
My ultimate goal is to sum what is in column I for the many types of orders, based on the aforementioned first and third criteria. Currently in Excel I switch out the second criteria based on what order type I am summing.
I've transferred the data over to Access from Excel (there were a million rows and Excel was crashing even after I pasted values).
How do I structure a query to accomplish what the SUMIFS statement accomplished?
Another possible transition of sumifs from excel into Access !
select sum(Iif(colA="someValue" and colB=5 and colC is null,valueColumn,0))
from mytable
Select sum(valueColumn) as theTotal
from tableName
where colA="someValue"
and colB=5
and colC is null
What is the most efficient way to format query results, wether in the actual SQL Server SQL code or using a different program such as access or excel so that the X (first row column headers) and Y Axis (first column field values) can be changed, but with the same query result data still being represented, just in a different way.
they way the data is stored in my database and they way my original query results are returned in SQL Server 2008 are as follows:
Original Results Format
And Below is the way I need to have the data look:
How I need the Results to Look
In essence, I need to have the zipcode field go down the Y Axis (first column) and the Coveragecode field to go across the top first Row (X Axis) with the exposures filling in the rest of the data.
The only way I can thing of getting this done is by bringing the data into excel and doing a bunch of V-LookUps. I tried using some pivot tables but didn't get to far. I'm going to continue trying to format the data using the V-LookUps but hopefully someone can come up with a better way of doing this.
What you are looking for is a table operator called PIVOT. Docs: http://msdn.microsoft.com/en-us/library/ms177410.aspx
WITH Src AS(
SELECT Coveragecode, Zipcode, EarnedExposers
FROM yourTable
)
SELECT Zipcode, [BI], [PD], [PIP], [UMBI], [COMP], [COLL]
FROM Src
PIVOT(MAX(EarnedExposers) FOR CoverageCode
IN(
[BI],
[PD],
[PIP],
[UMBI],
[COMP],
[COLL]
)
) AS P;
I have a table with look like that:
People..........Client
People1............clientA
People1............clientB
People1............clientC
People1............clientD
People2............clientE
People2............clientF
People2............clientG
and the result i want is this one :
People............Client
People1.................clientA+clientB+clientC+clientD (it's a single column)
People2.................clientE+clientF+clientG (it's a single column)
The table itself is coming from different request and i have about 20 people with max 30 clients each.
This the sql request type that i'm using to create this table :
SELECT DATA.People, DATA.client
FROM DATA
GROUP BY DATA.People, DATA.client;
I think i need a loop but have no idea of how i should do it.