Syntax for DISTINCT query on one column in MS Query - excel

I have an Excel spreadsheet which I use as a relational database for my milk round. I query this database using MS Query in Excel (Mac 2011 Version) to generate my delivery routes. One of the columns is the customer address and I'd like to have this shown once per order i.e. have a distinct query for just this column while displaying multiple other rows. It's purely for cosmetic purposes to make the spreadsheet less cluttered.
The main spreadsheet I use as my database has column headings which I have screenshotted, complete with some sample data:
From this main spreadsheet I use MS Query to generate my delivery route which looks like this:
As you can see there is a lot of repeated data in the route generated from the query. What I'd like to do is have just one instance of the address per customer's order, it would help with the legibility of the route when opened in an iPad. I hide other columns that aren't really necessary to help in that regard.
*EDIT
From isolated's comments below, here's a screenshot of ideally how the data returned from the query should look:
I've manually deleted the repeated info in the name & address column to achieve the desired result. I've also hidden some columns that aren't really necessary and I use some conditional formatting rules to help distinguish each customer's order.
EDIT*
I have tried using a group by clause and the following window function but can't get it to work:
SELECT *
FROM (
SELECT “All Orders”.”Route ID”,
“All Orders”.Name,
“All Orders”.Address
ROW_NUMBER() OVER(PARTITION BY “All Orders”.Address
ORDER BY “All Orders”.Address DESC) AS row_number
FROM “All Orders”
) AS rows
WHERE row_number = 1;
Whenever I try to run the query I get an error message regarding syntax. Hopefully someone can tell me where I'm going wrong!

I don't know MS Sql at all, but you could do something with a formula in excel. If you don't like this solution, simply put a comment below that you would still like a sql route and I can get you a query to try to adapt to ms sql.
Create another column and call it address2 (or several more columns if your address field is multiple columns).
Then use this/these formula and adjust as needed:
Column F (address2): =IF(A2=A1,"",C2)
Column G (town2): =IF(A2=A1,"",D2)
You can then hide columns C and D.
=============
U P D A T E
Here's a method that works in many dbms such as postgres, but I don't know how to adapt [rank() over (partition by...] to excel sql.
select account,
cust_name,
item,
case
when prod_rank = 1 then address
else ''
end address
from (
select
account,
cust_name,
item,
address,
rank() over (partition by account order by item) as prod_rank
from table1
)z
order by account, item
I tried a few variations in excel sql and finally got this one to work.
select a.Account,
a.Name,
a.Product,
Iif(a.product = b.min_item,a.address,'') as [address]
FROM table1 as a
,(
select
z.Account,
min(z.Product) as min_item
FROM table1 as z
group by z.Account ) as b
where b.account = a.Account
order by a.account, a.product

Related

How to make a dynamic list from a multi variable table with boolean values?

Please help, very stumped with this one.
I've been provided with a matrix table as seen in image attached. I have a list of users, as well as the courses that they need to be enrolled in.
I'm trying to create a list that will repeat the usernames for every course that they're enrolled in (in column A, and then a list of the courses they are enrolled in (column B)
So far, I've been able to create such a list by creating a pivot table from this data, double clicking the grand total, and sorting the results, but this is a very manual process, and it needs to be replicated by others.
Is that possible?
Google Drive link: https://drive.google.com/file/d/1zXsWZCguia-SLaYAP-81kMX819879zzX/view?usp=sharing
This is just something to get you started:
Convert your data to an Excel table
Steps:
In Excel:
Select the data range (starting from row 3)
Press Ctrl + T
Select Data | Get data from table
Transform your data in Power Query
In Power Query:
Select the columns headers (from FirstName to User)
Right click them
Select UnPivot other columns

Table with one unique value from a Unique ID in PowerBI

I will need some help calculating or extracting a data for Power BI. I thought using either Dax or Power BI query, but cannot get what I am looking for.
I have a lot of data from a webanalytics tool, for each line I have a unique ID (ID_Visit), a unique client ID (ID_Client), a datetime (date_ID) & an average number of page per visit (Page_Av). For each unique ID I already have the mean of average page per unique ID, how can I get only one line per dateid and ID visit ?
My table is like that
What I would like is unique value like this
C1 <=> 3
C2 <=> 2
C3 <=> 7
For Power BI, it will be displayed like this :
I tried with Groubby function ; but it does not work for me.
Thanks
Go to the query editor and add a blank query. Refer this blank query to your raw data:
Group by your two columns ID_Client and Page_Av:
Result:
If all you are looking for is to display this information in a chart, then you can directly use the table in the chart. If you are going with a bar chart:
Put ID_Client in the Axis
Move Page_Av to the Values space
Click on the small down arrow next to Page_Av in the values space
You can select a summarizing option from here
For your scenario max/min/avg would work
Hope this helps.

query does not include the specified expression 'ColB' as part of an aggregate function

I am using this query and it is working perfect but in excel as a database, it is giving me an error of aggregate function -
solution: once I add all column in the group by I don't get sum and Group by doesn't work.
select ColA,ColB,ColC,ColdD,SUM(ColE),ColF,ColG FROM automate GROUP BY ColA
One picture indicates table structure:
Another one is expected output:
Please help me if someone knows- MS-Access / excel as database
Every field in your SELECT part must be either GROUPed BY or aggregated. If all values are guaranteed to be the same or if you don't care which value will be picked use FIRST(), otherwise use the appropriate aggregate function (MIN, MAX, FIRST, LAST, SUM, etc.)
Example:
SELECT ColA, FIRST(ColB), FIRST(ColC), FIRST(ColdD), SUM(ColE), FIRST(ColF), FIRST(ColG) FROM automate GROUP BY ColA

How to only add rows to table which don't exist MSaccess

My first post/question here, I have searched and tried many options but nothing seems to fit exactly what I need.
I am building an Access DB to manage scheduling work and assigning employees to that work. The Scheduled work comes from an Excel Sheet that I've imported into Access. I don't have control over how the data comes to me or the format.
So I have a table 'tblTempP6' with the scheduled work for the year. Several columns determine a unique entry.
I have another table 'shtP6DataEast' has an index which is the primary key. the two tables are the same except shtP6DataEast entries has and index. so i can access specific entries and assign employees etc..
Each time i get a new sheet to import the existing scheduled work is still in the sheet and i dont want duplicate entries with different ids.
I have tried Left Joins but have had issues because i need to use 4 columns as the unique identifier...
Any thought?
Thanks
forgive my NOOB Question
SELECT tblTempP6.[Project #], tblTempP6.[Work Order #], tblTempP6.[Project Name], tblTempP6.[Activity Name], tblTempP6.[TOA #], tblTempP6.Start, tblTempP6.Finish, tblTempP6.[Budgeted Labor Units], tblTempP6.[S/S - SUBSTATION], tblTempP6.iCircuit, tblTempP6.isStreet, tblTempP6.[Test Group Work Type], tblTempP6.Comments, tblTempP6.[Resource IDs]
FROM tblTempP6
LEFT JOIN shtP6DataEast ON tblTempP6.[Project Name] = shtP6DataEast.[Project Name]
WHERE (((shtP6DataEast.[Project Name]) Is Null));
Well I will try to answer this generally, using tmp as the name of the table with the freshly imported and possibly duplicated data, and dat as the name of the table with the previously imported, permanent data.
There's a bunch of ways to do it, here's how to do it with LEFT JOIN:
SELECT *
FROM tmp
LEFT JOIN dat ON tmp.KeyCol1 = dat.KeyCol1
AND tmp.KeyCol2 = dat.KeyCol2
AND tmp.KeyCol3 = dat.KeyCol3
AND tmp.KeyCol4 = dat.KeyCol4
WHERE dat.IDcol IS NULL
You do the left join against the existing data, and then exclude all rows that actually matched to that table.

Import 2 or more columns from Excel into 1 column Access

I have an Excel report that is the output of an opinion tool. In this Excel I have all the responses that the people submit for my quizz, in the questions that are multiple choise answer the tool output those questions like one question per option and only the selected option is the column with data in the Excel. For example, if my quizz is like this:
Q1 Your name:
R1 =
Q2 Options
opt 1
opt 2
opt 3
The Excel report will appear like this
Excel Report
So I want that when I import the Excel to Access it can automatically merge those columns to have only to headers in the Access table: "Q1 Your name:" & "Q2 Options"
Also, for context of the job, I will make some other editions to that imported table and then copy to another Access table (table 2) so even if there is a way to merge those Access columns before copy to the another one I will accept it like, I don't know, insert from this column and if empty insert from that column, I'm not good at doing queries sorry. Only the table 2 will have information, the first table would be like a temporary one so I will daily delete information from that one and preserve the important data en the table 2
Thanks for the support
Simplest way I can see to achieve your goal is to concatenate the three columns; since by the sound of it you will only ever have a value in one column per question per record. You could do this in Excel prior to the import, you could use a calculated field on the table or you could build a query that concatenates all your questions. My suggestion would be Excel since using the =CONCATENATE() function is probably going to be easiest option for you.
If you do import your raw data into Access you will need to assign unique column names, ie Q2_Op1, Q2_Op2, Q2_Op3.
The query syntax to concatenate these fields one would be something like:
SELECT Q1_Name, [Q2_Op1] & [Q2_Op2] & [Q3_Op3] AS Q2_Options
FROM Table1;
Where Q1_Name, Q2_Op1, Q2_Op2, Q3_Op3 are the column names on the imported data table.

Resources