October 22, 2024
Chicago 12, Melborne City, USA

SQL

Structured Query Language (SQL) is a language for querying databases. Questions should include code examples, table structure, sample dat

SQL

Rows matching conditions in a directly related table or another, related indirectly

Suppose three tables: create table table_a( id int generated by default as identity primary key); create table table_c( id int generated by default as identity primary key , active boolean); create table table_b( id int generated by default as identity primary key , active boolean , table_a_id int references table_a(id) , table_c_id int references table_c(id));

Read More
SQL

Using SQL in an Excel workbooks exhibits strange behaviour until the device is rebooted

I have a macro that runs a a number of different SQL statements. I use the same macro each day. The 1st step is I update the Pending worksheet. The 2nd step is to identify new contracts. New contracts are identified by a SQL statement that compares the Pending worksheet to the History worksheet. Anything

Read More
SQL

SQL table Alias

I have the following SQL statement where the Company table is joined 3 times. There is a CompanyName column in the Company table How do I tell my c# code which alias to read? reader["ex.CompanyName"] does not work Select * from Shipments sh inner join Company cn on cn.CompanyId = sh.ConsigneeID inner join Company ex

Read More
SQL

Left outer join from more tables with dates

A is the main table, with a unique ID. B, C and D are tables containing this ID and Dates and other infos. I have to create a list: A.ID, A.info, a DATE existing in B, C OR D and the other info from B, C and D. Example: A ID_A...infoA and other 1......Tom B

Read More
SQL

Select rows with at least one non-zero value

I have a table with 20+ columns and some rows would only have 0s for all the column. Is there a way to filter out rows where all the columns have 0? I could use select * from table where concat(col1, col2, col3) != '000' but I don’t want to write 20+ zeroes in my

Read More
SQL

Query to get last position by User

I have the following table CREATE TABLE [dbo].[PRC_PosizioniWalkers] ( [ID] [int] IDENTITY(1,1) NOT NULL, [idWalker] [int] NOT NULL, [username] [nvarchar](200) NOT NULL, [data] [datetime] NULL, [Latitudine] [nvarchar](50) NOT NULL, [Longitudine] [nvarchar](50) NOT NULL, CONSTRAINT [PK_PRC_PosizioniWalkers] PRIMARY KEY CLUSTERED ([ID] ASC) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS =

Read More
SQL

Oracle Execute Immediate into variable error: inconsistent datatypes

I have a script, that generates a dynamic statement to capture null and not null information about each column in a given table. I was able to get the script to run initially, but only received the message PL/SQL procedure successfully completed. After some additional research, it looks like I need to put my dynamic

Read More
SQL

Sort by Date Day Names

I have this code that successfully displays the number of orders according to the day-name of the week. However, I wanted to display the days name sorted accordingly. I have this code below: SELECT DATENAME(dw, orderDate) AS day_of_week, COUNT(orderID) AS total_orders FROM Orders GROUP BY DATENAME(dw, orderDate) Here’s the output: But I wanted the Days(name)

Read More
SQL

How to sort the Date Names accordingly using SQL

I have this code that successfully displayed the number of orders according to the day-name of the week. However, I wanted to display the days name sorted accordingly. I have this code below: SELECT DATENAME(dw,orderDate) AS day_of_week, COUNT(orderID) AS total_orders FROM Orders GROUP BY DATENAME(dw,orderDate) Here’s the output: But I wanted the Days(name) sorted from

Read More
SQL

SQL “EXCEPT” vs “WHERE NOT IN”

I would expect the following two queries to yield identical results; however, I have several instances where the first is empty while the second returns several hundred rows. How is this possible? Working in Azure Synapse using T-SQL SELECT [value] FROM table_a WHERE [value] NOT IN (SELECT [value] FROM table_b) SELECT [value] FROM table_a EXCEPT

Read More