I have one table and about 20 columns which contain more than 5000 rows. Right now I want to display all 20 columns in console, and I came to thirth column and I get error Here is my code: This peace of code make me error When I change this to Anyone can guide me and tell me what’s wrong
MySql last record from group by item_id with order by date
My database table name is ledgers and fields are id, item_id, date, …other fields I Want the last record from (groupBy item_id order by date ASC). from each group. I tried below query Can you guys please help. Answer You can filter with a correlated subquery: For performance, consider an index on (item_…
how to use NULLIF with aggregate function
I’m using SQL Management Studio 2012. I’m getting a ‘Divide by zero error encountered’ error. I’m aware this requires the use of NULLIF but I’m unsure how to use it in my query. This is my query I’ve actually asked this question before which some kind person answered …
SQL QUERY TO SHOW FIRST IN AND FIRST OUT
my query like this : output getting : BUT I NEED TO SHOW THE RESULT LIKE THIS Answer Assuming that your in and outs are interleaved, you can use row_number() and aggregation: For your particular query, this is a little simpler:
SQL Server Lag and Lead
I’m using SQL Server 2014. I have a table: I have some data: If I perform a LAG and LEAD using the below for one Job, I get the desired result: Result: Notice the NULL for the first FromActionCode and LastToCode, which is correct. However, If I look at all records, it messes it up: What am I missing? An…
Week number and its Sunday
I have a simple question. If I have a week number, how I assigned a Sunday to it? (In t-sql) For example. I have number 15 as input (which is week from 8.4. to 14.4., from monday to sunday), and I will need it to be shown as 14.4. as output. The things is, that I have a column of
Trying to filter null values in aliased column – postgresql?
have the following postgresql command: I would like to add in a where clause to filter out where the link value is not null – so it contains a value. when i run the following i get an error message: column link not found any ideas on how to solve this problem? Answer In a SQL SELECT query, the logical
Left Join return not all row
I have two tables: baseresulttables and historyviewdemands. I want to get the first n entries from baseresulttables, and if there are values for them in historyviewdemands. But the query works like an inner join. What am I doing wrong? baseresulttables historyviewdemands Query: reuslt Answer Using LIMIT witho…
compute SUM for 1st row and zero total duplicates
I have sql query that uses windows function and i manage to compute the sum of records from table 2 however I’m having trouble in removing the duplicate of total from table 1 and display the sum only in row 1. Query: Output: +———+———–+———-+R…
SQL between date range returning a wrong result
I’m writing a SQL query to get rows between a date range format: mm/dd/yyyy I used the between operator but the result shows out of the range dates The expected result : dates of year 2020 only The actual result is: Answer I assume that you use SQL Server. Maybe you think that % is a wildcard char in th…