I've been trying to figure this out for a little bit now and i keep running into dead ends. Maybe someone here can help me. I work for a company and we are going to be receiving a file for units we are going to repair. In certain situations, we receive one claim for repair that has multiple units contained within it. I only have one field to import the serial number and I need to combine how ever many rows there is for a specific claim.
For example the file I would get would look like:
ClaimNumber SerialNumber
555 12345
555 123456
555 1234567
556 4321
557 3421
558 9876
558 98765
So i need to export this table combining the serial numbers into one field like:
ClaimNumner SerialNumber
555 12345, 123456, 1234567
556 4321
557 3421
558 9876, 98765
I am limited on receiving the file in this format, so this is all i have to work with. There are other fields in the file but i think these are the ones that I should be looking at. let me know if anyone has any ideas. Thanks!!
Given that your table is named Claim, then I think this should do it, (not tested)
Select Main.ClaimNumber,
Left(Main.Serials,Len(Main.Serials)-1) As "Serials" From(Select distinct T2.ClaimNumber,
(Select T1.SerialNumber + ',' AS [text()]
From Claim T1
Where T1.ClaimNumber = T2.ClaimNumber
ORDER BY T1.ClaimNumber
For XML PATH ('')) [Serials]
From Claim T2) [Main]
Related
I am quite a novice in programming and I kind need your help regarding SQL and an issue I notice.
I have a table:
date, ID, secondary ID, expenses
jul2020 258 0004 1000
jul2020 xxx xxxx xxx
...... .... .... .....
aug2020 258 0008 2000
aug2020 xxx xxxx xxx
aug2020 500 0004 1000
Id and secondary should be unique and always matching. But I notice that they are not. It's either correct the ID or the secondary ID. I want to sum all the D column per unique ID.
Thanks for reading and if you have any ideas would be very helpful
UPDATE: everything is numeric even ID. It's like this. As you can see we have different dates (but for the same date multiple customers). I notice that customer 258 for secondary ID 0004 during the years the ID or the secondary ID changes. And I wan to assign the same ID as the first date and the same secondary ID as the first date ( or any day just to be consistent). I want to to do this cause I want to know how many expenses each customer has during the years. There are like 50m obs.
Hi I have one table and one input number. based on which I need an output like mentiones below.
TABLE: tab
col1. col2
113. 568
123. 456
456. 789
789. 102
345. 987
102. 890
890. null
Input Number: 789
Output:
ouput_col
102
890
456
123
Please help me to get a right SQL query or procedure which will give me desired output based on the provided input number. It should be generic query work for data larger than this, which can check backward and forward interlinked data and returns all in a single column as mentioned in output_col. Thanks in advance.
This question already has answers here:
Simulating group_concat MySQL function in Microsoft SQL Server 2005?
(12 answers)
Closed 4 years ago.
I have looked at what was marked as the duplicate of this and it is not. I'm pulling from two tables, not one.
First, allow me to say I had nothing to do with the design of this database.
I have two tables that must be joined, and then an unknown amount of rows where the data must be concatenated into one giant string. They are joined by the Record ID.
Item table:
Item RecordID
---------------------
Car A 123
Car B 456
Car C 789
Yes, the words literally cut off in the middle. There should be nothing added between the values, and I also need to keep the commas and other special characters.
Details table:
RecordID Details
--------------------------------
123 black pain
123 t, radials
123 , green le
123 ather, spo
123 rt steerin
123 g wheel, b
123 uilt-in GP
123 S
456 standard
789 black leat
789 her, teles
789 coping ste
789 ering whee
789 l, seven c
789 up holders
789 , heavy du
789 ty mudflap
789 s
What I want to end up with is this:
ItemID RecordID Details
----------------------------------------------------------------------------
Car A 123 black paint, radials, green leather, sport steering wheel, built-in GPS
Car B 456 standard
Car C 789 black leather, telescoping steering wheel, seven cup holders, heavy duty mudflaps
I've looked at all the XML ones and can't figure out how to do this.
Thanks in advance.
There is no guarantee that your STUFF/ FOR XML PATH will produce the results you ask for unless you have an IDENTITY field in your Details table or some other value that you can sort by that will force the order of the Details text.
Usually you could use the STUFF command with an ORDER BY statement
SELECT
Item.Item AS ItemID,
Item.RecordID,
STUFF( (
SELECT
'' + Details
FROM
Details
WHERE
Details.RecordID = Item.RecordID
-- ORDER BY SomeLineIndicator
FOR XML PATH ('')
), 1, 0, '' ) AS Details
FROM
Item
I tried this on my box without the ORDER BY and just so happened to get the result you're asking for, but you really can't rely on these results without a field you can use to force the order.
Please read this post and the linked articles for more information about why you'd need a field for this and why you can't depend on an undetermined internal "index" to take care of it for you: Default row order in SELECT query - SQL Server 2008 vs SQL 2012
I have a tricky problem that I wouldn't mind a bit of help on, I've made some progress using queries that I've here and elsewhere, but am getting seriously stumped now.
I have a mailing list that has numerous near duplications that I'm trying to combine into one meaningful row, taking data such as this.
Title Forename Surname Address1 Postcode Phone Age Income Ownership Gas
Mrs D Andrews 122 Somewhere BH10 123456 66-70 Homeowner
Ms Diane Andrews 122 Somewhere BH10 123456 £25-40 EDF
and making one row along the lines of
Title Forename Surname Address1 Postcode Phone Age Income Ownership Gas
Mrs Diane Andrews 122 Somewhere BH10 123456 66-70 £25-40 Homeowner EDF
I have over 127 million records, most duplicated with a similar pattern, but no clear logic as was proven when I added an identity field. I also have over 90 columns to consider, so it's a bit of work!
There isn't a clear pattern to the data, so I'm thinking I may have a huge case statement to try to climb over.
Using the following code I can get a decent start on only returning the full name, but with the pattern of data - trying to compare the fields across rows is as follows.
SELECT c1.*
FROM
Mailing c1
JOIN
Mailingc2 ON c1.Telephone1 = c2.Telephone1 AND c1.surname = c2.surname
WHERE
len(c1.Forename) > len(c2.Forename)
AND c2.over_18 <> ''
AND c1.Telephone1 = '123456'
Has anyone got any pointers as to how I should progress please? I'm open to discussion and ideas...
I'm using SQL 2005 and apologies in advance if the tagging is all over the place!
Cheers,
Jon
Would it work by assuming that all persons with the same surname and phone number (Do all persons have a phone?) were the same person?
INSERT INTO newtable <fieldnames>
SELECT lastname,phone,max(field3),max(field4)....
FROM oldtable
GROUP BY lastname,phone
But that would collapse John Smith and Jack Smith living together into one person.
Perhaps you should consider outsourcing it to a data-entry sweatshop somewhere, adter you have preprocessed the data. :-)
And/or be prepared to take the flack for mistaken bundling.
Perhaps adding something like "To improve our green footprint, we have merged x listings on your adress together. If you would like separate mailings, please contact us"
This query is supposed to run with ms access 2003 using SQL. the function JOIN is NOT supported explicitly. implicitly in the WHERE clause is fine...implicity anywhere is fine as long as the word JOIN INNER JOIN Etc is not used.
DayNumnber PastTime
.
.
.
333 Homework
333 TV
334 Date
620 Chores
620 Date
620 Homework
725 Chores
725 Date
888 Internet
888 TV
.
.
.
Hey I would like a query that can Show the most important past time done for each day (TV and internet do not count!) .So importance would be Homework > Chores > Date.So:
DayNumber PastTime
333 Homework
334 Date
620 Homework
725 Chores
Something that might change this problem. Altho all the different past times are listen in a table together. but that was because i appended the table. originally the homework entries. chore entries and date entriess . internet entriess. tv entries. came from different tables.
eg homework 333
homework 620
Is it easier to do it without appending these tables first? I would hopefully like it to be done with the appended table but ya
I was thinking of a mixture of insert. delete... but the hardest part is checking that there is something there for a date a few things and how to put the more important thing done that day . Thank you
Create another table with:
Pri | PastTime
--------------
1 | Homework
2 | Chores
3 | Date
This is a priority list for the items.
Next do:
SELECT MIN(Pri), DayNumber
FROM PastTime_table, Priority_table
WHERE PastTime_table.PastTime = Priority_table.PastTime
GROUP BY DayNumber
This will give you the most important past time for each day. And because TV and Internet are not listed they will not show up.
But it will give you a number, and not the name.
If you had a better SQL you could then join this back to the Priority_table and lookup the name. But I guess you will have to do that part manually.
If you are willing to change the name and call them:
A_Homework
B_Chores
C_Date
instead then you could do (without any extra table):
SELECT MIN(PastTime), DayNumber
FROM PastTime_table
GROUP BY DayNumber
Since it sorts the name alphabetically it will always give you the best one.
You can add a WHERE to remove TV and Internet.