I am trying to insert data into my target table from my source table where in the target table I have an additional column called SaleTo. SaleTo = the SaleFrom based on the MAX SaleSequence. Example of the source table: SaleNo SaleFrom SaleSequence 1 Alabama 2 1 Minnesota 1 1 Virginia 3 Example of target tabl…
Tag: sql-server
PIVOT (without aggregation?) in SQL
I am trying to put together a select statement on some data like the below using TSQL: Table1: Property Detail Record No Surname Smith 1 First Name Anne 1 Title Mrs 1 Gender F 1 Surname Jones 2 First Name Ben 2 Title Mr 2 Gender M 2 Which I am hoping to get results which would appear like this:
SQL: Temp tables and script failing on second use, but works on the first
I have a script that makes use of temp tables. When I disconnect from the server, connect, and run the script, it works as expected. If I run the same script twice, it works the first time, and then on the second time, complains that; Column name or number of supplied values does not match table definition. I…
Unfold Data by Column
I have a dataset that looks like this (where the codes of ID1 and ID2 can never be the same. So putting them together they are still unique): ID ID1 Name AGE ID2 Primary Secondary 1 1234 Jim 34 1111 Mars A 2 1234 Tom 24 1111 Mars A 3 1234 Rick 55 1112 Mars B 4 2222 Ann 22
SQL statement to increment multiple case functions from multiple tables
I’m trying to increment the SQL generated column “Counter” by making 1 case statement. I get the error “Invalid Column” for Counter in my case statement. Any help is appreciated. Answer Shot in the dark. It’s not clear what order you need the rows to be counted or what the …
SQL query lists belonging to main table as columns on main table
I have a follow-up question based on the question asked in this topic: SQL query subtable in columns from main query I’ve managed to get the following table with the query answered in the topic above: uuid code title-en title-de 111-etc 123 english 123 deutch 123 222-etc 321 english 321 deutch 321 Next …
How can I write a SQL query to display ID, First Name, Last Name of all players with more than 2000 career hits based on schema
I am new to SQL and DB management. I am working on writing queries based on a schema which you can find below. This is an exercise for me to get familiar reading, writing queries on SQL Server for my job. Could you please help me out defining query based on the schema and simply explain the logic? Thanks a
How can I Select list of list or object of object not like a table? (SQL Server)
I have a problem in SQL Server: I want select list of list or object of object not a table. Example : Like a json example : Is there a solution to this problem for me? I want to output a stored procedure that I can use for the web service Answer Given this sample data: You can just apply
SQL query subtable in columns from main query
I have 2 tables with a main table with a unique UUID as index and a sub-table containing the languages of 1 row of the main table. E.g. table_1 uuid code 111-etc 123 222-etc 321 table_1_lang uuid lang_code title 111-etc en english 123 111-etc de deutch 123 222-etc en english 321 222-etc de deutch 321 I want t…
Function to check if a date is a Christmas day or New Years day
I am creating a function in SQL Server to validate if given date is a Christmas day or new year’s day. Please kindly advise if following code is OK: Above is the simplest way I can think of regardless or timezone and date format Answer I would create a table-valued function like this, rather than what I…