Concatenation of SQL 1 to many tables - sql

My apologies if this has already been covered, I just could not find a suitable answer. If anyone know of one, please do let me know.
I wrote a quick tool to take all the fields in the second table and concatenate them, then update the AddtionalTripLineInfo field in the first table. it was fairly quick, however, my question is, is there an easier straight with a SQL query to do this and make the formatting the same?
I'm using SQL Server and it doesn't matter if this is not looking right or formatting, this is the job I was dealt and that is the format they want.
thank you in advance!

From your image I'm going to guess you're using SQL Server, if so...
CONCAT() will combine the SeqNo and the Distance
STRING_AGG() will combine the rows
SELECT
TripID,
STRING_AGG(
CONCAT(SeqNo, '|', ThruWayDistanceCalculated),
'~'
) WITHIN GROUP (
ORDER BY SeqNo
)
FROM
example
GROUP BY
TripID
Demo : https://dbfiddle.uk/?rdbms=sqlserver_2019&fiddle=c18456392ee5a242d45a945a8aaadbb2

Related

Have multiple aggregations in a query always the same order?

Im asking for PostgreSQL specifically, but answers for other popular SQL implementations are appreciated as well.
Given an sql query with multiple aggregates, especially array_agg, is the order of the aggregated values deterministic?
Example:
SELECT ARRAY_AGG(columnA), ARRAY_AGG(columnB) FROM myTable
GROUP BY columnC
Can I rely on both arrays to have the same order, meaning values at position i in both arrays will belong to the same source row?
I can't find anything about this in the docs and I'm unsure because I've read that parallelization could be used in calculating aggregates, which I'm afraid could possibly result in non-deterministic orders.
The order is never deterministic if you don't provide an order by
So if you need a specific order, then specify it:
SELECT ARRAY_AGG(columnA order by some_sort_column),
ARRAY_AGG(columnB order by some_sort_column)
FROM myTable
GROUP BY columnC
I'm not sure why people here didn't understand the question and all of them quote the documentation incorrectly, by providing correct answer, but for different question.
Yes, both aggregate functions are called on the same row, at the same time, and this is guided by the query parser.
If you don't believe or trust it you can do something better that doing the sorting:
SELECT ARRAY_AGG(CONCAT(myTable.ctid, ':', columnA)),
ARRAY_AGG(CONCAT(myTable.ctid, ':', columnB))
FROM myTable
GROUP BY columnC
now you'll see that both result aggregates having the same ctid on both sides, for each element of the array, simply because both are called at the same time and there is no way around for PG to do something else.
Assuming that is not enough to convince you, then you have no other choice but to refactor your original query like this:
SELECT ARRAY_AGG(ARRAY[columnA, columnB])
FROM myTable
GROUP BY columnC
Now you have what you want: columnA and columnB together!

any SQL standard where you don't have to SELECT the column you GROUP BY with in order to see it?

select count(viewer)
from table
group by movie_title
Is there any SQL variant where you see the movie_title in the result to a query like this?
I have to grade some tests sometimes, and I see these soooo many times, I am actually curious.
In short, will any SQL variant give back the same results as below?
select movie_title,
count(viewer)
from table
group by movie_title
ok thanks #a_horse_with_no_name, I just wanted to be sure.
"If any database did return the movie_title in the first query this would be a major violation of the SQL standard and its rules. A will only return the columns (or expressions) listed in the SELECT list. If that was the answer to a test question I would probably mark it as wrong assuming the question was "show the title of each movie and the number of viewers" - or something along the lines."

Select all values in a column into a string in SQLite?

In SQL Server, I can do this with the help of Cursor (to loop through each row in the result set and build my string). But I don't know how to do the same in SQLite, this task is not like the so-called Pivoting which as far as I know, we have to know the exact rows we want to turn to columns. My case is different, I want to select all the values of a specified column into a string (and then can select as a column of 1 row), this column can have various values depend on the SELECT query. Here is a sample of how it looks:
A | B
------------
1 | 0
8 | 1
3 | 2
... ....
I want to select all the values of the column A into a string like this "183..." (or "1,8,3,..." would be fine).
This can be used as a column in another SELECT, I have to implement this because I need to display all the sub-infos (on each row of column A) as a comma-separated list in another row.
I don't have any idea on this, it seems to need some procedural statements (such as placed in a procedure) but SQLite limits much on how I can do with loop, variable declaration,...I'm really stuck. This kind of task is very common when programming database and there is no reason for me to refuse doing it.
Please help, your help would be highly appreciated! Thanks!
If you're just trying to get all the values from Column A into a single record, then use GROUP_CONCAT:
select group_concat(a, ',')
from yourtable
SQL Fiddle Demo
The main problem is that you are think like an application programmer. SQL does a lot of things for you.
SELECT *
FROM tableA
WHERE A IN (SELECT A
FROM tableB)
No need to resort to cursors, stored procedures and multiple queries.

SQL Query using IN but where all results have to have the same grouping ID

Unsure exactly how to put this question so apologies for the awkward phrasing, generally when I know how to properly describe a problem I can use the site search to look for it, or Google etc.
Anyway, simple enough issue - I'm querying a link / junction table with a variable array of AttributeIds, for example the IDs 14 and 17. Using the following table:
http://img220.imageshack.us/img220/5999/setattributecombo.gif
The only valid result I want to return from this query is where the ProductSetId is the same, so instead of 'IN' I want something like
WHERE AttributeIds IN 14,17 AND ProductSetId is the same
In the above example, the only valid result would be 5 but if I use an IN query I get 2,5,7 as results.
Any ideas?
SELECT ProductSetID, COUNT(*) AS CountOfMatchingRows
From MyTable
WHERE AttributeId IN (14,17 )
GROUP BY ProductSetID
HAVING COUNT(*) = 2
It sounds like you want to perform Relational Division. A good article on various methods is over at Simple-Talk. http://www.simple-talk.com/sql/t-sql-programming/divided-we-stand-the-sql-of-relational-division/

How to get the last tuple from a table with SQL Server 2008 R2?

I want to get a field from the last tuple I have inserted to a table using SQL Server 2008 R2?
Basically, what you need to do is:
SELECT TOP 1 (list of fields)
FROM dbo.YourTable
ORDER BY (some column) DESC
You need to define how to order the data so that you get the "latest" . If you sort by string, int or datetime, you'll probably want to sort in a descending order to get the most recent first.
Since you didn't provide any table structure or field names at all, this is the best we can do..... next time: please ask a bit more focused - provide more details! We're good programmer - but not mind readers. We don't know your system or your database or your setup - you need to tell us.
There is no way without an identity or "inserteddateTime" column to find this out. A table is an unordered collection of data and only an ORDER BY will impart an definitive ordering. You can not rely on anything else.
SELECT TOP 1 *
FROM MyTable
ORDER BY Whatever DESC
Can you explain more about the bigger picture please?