sql

Why do SQL id sequences go out of sync (specifically using Postgres)?

时间秒杀一切 提交于 2021-02-13 12:02:15
问题 I've seen solutions for updating a sequence when it goes out of sync with the primary key it's generating, but I don't understand how this problem occurs in the first place. Does anyone have insight into how a primary key field, with its default defined as the nextval of a sequence, whose primary keys aren't set explicitly anywhere, can go out of sync with the sequence? I'm using Postgres, and we see this occur now and then. It results eventually in a duplicate key constraint when the

It's possible to create a rule in preceding rows in sum?

人盡茶涼 提交于 2021-02-13 05:46:45
问题 I have a historical stock variation values and have to calculate the stock value. But I also have a rule that the stock is max(stock, 0), or in other words, if stock is negative, I consider it as zero. To make this in Big Query, I think the way is like this: with stock as ( SELECT -7.34 variation, UNION ALL SELECT -1.81 UNION ALL SELECT -0.51 UNION ALL SELECT 17.19 UNION ALL SELECT -1.63 UNION ALL SELECT 2.82 UNION ALL SELECT -1.00 UNION ALL SELECT 0.56 UNION ALL SELECT -17.92 UNION ALL

Cleaner SQL CTE than the current one

﹥>﹥吖頭↗ 提交于 2021-02-11 18:23:30
问题 Currently I use the following CTE to grab a category and any category below it: WITH RECURSIVE tree AS ( SELECT * FROM ( SELECT categoryId, categoryName, categoryParentId, categoryDescr, categoryType, categoryDC, categoryLedgerId, 1 as categoryDepth FROM tbl_categories WHERE categoryId = '.$categoryId.' UNION SELECT categoryId, categoryName, categoryParentId, categoryDescr, categoryType, categoryDC, categoryLedgerId, 1 as categoryDepth FROM tbl_categories_custom WHERE categoryId = '.

Select the non-null value if exists else null, but always select the row

情到浓时终转凉″ 提交于 2021-02-11 18:22:58
问题 I hope I can explain myself good, so I have 2 tables with one-to-many connection (TableA and TableB). Now, TableB has a column with a foreign key form TableA, and an ID (foreign key) to a third TableC. Now usually 2 to 3 rows share the same FK from TableA and this are my possible scenarios: All rows with same FK_TableA have the same FK_TableC All rows with same FK_TableA have NULL for FK_TableC All rows with same FK_TableA have either NULL or same FK_TableC like so: TableB -------------------

Select only partial result but get total number of rows

南楼画角 提交于 2021-02-11 18:22:43
问题 I got stuck on SQL subquery selection. Right now, I have a table products: id | name | description ----+-------+---------------------- 6 | 123 | this is a + | | girl. 7 | 124 | this is a + | | girl. 8 | 125 | this is a + | | girl. 9 | 126 | this is a + | | girl. 10 | 127 | this is a + | | girl. 11 | 127 | this is a + | | girl. Isn't this? 12 | 345 | this is a cute slair 13 | ggg | this is a + | | girl 14 | shout | this is a monster 15 | haha | they are cute 16 | 123 | this is cute What I want

Access SQL Full Outer Join

喜你入骨 提交于 2021-02-11 18:21:56
问题 Hi I was trying to Full Outer join two tables on access because I want to keep all items. here is my code: SELECT aa.*, bb.firstname, bb.lastname, bb.totalcost FROM (select IT.*,HR.*from IT left join HR on HR.firstname=it.firstname and HR.lastname=IT.lastname) AS aa FULL OUTER JOIN 2016totalcost AS bb ON (bb.lastname=aa.IT.lastname) AND (bb.firstname=aa.IT.firstname); But I got error syntax error in from clause Thanks for help 回答1: Access does not recognize a Full Outer Join. Here is an

Access SQL Full Outer Join

梦想与她 提交于 2021-02-11 18:21:56
问题 Hi I was trying to Full Outer join two tables on access because I want to keep all items. here is my code: SELECT aa.*, bb.firstname, bb.lastname, bb.totalcost FROM (select IT.*,HR.*from IT left join HR on HR.firstname=it.firstname and HR.lastname=IT.lastname) AS aa FULL OUTER JOIN 2016totalcost AS bb ON (bb.lastname=aa.IT.lastname) AND (bb.firstname=aa.IT.firstname); But I got error syntax error in from clause Thanks for help 回答1: Access does not recognize a Full Outer Join. Here is an

SQL Server : find duplicate record by column value

依然范特西╮ 提交于 2021-02-11 18:16:37
问题 I have a value like below dataset. Now how I can find the duplicate DataSetID like: 201 & 401 is duplicate record. 回答1: Use PIVOT and ROW_Number For Non Duplicates FIDDLE DEMO SELECT * FROm Tbl WHERE DateSetID IN ( SELECT DateSetID FROM ( SELECT DateSetID,[Name], [Age], [Gender],ROW_NUMBER() OVER (PARTITION BY [Name], [Age], [Gender] ORDER BY DateSetID) RN FROM (SELECT * FROM Tbl) AS SourceTable PIVOT(MAX(ColumnB) FOR ColumnA IN ([Name], [Age], [Gender]) ) AS PivotTable)Tmp WHERE RN = 1 );

How to update SQL row based on two or more duplicate coluns

你。 提交于 2021-02-11 18:16:14
问题 I have the following SQL table. I want to be able to insert a new row (if it doesn't exist) or update a value in an existing row (in this case transaction based TWO columns which are (ds, item) . ID ds item transactions 1 2020-02-25 Baba Ghanous 1.0 2 2020-02-25 Shawerma 1.0 3 2020-02-25 Steak 1.0 4 2020-02-25 fish n chips 1.0 5 2020-02-25 Besara 1.0 Initially, i started with the following query: INSERT INTO forecast_sales (ds, item, transactions) VALUES('2020-02-25', 'Shawerma', 1.0) ON

SQL Server : find duplicate record by column value

半世苍凉 提交于 2021-02-11 18:16:06
问题 I have a value like below dataset. Now how I can find the duplicate DataSetID like: 201 & 401 is duplicate record. 回答1: Use PIVOT and ROW_Number For Non Duplicates FIDDLE DEMO SELECT * FROm Tbl WHERE DateSetID IN ( SELECT DateSetID FROM ( SELECT DateSetID,[Name], [Age], [Gender],ROW_NUMBER() OVER (PARTITION BY [Name], [Age], [Gender] ORDER BY DateSetID) RN FROM (SELECT * FROM Tbl) AS SourceTable PIVOT(MAX(ColumnB) FOR ColumnA IN ([Name], [Age], [Gender]) ) AS PivotTable)Tmp WHERE RN = 1 );