common-table-expression

FIFO Stock Valuation Through CTE-Recursion

纵饮孤独 提交于 2019-12-11 19:37:08
问题 I have copied it from this site because it's been already closed but I needed it for further solution. thus, kindly help me out..... Problem : it's calculating the closing stock valuation through FIFO of issue as a whole. but i need cost of issues into Price column in the same row it belongs to itself. declare @Stock table (Item char(3) not null,[Date] datetime not null,TxnType varchar(3) not null,Qty int not null,Price decimal(10,2) null) insert into @Stock(Item , [Date] , TxnType, Qty,

Insert into a Temp Table in a CTE

♀尐吖头ヾ 提交于 2019-12-11 19:28:12
问题 I am trying to insert data into a temp table within a CTE. Here is the Code I am using: Can someone provide a little guidance on why it is not working. Create Table #Clients (HospMastID smallint Null ,HospCode smallint Null ,ClientID smallint Null ,ControlGroup smallint Null); ,Clients as (Insert Into #Clients Select Distinct HospMastID ,HospCode ,ClientID From Final) Thanks, Scott 回答1: Simply, you cannot use the INSERT function inside a CTE. Assuming "Final" was one of the other CTE's in the

SQL Server CTE left outer join

半世苍凉 提交于 2019-12-11 18:30:17
问题 I have 2 tables in SQL Server 2008, customertest with columns customer id ( cid ) and it's boss id ( upid ), and conftest with cid , confname , confvalue customertest schema and data: conftest schema and data: I want to know how to design a CTE that if cid in conftest doesn't have that confname 's confvalue , it will keep searching upid and till find a upper line which have confname and confvalue . For example , I want to get value of 100 if I search for cid=4 (this is normal case). And I

Get data copied by a function

浪尽此生 提交于 2019-12-11 18:28:30
问题 I have a quite complicated data structure that lies in several tables. I have a function that makes a copy of that structure. I want to make a copy and get newly created data in a single query like this: SELECT * FROM main_table JOIN other_table ON (main_table.id = other_table.main_id) WHERE main_table.id = make_copy(old_id); The copy is successfully created, but is not returned by the above query. I guess it is not yet visible for the outer query or somehow committed . I have also tried to

Create a view that groups some data, but doesn't group other data

时间秒杀一切 提交于 2019-12-11 18:08:10
问题 I am attempting to write a query to populate a report. SQL Fiddle - http://sqlfiddle.com/#!3/920eb The query relies on several different tables HardwareSupportRequest tblCHSRLaborPerCHSR LaborTypes tblMaterialUsed tblMaterial Inside of HardwareSupportRequest , I have a huge array of fields, but the pertinent ones are CHSRNumber - Basically the ID/unique identifier Building - Used for grouping by location WorkType - Can be 'Electric Install','Electric Removal', if it's something else, it'll be

Daily snapshot table using cte loop

眉间皱痕 提交于 2019-12-11 17:24:32
问题 I need daily snapshot of how many employees are employed on any given day (defined as @date between Start_Date & End_Date), with one line per date. At one point, the below script would return all daily snapshots, but they were each returned within a separate results window. I couldn't get the query to combine all results to insert each new result into #PLEASEWORK . I need to get the below to work for two consecutive days. It uses int dates. I have inherited this problem and am building a date

I updated to MariaDB 10.2.20 to use CTE. Still getting "Unrecognized Statement type. (near WITH) in phpMyAdmin

余生颓废 提交于 2019-12-11 15:35:23
问题 I am wanting to use CTE so I updated to MariaDB 10.2.20. phpMyAdmin is giving an error on the use of "WITH". I am not able to find why "WITH" is not supported in this version. MariaDB starting with 10.2.1¶ Common Table Expression WITH was introduced in MariaDB 10.2.1. MariaDB starting with 10.2.2 Recursive WITH has been supported since MariaDB 10.2.2. https://mariadb.com/kb/en/library/with/ 回答1: It looks like a bug in phpMyAdmin, see this open bug since August 2017: https://github.com

CTE to represent a logical table for the rows in a table which have the max value in one column

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-11 14:59:39
问题 I have an "insert only" database, wherein records aren't physically updated, but rather logically updated by adding a new record, with a CRUD value, carrying a larger sequence. In this case, the "seq" (sequence) column is more in line with what you may consider a primary key, but the "id" is the logical identifier for the record. In the example below, This is the physical representation of the table: seq id name | CRUD | ----|-----|--------|------| 1 | 10 | john | C | 2 | 10 | joe | U | 3 |

Why can't I use a CTE in my select statement?

孤人 提交于 2019-12-11 13:43:14
问题 I have a Mule application running a DB query which runs perfectly well (albeit a little bit slow). It's details are: Mule 3.7.2 EE Anypoint Studio 5.4.1 jdk1.7.0_51 Using oracle ojdbc6 driver Now when I create a database connection using the ojdbc6 driver, without the use of a Common Table Expression (CTE) the query runs fine (at 2.5 secs). When I utilise my CTE, the query fails with the error: org.mule.api.MessagingException : Query type must be one of '[SELECT, STORE_PROCEDURE_CALL]' but

Postgresql recursive CTE results ordering

非 Y 不嫁゛ 提交于 2019-12-11 13:31:12
问题 I'm working on a query to pull data out of a hierarchy e.g. CREATE table org ( id INT PRIMARY KEY, name TEXT NOT NULL, parent_id INT); INSERT INTO org (id, name) VALUES (0, 'top'); INSERT INTO org (id, name, parent_id) VALUES (1, 'middle1', 0); INSERT INTO org (id, name, parent_id) VALUES (2, 'middle2', 0); INSERT INTO org (id, name, parent_id) VALUES (3, 'bottom3', 1); WITH RECURSIVE parent_org (id, parent_id, name) AS ( SELECT id, parent_id, name FROM org WHERE id = 3 UNION ALL SELECT o.id,