Skip to content

Tag: sql-server

How to select all parents of a child from a table

I am using SQL Server. My table is called Employees. It looks like this: I am trying to build a query that returns all parents of the children So the query would return the parents of Bin and Sam are I tried this but wasn’t successful but this can only work for 1 parent How can I get the whole

Identify the pattern to get desired output

We have below table contains multiple code against the ID and CounterID. Need an output based on code and id. Condition: Against the ID and CounterID case 1 : if there is code CI and CO then no record case 2 : if there is code CI and CO and CI then last record with code CI case 3 :

Displaying multiple database tables in one table

I have multiple databases with the same table (an Eventlog with different values). The names of these databases are subject to change. I am trying to display the Eventlog tables in one consolidated table with the corresponding database name. I tried to using cursor and dynamic SQL statement to achieve this wi…

Dynamic columns using pivot

I have a table that looks like this: Main table id bomname styleid 1 bom1 101 2 bom2 102 3 bom3 103 this is detail table Id bomId bomKey bomValue 1 1 part cllr 2 1 unit kg 3 1 qty 123 4 1 part body 5 1 unit kg 6 1 qty 456 7 2 part slm 8 2